我对如何确保我的应用脚本获取Google表格的最新更改感到困惑。我的工作表上有一个图像按钮,它调用一个名为StartSync的脚本(如下所示)。这个想法是它从活动工作表中获取数据,然后将其发送到Firebase。
但是如果用户更改了一个单元格然后运行该脚本,那么发送的数据似乎只有几分钟的时间。换句话说,如果我检查哪些数据出来了,它似乎并不反映最近的变化。事实上,当我第一次打开窗口并出现“工作......”字样时,传出的数据看起来就像是状态。
我不确定这是不是因为脚本早前被评估了?或者是否有一些App Script命令可用于确保在找到API时找到最新的更改?任何帮助非常感谢。
function syncMasterSheet(excelData) {
/*
We make a PUT (update) request,
and send a JSON payload
More info on the REST API here : https://firebase.google.com/docs/database/rest/start
*/
var options = {
method: 'put',
contentType: 'application/json',
payload: JSON.stringify(excelData)
};
var fireBaseUrl = getFirebaseUrl('language')
/*
We use the UrlFetchApp google scripts module
More info on this here : https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app
*/
try {
var response = UrlFetchApp.fetch(fireBaseUrl, options);
var userResponse = ui.alert('Done syncing. Do you need to take a closer look at what data went out?', ui.ButtonSet.YES_NO);
if (userResponse == ui.Button.YES) {
ui.alert(response);
}
}
catch(e) {
SpreadsheetApp.getActiveSpreadsheet().toast(e);
}
}
function startSync() {
//Get the currently active sheet
var sheet = SpreadsheetApp.getActiveSheet();
SpreadsheetApp.getActiveSpreadsheet().toast('Synced.');
//Get the number of rows and columns which contain some content
var [rows, columns] = [sheet.getLastRow(), sheet.getLastColumn()];
//Get the data contained in those rows and columns as a 2 dimensional array
var data = sheet.getRange(1, 1, rows, columns).getValues();
//Use the syncMasterSheet function defined before to push this data to the "masterSheet" key in the firebase database
syncMasterSheet(data);
}
答案 0 :(得分:1)
SpreadsheetApp.flush()
应该更新电子表格。把它放在事情的开头
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#flush()