Home > Back-end >  Not being able to transfer value from Google Script file to HTML file
Not being able to transfer value from Google Script file to HTML file

Time:08-19

I have this function that returns the value in the 3rd row

function sheetData(id_test, id_group, nr_test) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Teste');
  const range = sheet.getDataRange();
  const res = range.getValues();
  const data = res.slice(1);
  let value;
  data.forEach((row) => {
    if (id_test == row[0] && id_group == row[1] && nr_test == row[2]) {
      value = row[3];
    }
  })
  return value;
}

But when I call it to use the returned value in HTML like this:

  <? let value = sheetData(); ?>
  <p > <?= value ?> </p>

I get nothing. Could it be that I'm already calling this function here like this?:

 send_button.addEventListener('click', () => {
    let id_test = document.querySelector('.id-test').value;
    let id_group = document.querySelector('.id-group').value;
    let nr_test = document.querySelector('.nr-test').value;
    google.script.run.sheetData(id_test, id_group, nr_test);
  });

CodePudding user response:

You get nothing because you are not passing any parameter, so they become undefined. To fix this add the default parameters, i.e. <? let value = sheetData('value 1','value 2','value 3'); ?> or sheetData(id_test = 'value 1', id_group = 'value 2', nr_test = 'value 3').

Related

CodePudding user response:

Try something like this:

send_button.addEventListener('click', () => {
    let id_test = document.querySelector('.id-test').value;
    let id_group = document.querySelector('.id-group').value;
    let nr_test = document.querySelector('.nr-test').value;
    google.script.run
    .withSuccessHandler(function(v){
      document.getElementById("id").value = v;
    })
    .sheetData(id_test, id_group, nr_test);
  });
  • Related