Question

I have two combo boxes in cells that are populated by ranges in the sheet. When the first one changes, I build a URL and call:

var resp = UrlFetchApp.fetch(url, {"method": "get","muteHttpExceptions":false});

And it populates a 2nd range which controls the 2nd combo box. This works just fine in debug mode. But my goal is for this to work using the onEdit mode. If the first combo box is changed, the method runs and populates the second. Using this statement:

mydoc.getActiveSheet().getRange(15, 1).setValue("Some debug message");

I have it placed throughout the method to see where it dies. I have a statement right after the UrlFetchApp.fetch() method that never writes, so I know that's where the problem is. Can you make these types of calls during events?

Again, it works just fine running it manually via the script editor, but not when called from onEdit.

I see this question where they do not allow it, but the last comment in the thread said he got it to work by attaching another custom method to onEdit. I call URLFetchApp from another method, but I tried creating a myOnEdit function, and called URLFetchApp from there, but it still fails. Not sure what he meant by manually attaching to the event...

Was it helpful?

Solution

Using a simple trigger:

function onEdit(e) {
  var response = UrlFetchApp.fetch('http://www.google.com/');
  Logger.log(response.getContentText());
}

I get the following error (View -> Execution transcript):

Execution failed: You do not have permission to call fetch (line 2, file "Code") [0.008 seconds total runtime]

One possible workaround is to create a installable trigger:

function myOnEdit(e) {
  var response = UrlFetchApp.fetch('http://www.google.com/');
  Logger.log(response.getContentText());
}

/*
In the Spreadsheet, create new trigger:
Run: myOnEdit
Events: From spreadsheet
On edit
*/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top