2
votes

I have a script which uses googlesheets, I am new to this also, there the trigger onOpen() and when the sheet is opened it runs whats inside it, however I wanted to run a function even when sheet is not opened. Do I need a trigger to do this? What should I use to do this?

I tried:

OnCreate Installable OnEdit

I want to create a time-based trigger will the script run, even if the sheet is not open and/or even if I'm not logged in to Sheets.

I tried:

function onOpen() {

     // Trigger every 10 minutes.
    ScriptApp.newTrigger('myFunc') //Run Update All Sheets
        .timeBased()
        .everyMinutes(10)
        .create();

}

and outside the script. I wonder which of these works on background.

 // Trigger every 10 minutes.
ScriptApp.newTrigger('myFunc') //Run Update All Sheets
        .timeBased()
        .everyMinutes(10)
        .create();

function onOpen() {


}
2
You can do this through time-driven triggers. developers.google.com/apps-script/guides/triggers/…Anees Hameed
See tag info page for documentation and search the doc.TheMaster
@AneesHameed thanks man but I've already wandered on that docs, and I did try the sample in trigger.gsSlainesuuu
@TheMaster which one? For me I only know the installable, onOpen, onEdit Triggers, but I think not once of them solves my issue. As they only work when sheets is opened by userSlainesuuu
I edited the question to show my pitiful attempts to run my funcSlainesuuu

2 Answers

2
votes

It is possible to add a trigger programmatically.

https://developers.google.com/apps-script/guides/triggers/installable


Managing triggers programmatically
You can also create and delete triggers programmatically with the Script service. Start by calling ScriptApp.newTrigger(functionName), which returns a TriggerBuilder.

The following example shows how to create two time-driven triggers—one that fires every 6 hours, and one that fires every Monday at 9 a.m. (in the time zone that your script is set to).

triggers/triggers.gsView on GitHub
/**
 * Creates two time-driven triggers.
 */
function createTimeDrivenTriggers() {
  // Trigger every 6 hours.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .everyHours(6)
      .create();

  // Trigger every Monday at 09:00.
  ScriptApp.newTrigger('myFunction')
      .timeBased()
      .onWeekDay(ScriptApp.WeekDay.MONDAY)
      .atHour(9)
      .create();
}

This next example shows how to create an installable open trigger for a spreadsheet. Note that, unlike for a simple onOpen() trigger, the script for the installable trigger does not need to be bound to the spreadsheet. To create this trigger from a standalone script, simply replace SpreadsheetApp.getActive() with a call to SpreadsheetApp.openById(id).

triggers/triggers.gsView on GitHub
/**
 * Creates a trigger for when a spreadsheet opens.
 */
function createSpreadsheetOpenTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onOpen()
      .create();
}

To programmatically modify an existing installable trigger, you must delete it and create a new one. If you have previously stored the ID of a trigger, you can delete it by passing the ID as an argument to the function below.

triggers/triggers.gsView on GitHub
/**
 * Deletes a trigger.
 * @param {string} triggerId The Trigger ID.
 */
function deleteTrigger(triggerId) {
  // Loop over all triggers.
  var allTriggers = ScriptApp.getProjectTriggers();
  for (var i = 0; i < allTriggers.length; i++) {
    // If the current trigger is the correct one, delete it.
    if (allTriggers[i].getUniqueId() === triggerId) {
      ScriptApp.deleteTrigger(allTriggers[i]);
      break;
    }
  }
}



1
votes

Open your google sheet Click Tools Click Script Editor

Make a function. Example Birth day greeting every day is shown below

function hbd_mail() { //birth day mail daily 7 am from hrd login
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sheet = ss.getSheetByName("Emp");
   var today=new Date();
   lastRow = sheet.getLastRow();
   var values = sheet.getRange('A:T').getValues();
   for (i=1;i<lastRow;i++){
      if (values[i][14]=="") {} else {
      if ( Utilities.formatDate( values[i][14] ,"GMT+05:30", "dd-MMM") == Utilities.formatDate( today ,"GMT+05:30", "dd-MMM") && values[i][18]=="Yes"  ) {
      var message = 'Dear '+ values[i][1]+',\n\nMegawin Collective wishes you a very happy birth day ('+  Utilities.formatDate( values[i][14] ,"GMT+05:30", "dd-MMM")+ ').\n\nHRD Megawin Switchgear Pvt Ltd';
     var subject = "Happy Birth Day "+values[i][1];
     if (values[i][7]=="") {} else {GmailApp.sendEmail(values[i][7], subject, message);}
      }//if
     }
   }//for
}

Save the script
Click the Clock in the menu
It will ask you the function name - select the above function
Then select time based
Clcik every hour / day / week etc
Save it.

Now the function will run automatically from google server (at the frequency selected by you) without your having to do anything.

It is pretty simple.