0

Per code below, a direct call to getWeather in the editor executes successfully to completion. But when triggered via onEdit in the spreadsheet, it properly displays my debug line "In getWeatherDaily1" but never completes after that. What am I doing wrong? I need to use onEdit to run my function.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("Waypoints");

function onEdit(e) {
  if (e.source.getSheetName()==sh.getName()) {
    getWeather();
  };
};

function getWeather() {
  var lat = 34.639702;
  var lng = -84.191499;
  getWeatherDaily(lat,lng);
};

function getWeatherDaily(theLat,theLng) {
  var gpsURL = "https://api.weather.gov/points/" + theLat + "," + theLng;
SpreadsheetApp.getUi().alert("In getWeatherDaily1 " + gpsURL + "."); // Debug
  var gpsResponse = UrlFetchApp.fetch(gpsURL, {method: 'GET',headers: {'Content-Type': 'application/json','Accept': 'application/json'} });
SpreadsheetApp.getUi().alert("In getWeatherDaily2 " + gpsResponse); // Debug
};
3
  • Log more of the steps to see what is going on. Not alerts Logger.log(varToLog)
    – Blindspots
    Jan 2, 2023 at 18:28
  • Welcome to Web Applications Stack Exchange. Jan 2, 2023 at 18:41
  • Not sure how to do that in the editor with onEdit. Can you give me a clue? I can run getWeather directly in the editor, but that is not where the problem is.
    – Boof
    Jan 2, 2023 at 18:42

1 Answer 1

0

You can check My Executions for failed executions, and view those logs to learn more about why the function fails.

Chances are that the function is failing because it cannot access the UrlFetch class. Simple triggers such as onEdit(e) run in a restricted context where not all classes are available. See Authorization modes.

To make it work, use an installable trigger instead of a simple trigger.

2
  • I didn't even know I could look at past executions. That's great. I see it is a permissions issue. The reason I'm using onEdit is to run this in my phone. Maybe installable triggers won't work on mobiles. I'll look into it.
    – Boof
    Jan 2, 2023 at 18:49
  • Installable triggers work fine on mobile — Apps Script runs on Google's servers rather than in your end device. Jan 2, 2023 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.