0
votes

I have a google apps script which fetches data from spreadsheet. The data is fetched pretty slowly, I think because of amount of data. The first call of this function takes till a second:

sheet.getRange(row, coll).getValue()

I think there must be away to create global variables which will be available all the time the spreadsheet is opened. I would store the whole spreadsheet on open and access it, which should be much faster. But I could not find this option.

1

1 Answers

1
votes

You can use: sheet.getDataRange().getValues(); it will result in a 2D array containing all values in the sheet up to the last row and column containing data.

For example, if you have a sheet with data in cells A1:B10 and then data in cell E20 the .getDataRange() method will get the range A1:E20 including empty cells.

You can then use loops or filters to access or modify the arrays.

Without more information or knowing what exactly you are trying to achieve that is the best method I can give you.

NOTE: Storing the whole spreadsheet onOpen() is not a very good idea because any changes to the spreadsheet will not be picked up unless you refresh or reopen the spreadsheet.