Introducing the Script Service

Thursday, April 5, 2012 | 12:00 PM


Triggers in Apps Script allow you to schedule your script’s execution in response to an event, at a specific time and date, or at given time intervals. Publishing scripts as services allows you to deploy your web apps with a click of a button. The new Script service lets you perform both of these tasks programmatically. With the Script service at your disposal, you can create smooth install flows, chain your scripts’ executions, generate summaries of current triggers, and even programmatically publish the scripts as services.

Create smooth install flows

You can now set up your triggers from a menu. Here is an example of how you would schedule function foo() to run one year from now via a custom menu in Google Spreadsheets:

function onOpen() {
             [{name: "Create Trigger", 
               functionName: "createTrigger"}]);

function createTrigger() {
  var now = new Date();

Chain script executions

By programmatically setting triggers, you are able to schedule a future script’s executions from the script that is currently running. Here is an example of a function that schedules another function to execute two hours after it completes:

// this function is run to send the initial reminder and schedule a subsequent one
function sendRequest() {
                     "Assignment #1 is due in 2 and a half hours!",
                     "Your first assignment is due in 2 and a half hours!");
  var now = new Date();
  var later = new Date(now.getTime() + 2 * 60 * 60 * 1000);

// this function will execute two hours later, sending another reminder
function sendReminder() {
                     "Assignment #1 is due in 30 minutes!",
                     "Your first assignment is due in half an hour!");

Generate a summary of current triggers

With the ability to schedule triggers programmatically, it is important to keep track of the existing ones. The function below logs all the triggers that are associated with the current script. Alternatively, you could also write this summary to a spreadsheet or email it to yourself.

function logMyTriggers() {
  var triggers = ScriptApp.getScriptTriggers();
  for(i in triggers) {
    Logger.log("Trigger ID: " + triggers[i].getUniqueId()
               + "\nTrigger handler function: "
               + triggers[i].getHandlerFunction()
               + "\nTrigger type: " + triggers[i].getEventType()
               + "\n----------------------");

Publish scripts programmatically

You can now let your users publish your scripts through a Google Spreadsheets custom menu as a part of the script’s set up process. Below is an illustration of this process using an example of a simple note-taking web app. Once a user clicks the menu item, the script becomes published as a service and the URL is presented back to the user via a dialog box.

function doGet(e) {
  var app = UiApp.createApplication();
  createApp(app, false);
  return app;

function doPost(e) {
  var now = new Date();
                    "Notes taken at " + now, e.parameter.notes);
  var app = UiApp.getActiveApplication();
  createApp(app, true);
  return app;

function createApp(app, isSubmitted) {
  var vp = app.createVerticalPanel();
  var title = app.createHTML("Enter your notes into the text area below." +
                             "<br>Hit submit to email them to yourself!")
                 .setStyleAttribute("fontSize", "25px");
  var lbl = app.createLabel("Notes submitted!")
               .setStyleAttribute("fontSize", "17px");
  var form = app.createFormPanel();
  var notes = app.createTextArea()
  var sb = app.createSubmitButton("Submit");
  if (isSubmitted)
  return app.add(form);

function onInstall() {

function onOpen() {
             [{name: "Publish as a service.", 
               functionName: "setup"}]);

function setup() {
  var url = "";
  try {
    url = ScriptApp.getService().getUrl();
    Browser.msgBox("Your web app is now accessible at the following URL:\n"
                   + url);
  } catch (e) {
    Browser.msgBox("Script authorization expired.\nPlease run it again.");

The Script service provides you with the means of creating even more powerful applications and makes the development and deployment process smooth.

For more information, please visit the Script service Reference Documentation and User Guide.

Anton Soradoi profile

Anton is a Developer Support Specialist at Google. He works closely with Apps Script developers to help them discover its full potential. Prior to Google, he worked as a Web Developer.


Dave said...

nice features being added every month, please fix the issue of Apps Script Gadget not working with custom web address mapping first, that is the major problem for google apps script developers, hopefully you will announce something soon.

Stephen Hind said...

I couldn't agree more Dave: there's no point in using Apps Script when you cannot use it on your mapped website. When you think this is just an arbitrary decision by Google and they can fix it in seconds then it's just infuriating!

Blair Kutzman said...

Very cool!!

Yohannes said...

I love apps script, specially how it easily integrates with google services, but the gadget not working with mapped websites is very sad, that is 2nd most starred issue, but so far no fix after almost a year

Best Business Brands said...

Triggers in Apps Script allow you to schedule your script's execution in response to an event, at a specific time and date, or at given time ...

Marcos Novacovsky said...

Since the introduction of scripts version management, publishing of scripts programmatically is not working anymore.

Hope this be solved asap...