以下代码允许我每天晚上7点复制(备份)我的电子表格。 然而,当我在早上检查我的文件夹时,我有大约20份副本。我只想制作一份。
我该如何改变?
见下面我的代码。
提前感谢您的帮助。
// 18 April 2018
// Google Apps Script to make copies of Google Sheet in specified destination folder
var RUNLOOP = true;
function createTriggers() {
var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY];
for (var i=0; i<days.length; i++) {
ScriptApp.newTrigger("makeCopy")
.timeBased().onWeekDay(days[i])
.atHour(19).create();
killTrigger();// delete the trigger
}
}
function makeCopy() {
for (i=0; i<1; i++){ //to make only one copy
// generates the timestamp and stores in variable formattedDate as year-month-date
var formattedDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "dd-MM-yyyy' 'HH:mm:ss");
// gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Backup " + formattedDate;
// gets the destination folder by their ID. with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("1NMA8nNIr2ZLZKm1PkKolORhyjLk3xKdx");
// gets the current Google Sheet file
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
// makes copy of "file" with "name" at the "destination"
file.makeCopy(name, destination);
}
}
function killTrigger(){
var trigger = ScriptApp.getProjectTriggers()[0];
ScriptApp.deleteTrigger(trigger);
}
答案 0 :(得分:1)
看起来你正在重新创建触发器而不删除它们,
以下是满足您要求的代码,
var RUNLOOP = true;
function createTriggers() {
killTrigger(); // Delete any previous triggers.
var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY];
for (var i=0; i<days.length; i++) {
ScriptApp.newTrigger("makeCopy")
.timeBased().onWeekDay(days[i])
.atHour(19).create();
}
}
function makeCopy() {
// generates the timestamp and stores in variable formattedDate as year-month-date
var formattedDate = Utilities.formatDate(new Date(), SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "dd-MM-yyyy' 'HH:mm:ss");
// gets the name of the original file and appends the word "copy" followed by the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Backup " + formattedDate;
// gets the destination folder by their ID. with your folder's ID that you can get by opening the folder in Google Drive and checking the URL in the browser's address bar
var destination = DriveApp.getFolderById("xxxyyy-zzz");
// gets the current Google Sheet file
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
// makes copy of "file" with "name" at the "destination"
file.makeCopy(name, destination);
}
function killTrigger(){
var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
ScriptApp.deleteTrigger(triggers[i]);
}
}
首次运行killTrigger()
功能一次,这将删除以前的所有触发器。现在也运行一次createTriggers()
方法。这将为您创建5个触发器(每个工作日1个)。它将在19小时运行并调用makeCopy()
方法。就是这样。
答案 1 :(得分:-1)