Q:

How to get data from Google Spreadsheet to HTML as a javascript variable?

I am trying to create webapp with html service. Here is my code.
The code in code.gs file.
function doGet() {
  return HtmlService
      .createTemplateFromFile('userendhtml')
      .evaluate();
}

function getData() {
var sss = SpreadsheetApp.openById('xxxx'); 
var rawData =  sss.getDataRange().getValues()     
var data = []                       
for (var i = 0; i< rawData.length; i++){

data.push(rawData[i])

}
  return data
}

Now I want to access this data as javascript variable for further processing (for creating table with querying). The code in userendhtml.html is
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
<script>
 var data2 = getData();
</script>
  </body>
</html>

This doesn't store data to variable data2. So how to get data from spreadsheet in variable in javascript? 

A:

You want to use google.script.run in conjunction with withSuccessHandler(...)
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <script>
      var data2;

      google.script.run.withSuccessHandler(function(ret){
        data2 = ret;
      }).getData();
    </script>
  </body>
</html>

