2

I am trying return an array and use it in a javascript function, but it doesn't seem to work. My Code.gs is as 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();
    for (var r=1; r<values.length; r++) {
       var row = values[r];
       locations.push(row[0]);
    }
    return locations;
}

The function in my test.html looks as follows:

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

So I want to pass the array and its contents to the hello function in my test.html. How can I make this work?

2
  • this is another way to access spreadsheet data developers.google.com/apps-script/guides/html/… Commented Jun 14, 2015 at 15:41
  • Tnx for the link but the problem is that getValues() returns an array in an array. So still have to loop it through to get the following structure: test1, test2. Instead of, [test1], [test2]. Therefore, I need to return an array Commented Jun 14, 2015 at 16:48

1 Answer 1

8

You need a withSuccessHandler() method chained to your google.script.run:

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

//This captures the returned string from the server side code
function injectHTML(argReturnedArray) {
  //To Do - code to inject the HTML

};

Unfortunately, server side .gs code will only return a string. But there's a way to deal with that. Use:

JSON.stringify(yourArray);

Your array is named locations.

return JSON.stringify(locations);

Now you need to convert the JSON string back to an array:

function injectHTML(argReturnedArray) {
  /* Put the array into the browsers window object in order to make the
  *  array named myReturnedArray available to all other functions.
  */
  window.myReturnedArray = JSON.parse(argReturnedArray);

  //To Do - code to inject the HTML
};

//Get the array in another function
function myOtherFunction() {
  //Get the array from the browsers window object
  var theArray = window.myReturnedArray;
  var i=0, thisElement="";
  for (i=0;i<theArray.length;i+=1) {
    thisElement = theArray[i];
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting. Tnx for your comment, It returns the contents of my array now! But how can I use the var myTwoD_Array in another function? As the locations i'm returning need to be used in other functions as well.
Put the array into the browsers window object. See updated answer. You could create a global variable, or create a global object, then put the array into the global object, but putting it into the window object is the easiest and most straight forward.

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.