Create Time-driven Trigger in Gmail Add-on using Google Apps Script


Time-driven triggers

As per the definition from Google developers this link,"A time-driven trigger (also called a clock trigger) is similar to a cron job in Unix. Time-driven triggers let scripts execute at a particular time or on a recurring interval, as frequently as every minute or as infrequently as once per month."

 So let's see how to create a Time-driven trigger using Google Apps Script(GAS) for a project. If you are new to Google Apps Script follow these links:
https://developers.google.com/apps-script/overview

Set up:

  1.  Visit script.google.com to open the Script editor. 
  2. Create a New Script with any name you want. Here I am using MyApp.
  3. The first file with sample myFunction you can see is Code.gs
  4.  Now go to View -> Show manifest file, it will display the manifest file appsscript.json, This file has a basic project information that Apps Script needs to run the script successfully. For more information about manifests visit this link .

 Creating Time-driven Trigger: 

  1. Now in code.gs file write the following function and Save the code using Ctrl+Shift+S or Cmd+Shift+S in mac, 

    function myTrigger() {
      // code you want to trigger on each time interval
      Logger.log('Trigger on work.');
    }
  2.   Now go to Edit -> Current Project's Triggers. Click on  No triggers set up. Click here to add one now.

  3. Select trigger method, in this case myTrigger()
    Trigger type -> Time-driven
    Timer -> whatever you want, here I selected Minutes timer
    Frequency -> Every Minute
  4. Click on Save to save changes.
    Adding Time-driven trigger
  5. Now press Ctrl+Enter or Cmd+Enter in mac to open Logs window to see the logs generated Logger.log() of myTrigger() function. Or go to View -> Logs.You can see the message 'Trigger on work' with the timestamp. Every minute this message will generate with different timestamp.

This is how you can create Time-driven trigger in Google Apps Script. It can be created for Spreadsheet, Docs, Forms or Gmail addon.

Comments