Google Apps Script return array values and use them in a javascript function -


i trying return array , use in javascript function, doesn't seem work. code.gs follows:

function doget() {   return htmlservice.createhtmloutputfromfile('test')   .setsandboxmode(htmlservice.sandboxmode.iframe); }  function test() {     var locations = [];     var ss = spreadsheetapp.openbyurl('https://docs.google.com/spreadsheets/d/13q7piemuhll6_5xbupbavabqalt9fnfnoio-hwy_pfc/edit'),     sheet = ss.getactivesheet(),     range = ss.getrange("d2:d4"),     values = range.getvalues();     (var r=1; r<values.length; r++) {        var row = values[r];        locations.push(row[0]);     }     return locations; } 

the function in test.html looks follows:

function hello() {    google.script.run.test(); } 

so want pass array , contents hello function in test.html. how can make work?

you need withsuccesshandler() method chained google.script.run:

function hello() {   google.script.run     .withsuccesshandler(injecthtml)     .test(); }  //this captures returned string server side code function injecthtml(argreturnedarray) {   //to - code inject html  }; 

unfortunately, server side .gs code return string. there's way deal that. use:

json.stringify(yourarray); 

your array named locations.

return json.stringify(locations); 

now need convert json string array:

function injecthtml(argreturnedarray) {   /* put array browsers window object in order make   *  array named myreturnedarray available other functions.   */   window.myreturnedarray = json.parse(argreturnedarray);    //to - code inject html };  //get array in function function myotherfunction() {   //get array browsers window object   var thearray = window.myreturnedarray;   var i=0, thiselement="";   (i=0;i<thearray.length;i+=1) {     thiselement = thearray[i];   } }; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -