【问题标题】:How to set formula to get value from specific sheet [duplicate]如何设置公式以从特定工作表中获取值[重复]
【发布时间】:2020-11-01 21:06:09
【问题描述】:

我在 sheet1 单元格 (1,9) 中设置公式,以通过谷歌应用程序脚本从其他工作表中获取值。其中“工作表”是一个变量,我需要从特定工作表中获取价值。下面的代码显示“=sheet!B1”而不是“=6!B1”。

var ss1 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxx/edit#gid=0");
var sheet1 = ss1.getSheetByName("Sheet1");  
var sheet;
sheet = 6;
var sheet = sheet.toString();
sheet1.getRange(1,9).setFormula('sheet!B1')
}

【问题讨论】:

  • setFormula(`${sheet}!B1`)

标签: javascript string google-apps-script google-sheets google-sheets-formula


【解决方案1】:
  1. Google Apps 脚本使用 JavaScript 作为编程语言
  2. setFormula 需要一个字符串作为参数。

考虑到上述情况,您可以使用任何可用的字符串操作技术,使用您的项目正在使用的运行时支持的 JavaScript 版本。

您可以使用的其他技术:

  1. 字符串连接运算符,即sheet + '!B1'(如果您使用的是旧的 (Mozilla Rhino) 或新的运行时 (Chrome v8))
  2. 字符串模板,即`${sheet}!B1`(仅当您使用新的运行时 (Chrome V8) 时)

【讨论】:

  • 适用于您的第一个建议。
  • 它仅在我通过函数 my Function { } 测试时才有效。我不知道为什么它显示“#REF!”当我使用函数 doGet(e) { } 并弹出“未解决的工作表名称 xx”时,在单元格中。我还尝试单击单元格然后 ENTER,令人惊讶的是它确实有效。如何纠正?
  • 请发布一个新问题,包括minimal reproducible example
猜你喜欢
  • 1970-01-01
  • 2019-11-14
  • 2018-09-05
  • 1970-01-01
  • 2022-12-04
  • 2021-08-26
  • 2017-04-02
  • 2022-11-18
  • 1970-01-01
相关资源
最近更新 更多