google apps script - how to obtain the original array out of cacheService using GAS -
i'm experimenting cache.
i noticed if store array of strings strings separated komma
.
couldn't find method allows retrieve array array, string seems possible.
retrieval of array splitting string obtained cause problem if array elements contain komma's. can happen if storing titles of books or newspaper articles etc.
function test() { var delimiter = '\r\n'; var xx = ''; (var i=0; i<10; i++) xx = xx + '_' + i; var nummax = 1024; var = ','; var string = []; var total = ''; (var i=0; i<nummax; i++) { string[i] = 'string[' + + ']=' + + + xx; total = total + string[i] + delimiter; } total = total.substr(0, total.length - delimiter.length); // remove delimiter @ end of string cachescript.put('test_total', total); cachescript.put('test_string', string); var test_total = cachescript.get('test_total'); logger.log('test_total=\n' + test_total); var test_string = []; test_string = cachescript.get('test_string'); // not retrieve array string logger.log('test_string=\n' + test_string); var wrong = test_string.split(','); // not retrieve original array var right = test_total.split(delimiter); // reproduce original array }
so know if data stored using cacheservice can restored original array without adding delimiter myself (like in 'total') or splitting on komma (like in 'test_string')?
you can use combination of join(delimiter)
, split(delimiter)
character never present in "normal" string.
i use |
or ∏
in example below
var array = ['any string,really any','test-any-chrs','56']; var string = array.join('∏'); var recovered = string.split('∏'); logger.log('array = '+array+' length = '+array.length); logger.log('string = '+string); logger.log('recovered = '+recovered+' length = '+ recovered.length);
note if want more serious there javascript method : json.stringify(object) , json.parse(string)
Comments
Post a Comment