How to use the two-dimensional array in an html page script after getting it from a doGet

145 views
Skip to first unread message

Email Test Corsi Google

unread,
Jun 7, 2023, 6:24:21 AM6/7/23
to Google Apps Script Community
how can I retrieve the two-dimensional "data" array from the doget that I report below, in the "Interface" html page where I want to create a select with a script with the options retrieved from the second element of the aforementioned array?

    var sheet = SpreadsheetApp.openById("").getSheetByName("Provincie X Regione");
    var sheet2 = SpreadsheetApp.openById("").getSheetByName("ComuniXProvReg");
    var data = sheet.getDataRange().getValues(); // Provincie X Regione
    var data2 = sheet2.getDataRange().getValues(); // ComuniXProvReg
    data.shift(); // elimino la prima riga di data
    data2.shift(); // elimino la prima riga di data

  function doGet(e) { // Passo un vettore

    var template = HtmlService.createTemplateFromFile('Interfaccia');
    template.data = data;
    template.data2 = data2;
    return template.evaluate().setSandboxMode(HtmlService.SandboxMode.IFRAME);
  }

Radek R

unread,
Jun 7, 2023, 1:52:19 PM6/7/23
to Google Apps Script Community
Hey,

I would not pass the whole array to the html if not needed.
If you need to convert that array to select options then maybe something like this:
const arrOptions = ['Opt1', 'Opt2'];
const html = HtmlService.createTemplateFromFile('index');
const options = arrOptions.map(option => `<option>${option}</option>`).join("");
html.options = options;

and in the index.html file
<select>
    <?!= options ?>
</select>

If you want to get whole array you can use something like this (or load data on page load by making backhand call with google.script.run):
function doGet() {
  const data = [[1,2], [3,4]];
  const addHtml = `<script>
  var data = ${JSON.stringify(data)};
  console.log(data);
  </script>`;
  return HtmlService.createHtmlOutputFromFile('index').append(addHtml);
}

Email Test Corsi Google

unread,
Jun 8, 2023, 3:01:39 AM6/8/23
to Google Apps Script Community
Thanks for the replies, but I need to bring the two-dimensional array into the html page to be able to manipulate it with scripts without calling the data each time with a google.script.run in order to speed up and make processing times more efficient.
I specify that from the array I have to retrieve select options based on the value selected by another select.
Thanks in advance to anyone who can give me the solution.
Reply all
Reply to author
Forward
0 new messages