Call back function with returned array

Viewed 15

google.script.run.withSuccessHandler(arr).test()

I am developing a web app based on google sheets.

The test function returns an array (arr in this example), which is populated from a range of values in the spreadsheet.

The goal is, in the client-side, when the user clicks a button, it brings the next element of the array.

I am trying to make the returned array a global one so as the server-side function (google.script.run) does not run each time the user clicks the button. I made few attempts to make this array global to no avail.

To give more context, this a multi-dimentional array. Something like: [[2022-01-07, $1,645.00, John, 1.0], [2022-01-08, $1,725.00, John, 2.0]]

So, when the user (John) clicks on a button to see values from 2022-01-07, he will be presented with these values. When he clicks to advance the date one more day, he will be presented with the values from 2022-01-08 and so on.

I would like to run the sever-side script only once, have all its elements available at the client side, so when the user changes the date, there is no need to run server-side script. Thank you for your help.

1 Answers

Here is the code on the server side:

function getData(){
  ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("ledger")
  var allRows=ss.getRange("allrows").getValue()
  logs=ss.getRange("P3:T"+ allRows).getValues()
  var col=0
  var logsall=logs.map(function(v){
    return [v[col].toISOString().substring(0,10),v[col+1].toLocaleString('en-US', {style: 'currency', currency: 'USD'}),v[col+2].toString(),v[col+3].toLocaleString('en-US', {style: 'currency', currency: 'USD'}),v[col+4]]
  })
 return logsall
  }

On the server side:

function setStatus(){
 google.script.run.withSuccessHandler(arr=> 

alert(arr[0][0])

).getData();
}
Related