Friday, August 17, 2012

Use Any Editor with Google Apps Script

The excellent syncing built into Google Drive makes it easy to create javascript files on your computer and run them within Google Apps. This approach means you can use your favorite text editor rather then the online code editor, but still easily test in Google Apps Script without needing to copy and paste repeatedly.

You can even write code in coffeescript and have it automatically compile to javascript and sync to the cloud, ready to test. Finally, although Google recently introduced Libraries in Google Apps Script, this approach represents a simple way to share code between projects while still supporting granular permissions.

To get started:
  1. Set up Google Drive syncing on your development computer.
  2. Create a new javascript or coffeescript file in your Google Drive folder (coffeescript folks may want to set up an automatic compilation watcher on that folder).
  3. Open your file in the online Google Drive. It will show in a document viewer, but the important thing to find is the document's id. This is in the url: https://docs.google.com/file/d/{DOCUMENT ID}/edit .
  4. Create a new Google Apps Script application and paste in the loader code below. Be sure to customize it for the document id you found in step 3. Also, if your code requires any permissions, you'll have to reference them here since Apps Script isn't able to find them in a loaded file.

That's it! When you save the file on your computer, it will sync to Google and Apps Script will load the latest version.  If you distribute your application to other users (for example, by including it within a shared spreadsheet) remember to set the sharing permissions on the javascript files you are loading or a folder containing both the application and javascript files.

You may also be interested in my open source Apps Script user interface library.

4 comments:

  1. very interesting use of the eval function. I will have to try it out.

    ReplyDelete
  2. Awesome! Can you elaborate on how to require permissions? I wish there was a way to automatically proxy the calls requiring permission so that the loaded code doesn't need to be refactored and can just do AppScriptObject.protected_function().

    ReplyDelete
    Replies
    1. Hi Geoffrey,

      Apparently Google scans the functions called in your code to determine which permissions to request from the user. Since the approach I've outlined loads your code at runtime, it doesn't show up in this scan and the user isn't prompted for permissions. To fix this, I call the functions that require permissions within a function in the loader script (called "bootstrap" in the example above). This function will never be run, but Google's scan picks it up and prompts the user so the actual function calls within the loaded code will work. In the example above, I've done this with the "msgBox" permission. Hope this helps!

      Dan

      Delete
  3. For huge, multi-project files, would your solution work? Would I need to create a "mirror" file in my Google apps script project for each corresponding file on my Google Drive?

    ReplyDelete