如何在应用程序脚本中为.getRange()创建动态源?
原始问题: 我在Google电子表格中有多个标签,代表不同的数据源。当前,我有一个变量( var = quote1location ),该变量等于我想根据其他逻辑从其他逻辑中获取数据的表名称。
假装 Quote1Location 可以根据逻辑等于'Sheet1','Sheet2'或'Sheet3'等于“ Sheet1”,“ Sheet2”或“ Sheet3”,但在这种情况下,它等于“ Sheet1”。
var totalpeople = quote1location.getRange('a1')。getValue();
在上述功能中,应用程序脚本会返回错误,说'quote1lage.getRange不是函数``因为应用程序脚本不是替换我指定的变量的值('sheet1'),而是使用变量名称(而不是'quote1 -location'。我希望应用程序脚本将其处理为'Sheet1.getRange('a1'' )
。
).getValue ( 谢谢大家的回答。我要做的是在“ getrange()”函数中使用字符串。假装我有两个名为“ Sheet1”和“ Sheet2”的Google表格,并且我有一个变量,可以帮助我确定要抓取哪些表作为数据参考。我试图将变量设置为( var source = 1 )或( var source = 2 ),以便我可以在我的getRange()函数中使用此变量像这样:('sheet' + source).getRange('a1')。getValue();
我在这里尝试做的是 var source = 1 ,然后,我将从“ Sheet1”中获取数据。如果 var source = 2 ,那么我将从'Sheet2'中获取数据。
问题(如回应的人所述)是我试图在字符串上使用.getRange(),而不是像特定电子表格那样的对象。而不是使用 var source = 2 或 var source = 1 我
ss = SpreadSheetApp.getActiveSpreadsheet()
if(somevariable = somecondition){
var source = ss.getSheetbyName('Sheet1')
}
if(someothervariable = someothercondition){
var source = ss.getSheetByName('Sheet2')
}
现在应该使用getrange()'on' source ',它将调用我指定的表格,而不是试图检索不起作用的字符串范围。
非常感谢所有提供反馈的人。
Original Question:
I have multiple tabs in a Google Spreadsheet that represent different data sources. Currently, I have a variable (var = quote1location) that is equal to the sheet name that I would like to get my data from based on other logic.
Pretend that quote1location can equal 'Sheet1', 'Sheet2', or 'Sheet3' depending on the logic but for this case, it equals 'Sheet1'.
var totalpeople = quote1location.getRange('A1').getValue();
In the function above, Apps Script will return an error saying 'quote1location.getRange is not a function' because Apps Script is not substituting the value of the variable that I have designated ('Sheet1') but is using the variable name ('quote1location' instead. I would like Apps Script to process this as 'Sheet1.getRange('A1').getValue()'.
Your help would be appreciated
Answer:
Thank you all for your responses. What I was trying to do is use a string in the 'getRange()' function. Pretend I had two Google Sheets named 'Sheet1' and 'Sheet2' and I had a variable that helped me determine what sheet to grab as my data reference. I was trying to set a variable as either (var source = 1) or (var source = 2) so that I could then use this variable in my getRange() function like this: ('Sheet' + source).getRange('A1').getValue();
What I was trying to do here is if var source = 1, then I would get my data from 'Sheet1'. If the var source = 2, then I would get my data from 'Sheet2'.
The issue (as mentioned by those who responded) is that I was trying to use .getRange() on a string, not an object like a specific spreadsheet. Instead of using var source = 2 or var source = 1 I should be using
ss = SpreadSheetApp.getActiveSpreadsheet()
if(somevariable = somecondition){
var source = ss.getSheetbyName('Sheet1')
}
if(someothervariable = someothercondition){
var source = ss.getSheetByName('Sheet2')
}
Now when I use getRange()' on 'source', it will be calling the sheet that I have designated rather than trying to retrieve a range from a string which will not work.
Thank you very much to all who provided feedback.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您正在寻找:
或者:
这些是您希望使用的确切语法,我希望这可以帮助您更好地了解如何完成访问工作表。
I believe you are looking to do:
Alternatively:
Whether these are the exact syntax you're hoping to use or not, I hope this helps you better understand how to accomplish accessing a Sheet.