function recordCwaWeather() {
const sheetId = '你的 Google Sheet ID';
const sheetName = '工作表1';
const authorizationCode = '你的 CWA API 授權碼';
const apiUrl = 'https://opendata.cwa.gov.tw/...';
try {
const options = {
'method': 'GET',
'headers': { 'Authorization': authorizationCode }
};
const response = UrlFetchApp.fetch(apiUrl, options);
const data = JSON.parse(response.getContentText());
const spreadsheet = SpreadsheetApp.openById(sheetId);
const sheet = spreadsheet.getSheetByName(sheetName);
if (data && data.records && data.records.location) {
data.records.location.forEach(location => {
// ... 資料解析邏輯 ...
const rowData = [new Date(), location.locationName, ...];
sheet.appendRow(rowData);
});
}
} catch (error) {
Logger.log('發生錯誤:' + error.message);
}
}