Google TypeError: ScriptApp.newTrigger(...).forSpreadsheet(...).timeBased is not a function

Viewed 529

I get error message: "TypeError: ScriptApp.newTrigger(...).forSpreadsheet(...).timeBased is not a function.." (in both cases below)

function testTrigger1(){
    var ss = SpreadsheetApp.getActive().getId();
    ScriptApp.newTrigger('processAccessRequests')
    .forSpreadsheet(ss)
    .timeBased()
    .everyMinutes(30)
    .create(); 
}

function testTrigger2(){
    var ss = SpreadsheetApp.getActive();
    ScriptApp.newTrigger('processAccessRequests')
    .forSpreadsheet(ss)
    .timeBased()
    .everyMinutes(30)
    .create(); 
}

Does this not work from a spreadsheet script? Or do I need to do something with authorization?

2 Answers

The error is pretty explicit :). It means that the function timeBased() does not exist in the SpreadsheetTriggerBuilder class.

When you call forSpreadsheet(key) it returns a SpreadsheetTriggerBuilder object, hence the error when trying to call timeBased() on it.

If you are creating a trigger which is time based, irrelevant of the editor use the ClockTriggerBuilder. You get an instance of it by calling timeBased() on a TriggerBuilder instance.

Example:

function timeTrigger(handler, mins){
    ScriptApp.newTrigger(handler)
    .timeBased()
    .everyMinutes(mins)
    .create(); 
}

forSpreadsheet() is only used when you need a spreadsheet based trigger like edit from a spreadsheet or opening the spreadsheet. For time based triggers, you don't need forSpreadsheet

Related