J. G.
J. G.

Reputation: 1832

Webapp, return text string not working as expected

This is the code in my html page:

<table id="table_id" class="display"></table>
<div id="status">Goodbye</div>

and then later

 <script>
  document.getElementById('table_id').innerHTML =   google.script.run.getTable(); 
  document.getElementById('status').innerHTML ='<p>Thanks!</p>';</script>

The status part DOES work (I was testing to make sure that scripts were acknowledged in that part of the page.

the getTable function,though, is setting to "undefined".

getTable is in my code.gs page

function getTable(){
  var sh = SpreadsheetApp.openById('XXX-t_SjDjQhe3enJYB40');
  var ss = sh.getSheetByName("Form");
  var data = ss.getDataRange().getValues();
  var htmlTable = '<tr><th>Project</th><th>Who</th><th>Status</th><th>Details</th><th>Advance Status</th></tr>';

  for (var i=0; i<data.length;i++)
    {
    var project = data[i][11]; 
    var who = data[i][1];
    var status = data[i][10];
    var link = data[i][12];
    if (status == ""){ status = 1; ss.getRange(i+1,9).setValue(1);  }      

      htmlTable +="<tr>";          
       htmlTable +="<td>"+project+"</td>";  
      htmlTable +="<td>"+who+"</td>";  
      htmlTable +="<td>"+status+"</td>";  
      htmlTable +="<td><a href='"+link+"'</a></td>"; 





    }//for loop

  htmlTable +='<td><input type ="button" id="arrow" value="-->" onclick =""/></td></tr>';
  //htmlTable += "</table>";

  return htmlTable;       

}//*****************************************************************************

Things I've tried.

Initially there were "table" wrappers in getTable (but then I thought that might be not cooperating with the innerhtml part). I also tested this function on its own and it does return a chunk of string that is the right html table, so it isn't returning nothing in the normal context.

Do I absolutely need to be using a successhandler and another function here? I feel like this looks like it should work

Upvotes: 0

Views: 49

Answers (1)

Karl_S
Karl_S

Reputation: 3554

Change the script section to

<script>
  document.getElementById('status').innerHTML ='<p>Thanks!</p>';
  google.script.run.withSuccessHandler(buildTable).getTable();

  function buildTable(myTable) {
    document.getElementById('table_id').innerHTML = myTable;
  }
</script>

Note that this is not tested but should get you close. The withSuccessHandler is used to determine what function is run when the server side function is successful. I would also add .withFailureHandler(tableFailure) and a function called tableFailure for if the server side function fails.

Upvotes: 1

Related Questions