Saturday, March 15, 2014

My First Script for Google Sites

After reading the fantastic book "Google Apps Script for Beginners," (read the review here), I decided to get my hands dirty and write a script to automate a task in Google Sites.

I've been getting things ready to have my students build a digital portfolio, using Google Sites. We're already using Google Drive for most of our work in English and Math, and I want them to create a digital record of what they've been doing, what they're proud of, and what they want to record.

One of the things I love about Google Drive is that you're always working on the latest version of a document. Changes are automatically saved. Updates are automatic. This is fantastic. No worrying about clicking on a save button.

But this leads to a conundrum with a digital portfolio: what if you want to document the learning process? Keeping a record of changes over time is easy for anyone with access to edit the document: you just look at the revision history. However, people who only have read access can't do this. In fact, if you want to publish a document, you have to make sure that you've shared it with the public. If you forget, when you look at your site, everything looks fine, but when anyone else wants to see it, the sharing settings won't let them see your work.

I thought of a way around this: using a file cabinet page to upload a version of the work that will serve as a snapshot of the learning. However, it can be a long process (relatively speaking) to download a pdf of your document then upload it as an attachment to your file cabinet page. This seemed like something that would be relatively simple to do with a script. It also happens to be one of my pet peeves about having a website: when I create something, I don't like being bothered with uploading it to my site.

So I sat down and started writing. Well, actually, the first thing I did was a search. I found some examples of publishing a folder to your site, which has been deprecated since you can now use a gadget to insert a folder directly into your site. But the bones of the script looked very similar to what I wanted to achieve. So I sat down and started writing. Here's what I came up with.
  1. Look at a folder specified by the user, and pull a list of all the documents contained in the folder (this is a particular application where I didn't want it to pull spreadsheets, presentations, folders, or any other items).
  2. For each item in the folder, check against the attachments already in the file cabinet page. If the file already exists, the script will skip attaching it.
  3. If the file does not exist, the script will create a pdf version of the document, and attach it to the page.
  4. It will star each item that has been successfully uploaded. This is for testing purposes, but I may implement functionality where if the document is already uploaded but is no longer starred, it will replace the attachment with a new copy.
I will set up a trigger to run the script once every night. This will keep attachments up to date on my site without any bother of uploading attachments. If you're interested in having a copy of the script, click on this link to generate a copy for yourself. You'll need to change the folder key and page URL variables at the top, and add a trigger to run the script when you want.

This is a very basic script that doesn't do much fancy stuff. I'm proud of what it does, and I'm happy that I was able to put it together without too much difficulty. If it sounds like something that interests you, please feel free to use it, modify it, and share it. I've included the full text of the script below, but only for the interest of my fellow geeks. If you're not sure about what any of that means, don't worry. If you want a hand setting up the script for yourself, let me know: I'm always happy to help out.

Enjoy!

/* 
// publishFileCabinet Script written by James Petersen.
// Full description is available online at http://openlessons.blogspot.com/2014/03/my-first-script-for-google-sites.html
// Published under the Creative Commons 4.0 Attribution - ShareAlike license.
// For more information, visit https://creativecommons.org/licenses/by-sa/4.0/
//
// Use this script to automatically publish documents from a folder to a file cabinet page.
// 
// Paste the folder key and the page URL into the variables below.
// If you want to automate the script, add a trigger by using the Resources - Current Project's Triggers
// menu option.
*/ 

// Paste the folder key of the folder you'd like to pull files from into the line below.
var folder = '0B7uRH5XJTbFzY0s4UVMwdDZqYmM';

// Paste the URL of the file cabinet page into the line below.
var pageUrl = "https://sites.google.com/a/lbpearson.qc.ca/jpetersen02/home/mat106-04/learning-checklist-file-cabinet";


// Modifying any of the code below may break the script. Do so at your own risk.
function publishChecklists() {
  var files = DocsList.getFolderById(folder).getFilesByType("DOCUMENT");
  var page = SitesApp.getPageByUrl(pageUrl);
  var attachments = page.getAttachments();

  for (i in files) {
    Logger.log(i); // debugging console output
    var exists = false; 
    for (j in attachments) {
         if ((files[i].getName() + ".pdf") == attachments[j].getTitle()) {
           if (files[i].isStarred()){
             Logger.log(files[i].getName() + " is already attached.");
             exists = true;
           } else {
             attachments[j].deleteAttachment();
             Logger.log(attachments[j].getTitle() + " has been successfully deleted.");
           }
         }
    } 
    
    if (!exists){
       //page.addWebAttachment(files[i].getName(), '', files[i].getUrl());
       page.addHostedAttachment((files[i]), files[i].getName());
       files[i].setStarred(true);
       Logger.log(files[i].getName() + " attached to the File Cabinet page.");
       } 
    }
  Logger.log("Script Finished");
}