Start up for Google script

Google Script

Google service, let you create some script/macro for your documents/mail/calendar/… it based on javascript, and the site is:

script: https://script.google.com
advance google service api: https://console.cloud.google.com

reference development documents

Google release the development reference documents in

https://developers.google.com/apps-script/reference/

we can find the development api in there.

how to create a new script

it have 2 methods to create new script:

create new script from script.google.com

click ‘new script’ from https://script.google.com, then give it a project name

create new script from documents

in your documents (sheets, writer, …), choose “Script editor” from menu/Tools, and give it a project name

how to debug a script

it have many methods, I give 2 methods in here, add the code to your scripts:

Logger

e.g.
Logger.log('string');
Logger.log('string: %s', xxx);

and it will be shown in menu/View/Logs

console

e.g.
console.info('string');
console.info('string: %s', xxx);

and it will be shown in menu/View/Stackdriver logging

how to run the script

run function from script editor

the most easy way, you just use the menu/Run/Run Function in your script editor

run function from additional menu in your documents

before you start run the function, you have to add a additional menu to your documents, following is the example code:

  • first you need crate a function with function name – onOpen
  • this example is using google sheet
function onOpen() {
  var sheet = SpreadsheetApp.getActive();
  var menuItems = [
    {name: 'example', functionName: 'example'}
  ];

  sheet.addMenu('just sample', menuItems);
}
  • second, create the function “example”
function example() {
  console.info('run example');
}

then you need reopen your documents (close it then open again), after this you can run the script by using menu/just sample/example

run function from trigger events

there 2 way to register a new trigger event, and google provides many kind of events to use.

register by script.google.com

open one of projects from your script.google.com, you can find the menu in “…” icon, choose “trigger”, then you just following the dialog to select the current function and trigger evnets for your script.

register by function

in this case, you only need run the code just once, the trigger will be stay in your trigger list till you remove it. the code will be:

  • this example is using calendar event
function registerTrigger() {
  ScriptApp.newTrigger('triggerExample')
    .forUserCalendar('xxx@gmail.com')
    .onEventUpdated()
    .create();
}

function triggerExample(trigger_event) {
  console.info('trigger event: %s', trigger_event);
}

Google Script - calendar

Calendar Event Update

description

in this case, I used Google extend service api to get which day event is updated while onEventUpdated is triggered.

because the trigger event is not contain calendar event information, (e.g.) {calendarId=xxx@gmail.com, authMode=FULL, triggerUid=615558}

we need find it by ourself, the code will use the calendar cloud api to implemented. it will get the last updated event’s title.

pre-condition

  1. the trigger event are already registered with function - triggerCalUpdate
  2. all of permissions are allowed
  3. the “Google Calendar API” is enabled in your console.cloud.google.com

code

function triggerCalUpdate(trigger_event) {
  var day = new Date();
  var one_day = 24 * 60 * 60 * 1000;
  var start_day = new Date(day.getTime() - 360 * one_day);
  var end_day = new Date(day.getTime() + 360 * one_day);

  // set the events range are between one year ago to one year later
  var str_start = Utilities.formatDate(start_day, "GMT+8", "yyyy-MM-dd'T'HH:mm:ss'Z'");
  var str_end = Utilities.formatDate(end_day, "GMT+8", "yyyy-MM-dd'T'HH:mm:ss'Z'");

  var resource = {
    "orderBy" : "updated",
    "timeMin" : str_start,
    "timeMax" : str_end,
  };

  var events = Calendar.Events.list(triggerEvent.calendarId, resource).items;
  var index = events.length - 1;

  var calendar = CalendarApp.getDefaultCalendar();
  var event = calendar.getEventById(events[index].iCalUID);

  console.info('event title: %s', event.getTitle());
}

use case

  1. Goto your calnedar.google.com, then add a event
  2. Edit some event from your calendar

you can see the log to verify the result.