Friday, March 14, 2014

Week in Review - issue 1

Two thoughts this week - perseverance and focus - which are nicely encapsulated by these two quotes:

"I don't think there is any quality so essential to success as the quality of perseverance. It overcomes almost everything" – JD Rockefeller
“Focus is saying no to 1,000 good ideas.” — Steve Jobs

Interesting article regarding pricing inefficiencies on San Fransisco's Bay Area Rapid Transit, from a Twitter Data Scientist - read it here.

An in-depth article on getting into data science from Zipfian Academy, a San Francisco based data science education outfit - read it here.

Javascript coding articles for the weekend work - I want to add interactive % labels to a bar chart I'm building for a client project:
Adding labels to charts with script in Google Spreadsheets
JS Fiddle - a really smart looking browser based coding environment

Always be Coding - a great article on being a good developer from Medium (this is way ahead of where I am but is still very inspiring):
Always be coding

Several interesting posts on data driven projects, again all from Medium (which I'm really getting a lot of value from at the moment - it's a far superior reading experience that many other tech sites out there):
The Optimal post is 7 minutes from the Data Science team at Medium
Defining your target market and finding your niche
Interesting look at thought process behind building a SaaS business

Sunday, March 9, 2014

Adding a time and date stamp to the contacts database project

Just a small update to my contacts database app - I want to add a time and date stamp to each new entry that I add to the spreadsheet. This is achieved easily using the Javascript "new Date()" object, formatting it to look the way I want using "Utilities.formatDate" and then adding into my new row in the "apendRow" method.

The code to achieve this is as follows:

var d = Utilities.formatDate(new Date(),"EST","EEE', 'MMM dd', 'yyyy' at 'HH:mm:ss");
EST stands for Eastern Standard Time (I'm based on E coast of the US)
EEE gives the abbreviated 3 letter day of the week (e.g. Mon)
MMM gives the abbreviated 3 letter month of the year (e.g. Mar)
Then I modify my appendRow method to include the date variable "d" in the output. The code is as follows:

//apend row
sheet.appendRow([lastCell+1,d,textBoxValue1,textBoxValue2,textBoxValue3]);

So my contacts database now automatically adds a date/time stamp to each new entry I add, and looks like this:


References for this entry:
Stack Overflow on Javascript Date object
Stack Overflow on formatting Date object
Official Google documentation on formatDate

Wednesday, March 5, 2014

Adding rows of data through a GUI using Google Scripts

I want to create a simple form so that the user can enter some basic details into the spreadsheet. Of course, the easy way to do this would be to have the user just type them direct into the spreadsheet but where's the fun in that! Whether this app ever ends up being useful or not (or even ends up looking anything like the vision I have for it in my head) is another matter but I'm learning as I try to build it out.

The Google tutorial that I used for this post is found here.

So, in this post I'm going to setup a GUI interface that will prompt the user for inputs and then transfer these to the spreadsheet on the next blank line. The steps are as follows:

  1. Declare the GUI application
  2. Create the labels (e.g. "Enter Name") using the .createLabel() method
  3. Create the empty text boxes for the user inputs, using the .createTextBox() method
  4. Create a layout grid for these labels and boxes, using the .createGrid() method
  5. Assign the items to the grid using the .setWidget() method
  6. Create a "Submit" button that will do something once the user has entered data
  7. Create the panel component that will display the labels, text boxes and button
  8. Add the grid to the panel (which by definition adds the items as we've already added these to the grid in step 5)
  9. Add the button to the panel
  10. Add the panel to the GUI application
  11. Show the application
The code to do this is as follows, with the steps added as comments:

function gridUI() {
  // Step 1 - create the GUI application
  var testapp = UiApp.createApplication().setTitle('New Contact');
  
  //Step 2 - setup the three labels
  var label1 = testapp.createLabel('Enter name: ');
  var label2 = testapp.createLabel('Enter company: ');
  var label3 = testapp.createLabel('Enter web url: ');
  
  //Step 3 - setup the three text input boxes
  var textBox1 = testapp.createTextBox().setName('TextBoxName').setId('TextBoxName');
  var textBox2 = testapp.createTextBox().setName('TextBoxCo').setId('TextBoxCo');
  var textBox3 = testapp.createTextBox().setName('TextBoxURL').setId('TextBoxURL');
  
  //Step 4 - create and lay out the form grid
  var mygrid = testapp.createGrid(3,2);

  //Step 5 - add labels and text boxes to grid
  mygrid.setWidget(0,0,label1)
    .setWidget(0,1,textBox1)
    .setWidget(1,0,label2)
    .setWidget(1,1,textBox2)
    .setWidget(2,0,label3)
    .setWidget(2,1,textBox3);
  
  //Step 6 - create the form buttons
  var submitButton = testapp.createButton('Submit');
  
  //Step 7 - create the panel
  var panel = testapp.createVerticalPanel();
  
  //Step 8 - add the grid widget to the panel
  panel.add(mygrid);

  //Step 9 - add the button to the panel
  panel.add(submitButton);
  
  //Step 10 - add the panel to the GUI application
  testapp.add(panel);
  
  //Step 11 - display the panel
  var doc = SpreadsheetApp.getActive();
  doc.show(testapp);
}
Next step is to add an event handler to take the user entered data and drop this into the next blank row of the spreadsheet. There is a convenient method called "appendRow()" that I can use to achieve this relatively easily. Before getting to that however, I need to add an event handler to the submit button in the data entry form created above, then create a function that this event handler will call when the button is pressed, the ultimate outcome of which is to append the data.

The code to add the event handler is as follows - it is added between steps 10 and 11 above:

  //create the event handler for when the submit button is pressed
  var clickHandler = testapp.createServerHandler("respondToSubmit");
  submitButton.addClickHandler(clickHandler);
  clickHandler.addCallbackElement(panel);

The function code is then, placed as a new function under the closing "}" above:

//function to respond to the submit button
function respondToSubmit(e) {
  var testapp = UiApp.getActiveApplication();
  
  //get values of the three text boxes
  var textBoxValue1 = e.parameter.TextBoxName;
  var textBoxValue2 = e.parameter.TextBoxCo;
  var textBoxValue3 = e.parameter.TextBoxURL;
  
  //determine the next available row to input the new line of data
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow() + 1;
  
  //apend row
  sheet.appendRow([lastRow,textBoxValue1,textBoxValue2,textBoxValue3]);
  
  //close the dialog box
  return testapp.close();
}

When we press run, the GUI should look something like the image below. Entering data and then pressing submit should add that data in the last line of the spreadsheet, magic!



Monday, March 3, 2014

Creating drop down lists with javascript in a Google Spreadsheet

This function builds on my previous post about passing a range of data from a google spreadsheet into an array. Taking it one step further, I now want to display (some of) that data in a drop down list box that allows the user select a record so we can then perform actions on it e.g. displaying the data.

The starting point for this exercise is the Google Developer site, specifically this list box widget code.

The code creates a "panel" that sits above the spreadsheet - this is a graphical interface that will display text, buttons to etc. (a message box or input box is a simple example of a panel). Then I create a list box, to which I add the list items (these will come from the array). I add that list box to the panel, then add a button to the panel as well. The button has a click handler associated with it - this tells it what to do on the event of a click. In this case it'll call on another function that I'll define which simply displays the entry that the user has selected in the list box.

The data for this example is simple enough:


The script from my previous blog post adds this to an array so here I want to add code to the end of that script - the snippets below are not standalone, they will only work when added to the code from my previous example. Before the final closing "}", I add this code to declare the variables needed for the graphical panel and the list box:

  var app = UiApp.createApplication().setTitle('Search');
  var panel = app.createVerticalPanel();
  var lb = app.createListBox(true).setId('myId').setName('myLb');

Now I need to add some items to the list box. I want to do this using the variables I have stored in my array. Recall, the array is sized to hold all of the line items from my data range, which contains "N" rows in this case (where N is defined to equal the last row count of the data range). So I use a for loop to cycle through the values in the array and add each one to the list box (the array holds N rows, which are each in turn an array holding values "index", "name1", "name2" and "web" (see header row of image above)). In this case I've chosen to add name1 and name2 to my list box. The code is as follows:

  //create the list box items
  lb.setVisibleItemCount(4); // how many items show at once
  for (var k = 1; k < N; k++) {
    // add items to ListBox
    var listBoxString = mainObject[k].name1 + " " + mainObject[k].name2;
    lb.addItem(listBoxString);
  }

Finally, I want to add a button and event handler so that I can do something once the user has selected an entry in the list box. Several things happen in this snippet of code: I declare a button labeled "Select"; I declare a variable called "handler" that calls on the function called "select" and passes the data from the panel (i.e. the list box); and finally, I associate this handler to the button. I don't pretend to fully understand this yet but there are more details here on the Google Developer website.

  //add button and click handler
  var button = app.createButton('Select');
  var handler = app.createServerClickHandler('select').addCallbackElement(panel);
  button.addClickHandler(handler);

The final piece of the code is required to add the list box and the button to the panel, then add the panel and then display the panel.

  panel.add(lb);  // add list box to panel
  panel.add(button); // add button to panel
  app.add(panel);  // add panel to the app
  spreadsheet.show(app); // display the app i.e. the panel with list box

Adding this code to the array code from my earlier blog post should give the following output:


Cool huh! This took me quite a while to work out, progressing by adding static items to the list box, then data in static cells in my worksheet, then data from a static array I created, and finally the data from my dynamic array. I was stoked to get it working. The final piece of code is the "select" function that the button handler calls on - it's a simple display box to show the entry the user has selected, as follows:

function select(eventInfo) {
  var value = eventInfo.parameter.myLbName;
  Browser.msgBox(value);
  return app; 
}

The output should show:


Voila! Next up, doing something more useful than just displaying the entry from the list box.