0

I had been reading the PropertiesService documentation of Google AppScript. And I had been using the following code for my addon :

var userProperties = PropertiesService.getUserProperties();

I would just like to ask if this piece of code needs some exception handling. What is the best way of handling exceptions when using the PropertiesService?

Thanks in advance.

Rubén
  • 34,714
  • 9
  • 70
  • 166
GovZ
  • 253
  • 1
  • 11
  • 1
    Please add a brief description of your search efforts as is suggested in [ask]. P.S. Related [1](https://stackoverflow.com/q/27002846/1595451), [2](https://stackoverflow.com/q/43883266/1595451) – Rubén Dec 08 '21 at 16:43
  • Thank you for your comment @Rubén. I will keep that in mind. Your first related item, is a bit of an answer to my question. I think we will fall under the 500k quota. But it looks possible that a PropertiesService-Exception might happen during the execution of an addon. Aside from quota-related issues, have you seen any documentation related to PropertiesService-related exceptions? Thanks. – GovZ Dec 14 '21 at 12:54
  • Please [edit] the question to add all the relevant details to it (i.e. how the properties service is used, how many read/writes per execution / day, number of properties, maximum property size total properties size...) including a brief description of your search efforts. – Rubén Dec 14 '21 at 15:35

1 Answers1

2

Sample exception handling:

var userProperties = PropertiesService.getUserProperties();
var myProperty = userProperties.getProperty('myNumber');
if(myProperty){
  myProperty = JSON.parse(myProperty) + 1;
}
else{
 myProperty = 1;
}
userProperties.setProperty('myNumber', JSON.stringify(myProperty))
  • This code block increases a number stored in the user property myNumber by 1.

  • First time, when the property myNumber does not exist yet - it creates it by setting it to 1 and storing it with setProperty()

  • If the property already exists- the if statement is entered and the property (always stored as a string) can be parsed and handled as a number

UPDATE

For completeness sake including the information from the comments.

You can handle exceptions with script properties in the following way:

try{
  var userProperties = PropertiesService.getUserProperties();
  var myProperty = userProperties.getProperty('myNumber');
  if(myProperty){
    myProperty = JSON.parse(myProperty) + 1;
  }
  else{
   myProperty = 1;
  }
  userProperties.setProperty('myNumber', JSON.stringify(myProperty));
}
catch(error){
  console.log(error.stack);
}


ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thanks for the sample code. I am sorry for the unclear question. But just like your code, my code does not envelope the call for 'PropertiesService.getUserProperties()' with a 'try-catch'. The code assumes that it will never generate an exception. Is it safe to proceed without a try-catch? Thanks. – GovZ Dec 14 '21 at 12:56
  • 2
    Whenever possible, if you do not need to throw an error an `if... else` is better practice than a `try... catch`. In this specific case: if a property does not exist - it will be undefined , the `if` condition will evaluate to `false` and thus the `else` condition will be fullfilled. Absolutely no need for `try...catch`. – ziganotschka Dec 14 '21 at 13:06
  • @zigannotschka thanks. What happens if you hit the 50k daily quota for PropertiesService though? Won't it generate an exception? But just that the result is undefined? Thank you again. – GovZ Dec 14 '21 at 13:12
  • 1
    Ok, you are expecting to hit this quota, then you might use a `try...catch` indeed :-) This would be as simple as pasting the whole code block in my answer into the `try` block. – ziganotschka Dec 14 '21 at 13:28