Grant
Grant

Reputation: 131

Can not return 2D Array in Google Script?

I am creating a Google Script to poll data from a spreadsheet (full code in the question here) and am getting this error when the google script is called: Uncaught Error: The script completed but the returned value is not a supported return type. The google script is trying to return a 2D array - how can I fix the function so that it successfully passes the 2D array back to the next Javascript function?

function handleFormSubmit() {
    var formObject = document.getElementById('text').value;
    google.script.run.withSuccessHandler(createTable).getSportData(formObject);
  }

 /**
 * Adds an html table
 */ 
 function createTable(tableData) {
  console.log("The tableData for createTable() is " + tableData);
  var table = document.createElement('table');
  var tableBody = document.createElement('tbody');

tableData.forEach(function(rowData) {
  var row = document.createElement('tr');

  rowData.forEach(function(cellData) {
    var cell = document.createElement('td');
    cell.appendChild(document.createTextNode(cellData));
    row.appendChild(cell);
  });

  tableBody.appendChild(row);
});

table.appendChild(tableBody);
document.body.appendChild(table);
}

Upvotes: 0

Views: 651

Answers (1)

azawaza
azawaza

Reputation: 3084

In your getSportData function return JSON.stringify(your2Darrayvar) instead of the actual array. Then in your createTable function use JSON.parse(data) to convert it back to proper 2D array.

Upvotes: 1

Related Questions