脚本的其中一个步骤是获取有关城市当前天气的信息。但是有时,如果我要查找,则在API数据库中找不到城市名称,这会导致完全暂停脚本其余部分的错误。我想知道我可以调整什么,以便在搜索城市时出现错误,它将跳至脚本的下一步。
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});
}
当API找不到我要找的城市时,该错误会在第41行发生(var response = UrlFetchApp.fetch(apiCall);)
如果发生此错误,我希望不再暂停脚本,而是从第83行继续(var电子表格= SpreadsheetApp.getActiveSpreadsheet(); // planilha ativa)
答案 0 :(得分:2)
使用UrlFetchApp参数检查是否返回有效响应:
muteHttpExceptions:布尔值:如果为true,则如果fetch不抛出异常 响应代码表示失败,而是返回HTTPResponse。 默认值为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
检查作为错误响应对象得到的结果。但是错误响应应该是404。