我正在尝试集成此天气数据API,以自动将数据输出到Google表格中。运行脚本时收到以下错误:TypeError:无法从未定义中读取属性“ 0”。 (第43行,文件“代码”)
对于上下文,以下各列具有以下数据:
A:第#行
B:城市编号
C:城市名称
D:时区调整-1
E:经度
F:纬度
//SECTION 3: WEATHER DATA PULL
function getSheetName() {
//Return the name of the active sheet (tab)
return
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetName();
}
//Climacell WeatherData - Hourly function getClimaCell() {
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ClimacellPull");
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var weather_data = sheet.getSheetValues(3,5,200,80); //Up to 200 cities, getSheetValues(startRow, startColumn, numRows, numColumns);
var numRows = lastRow - 1;
for(var i = 0; i var lat = weather_data[i][0];
var lng = weather_data[i][1];
if(lat !== '') {
var oneDayAhead = new Date(Date.now() + (24 * 60 * 60 * 1000));
var oneDayAheadString = oneDayAhead.toISOString();
var url = 'https://api.climacell.co/v3/weather/forecast/daily?lat=' + lat + '&lon=' + lng + '&num_days=1&num_hours=24&unit_system=us&fields=temp,precipitation,precipitation_accumulation'; //switch us 'to' 'si' if need to convert to metric system
// var url = 'https://api.climacell.co/v3/weather/forecast/daily?lat=' + lat + '&lon=' + lng + '&start_time=now&end_time=' + oneDayAheadString + '&unit_system=us&fields=temp,precipitation,precipitation_accumulation'
var options = {
'method': 'get',
'muteHttpExceptions': true,
'headers': {
'accept': 'application/json',
'apikey': 'API KEY REDACTED FROM STACK OVERFLOW'
}
};
var response = UrlFetchApp.fetch(url, options);
var json = response.getContentText();
var data = JSON.parse(json);
sheet.getRange(3+i,7).setValue(data.daily[0].temp[0].min.value); //Daily Min Fahrenheit
sheet.getRange(3+i,8).setValue(data.daily[0].temp[1].max.value); //Daily Max Fahrenheit
sheet.getRange(3+i,9).setValue(data.daily[0].precipitation_accumulation.value); //Daily Precipitation Accumulation in in
sheet.getRange(3+i,10).setValue(data.daily[0].precipitation[0].max.value); //Daily Max Precipitation Intensity in in/hr
// for (var j = 0; j // sheet.getRange(2,11+j).setValue(data.hourly[j].observation_time.value); //Hourly TimeStamp
// }
//Hourly Data for (var k = 0; k sheet.getRange(3+i,11+k).setValue(data.hourly[k].temp.value); //Hourly Temperature in Fahrenheit
sheet.getRange(3+i,35+k).setValue(data.hourly[k].precipitation.value);
//Hourly Precipitation Intensity in/hr
}
}
}
}
我希望脚本将天气数据转储到Google表格中,但遇到上述错误。