Using Properties as a global state variable

46 views
Skip to first unread message

rob wilson

unread,
Sep 16, 2023, 12:10:14 PM9/16/23
to Google Apps Script Community
Not a *total* beginner but looking for help please with a G Sheets script.

I'm developing a "Job sheet" for a community repair shop. Buttons will allow the user to Add, Edit, Archive jobs.

 I have functions for each operation, and an onEdit(e) function which verifies and carries out housekeeping for each operation.

onEdit(e) needs to know which operation is being done, and my first attempt was a global variable state which was set by the originating function and tested later  by the onEdit function. 
That didn't work (it always tested as 'IDLE', my initial state. Some research showed people had success with the Properties class/services. This isn't working either! 

Some snippets:
This is outside all the functions:
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty('state', 'IDLE');

A button function:
function addJob() {
userProperties.setProperty('state', 'ADD');

I have checked that it is set properly using:
let state = userProperties.getProperty('state');
ui.alert('State: '+state);

onEdit function:
function onEdit(e){
let state = userProperties.getProperty('state');

This shows state as IDLE!
I'd appreciate any help, thanks.

CBMServices Web

unread,
Sep 16, 2023, 2:01:26 PM9/16/23
to google-apps-sc...@googlegroups.com
Hi Rob,

Using Properties can work but if you want a state per job, why not just have a cell in your spreadsheet that records it? That way it is also visible to you and others.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/7fe8feca-a430-450c-8702-be62eb27effbn%40googlegroups.com.

rob wilson

unread,
Sep 16, 2023, 3:54:01 PM9/16/23
to Google Apps Script Community
Hi George,
That sounds nice and simple! I'll try it.
Rob

Reply all
Reply to author
Forward
0 new messages