如何获得不是第一张纸的纸? [英] How to get sheet that is not first sheet?
问题描述
我是Google脚本的新手,如果这是一个幼稚的问题,我深表歉意.我不知道如何获取不是第一张工作表的电子表格中工作表的变量引用.
I am new to Google scripting, so I apologize if this is a naive question. I do not know how to get a variable reference to a sheet within a spreadsheet that is not the first sheet.
在电子表格中,我有两张纸Agenda
和Info
.议程是第一张工作表(索引为0),信息是第二张工作表.我可以获得对Agenda
的引用,但无法获得对Index的引用.这是我尝试过的代码:
In my spreadsheet, I have two sheets, Agenda
and Info
. Agenda is the first sheet(index 0) and Info is the second. I can get a reference to Agenda
, but I cannot get a reference to Index. This is the code I have tried:
var info_sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Info');
但是,
info_sheet.getName()
始终总是为Agenda
.我该怎么办?
info_sheet.getName()
always comes out being Agenda
, though. What do I do?
推荐答案
有两种方法可以访问电子表格中的特定工作表:通过您在代码中显示的名称或在第二种方法中的索引下面的示例.
There are 2 ways to get access to a specific sheet in a spreadsheet : by its name as you were showing in your code or by its index like in the second example below.
只需运行此测试,即可在至少包含2张纸的电子表格上查看结果(我将工作表名称更改为美国语言环境中的标准默认值",以使该测试适用于任何人,然后将其重新设置为您的参数如果需要)
Just run this test to see the results on a spreadsheet with at least 2 sheets (I changed the sheet names to the 'standard default values in US locale' to make the test working for anyone, re-set it to your parameters if needed)
function testSheetNames(){
var info_sheet_example1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2');
var info_sheet_example2 = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];
Logger.log('name method result = '+info_sheet_example1.getName());
Logger.log('index method result = '+info_sheet_example2.getName());
}
也就是说,您的示例应该返回正确的值,我不确定为什么它不在您的测试中.
That said, your example should return the correct value, I'm not sure why it didn't in your tests.
这篇关于如何获得不是第一张纸的纸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!