2021年4月5日星期一

Array returns as null in JS but "non-null" in GS

I'm attempting to use Apps Script and JS on a web app to retreive information stored in a google sheet, then output it to the webapp. However, I keep getting the error "cannot read property of null".

Problem is, when i run the apps script side of the code (as follows), my array is fine and when I log it to the console, it comes up no problem :

function getInfoEdit(employeeInfo){    var ss = SpreadsheetApp.openById(id);    var wsData = ss.getSheetByName('DATA');    var data = wsData.getDataRange().getValues();      var infoArray = new Array();      //FIND     var matricule = employeeInfo.name;    Logger.log(matricule);    for(var i = 0; i<data.length;i++){      if(data[i][41] == matricule){ //[41] for column AP        var row = i+1; // row of data to be retreived        Logger.log(row);      }}      for (var i=1; i<42 ; i++){      infoArray.push(wsData.getRange(row,i).getValue()); // push data into array    }    return (infoArray); // return the array  }  

When I run my code from the web app, I get the error. Here is the JS code :

// This function is executed when I click on a button on the web page  function clickGetInfo(){      var employeeInfo = {};    employeeInfo.name = document.getElementById('autoEmployee').value;    google.script.run.withSuccessHandler(updateText).getInfoEdit(employeeInfo);  }    function updateText(infoArray){  var elements = ["matricule","sexe", "civ", "nom", "prenom", "nomJf", "birthday", "nationalite", "adresse", "codePostal", "ville", "pays", "communeNais", "paysNais", "dptNais", "numerosecu", "email",   "telephone", "shop", "contrat", "hhebdo", "dateDebut", "periodeEssais", "dateFin", "tuteur", "poste", "statut", "echelon", "salaire", "iban", "bic", "domiciliation",   "primeTel", "primeFroid", "comment", "rqth", "commentRqth", "titreSejour", "numeroTitre", "dateExpiration","dateIngestion"]; // this is the array of id's for the html code      for (var i = 0;i<elements.length;i++){    // console.log(document.getElementById(elements[i]).value);    // console.log(infoArray);    document.getElementById(elements[i]).value = infoArray[i];    }  }  

I've narrowed the error down to the last line of code, and to be more specific, the infoArray[i]. It just comes out as an array full of "null", even though when I run the GS code, it retruns the array as it should, with all the info as it is in the spreadsheet.

I've read at least 10 other threads, but still can't seem to figure out why this won't work. Any insight would be much appreciated !

Thank you

https://stackoverflow.com/questions/66961408/array-returns-as-null-in-js-but-non-null-in-gs April 06, 2021 at 08:48AM

没有评论:

发表评论