Cache Service allows a script to temporarily cache results.
But puts values like Array into Cache, when you take it out will become a String.
Example :
Put array into cache when get will return a string.
So we can use Split string, return result to a array object.
- var cache = CacheService.getUserCache();
- function putCache(key, array) {
- cache.put(key, array);
- }
- function getCache(key) {
- var result = cache.get(key).toString().split(",");
- return result;
- }
Or we can use External APIs JSON, is much easier to keep original Array structure
- var cache = CacheService.getUserCache();
- function putCache(key, array) {
- var payload = JSON.stringify(array);
- cache.put(key, payload );
- }
- function getCache(key) {
- var payload = cache.get(key);
- var result = JSON.parse(payload);
- return result;
- }
沒有留言:
張貼留言