简体   繁体   中英

Having trouble returning a 2D array from Google script to use in my HTML

I'm trying to return a 2D array from a google spreadsheet using this:

function getData(){
      //this function reads data from the sheet and retuns a 2D array
          var spreadsheetID = //spreadsheet ID;
          var range = 'SchoolData!A2:B';
          var data = 
          SpreadsheetApp.openById(spreadsheetID).getRange(range).getValues();
          return data;
     }

The returned data should be processed in the compareID() function upon submission of my HTML form.

    <input type="submit" value="Submit" onclick= "authorise()">


    function authorise(){
        alert("inside authorise");
        google.script.run
        .withFailureHandler(errMsg)
        .withSuccessHandler(compareID)
        .getData();

    }

    function compareID(data){
        alert("inside compare");
        //process the data
    }


    function errMsg(){
        alert("no data was returned ");
    }

My code seems to always go to errMsg() and never enters the compareID() function. I even tried to use JSON.stringify(data) in the Google script file and return a String then JSON.parse(data) in the HTML script but still did not get the required results.

What should i do to get my code to enter the compareID() function?

Using your code as shown above i get "inside compare". And data is correct values. Is errMsg called anywhere else in your html?

function test() {
  try {
    var htmlTemplate = HtmlService.createTemplateFromFile('HTML_Test');
    var htmlOutput = htmlTemplate.evaluate();
    SpreadsheetApp.getUi().showModalDialog(htmlOutput, "Sample");
  }
  catch(err) {
    Logger.log(err);
  }
}

function getData(){
  //this function reads data from the sheet and retuns a 2D array
  var spreadsheetID = SpreadsheetApp.getActiveSpreadsheet().getId();
  var range = 'Sheet2!A2:B';
  var data = SpreadsheetApp.openById(spreadsheetID).getRange(range).getValues();
  return data;
}

<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<div>
  <input type="submit" value="Submit" onclick= "authorise()">
</div>
<script>
  function authorise(){
    alert("inside authorise");
    google.script.run
    .withFailureHandler(errMsg)
    .withSuccessHandler(compareID)
    .getData();
  }

  function compareID(data){
    alert("inside compare");
    //process the data
  }

  function errMsg(){
    alert("no data was returned ");
  }
</script>
</body>
</html>

It looks like you are trying to use the Google Sheets API directly, but when using Apps Script you have access to the much more robust and convenient SpreadsheetApp service.

var data = SpreadsheetApp.openById(spreadsheetID).getRange(range).getValues();

see: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM