Wednesday, May 22, 2013

Learning How to Script

So I have been loving the workflow that I have gained by using the scripts from the Google Apps Scripts Demi-gods that I have run across: +Andrew Stillman+Bjorn Behrendt+Henrique Gonçalves Abreu, and all...
Now I want to learn how to script on my own to enhance some of the Google Workflows I have developed, like my Parent/Teacher Conference Scheduling and my Seat Problems.
I am going to try to document my learning process in this blog for a few reasons, mainly to share the process and possibly create a course so my students can learn to script.

The first project I want to tackle is adapt my Seat Problem workflow. Currently, my students get an email everyday (actually 3 times a week, since I don't see my students every day) with a math problem they need to solve. The answer to the math problem is the seat they will sit in during the next class. I love this for many reasons, but I feel like it is little to ask from them and I have always thought it would be cool to do!
Now, the only assessment I do on this problem is to see if they are seated in the correct seat. I would like to see their thought process on their solutions and to hold them a little more accountable for their own work.
My thoughts are I would like to have a Google Doc in my students' Google Drive Assignment folders (that were created with gClassFolders, which requires a more in-depth blog post from me). I would like this document to be a running, living document for the entire year of Seat Problems. My reasoning is 2-fold:
  1. The students will always know where to go to find their Seat Problem.
  2. I think it will be great for students to see their knowledge progress throughout the year. (I differentiate each problem based on student performance and connect them with the content we are working on in class).
I have my problems hosted in a Google Spreadsheet. (I create them using Wolfram's Mathematica, but again that process is for a future blog post) I use LaTeX form and push them through the Google Chart API so they have nice math type. For example if I wanted to have them simplify the expression 3x-2y, I use the URL:

which yields:  

This is helpful when I use exponents or fractions, because 

looks much better than 3x/2 and 2^((3x)/2).

So my goal is to take a URL which is returned as an image and write this to a specified Google Doc. After a few days trying to get DocAppender to write for me, I finally figured out the working code to add an image to a Google Doc body. As you can see I stole a lot from previous scripts. StackOverflow,  +Andrew Stillman's amazing cache of scripts, and the Google Developers Documentation were the main learning tools I used.

Here is the code I used:

function writeToDoc() {
  //stolen from Doctopus to open up the current spreadsheet and read rows data
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetName = ScriptProperties.getProperty('sheetName');
  var sheet = ss.getSheetByName(sheetName);
  var data = getRowsData(sheet);
 //This fetches a document by ID (found in the URL) and opens it up.

  var doc = DocumentApp.openById('1Haex8jdw1n1G1dwKw9Y9i1iMbW3b74D9_o3EfrDvnp0'); //would like to replace this with the file key by student
 //You could also use var doc = DocumentApp.create(name) to create a new document instead of opening an existing one.
 // The following code fetches the picture from the problemUrl
  // and puts it in the document created above taken ideas from:

  //find the Url
   var problemUrl = ""; //replace from student problem
  //turn the image into a blob
   var problemBlob = UrlFetchApp
  //find the body of the document
  var body = doc.getActiveSection();
  //put the image into the body of the document found above. Got information from here:
  var dateForClass = body.appendParagraph('Seat Problem for Class Date of ##-##-##');
  var seatProblem = body.appendImage(problemBlob);

It works!! You can see that it writes the problem in the Document body. You can also see the Document ID in the URL at the end, between the /document/d/     URL ID     /edit

Now, I need to automate it. I will use the templates from the Tutorial and cannibalize the DocAppender script to read a cell from a spreadsheet and write it to the correct Document based on the information in the sheet.