2
votes

I'm playing around with Google Apps script and encountering a weird error when trying to programmatically create a trigger (not bound to a specific file or anything).

I am trying to run a certain code - basically to edit a document in Google Docs whenever it opens, but I'm experiencing this error:

Unexpected error while getting the method or property create on object ScriptApp.DocumentTriggerBuilder. (line 9, file "Code")DetailsDismiss

Here's the code that I'm using:

 /**
 * Creates a trigger for when a spreadsheet opens.
 */
function createSpreadsheetOpenTrigger() {
  var d = DocumentApp.getActiveDocument();
  ScriptApp.newTrigger('myFunction')
        .forDocument(d)
        .onOpen()
        .create();
}


function myFunction() {
     // change the body or something...
}
2

2 Answers

1
votes

getActiveDocument won’t work if the script isn’t bound to a document. You want openById.

0
votes

Why are you trying to create a trigger on open? onOpen is a trigger itself.

Try this

function onOpen(e)
{
    myFunction();
}