Home > database >  Income to property is not absorbed in the same run
Income to property is not absorbed in the same run

Time:08-22

This is my code

function myFunction() {
  var userProperties = PropertiesService.getUserProperties();
  var property = userProperties.getProperty('property');
  if(property === null){
    Logger.log('null')
    userProperties.setProperty('property', '2');
  }
  Logger.log('property: '   property);
  userProperties.deleteAllProperties()
}

I created a log in it twice First time to identify whether property is null and if it is null insert the number 2 into it A second time is to see if the number 2 exists in property

The problem is that on the first run I get the first log null and the second log property: null and on the second run I get property: 2. Can't this be updated in the same run?

I apologize for my broken English and thank you for the help

CodePudding user response:

Since the variable has already been defined it needs to be redefined to contain a new value The solution is like this

function myFunction() {
  var userProperties = PropertiesService.getUserProperties();
  var property = userProperties.getProperty('property');
  if(property === null){
    Logger.log('null')
    userProperties.setProperty('property', '2');
    property = userProperties.getProperty('property');
  }
  Logger.log('property: '   property);
  userProperties.deleteAllProperties()
}

CodePudding user response:

Unfortunately no because setProperty() returns the Property Service not the property. However simply set the value of property before setProperty().

function myFunction() {
  var userProperties = PropertiesService.getUserProperties();
  var property = userProperties.getProperty('property');
  if(property === null){
    Logger.log('null')
    property = '2';
    userProperties.setProperty('property', property);
  }
  Logger.log('property: '   property);
  userProperties.deleteAllProperties()
}

Reference

  • Related