简体   繁体   English

将多个工作表从一个电子表格复制到另一个电子表格

[英]Copy several sheets from one spreadsheet to another spreadsheet

I'm trying to create a script to copy several sheets from one (unopened) spreadsheet to another (active spreadsheet). 我正在尝试创建一个脚本,以将多个工作表从一个(未打开的)电子表格复制到另一个(活动的电子表格)。

I have searched through other solutions and using some other suggestions I have put together this piece of code. 我搜索了其他解决方案,并使用了将这些代码组合在一起的一些其他建议。 I have no programming experience and just started learning Javascript when my school started using Google apps. 我没有编程经验,只是在我的学校开始使用Google应用程序时才开始学习Javascript。

It has taken me about 5 hours get this far and I think I am almost there, but I can't get it to work. 我花了大约5个小时才走到现在,我想我已经快到了,但是我无法使它工作。

function copySheets() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var infoPageSheet = ss.getSheetByName("infoPage");
    var sheetID = infoPageSheet.getRange("B11").getValues().toString(); //gets the ID of the spreadsheet that contains the sheets I need to copy from cell B11 
    var classSelect = infoPageSheet.getRange("B12:B17").getValues().toString().split(","); // gets the names of the sheets I need to copy (one name per cell)
    var stuNameList = SpreadsheetApp.openById(sheetID); // opens the target sheet

    for (var i = 0; i < classSelect.length; i++) {
        var copy = stuNameList.getSheetByName(classSelect[i]);
        if (copy) {
            Logger.log("copy " + classSelect[i] + " already exists");
        } else {
            stuNameList.copyTo(ss).setName(classSelect[i]).showSheet();
        } 
    }
}

I will input the ID and the names of the sheets I want to copy into the active spreadsheet. 我将输入ID和要复制到活动电子表格中的工作表的名称。 I will run the script and the script should get the sheets from the source file (unopened) and copy them to the active spreadsheet with the same name. 我将运行该脚本,该脚本应从源文件中获取工作表(未打开)并将其复制到具有相同名称的活动电子表格中。

Please feel free to be as critical of my code as you want, any help would be greatly appreciated.. 请随意批评我的代码,任何帮助将不胜感激。

UPDATE 更新

I have got this working now 我现在已经开始工作了

function getVars() {

   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var sortSheet = ss.getSheetByName("Sorted Scores");
   var infoPageSheet = ss.getSheetByName("infoPage");
   var sheetID = infoPageSheet.getRange("B11").getValue().toString();
   var classSelect1 = infoPageSheet.getRange("B12:B18").getValues().toString().split(",");

   var classSelect = new Array();
     for (var i = 0; i < classSelect1.length; i++) {
        if (classSelect1[i]) {
          classSelect.push(classSelect1[i]);
  }
}
       var stuNameList = SpreadsheetApp.openById(sheetID);

      Logger.log(sheetID)
       for (var i = 0; i < classSelect.length; i++) {
        var get = (classSelect[i]);
        var copy = ss.getSheetByName(classSelect[i]);
           if (copy) {
             Logger.log("copy " + classSelect[i] + "already exists");
         } else {
            stuNameList.getSheetByName(get)
            .copyTo(ss)
            .setName(get);
             Logger.log(classSelect)
    }
  }
}

Your "sheetID" seems to be a big string with multiple sheet ids. 您的“ sheetID”似乎是一个包含多个工作表ID的大字符串。 What I mean to say is that 我的意思是说

Logger.log(sheetID) //logs "sampleid,sampleid2,sampleid3"

I don't think openById() of stuNameList accepts that (returns a Bad value error). 我认为openById() stuNameList接受(返回错误值错误)。 Try using .openById(sheetID[i]) on your else . 尝试在您的else上使用.openById(sheetID[i])

The copyTo method is only available for the Sheet class. copyTo方法仅适用于Sheet类。

for (var i = 0; i < classSelect.length; i++) {
    var copy = stuNameList.getSheetByName(classSelect[i]);
    if (copy) {
        Logger.log("copy " + classSelect[i] + " already exists");
    } else {
        ss.getSheetByName(classSelect[i])
          .copyTo(stuNameList)
          .setName(classSelect[i]);
    } 
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 电子表格可复制其他电子表格中的信息-Javascript - Spreadsheet to copy informations from another sheets - Javascript 在编辑时将数据从一个电子表格复制到另一个电子表格 - Copy data from one spreadsheet to another on edit Google脚本会根据日期将范围从一个电子表格复制到另一个电子表格 - Google scripts copy range from one spreadsheet to another based on date Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet 将一行从一个Google表格电子表格复制到表单提交中的另一表格 - Copy a row from one Google Sheet Spreadsheet to Another on Form Submit 如何在编辑时将数据从一个电子表格复制到另一个电子表格 - How to copy data from one spreadsheet to another on edit 将数据从一个电子表格复制到另一种保留格式且不使用公式 - Copy data from one Spreadsheet to another keeping format and without formulas 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 我可以将数据从一个电子表格复制到新的电子表格吗? - Can I copy data from one spreadsheet to a new spreadsheet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM