2

I have an array in the following format:

agendaItems = [{"topic":"blah", "description:"blah"}, {"topic":"blah2", "description":"blah2"}].

I need to update the values in this array in a handler and handler does not allow global variables to be modified. I know I have to use either CacheService or ScriptProperties. However, I can't seem to make it work:

If I use CacheService, I get something like this: "[object oject][object object]"

CacheService.getPublicCache.put('agenda', agendaItems);

If I use ScriptProperties, I get something like this: ""[Ljava.lang.Object;@429bd3a7"

ScriptProperties.setProperty('agenda', agendaItems');

Am I doing this wrong or is there a better way? Any advice is appreciated.

1 Answer 1

1

The Cache class works with strings. You have to use the Utilities.jsonStringify and Utilities.jsonParse methods to convert the array to a string and vice versa. Here is slightly modified code which I use

this.getData = function(id, cacheKey) {
  var cache = CacheService.getPrivateCache();
  var cachedString = cache.get(cacheKey);
  var lstData;
  if (cachedString == null) {
    lstData = getNotCachedData_(id);
    cache.put(cacheKey, Utilities.jsonStringify(lstData));
  }
  else {
    lstData = Utilities.jsonParse(cachedString);
  }
  return lstData;
}

The ScriptProperties Service also works with strings. It is not obvious due to the not complete documentation, the value parameter has type var and not String, but it is true.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, using json is giving me the correct data. However, I'm getting an error "Error Encountered: Invalid Argument" after I added the json code to a handler function and I have no clue what is causing this. I copied the code to a non-handler function and there is no error.
@mdho, I answered to your next question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.