简体   繁体   English

获取标签/工作表名称的脚本在Google电子表格中不起作用

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

相关问题 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 创建新的电子表格,以表格 Google Scripts 的名称命名 - Create new spreadsheet name it by the name of sheet Google Scripts 在单个工作表而不是整个电子表格上运行Google脚本 - Run Google script on single sheet instead of entire spreadsheet Google Sheet Script - 日期差异 - 当前日期() - 电子表格中的日期() - Google Sheet Script - Date Difference - Current Date() - Date() in spreadsheet Google电子表格脚本。 仅适用于一张纸 - Google Spreadsheet Script. Only work for one sheet Google Sheets 脚本按工作表名称过滤 - Google Sheets script filter by sheet name Google Spreadsheet:如何使用Google Apps脚本命名范围 - Google Spreadsheet: How to name a range using Google Apps Script Google App Script - 导出没有工作表名称的活动工作表 - Google App Script - Export Active Sheet Without Sheet Name 编写Google表格脚本,以仅在电子表格的一张表格中为每个新提交的表格提供具体的编号。 - Script google sheet to give a number specific on each new form submitted, only for one sheet of the spreadsheet. 如何在不使用电子表格 ID 的情况下使用 Google 脚本从另一个 Google 表格导入数据(2020 年 7 月更新停止了我的代码工作) - How to import data from another Google Sheet using Google Script without using Spreadsheet ID (July 2020 update stopped my code working)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM