1

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

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

我试图用来重置工作表单元格的功能:

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"
    }
  ]}
}

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

4

2 回答 2

0

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

在脚本文件上更正此部分后,该函数按预期运行。谢谢大家的帮助!

于 2018-04-23T18:27:34.480 回答
0

你有两个问题:

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

  2. 宏不应该接受参数或返回值。宏的每个 Apps 脚本文档

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

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

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

代码.gs

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

宏.gs

function ResetPoints() {
  ...
}

清单.json

{
  "timeZone": "America/New_York",
  "dependencies": { },
  "exceptionLogging": "STACKDRIVER",
  "sheets": {"macros": [
    {
      "menuName": "ResetPoints",
      "functionName": "ResetPoints",
      "defaultShortcut": "Ctrl+Alt+Shift+5"
    }
  ]}
}
于 2018-04-23T05:06:24.690 回答