我正在寻找一种自动备份Google表格文件的方法,而无需功能,只需数字即可。
我从各种渠道搜集了2个脚本:
// Abhijeet Chopra
// 26 February 2016
// Google Apps Script to make copies of Google Sheet in specified destination folder
function makeCopy() {
// generates the timestamp and stores in variable formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd' '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() + " Copy " + formattedDate;
// gets the destination folder by their ID. REPLACE xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 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("1ysgERhjmrnq5Uzb9Lu7CtqOWccVTHyVj");
// 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 getRangeValues() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A2:B4");
var values = range.getValues();
return values;
};
来自多个工作表文件的数据被拉入一个主文件中,我想每个星期备份一次,但是问题是'makeCopy'函数的问题,被拉入主文件副本中的数据将来自原始工作表,因为我正在使用importrange函数,该函数需要唯一的工作表ID,而副本具有另一个ID。如何将这两个结合在一起?
试试这个:
function makeCopy() {
var folder=DriveApp.getFolderById("1y66aE2WuaRQQM5fyevXEl5uhJamk9VF7");
var ss=SpreadsheetApp.getActive();
var file=DriveApp.getFileById(ss.getId());
var f=file.makeCopy(folder);
var copy=SpreadsheetApp.openById(f.getId());
var shts=copy.getSheets();
SpreadsheetApp.getUi().alert('Go to other sheet to authorize Import. Hit Okay after authorizing the Import');
shts.forEach(function(sh){
var rg=sh.getDataRange();
var dvA=rg.getValues();
sh.clearContents();
rg.setValues(dvA);
});
}
我认为问题在于我们需要在另一张纸上授权进口。尝试一下。查看它是否适用于你的电子表格。
谢谢!对其进行了尝试,并进行了一些调整之后,它开始起作用,但是有些事情很有趣:最后一部分清除了内容,而setValues清除了原始文件中的引用,而不是刚刚制作的副本。我该如何解决?我将在下面的答案中张贴我使用的脚本,以便您查看!我们越来越近了!
您是对的,我已对其进行了修改,现在它正在删除副本中的公式。
谢谢!现在可以正常工作,副本中的公式已删除,但是被拉到原始文档中的数据现在不存在。我在A1单元格中使用了'IMPORTRANGE'功能,其余的工作都完成了,现在A1中有一个#REF,整个工作表都是空的。我可以考虑一种解决方法,方法是让脚本查看原始文件中的值,然后用副本中的纯原始值替换公式和所有内容。您认为可以做到吗?
在我的示例中,我没有遇到任何数据或公式丢失的情况。您可以共享电子表格的示例副本吗?
就是这样: docs.google.com/spreadsheets/d/… ,这是复制的版本: docs.google.com/spreadsheets/d/…