[英]Script for getting the tab/sheet name not working in google spreadsheet
I have following google spreadsheet bound script whose job is to create copy of the 'template' sheet whenever there is data in the A:A column of the 1st Sheet ('Projects'). 我有下面的谷歌电子表格绑定脚本,其工作是只要第一张表的A:A列(“项目”)中有数据,就创建“模板”表的副本。
This script was working absolutely fine. 这个脚本工作得很好。 However, later I have added a line of code with this script (the last line of the code in the script below) intending to extract and put the newly created sheet's name in one of its cell - J2.
但是,稍后我在此脚本中添加了一行代码(以下脚本中代码的最后一行),旨在提取新创建的工作表名称并将其放置在其单元格之一-J2中。
Somehow that line of code is not working while the rest of the code does its job properly. 某种程度上,该代码行无法正常工作,而其余代码则可以正常工作。 Any suggestion on this would be greatly appreciated.
任何对此的建议将不胜感激。 Thank you!
谢谢!
function onOpen() {
var menu = [{
name : "Add",
functionName : "newSheet"
}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Sheet", menu);
}
function newSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName("Template");
var sheet1 = ss.getSheetByName("Projects")
var getNames = sheet1.getRange("A:A").getValues().filter(String).toString().split(",");
for (var i = 0; i < getNames.length; i++) {
var copy = ss.getSheetByName(getNames[i]);
if (copy) {
Logger.log("Sheet already exists");
} else {
templateSheet.copyTo(ss).setName(getNames[i]);
ss.setActiveSheet(ss.getSheetByName(getNames[i]));
ss.moveActiveSheet(ss.getNumSheets());
ss.getSheetByName(getNames[i]).copyTo(ss.getSheetByName(getNames[i]).getRange("J2"));
}
}
}
According to GAS documentation, the copyTo() method of the Sheet class only accepts Spreadsheet instances as parameters. 根据GAS文档,Sheet类的copyTo()方法仅接受Spreadsheet实例作为参数。 It doesn't work because you are passing Range as parameter.
它不起作用,因为您要传递Range作为参数。 Also, getSheetByName() returns the Sheet object, so you aren't actually copying the name of the sheet.
另外,getSheetByName()返回Sheet对象,因此您实际上并没有复制工作表的名称。
If you'd like to modify specific range, it's much more convenient to use Range instead. 如果您想修改特定范围,则改用Range更为方便。
var sheet = ss.getSheetByName(getNames[i]);
var cell = sheet.getRange("J2");
cell.setValue(getNames[i]); // writing the name of the sheet to cell J2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.