【问题标题】:Google Apps Script copyTo() function - it only copies the sheet and not the contentGoogle Apps Script copyTo() 函数 - 它只复制工作表而不是内容
【发布时间】:2019-09-07 15:28:29
【问题描述】:

我正在尝试将模板电子表格复制到新电子表格。它确实设法从模板创建选项卡,但所有选项卡都是空白/不受影响。

我获得了对模板/源和目标工作表的访问和编辑权限。

当我在 Google 脚本编辑器中运行调试时,我得到“sourceSheet”作为一个对象,当我使用 getName() 检查它时,它显示模板的名称,而“source”显示模板中选项卡的名称.

function onOpen(e){
    var installedStatus = PropertiesService.getScriptProperties().getProperty("installed");
    if(installedStatus != "true"){
        var ss = SpreadsheetApp.getActiveSpreadsheet();

        var documentName=ss.getName();
        if(ss.getName() == "Namnlöst kalkylark"){
            ss.rename("Dashboard 2.0 - (Kundens namn)");
        }

        var omKund = ss.getSheetByName("Om kund");
        if(omKund == null ){
            copySheets("Om kund");
        }
        PropertiesService.getScriptProperties().setProperty("installed", "true");
    }
}

function copySheets(tabname){

  var sourceSheet = SpreadsheetApp.openById("ID to template spreadsheet here");
  var source = sourceSheet.getSheetByName(tabname);
  var sValues = source.getDataRange().getValues();
  var destination = SpreadsheetApp.openById(SpreadsheetApp.getActive().getId());
  source.copyTo(destination);

  var destinationSheet = destination.getSheetByName("Copy of "+sourceName);  
destinationSheet.getRange(1,1,sValues.length,sValues[0].length).setValues(sValues);
  destinationSheet.setName(sourceName);
}'

【问题讨论】:

标签: google-apps-script google-sheets


【解决方案1】:

您的变量“sourceName”在被调用之前未设置为任何值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多