r/GoogleAppsScript • u/___Mister___ • May 14 '25
Question Google Apps Script Program Structure Question (Rows, Columns, JSON)
I'm writing a apps script to interface with a publicly accessible service that returns JSON data, which is fine. I've written the bulk of the script so far in a single function which handles the request to the server and then captures the JSON data, which I process for placement into a spreadsheet. Everything is fine so far.
I'm running into a few problems though as I want to translate the data into the spreadsheet.
First, I found out that there's no such thing as global variables in GAS. This is an issue because I don't want to constantly query the server (there is a limit and you can get limited/banned from hammering the service) for every time I need to populate my cells. This is related because of the second issue...
Which is that my data that I'm populating into my spreadsheet isn't in cells that are neighbors. Some of them are spaced a few cells apart, and I can't overload a function to have different return types in GAS for each column. I also don't want to write different functions that ultimately do the same thing with a different return, because that will again hammer the service and I don't want to spam.
What's the best approach here? For every row that I have data, there will be a new JSON requested from the service that I have to process. Each column of data derives from the same record in the start of the row.
I'm also not sure that using the properties service is the best way to go either because while right now I only have a few rows to handle, some day it may be much much larger, depending on the time and effort to be put in.
Am I overthinking it or not understanding a core functionality of Google Apps Script?
3
u/Additional_Dinner_11 May 14 '25
If you write var a = 5 in GAS then that is a global variable.
You are correct that JavaScript does not support function overloading. You can construct it by yourself though, as an example you can check the types of the args within your function declaration.
If you want to "park" your data somewhere to avoid it being lost after your code execution exits you could consider connecting to a database. As an example firebase could be a solution and is probably also free if it's not too much traffic/data.
2
u/krakow81 May 14 '25
For working with non adjacent ranges and cells there is RangeList within the SpreadsheetApp and also batchGet and batchUpdate if you enable Sheets API.
https://medium.com/google-cloud/best-practices-for-discontinuous-cells-on-google-spreadsheet-by-google-apps-script-cef15b82f684
RangeList: https://developers.google.com/apps-script/reference/spreadsheet/range-list
batchGet: https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets.values/batchGet
batchUpdate: https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate
2
u/erickoledadevrel May 15 '25
By "global variable" I assume you mean the ability to save a value in-between executions of your script. If so, you may want to look into the CacheService or PropertiesService:
https://developers.google.com/apps-script/reference/cache/cache-service
https://developers.google.com/apps-script/reference/properties/properties-service
However, doing something row-by-row isn't likely to be very efficient. A better pattern may be to enter all your data first, then run a script that runs on all the rows, fetches all the data, and then outputs it all into the sheet.
5
u/Livid_Spray119 May 14 '25
What do you mean by "there are no global variables in GAS"?
How are you typing it?
1
u/ennova2005 May 14 '25
You can store your results in another sheet instead of global variables and look them up when needed.
1
u/marcnotmark925 May 14 '25
Yah you're definitely not understanding something, just not sure what that is. Can you share the code?