Pass an array of JSON objects from Google Sheets to HTML service
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:
function onSuccess(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.