[英]How to make a copy of a single sheet and save it to a folder in google sheets
I'm currently working on a problem I have where I have around 20 different sheets that I'm working with. 我目前正在解决一个问题,我正在处理大约20个不同的工作表。 With each sheet, I've created a button that says "export" that will save the single sheet (if I can figure it out) to a specified folder. 对于每张工作表,我创建了一个显示“导出”的按钮,该按钮会将单张工作表(如果我能弄清楚的话)保存到指定的文件夹。 I then have a script that clears the sheet so that it's new again (That works great!). 然后,我有一个脚本来清除工作表,以便再次使用它(效果很好!)。 I'm using this in our brewery to take the date of beers in tanks and when the tank is empty, the data is then stored for future reference. 我在我们的啤酒厂中使用它来获取罐中啤酒的日期,当罐中的啤酒为空时,数据将被存储以备将来参考。
So, as of now, the code work pretty much as I'd like it to, but it's saving all of the sheets as one file and now only exporting the sheet that I'm currently looking at. 因此,到目前为止,该代码的工作原理与我想要的差不多,但是它将所有工作表保存为一个文件,并且现在仅导出我当前正在查看的工作表。
Any help on this would be greatly appreciated. 任何帮助,将不胜感激。
I've tried a couple different codes and this is as close as I've gotten. 我尝试了几种不同的代码,而这与我所获得的差不多。
function createCopy() {
var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A29").getValue();
var destinationFolder = DriveApp.getFolderById("<<google drive folder>>");
DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId()).makeCopy(myValue,destinationFolder);
}
If all goes well, it will save a single sheet to my destination folder. 如果一切顺利,它将单张纸保存到我的目标文件夹中。
I don't think there is a way (or rather, I don't know of one) to copy just one sheet out of the spreadsheet, except by creating a new spreadsheet, copying your sheet there and moving that new spreadsheet in your folder. 我认为没有办法(或者说我不知道)从电子表格中仅复制一张表格,除非创建一个新的电子表格,将表格复制到该表格中,然后将新的电子表格移动到文件夹中。 Makes your code a bit longer, but I believe this should do the trick: 使您的代码更长一些,但是我相信这应该可以解决问题:
function createCopy() {
var myValue = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("A29").getValue();
var destinationFolder = DriveApp.getFolderById("<<google drive folder>>");
var backupSpreadsheetId = SpreadsheetApp.create(myValue).getId();
var backupSpreadsheet = SpreadsheetApp.openById(backupSpreadsheetId);
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().copyTo(backupSpreadsheet);
backupSpreadsheet.deleteSheet(backupSpreadsheet.getSheets()[0]);
var backupFile = DriveApp.getFileById(backupSpreadsheetId)
var currentParent = backupFile.getParents().next();
destinationFolder.addFile(backupFile);
currentParent.removeFile(backupFile);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.