Search code examples
javascripthtmlgoogle-apps-scriptgoogle-visualizationgoogle-apps

How to retrieve data from spreadsheet for chart google script. (Using HTML Service)


Hey good morning/afternoon/evening depending where you guys are at!

Just have a quick question for you guys. I've been stuck on this for a bit, and I've tried looking around, but i can't seem to find the answer for this specific issue.

So I'm in the process of trying to create a column chart using google scripts with the html service (since they UIApp service is depreciated now). I'm trying to retrieve the data from a spreadsheet have already created. But when i use this code, the page comes up blank as if there is some error in the background.

var sheet = SpreadsheetApp.openById("1gdRB6FFV426bAj95C0xJlqSucnnX0Z5ATVQdC2");

So here are some specifics:

1) I put this line in my Index.html file within a function I have called

drawChart();

2) I do have them wrapped in this tag.

<script type="text/javascript">

3) I know that this line of code is the cause of the issue because as soon as i comment it out my temp graph with temp data pops up, and page runs fine. But as soon as uncommented it blanks out the whole page.

Any ideas?

I'm wondering if I have to actually place this line of code within the "Code.gs" file, then some how transfer the data from Code.gs to my Index.html file. If that's the case can anyone point me in the direction to where I can follow the direction on how to do it?

Thank you guys in advance.

Sincerely, Sicariuxs


Solution

  • You must use a withSuccessHandler(functionNameToReceiveData).

    <script>
      function getData() {
        google.script.run
          .withSuccessHandler(functionNameToReceiveData)
          getData();
      };
    
      function functionNameToReceiveData(theDateReceivedHere) {
        Logger.log('theDateReceivedHere: ' + theDateReceivedHere;
    
      };
    </script>
    

    You can't use: var data = google.script.run.getData(); You must use a withSuccessHandler(functionNameToReceiveData).