【问题标题】:Script to duplicate inactive sheet in Google Sheets在 Google 表格中复制非活动工作表的脚本
【发布时间】:2021-02-13 20:03:34
【问题描述】:

我在下面有这个脚本,它非常适合复制工作表并从单元格 A3 的内容重命名。

现在我想运行这个确切的功能,但在非活动(甚至隐藏)的工作表上。它可以是仅复制值的副本,因为不需要复制所有深层数据。

function AddReportSheet() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
    var myValue = SpreadsheetApp.getActiveSheet( ).getRange("A3").getDisplayValue();
    SpreadsheetApp.getActiveSpreadsheet().renameActiveSheet(myValue);
}

【问题讨论】:

  • 您想复制所有非活动工作表?还是特定的床单?
  • @Marios 我只想复制一张特定的表格。

标签: google-apps-script google-sheets spreadsheet


【解决方案1】:

解释:

您的目标是复制特定工作表并根据原始工作表的单元格 A3 的值设置副本的名称。

解决方案:

复制工作表:

选择要复制的工作表的名称。在以下示例中,Sheet1

function AddReportSheet() {   
    const sheetName = "Sheet1"; // put the name of your sheet to duplicate
    const ss = SpreadsheetApp.getActive();
    const sheet = ss.getSheetByName(sheetName);
    const myValue = sheet.getRange('A3').getDisplayValue();
    sheet.copyTo(ss).setName(myValue);   
}

复制工作表并仅保留值和格式:

如果您只想保留重复工作表的值,则可以使用copyTo(destination, options) 并仅保留内容,例如{contentsOnly:true}.

function AddReportSheet() {   
    const sheetName = "Sheet1"; // put the name of your sheet to duplicate
    const ss = SpreadsheetApp.getActive();
    const sheet = ss.getSheetByName(sheetName);
    const myValue = sheet.getRange('A3').getDisplayValue();
    const new_sheet = sheet.copyTo(ss).setName(myValue);   
    const rng = new_sheet.getDataRange();
    rng.copyTo(rng, {contentsOnly:true})
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多