user3783733
user3783733

Reputation: 43

How to store global values correctly using Properties service in GAS?

I have this problem using Google apps script. I have a menu in a spreadsheet with two options (Set Password and Add Time Record). These options raise a UI service user interfaces for prompt its data respectively. For access to Add Time Record I ask before if user is authenticated. I use scriptProperties = PropertiesService.getScriptProperties() and setProperty('authenticated', false) for save the initial value of authenticated.

1- I click Set Password, I do login ok and close the UI.

2- I click Add Time Record and I expect to receive my AddTime record Ui (because I Set Password first) but instead I receive Set Password UI. Even if I login again I receive the same UI …again and again.

Is like authenticated is reset to false every time I do click in a menu option no matter the action I did before. This is the expected behavior? Or I’m doing anything wrong? Thanks a lot for your help.

var scriptProperties = PropertiesService.getScriptProperties();
scriptProperties.setProperty('authenticated', 'false');
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('ProWorkflow')
    .addItem('Set Pasword', 'getPassword').addSeparator()
    .addItem('Add Time Record', 'isAuthenticated').addSeparator()
    .addToUi();
}
function isAuthenticated()
{
  var scriptProperties = PropertiesService.getScriptProperties();
  var value = scriptProperties.getProperty('authenticated');
  if(value =='false'){
    getPassword(); // at the end of this function I set scriptProperties.setProperty('authenticated', ‘true’);
  }
  return addTimeRecord();
}
function getPassword(e)
{
    …
    …
    …
   var scriptProperties = PropertiesService.getScriptProperties();
   var value = scriptProperties.getProperty('authenticated');
   value = 'true';  
   scriptProperties.setProperty('authenticated', value); //change de value of uthenticated 
   return app.close(); 
}

Upvotes: 3

Views: 10228

Answers (2)

Jack
Jack

Reputation: 10613

I wasn't clear on where Project Properties can be set if you want to do it programmatically. I added a function in my script:

function setScriptProperties() {
    var scriptProperties = PropertiesService.getScriptProperties();
    scriptProperties.setProperty('KEY', 'Some value.');
}

Then I used the UI to run this function 'manually' from the GAS interface.

enter image description here

After calling the function once from the UI, project properties are set.

Upvotes: 5

Serge insas
Serge insas

Reputation: 46802

"Is like authenticated is reset to false every time I do click in a menu option no matter the action I did before"

So it is actually.

When placing your line outside of any function it is executed on each run of ANY function so your line scriptProperties.setProperty('authenticated', 'false'); sets it to false every time.

Move it where it should be, scriptProperties and userProperties are - by definition - working as global variables since they are stored in a global script/user scope, that's actually what they are designed for..

Look also at what you wrote in the getPassword function... what you show above is not very logical (see comments in code):

   var value = scriptProperties.getProperty('authenticated');// you get a value
   value = 'true';  // and change it immediately to a constant... what's the point ?
   scriptProperties.setProperty('authenticated', value); //change de value of uthenticated 

Upvotes: 1

Related Questions