我有一个归档脚本现在可以正常工作,但是在备份时我确实从第一张单元格A2中得到了正确的日期,但在另外3张表格中,我在归档日期戳列中获得了01/01/1970片。所以我假设我需要暂停脚本以允许新页面从工作表名称加载A2中的日期。我在第33行添加了一个停顿,但现在我在所有工作表中都获得了01/01/70!我现在让它一夜之间运行,当我检查这个时,我有ll 4张01/0/1970,所以我假设延迟不够或在脚本中的错误位置,有人可以帮忙吗?
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);
var getDate = new Date(getDate);
if (getDate.getDate() === "Loading Data...") {
Utilities.sleep(10000);
}
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']]);
}
}
}
}