[英]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.