简体   繁体   English

我怎样才能获得当前的工作表名称?

[英]How can I get current sheet name?

I want copy current sheet to new one and sheet name change. 我想要复制当前工作表到新工作表和工作表名称更改。

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('sheet_name');  <=== question 1
var copy = sourceSheet.copyTo(ss);
ss.setActiveSheet(copy);// set it active
ss.moveActiveSheet(ss.getNumSheets());// move it to the last position 
copy.setName(' newSheetName is add 1 from current sheet name'); <== question 2

question 1: 问题1:
I need current sheet name or ID. 我需要当前的工作表名称或ID。

question 2: 问题2:
if current sheet name is '10', add 1 for new Sheet name 如果当前工作表名称为“10”,则为新工作表名称添加1
I don't know how to add string type of sheet name. 我不知道如何添加字符串类型的工作表名称。

Question 1: 问题1:

The Id refer to the spreadsheet, the name refer to the sheet. Id指的是电子表格,名称参考表格。 If you want to get the name of the sheet, take a look at the documentation on sheet.getName() method . 如果要获取工作表的名称,请查看sheet.getName()方法上的文档。

To get the name the active sheet: 要获取活动工作表的名称:

var actualSheetName = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();

Question 2: 问题2:

You can use the substring method to get the last characters of the sourceSheet string and use parseInt method to manipulate the characters as int value. 您可以使用substring方法获取sourceSheet字符串的最后一个字符,并使用parseInt方法将字符作为int值进行操作。

Example: Your sheet's name is '10'. 示例:您的工作表名称为“10”。 You only need the parseInt method like: 你只需要parseInt方法,如:

var temp = parseInt(sourceSheet);
temp += 1;
copy.setName(temp.toString());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM