这可以使用Gsheet AppScript中的rangenames来处理吗?我遇到此错误:'typeError:sheet.getRangebyName不是函数。

发布于 2025-02-11 15:15:40 字数 440 浏览 1 评论 0原文

我正在尝试在AppScript中的对象中添加一些静态范围名称,我这样做是为了具有更大的灵活性,因为有20多个变量(列),因此,如果您尝试在Google表中添加一列,则代码为没有损坏,这是我已经编码的简化示例:

var sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses');

var bot_id = sheet.getRangeByName("bot_id");

var elements = {
  bot_id: bot_id,
}

var res = elements.bot_id.getValues();

但是,当我运行它时,我会收到以下错误:“ TypeError:Sheet.GetRangeByName不是函数”。

我该如何修复它并始终引用同一张纸,还有另一种更轻松,更有效的方法吗?

I am trying to add some static range names to an object in AppScript, I do this to have more flexibility since there are more than 20 variables (columns) and so that if you try to add a column in the google sheet, the code is not damaged, this is a simplified example of what I already have coded:

var sheet = SpreadsheetApp.getActive().getSheetByName('Form Responses');

var bot_id = sheet.getRangeByName("bot_id");

var elements = {
  bot_id: bot_id,
}

var res = elements.bot_id.getValues();

However, when I run it I get the following error: "TypeError: sheet.getRangeByName is not a function".

How can I fix it and always be referencing to the same sheet, is there another easier and more efficient way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆依然 2025-02-18 15:15:40

getRangeByName()不是表格的方法,而是电子表格。因此,您应该尝试:

var bot_id = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("bot_id");

getRangeByName() is not a method of Sheet but instead of Spreadsheet. So you should try:

var bot_id = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("bot_id");
只有一腔孤勇 2025-02-18 15:15:40

这样尝试:

function lfunko() {
  const ss = SpreadsheetApp.getActive()
  const sh = ss.getSheetByName('Sheet0');
  var bot_id = ss.getRangeByName("bot_id");
  Logger.log(JSON.stringify(bot_id.getValues()));
}

Try it this way:

function lfunko() {
  const ss = SpreadsheetApp.getActive()
  const sh = ss.getSheetByName('Sheet0');
  var bot_id = ss.getRangeByName("bot_id");
  Logger.log(JSON.stringify(bot_id.getValues()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文