Reputation: 2147
One of the steps of the script is to get information about the current weather of a city. But sometimes the city name is not found in the API database if I am looking for, causing an error that completely pauses the rest of the script. I would like to know what I can adjust so that if there is an error searching the city, it will skip to the next step of the script.
function TudoCompleto() {
var spreadsheet = SpreadsheetApp.getActive();
//Clima
spreadsheet.getRange('Monster!A1').activate();
spreadsheet.getCurrentCell().setFormula('=IMPORTXML(\'Clima\'!F20,"//*[@id=\'page_team_1_block_venue_info_3\']/div/div[1]/dl/dt")');
spreadsheet.getRange('Clima!F21:F45').activate();
spreadsheet.getRange('Monster!A1:A25').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
//Clima 2
spreadsheet.getRange('Monster!A1').activate();
spreadsheet.getCurrentCell().setFormula('=IMPORTXML(\'Clima\'!F20,"//*[@id=\'page_team_1_block_venue_info_3\']/div/div[1]/dl/dd")');
spreadsheet.getRange('Clima!G21:G44').activate();
spreadsheet.getRange('Monster!A1:A24').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
//CLIMA
spreadsheet.getRange('Clima!A3:B8').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Clima");
var apiKey = "MY API KEY"; // Or you can try my key
var cityName = sheet.getRange("A1").getValue();
// Go to https://openweathermap.org, register and get a free API key
var apiCall = "api.openweathermap.org/data/2.5/weather?q=" + cityName +"&appid=" + apiKey;
var response = UrlFetchApp.fetch(apiCall);
var data = JSON.parse(response.getContentText());
Logger.log(response.getContentText());
var weather = data["weather"][0]; //It's an array
var sys = data["sys"];
var main = data["main"]
var location = data["name"];
var country = sys["country"];
var weatherDesc = weather["main"];
var temp = main["temp"];
var minTemp = main["temp_min"];
var maxTemp = main["temp_max"];
var weatherData = [
["Location:", location],
["Country:", country],
["Weather:", weatherDesc],
["Teaperture:", temp],
["Min Temp:", minTemp],
["Max Temp:", maxTemp]
];
sheet.getRange(3, 1, weatherData.length, weatherData[0].length).setValues(weatherData);
//The API Call works
// The Data will be retrieved like below JSON file.
//{"coord":{"lon":139,"lat":35},
//"sys":{"country":"JP","sunrise":1369769524,"sunset":1369821049},
//"weather":[{"id":804,"main":"clouds","description":"overcast clouds","icon":"04n"}],
//"main":{"temp":289.5,"humidity":89,"pressure":1013,"temp_min":287.04,"temp_max":292.04},
//"wind":{"speed":7.31,"deg":187.002},
//"rain":{"3h":0},
//"clouds":{"all":92},
//"dt":1369824698,
//"id":1851632,
//"name":"Shuzenji",
//"cod":200}
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // planilha ativa
var sheet = spreadsheet.getSheetByName("Clima");
for (var i = 1; i < 10; i++) {
var cel = "H"+ i //
if (sheet.getRange(cel).getValue() != "") {
for (var u = 1; i < 1000; u++) {
var cel2 = "M"+ u //
if (sheet.getRange(cel2).getValue() == "") {
sheet.getRange("M"+u).setValue(sheet.getRange(cel).getValue());
break;
};
}
}
};
spreadsheet.getRange('Monster!A1').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
spreadsheet.getRange('Clima!F16').activate();
spreadsheet.getActiveRangeList().clear({contentsOnly: true, skipFilteredRows: true});
}
When the API does not find the city I am looking for, the error happens on line 41 (var response = UrlFetchApp.fetch (apiCall);)
If this error happened, I would like instead of pausing the script, it would continue from line 83 (var spreadsheet = SpreadsheetApp.getActiveSpreadsheet (); // planilha ativa)
Upvotes: 0
Views: 2639
Reputation: 874
Use UrlFetchApp parameters to check if valid response comes back:
muteHttpExceptions : Boolean : If true the fetch doesn't throw an exception if the response code indicates failure, and instead returns the HTTPResponse. The default is false.muteHttpExceptions
[https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app][1]
options = { 'muteHttpExceptions' : true}
var response = UrlFetchApp.fetch(apiCall, options)
if (reponse != 404){ code block to do if good response till line 83}
line 83
Check what you get as a bad reponse object. But bad response should be 404.
Upvotes: 3