URLfetch in case of failure

I have two combined blocks in cells that are filled with ranges in a sheet. When the first one changes, I create a URL and call:

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

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

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

I put it in the whole method to see where it dies. I have an instruction right after a method UrlFetchApp.fetch()that never writes, so I know where the problem is. Can you make these types of calls during events?

Again, it works great by launching it manually through the script editor, but not when called from onEdit.

I see this question where they do not allow this, but the last comment in the thread said that it worked by adding another custom toEdit method. I call URLFetchAppfrom another method, but I tried to create the myOnEdit function and called URLFetchAppfrom there, but it still doesn't work. Not sure what he meant by manually contacting the event ...

+3
source share
1 answer

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 way to solve the problem is to create an 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
*/
+12
source

Source: https://habr.com/ru/post/1688114/


All Articles