如何将第二个参数放置到 google.script.run.withSuccessHandler

How to place a second parameter to google.script.run.withSuccessHandler

我有以下工作代码:

google.script.run.withSuccessHandler(getSelectOption).getSpreadsheetRange('16EBPA1TeOoMgn9fBmCJN8LwIMEGEYBSEvv17','OLT35A006T_PferdNameRange');

除了来自 getSpreadsheetRange 的结果数组之外,我想给 getSelectOption 第二个参数。我该怎么做?

在你的情况下,使用 withUserObject(object) 怎么样?这样,您可以在函数 getSelectOption 处检索附加值。当你的脚本修改后,就变成了下面的样子

修改后的脚本:

google.script.run
.withUserObject("sample value")
.withSuccessHandler(getSelectOption)
.getSpreadsheetRange('16EBPA1TeOoMgn9fBmCJN8LwIMEGEYBSEvv17','OLT35A006T_PferdNameRange');

// By above modified script, you can retrieve `sample value` at the 2nd argument of `getSelectOption`.
function getSelectOption(a, b) {
  console.log(a)
  console.log(b)
}
  • 通过上面修改的脚本,您可以在getSelectOption的第二个参数处检索sample valuegetSelectOption 的第一个参数来自 getSpreadsheetRange.

  • 或者,我认为 评论中的建议也很有用。

  • 或者,您也可以使用函数getSelectOption,如下所示。在这种情况下,可以将 2 个参数作为数组检索。

      function getSelectOption(...a) {
        console.log(a[0])  // Return from "getSpreadsheetRange"
        console.log(a[1])  // Value from "withUserObject"
      }
    

参考:

编辑:

来自

The way I read the question is that the OP wants to return two values from the server-side getSpreadsheetRange() to the client-side success handler getSelectOption(). When you call withUserObject(), the user object is never seen by the server-side function. When the success handler is called, it receives that unchanged server-side user object. To return two values from the client side, the client-side function needs to be modified so that it returns a compound object such as an array, or a key-value pair object.

我想我可能误解了 OP 的目标。因此,从评论中,我了解到当返回 getSpreadsheetRange() 时,OP 想再添加一个参数并想在 getSelectOption().

处使用它

在这种情况下,我同意doubleunary的评论,并提出以下修改。

Google Apps 脚本端:

function getSpreadsheetRange(a, b) {

  // do something.

  return {arg1: "value1", arg2: "value2"}; // You can return the value as an object.
}

Javascript 边:

google.script.run
.withSuccessHandler(getSelectOption)
.getSpreadsheetRange('16EBPA1TeOoMgn9fBmCJN8LwIMEGEYBSEvv17','OLT35A006T_PferdNameRange');

function getSelectOption({arg1, arg2}) {
  console.log(arg1) // value1
  console.log(arg2) // value2
}