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

Viewed 486

I have the following working code:

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

Beside the result-array from getSpreadsheetRange I want to give a second parameter to getSelectOption. How can I do that?

1 Answers

In your situation, how about using withUserObject(object)? By this, you can retrieve the additional value at the function getSelectOption. When your script is modified, it becomes as follows.

Modified script:

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)
}
  • By above modified script, you can retrieve sample value at the 2nd argument of getSelectOption. The 1st argument of getSelectOption is from getSpreadsheetRange.

  • Or, I think that the suggestion from the comment by @doubleunary can be also useful.

  • Or, you can also use the function getSelectOption as follows. In this case, 2 arguments can be retrieved as an array.

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

Reference:

Edit:

From the following doubleunary's comment

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.

I thought that I might have misunderstood the OP's goal. So, from the comment, I understood that when getSpreadsheetRange() is returned, OP wanted to add one more argument and wanted to use it at getSelectOption().

In this case, I agree with doubleunary's comment and would like to propose the following modification.

Google Apps Script side:

function getSpreadsheetRange(a, b) {

  // do something.

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

Javascript side:

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

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