自动备份Google表格,而无需复制表格中附加的表格 [英] Automatic Google Sheets Back Up without Copying the Forms attached to the Sheets
问题描述
我该如何制作一个脚本,将备份的Google表格复制到Google云端硬盘中的备份文件夹中,而不复制该表格所附的Google表单的副本?
How do I make a script that copies a Google Sheet as a backup into a backup folder in the Google Drive, but not making copies of the Google Forms that are attached to the Sheet?
function backUp() {
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " Copy " + formattedDate;
var destination = DriveApp.getFolderById("1NNhZESRq_0_DVBwzA8fvxSYGMpOa6KC_");
var file = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
file.makeCopy(name, destination);
}
此文件会自动将原始文件复制到备份文件夹"中,但还会复制与其链接的Google表单...因此,保存后,我每天都会获得10多个副本.
This one automatically copies the original files into the Backup Folder, but it also copies the Google Forms that are linked to it... so I end up with 10+ copies every day after it saves.
推荐答案
好问题.就我所知,这并不难,我明白了:)
Great question. It was little hard for my knowledge, I catch it :)
无法复制没有表单的整个电子表格,因为它们<魔术>连接(魔术==>连接处于任何公共API可能性下).
Is unable to copy whole Spreadsheet without Form, because they are magically connected (magically ==> connection is under any public API possibilities).
您可以使用copy sheet to another spreadsheed
功能.
我尝试过,但它不会创建Form的副本,是的!
I tried it and it doesn't create copy of Form, yeah!
但在这种情况下,请使用脚本&驱动笨拙的API.结果看起来太骇客了:
But for this case have Script & Drive unwieldy API. Result looks too hacky:
function backUp() {
//Open old spreadsheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
//Prepare new name
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' 'HH:mm:ss");
var name = spreadsheet.getName() + " Copy " + formattedDate;
//Prepare file system objects
var oldfile = DriveApp.getFileById(spreadsheet.getId());
var destination = DriveApp.getFolderById("1NNhZESRq_0_DVBwzA8fvxSYGMpOa6KC_");
//create new spreadsheet
var newSpreadsheet = SpreadsheetApp.create(name);
//move to destination folder (yep, it's hacky)
var newFile = DriveApp.getFileById(newSpreadsheet.getId());
var parents = newFile.getParents();
while (parents.hasNext()) {
var parent = parents.next();
parent.removeFile(newFile); //remove from default folder
}
destination.addFile(newFile);
//copy all sheets to new spreadsheet
var sheets = spreadsheet.getSheets();
sheets.forEach(function(sheet) {
sheet.copyTo(newSpreadsheet);
});
//remove empty sheet (created with new spreadsheet)
newSpreadsheet.deleteSheet(newSpreadsheet.getSheets()[0]);
}
这篇关于自动备份Google表格,而无需复制表格中附加的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!