我有下面的腳本正在運行,但添加utilities.sleep
和應用程序刷新功能後,它不是。任何人都可以看到我做錯了什麼?當「nameofspreadsheet」運行時,腳本需要幾秒鐘的時間才能運行,並在工作簿名稱中放入正確的工作表名稱A2。因此,這意味着備份腳本運行,但是將01/01/1970輸入到歸檔表中,而不是使用該表的日期(我可能提前幾個月創建多個副本,因此只需在備份時使用)。Utilities.sleep沒有按預期運行
function onOpen() { // This function adds a custom menu to the spreadsheet (Backup to archive) so you can run the script from there.
var ui = SpreadsheetApp.getUi();
ui.createMenu('Backup')
.addItem('Backup','dataBackup')
.addToUi();
}
function nameOfSpreadsheet()
{
var s=SpreadsheetApp.getActive().getName().replace(/(\d{1,2}\.\d{1,2}\.\d{1,2}).*/,
'$1');
return s;
}
function dataBackup() {
var inputSS = SpreadsheetApp.getActiveSpreadsheet();
var archiveSS = SpreadsheetApp.openById('146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g');
var user = Session.getActiveUser().getEmail();
var sheetNames = ['AM trip', 'PM trip', 'Pool/Beach', 'Night Dive'];
for (var i = 0; i < sheetNames.length; i++) {
var inputSheet = inputSS.getSheetByName(sheetNames[i]);
var archiveSheet = archiveSS.getSheetByName(sheetNames[i]);
var date = inputSheet.getRange('A2').getValue(); // Changed to stop inadvertent cell changes, also made text white so not seen.
var data = inputSheet.getRange('E7:U37').getValues().filter(function(row) { return row[0] !== '' || row[1] !== ''});
for (var x = 0; x < data.length; x++) {
data[x].splice(0, 0, date);
}
var getDate = archiveSheet.getRange(archiveSheet.getLastRow(), 1).getValue();
var maxRowLength = data.reduce(function(length, row) { return Math.max(length, row.length); }, 0);
var date = new Date(date);
if (date.getDate() === "Loading Data...") {
Utilities.sleep(10000);
SpreadsheetApp.flush();
if (getDate.getDate() != date.getDate() || getDate.getMonth() != date.getMonth()) {
if (data.length != 0) {
archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), data.length);
archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, data.length, maxRowLength).setValues(data);
} else {
archiveSheet.insertRowsAfter(archiveSheet.getLastRow(), 1);
archiveSheet.getRange(archiveSheet.getLastRow() + 1, 1, 1, 2).setValues([[date, 'No Data']]);
}
}
}
}
}
錯誤的日期在哪裏出現?您的代碼似乎可以將正確的日期添加到數據行的頭部,因此您在哪裏看到零日期輸出? –