Saturday, September 7, 2013

Adding images into Forms with Scripts

While I have been working on inserting images into a Doc, I have been playing around with the forms scripts APIs and I made a quick script to add images as questions (I use LaTeX coding pushed through the Google Chart API to make nice math type). It is a bit clunky, especially since image items aren't questions, but if you put a blank text item afterwards, it will be the response repository for the image item.
Here is a link to the Form and my script is contained in the tools:

https://docs.google.com/a/saisstudent.sg/forms/d/1385oZ3maxBYO3nPHmVBo2Tik2IKAPCSNoTtGq3pXFFk/edit?usp=sharing

Here is the script:
function addLaTeXItem() {
   // Open a form by ID and add a new image item from a URL
 var form = FormApp.openById('1385oZ3maxBYO3nPHmVBo2Tik2IKAPCSNoTtGq3pXFFk');
 var problem = UrlFetchApp.fetch('http://chart.googleapis.com/chart?cht=tx&chl=4+x-3+y');
 form.addImageItem() //add an image item
     .setTitle('Your Seat Problem: Please write your answer in the box below the problem')
     .setHelpText('Solve This Problem') // The help text is the image description
     .setImage(problem); //the actual image
 form.addTextItem() //puts a blank text question below so the student can answer the imageproblem
     
}