Thursday, February 27, 2014

Scripting for Dynamic Ranges in Google Spreadsheets

In my previous post, I took the data from my google spreadsheet and passed into an array, so that I could then perform an operation (print out the third row of data). However, I was selecting a static range from my spreadsheet - fine for that example, but not suitable for any application in real life, where your database will change in size. This example below uses the same spreadsheet as yesterday's example.

So what I want to do is change the code to work whether my spreadsheet has 10 rows, 100 rows or 10,000 rows. I also want to simplify yesterday's code, which was largely based on this excellent google tutorial.
This is the code snippet that I want to change:

//select the data range - static for now
var dataRange = sheet.getRange("A1:D8");

Instead of referencing cells, I use the "getLastRow()" and "getLastColumn()" methods to find out the size of my data range and use that in my "getRange" method. The code is as follows, telling the computer to grab everything from cell (1,1) to (last row, last column):

// grab the range from cell (1,1) to last row and column
// and put into a variable called "dataRange"
var dataRange = sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn());

We can make the code cleaner by defining two variables, N and P, that hold the values of the last row and last column - we'll find these are useful elsewhere. The code to do this is as follows:

// get the size of the data range
var N = sheet.getLastRow();
var P = sheet.getLastColumn();
And then the code to grab the range becomes:

var dataRange = sheet.getRange(1,1,N,P);

So, this is good, I now have the data range selected. Time to pass into an array. For my own learning, I'm going to do this step by step, as follows: i) create a variable that holds the header labels (these are used as labels in the row arrays); ii) create a row array for one example row in my data range (I chose row 2); and finally iii) create a new array that holds each row array.

Step 1: create a variable of header labels by using the "getValues()" method on the data range. Notice the "[0]" which ensures I'm selecting the first row i.e. the header row. The code is:

// define headers variable as first row of data range 
// use as labels in my array
var headers = dataRange.getValues()[0];

Step 2: create an object array called "object" and pass in the data from a single row. The idea is to use a for loop that starts with the data in the first column, puts into the array with the label "header[0]", then repeat for the second column, which has label "header[1]" through to the last column. The code is as follows:

//get values from data range
var dataRangeValues = dataRange.getValues();

// create the empty array object
var object = {};

for (var j = 0; j < P; j++) {
   var cellData = dataRangeValues[4][j]; // select data from row 4
   object[headers[j]] = cellData; // pass into object array
}
To test, let's try printing out a value stored in the array. I chose something from the 4th column with the following snippet of code:

var stringToDisplay = object["web"];
Browser.msgBox(stringToDisplay);

The output should return a message box with "www.example4.com".

Step 3: finally, I want to grab every row array and pass these into a new array called "mainObjects". This is achieved by another for loop that cycles through from the first to the last row. The code is as follows:

// define empty array to hold all row arrays
var mainObject = []; 
  
for (var i = 0; i < N; i++) {
    
    // define empty array for each row
    var object = {}; 
    
    // for each row, loop to add data from each column
    for (j = 0; j < P; j++) {
      var cellData = dataRangeValues[i][j];
      object[headers[j]] = cellData;
    }
    
    // push each row array into the main array
    mainObject.push(object);
}

Lastly for tonight, let's test that the data is correctly placed into the "mainObjects" array by printing out an example value (the first name of the third row array), as follows:

var thirdEntry = mainObject[3];
Browser.msgBox(thirdEntry.name1);

The output should be a message box showing "Bob" as follows:



So, the full code for this example is:

function array2() {
  // select spreadsheet and sheet
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getSheets()[0];
  
  // get the size of the data range
  var N = sheet.getLastRow();
  var P = sheet.getLastColumn();
  
  // grab the range from cell (1,1) to the cell in the last row and last column
  // and put into a variable called "dataRange"
  var dataRange = sheet.getRange(1,1,N,P);
  
  // pass the header row into a headers variable for use as labels in my array
  var headers = dataRange.getValues()[0];
  
  var dataRangeValues = dataRange.getValues();
  
  // define empty array to hold all row arrays
  var mainObject = []; 
  
  for (var i = 0; i < N; i++) {
    
    // define empty array for each row
    var object = {}; 
    
    // for each row, loop to add data from each column
    for (j = 0; j < P; j++) {
      var cellData = dataRangeValues[i][j];
      object[headers[j]] = cellData;
    }
    
    // push each row array into the main array
    mainObject.push(object);
  }

  var thirdEntry = mainObject[3];
  Browser.msgBox(thirdEntry.name1);
  
}

Any thoughts or corrections welcome! Tomorrow I will show some of the work I've been doing to create a GUI to add rows of data to my spreadsheet. My goal is to build a contacts database in a google doc that I can add data to, edit existing data, delete data and display data. Each of these posts will be a building block for my final application.

Passing data from a Google spreadsheet into an array object

What's the deal here? I have 4 columns in my Google spreadsheet consisting of an index, a first name, a second name and a web address and I want to pass this data into an array so I can then perform functions, such as searching or displaying a specific entry. (I'm building a mini database app to manage contacts, so the next few blog posts will all be building blocks - small programs that can all be bolted together to create my full application.)

The following diagram shows my test data setup:


I'm working from the following tutorial and simplifying the code to fit my needs:

Extending Google Sheets

Before diving into the code, it's worth thinking about what we're trying to do here. I want to pass the data from each row into an array of elements e.g. [index, name1, name2, web] and then I want to hold all of these row arrays in a main array like this:

row 1:   [index[0], name1[0], name2[0], web[0]]
row 2:   [index[1], name1[1], name2[1], web[1]]
row 3:   [index[2], name1[2], name2[2], web[2]]
etc..

So in my case, with the above data, the array I want to have is:

[1, James, Smith, www.example1.com],
[2, Laura, Jones, www.example2.com],
[3, Bob, Philips, www.example3.com],
etc...

Moving now to the code, I need to start by making sure I'm in the correct spreadsheet and the correct sheet within that spreadsheet. Then I select the range of my data (I'm doing this with static references at the moment but will want to change to be dynamic further down the line). Finally, I call the function "getRowsData" (see below) with the arguments "sheet" (i.e. my current sheet) and "dataRange" (i.e. my range of data):

function arrayTest() {
  // select the current spreadsheet and first sheet within
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  
  //select the data range - static for now
  var dataRange = sheet.getRange("A1:D8");
  
    // For every row of employee data, generate an employee object.
  var employeeObjects = getRowsData(sheet, dataRange);
}

The function "getRowsData":
This function takes an input of two arguments: sheet and range. By passing the current sheet from the arrayTest function above I can reference it to identify the names in the header row, and thereby use these to name each item in my array e.g. for the column "name1" I want to identify each row entry as: name1[1] = "James"

function getRowsData(sheet,range) {
  var headersRange = sheet.getRange("A1:D1");
  var headers = headersRange.getValues()[0];
  return getObjects(range.getValues(), headers);
}

The function "getObjects":
You'll see that within "getRowsData" we also call on another function called "getObjects". We pass the data range and the header values (to use as names for each piece of data in the row arrays) to this function and it returns an array object consisting of each row of data.

// For every row of data in data, generates an object that contains the data.
function getObjects(data, keys) {
  var objects = [];
  for (var i = 0; i < data.length; i++) {
    var object = {};
    var hasData = false;
    for (var j = 0; j < data[i].length; j++) {
      var cellData = data[i][j];
      object[keys[j]] = cellData;
      hasData = true;
    }
    if (hasData) {
      objects.push(object);
    }
  }
  return objects;
}

In a nutshell what's happening here is that we declare an array object called "objects". Then for each row of data (the number of rows identified by the "data.length" syntax in the first for loop), each cell in turn in a given row is "pushed" into the array. The whole array, called "objects", is then returned as the output of this function.

So how do I know if it works?

Well, we can test it by writing a couple of lines of code to choose some information from the array. So in the first function, "arrayTest", drop in these two lines of code just above the final "}" (here, I've chosen the third entry as an example):

  //choose a test entry from the array
  var thirdEntry = employeeObjects[3];

and then finally, add these two lines to display the requested entry from the array (note, I'm only displaying name1 and name2 back but you could easily add in "index" and "web" as well):

  var stringToDisplay = "The third row is: " + thirdEntry.name1 + " " + thirdEntry.name2;
  Browser.msgBox(stringToDisplay);

And voila! You should get the following output:


Lastly, a caveat - I'm new to Javascript and I'm learning as I go so I don't promise that everything I write is perfect by any shot. However, by trying to explain what I'm doing, it's really helping me to understand the code and what each line does. Thank you for your patience. Any constructive feedback appreciated. Happy coding!

Wednesday, February 26, 2014

Hello world

Well, it has to start this way right? With the ubiquitous Hello World program, a program beloved of all beginner tutorials and computer books.

1. Create a new Google spreadsheet from the Drive home page and call it "Hello World".

2. On the menu bar, go to Tools > Script Editor…  which opens a new tab in your browser.

3. Delete the boilerplate code that is currently sitting in the main window and type in the following snippet of code:


function helloWorld () {
  Browser.msgBox("Hello World");
}


4. Save this script as "Hello World".

5. Hit run and switch back over to your Google spreadsheet.

6. Hey presto! There's a message box saying "Hello World"



Ok, what's this all about - I'll be posting my experiments and thoughts here as I learn to code. I've been working extensively doing data analysis in Excel for many years, MySQL on/off for the last year and have recently learnt VBA so that I can automate some of the manual tasks I do at work.

I recently completed the Javascript course on Codeacadamy (which I'd highly recommend) so I'm keen to put that into practise building small apps for Google spreadsheets.

Let's see where this journey takes me.

Ben, February 2014