Skip to content Skip to sidebar Skip to footer

Pass An Array Of Json Objects From Google Sheets To Html Service

I have a spreadsheet with a number of hotel names along with addresses, phone numbers and contact names as follows: I have a function which stores these hotels as JSON objects as

Solution 1:

Sorted this in the end with the help of @tehhowch.

I amended my server side function to return the JSON array:

function findHotel() {
  var jsonArr = [];

  for (var i = 0; i < nameRange.length; i++) {
    jsonArr.push({
      name: ss.getRange(nameRange[i]).getValue(),
      address: ss.getRange(addressRange[i]).getValue(),
      contact: ss.getRange(contactRange[i]).getValue(),
      tel: ss.getRange(telRange[i]).getValue(),
    });
  }
  return jsonArr;
};

Then on the client side I wrote the following function:

functiononSuccess(test) {
  var idArray = [1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 22, 23, 24, 25, 26, 29, 30, 31, 32, 33];
  var hotelArray = test;
  for (var i = 0; i < idArray.length; i++) {
    $("#result" + (idArray[i])).html(test[i].name);
  }
}

google.script.run.withSuccessHandler(onSuccess).findHotel();

The idArray is used to access the various 'result' IDs and I now can not only print the hotel names in the required place but I also now have full access to the arrays created on the server side.

Post a Comment for "Pass An Array Of Json Objects From Google Sheets To Html Service"