[英]Make copy of Google Spreadsheet on google shared drives via apps script
[英]Google apps script - Make a single copy of spreadsheet
以下代碼可讓我每天晚上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);
}
看起來您正在重新創建觸發器而沒有刪除它們,
這是滿足您要求的代碼,
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個)。 它將在19Hours上運行並調用makeCopy()
方法。 就是這樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.