7th grade students have a handle on using spreadsheets to store and display information, so we are taking our learning to the next level with Apps Scripts!

In our first class back from break, we started with a stock script and then got to hacking apart the code.  Although it looked scary, everyone succeeded in finding some info in the code that they could change to alter the outcome.

The script we started with was actually 2 scripts in one:

function onOpen() {
var sps = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sps.getSheets()[0];

var menuEntries = [ {name: "Ask Name", functionName: "whatIsName"}];
sps.addMenu("CLICK HERE SILLY", menuEntries);

}

function whatIsName() {
var sps = SpreadsheetApp.getActiveSpreadsheet();
var sheet = sps.getSheets()[0];

var myValue = Browser.inputBox("What is your name?");
sheet.getRange("B8").setValue("is silly.");

var b1Range = sheet.getRange("A8").setValue(myValue);
}​

The first function in this script creates a custom menu button called “Click here silly”. When clicked, a drop down menu appears with a single option, “Ask Name”.

Custom Spreadsheet Menu - "Click here silly"

Custom Spreadsheet Menu - "Click here silly"

The second function is called “askName” and it runs when someone clicks on the custom menu button.  This function pops up an alert window that asks the user’s name, and then places the answer in box A8 along with a message in box B8.

Custom pop-up box using Apps Script

Custom pop-up box using Apps Script

For our next class, we took a closer look at the script we used and took some notes on what we found:

//script – code that tells a computer what to do
//variable – placeholder that represents data (number, text) that can change

//Google Apps uses Javascript

//Parts of Javascript
// = “commenting out” tells the computer to ignore
//var = variable

//red text = display text, code computer should consider text
//blue text = variables