【问题标题】:How to use a Macro to set cell formulas to use a custom function in Google Sheets如何使用宏设置单元格公式以使用 Google 表格中的自定义函数
【发布时间】:2018-04-18 21:02:12
【问题描述】:

对于我的工作,我会自动执行程序以协助我的团队成员和员工。在同事的帮助下,我们创建了一个 Google Sheets 函数,该函数可以计算所有座席的出勤点,以将它们以数组的形式显示在表单上。由于计算量大,函数往往会经常崩溃。只需将公式重新粘贴到它所在的单元格中即可解决此问题。

Google 最近发布了适用于 Google 表格的宏,我已尝试将这些宏用于此目的;然而,由于该函数位于容器绑定脚本中,因此在尝试重置工作表上的函数时,宏会产生“引用错误”。我也尝试创建一个函数来执行此操作并收到相同的结果。

我尝试使用的函数来重置工作表的单元格:

function ResetPoints(){
  var spreadsheet = SpreadsheetApp.getActive();

  spreadsheet.getCurrentCell().setValue('').setFormulaR1C1(
    '=calculateAttendancePoints(ARRAYFORMULA(Overview5.0!R11C1:R30C1))'
  );
  spreadsheet.getCurrentCell().offset(20, 0).activate();

  spreadsheet.getCurrentCell().setValue('').setFormulaR1C1(
    '=calculateAttendancePoints(ARRAYFORMULA(Overview5.0!R31C1:R50C1))'
  );
  spreadsheet.getCurrentCell().offset(20, 0).activate();

  spreadsheet.getCurrentCell().setValue('').setFormulaR1C1(
      '=calculateAttendancePoints(ARRAYFORMULA(Overview5.0!R51C1:R70C1))'
  );
  spreadsheet.getCurrentCell().offset(20, 0).activate();

  spreadsheet.getCurrentCell().setValue('').setFormulaR1C1(
      '=calculateAttendancePoints(ARRAYFORMULA(Overview5.0!R71C1:R91C1))'
  );
  spreadsheet.getCurrentCell().offset(21, 0).activate();

  spreadsheet.getCurrentCell().setValue('').setFormulaR1C1(
      '=calculateAttendancePoints(ARRAYFORMULA(Overview5.0!R92C1:R100C1))'
  );
  spreadsheet.getCurrentCell().offset(1, 0).activate();
}

我只用一个单元格简化了宏。公式更新为“=calculateAttendancePoints(ARRAYFORMULA(#REF!))”错误消息为“未知函数:calculateAttendancePoints

function ResetPoints() {
  var spreadsheet = SpreadsheetApp.getActive(); 
  spreadsheet.getRange('A1').activate(); 
  spreadsheet.getCurrentCell().setValue('') 
     .setFormula('=calculateAttendancePoints(ARRAYFORMULA(Overview5.0!$A$11:$A$30))');
}

这是清单文件:

{
  "timeZone": "America/New_York",
  "dependencies": { },
  "exceptionLogging": "STACKDRIVER",
  "sheets": {"macros": [
    {
      "menuName": "calculateAttendancePoints",
      "functionName": "calculateAttendancePoints"
    },
    {
      "menuName": "ResetPoints",
      "functionName": "ResetPoints",
      "defaultShortcut": "Ctrl+Alt+Shift+5"
    }
  ]}
}

有没有办法使用宏间接或通过函数重置单元格中的自定义函数?

【问题讨论】:

  • 这没有意义ARRAYFORMULA(Overview5.0!R11C1:R30C1)。另一方面,calculateAttendancePoint 可能是问题所在。这个自定义函数有什么作用?
  • 宏必须在容器绑定脚本中,自定义函数也是如此,所以我不确定yet, since the function is in a container-bound script, the macro yields a "Reference error" when attempting to reset the function on the sheet.是什么意思
  • @Ruben,自定义函数在定义的范围内找到用户,计算他们所在的点,数组公式辅助显示一个范围内的数据。例如,A2:B3 john smith 2 Jack Johnson 0
  • 那么,function calculateAttendancePoints() 的定义在文档的脚本文件中吗?如果不是,那么它正确地说明该功能不存在。自定义函数和宏必须在使用它们的文档中
  • calculateAttendancePoints 是宏还是自定义函数?将其添加到问题中。

标签: google-apps-script custom-function google-sheets-macros


【解决方案1】:

你有两个问题:

  1. 包含特殊字符(例如空格或句点)的范围引用需要用单引号括起来:
    =myCustomFn(Overview5.0!B1) 应该是
    =myCustomFn('Overview5.0'!B1)
    这应该可以解决出现的#REF! 错误。

  2. 宏不应该接受参数或返回值。 Per Apps Script documentation for macros:

    1. 编写宏函数。宏函数不应接受参数且不返回任何值。

基于此,您需要从清单文件的sheets.macros 部分中删除calculateAttendancePoints 的声明。如果您已在逻辑上将您的 Apps 脚本项目分成多个 .gs 文件(例如 code.gs 和 macros.gs),那么将 calculateAttendancePoints 的定义从 macros.gs 移动到 code.gs 也是有意义的。您的 ResetPoints() 函数是一个有效的宏,因为它不接受任何参数并且不返回任何值。

所以一个工作脚本项目应该类似于这个布局:

code.gs

function calculateAttendancePoints(range) {
  ...
  return someValue;
}
function someOtherFunction(someArg) {
  ...
}

宏.gs

function ResetPoints() {
  ...
}

manifest.json

{
  "timeZone": "America/New_York",
  "dependencies": { },
  "exceptionLogging": "STACKDRIVER",
  "sheets": {"macros": [
    {
      "menuName": "ResetPoints",
      "functionName": "ResetPoints",
      "defaultShortcut": "Ctrl+Alt+Shift+5"
    }
  ]}
}

【讨论】:

  • 在尝试您建议的解决方案后,参数列表后出现错误“Missing”。(第 5 行,文件“宏”)Dismiss“这是提供错误的行:@987654335 @
  • @Josh 你在'Overview5.0' 之后多了一个)
【解决方案2】:

我解决了这个问题。似乎通过宏应用的 setValue('') 函数导致该函数确定错误。额外的引号导致脚本中出现错误,不允许函数运行。

在脚本文件上更正这一段后,函数按预期运行。 谢谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-06
    相关资源
    最近更新 更多