【发布时间】:2020-11-24 19:48:19
【问题描述】:
所以我有一个我在另一个项目中使用的存档器,用于逐页制作电子表格内容的存档副本。我的问题是,该存档器用于获取其他项目的电子表格的所有工作表。对于这个项目,我只希望某些工作表位于工作表数组中(不是全部)。我怎样才能修改这个功能来做到这一点?我尝试了几种方法,但它们总是搞砸地图功能。任何帮助将不胜感激,谢谢。
function Archive() {
var ui = SpreadsheetApp.getUi(); // Same variations.
var result = ui.prompt(
'What would you like to call this file?',
'Please enter file name:',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
if (button == ui.Button.OK) {
// User clicked "OK".
var spreadsheetId = "Spreadsheet ID"; // Please set the source Spreadsheet ID.
var destFolderId = "Target Folder ID"; // Please set the destination folder ID.
// Copy each sheet in the source Spreadsheet by removing the formulas as the temporal sheets.
var ss = SpreadsheetApp.openById(spreadsheetId);
var arraysheets = ["Budget Input","Budget (Universal)","Covid Parameters"]
var tempSheets = ss.getSheets().map(function(sheet) {
var dstSheet = sheet.copyTo(ss).setName(sheet.getSheetName() + "_temp");
var src = dstSheet.getDataRange();
src.copyTo(src, {contentsOnly: true});
return dstSheet;
});
// Copy the source Spreadsheet.
var destination = ss.copy(text);
// Delete the temporal sheets in the source Spreadsheet.
tempSheets.forEach(function(sheet) {ss.deleteSheet(sheet)});
// Delete the original sheets from the copied Spreadsheet and rename the copied sheets.
destination.getSheets().forEach(function(sheet) {
var sheetName = sheet.getSheetName();
if (sheetName.indexOf("_temp") == -1) {
destination.deleteSheet(sheet);
} else {
sheet.setName(sheetName.slice(0, -5));
}
});
// Move file to the destination folder.
var file = DriveApp.getFileById(destination.getId());
DriveApp.getFolderById(destFolderId).addFile(file);
file.getParents().next().removeFile(file);
ui.alert('Your file name is recorded as ' + text + '.');
}
if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
ui.alert('Operation Cancelled');
}
if (button == ui.Button.CLOSE) {
// User clicked X in the title bar.
ui.alert('You closed the dialog.');
}
}
【问题讨论】:
标签: google-apps-script google-sheets