【问题标题】:Archive (copy contents only) certain sheets in a Spreadsheet存档(仅复制内容)电子表格中的某些工作表
【发布时间】: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


    【解决方案1】:

    在运行地图功能之前,只需删除您不想复制的表格即可。例如,如果您不想包含表格 1、3 和 5,您可以这样做:

    var sheets = ss.getSheets();
    
    // remove sheet one which is at index 0
    sheets.splice(0, 1); 
    // remove sheet three which was at index 2 but now the array does not have 
    // sheet one so it will be at index 1
    sheets.splice(1,1);
    // remove sheet 5 which was at index 4 but now it is at index 2 as we removed 2 sheets
    sheets.splice(2,1);
    var tempSheets = sheets.map(function(sheet) {
    // ...

    参考文献

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      • 2010-10-30
      • 2023-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多