0

I have been trying to set up a dashboard with data from my google spreadsheet, using the HTML service. My code consists out of 4 different files;

  • Main.gs ( consists out of doGet & getSsData )
  • Index.html ( getContent from CSS and JS )
  • StyleSheet.html
  • JavaScript.html

When i execute my code and run it in the web application, it doesn't give me a dashboard but rather shows my raw code as output. (JS and CSS raw)

I've been searching different solutions such as this and this, but i could not make it work despite the solutions given there.

This is some of my code

main.gs

function doGet() {
var HTML = HtmlService.createTemplateFromFile("Index");

return HTML
  .evaluate()
  .setTitle('Dashboard demo')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);

}

function getSpreadsheetData () {
   var sheetId = ""
   var data = SpreadsheetApp.openById(sheetId).getSheetByName("Leadverwerking").getDataRange().getValues();

 Logger.log(data);

return (data.length > 1) ? data : null;   

}

index

<html>
<head>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons.css">
<script src ="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script type="text/javascript"  src="https://www.google.com/jsapi" ></script>

<?= HtmlService.createHtmlOutputFromFile("Stylesheet").getContent(); ?>
</head>
<body>


<div>
**content here**
</div>


   <?= HtmlService.createHtmlOutputFromFile("JavaScript").getContent(); ?>

  </body>
</html>

So i think it has something to do with the getContent function and the placement of this line of code, but i cannot figure out precisely what im doing wrong. Any ideas why it is not working ? Thankyou in advance!

EDIT

I've replaced my

<?= HtmlService.createHtmlOutputFromFile("Stylesheet").getContent(); ?>
<?= HtmlService.createHtmlOutputFromFile("JavaScript").getContent(); ?>

with

<?!= include('Stylesheet'); ?>
<?!= include('JavaScript'); ?>

And added a function in my main.gs

function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename)
    .getContent();

}

Now the webApp shows nothing at all.. but i guess the code is getting parsed ? Still no solution for the blank output.

Included JS

<script>
// Load the Visualization API and desired package
google.load("visualization", "1.0", {"packages":["controls"]});


$(function() {
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(sendQuery);


});

function sendQuery() {

google.script.run
.withSuccesHandler(drawDashboard)
.withFailureHandler(function(msg) {
  // Respond to failure conditions here.
            $('#main-heading').text(msg);
            $('#main-heading').addClass("error");
            $('#error-message').show();
          })
      .getSpreadsheetData();
  }

// Callback function to generate visualization using data in response parameter

function drawDashboard(response) {
    $('#main-heading').addClass("hidden");

    if(response == null) {
    alert("Error: Invalid source data")
    return;
    }

    else {
    var data = google.visualization.arrayToDataTable(response,false);

    var dashboard = new google.visualization.Dashboard(document.getElementsById("dashboard-div"));

    var pieChart = new google.visualization.ChartWrapper ({
      "chartType": "Piechart",
      "containerId": "piechart-div",
      // The pie chart will use the columns 'toelichting 1' and 'Beller'
      // out of all the available ones.
      'view" : { "columns": [1, 8]}
      });

    var table = new google.visualization.ChartWrapper({
        'chartType': 'Table',
        'containerId': 'table-div'
      });

      var tlSlider = new google.visualization.ControlWrapper({
      "controlType" : "NumberRangeFilter",
      "containerId" : "slider-div",
      "options": {
      "filterColumnLabel": "Toelichting"
      }

      });

      var beller = new google.visualization.ControlWrapper({
      "controlType": "CategoryFilter",
      "containerId": "selector-div",
      "options": { 
      "filterColumnLabel": "Beller"
      }
      });

      // afhankelijkheden opzetten tussen charts en controls
      dashboard.bind(tlSlider, [pieChart,table]);
      dashboard.bind(beller, [pieChart,table]);

      // draw alle visualizations in het dashboard
      dashboard.draw(data);
      }

}



</script>

Included CSS

<!DOCTYPE html>
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">

<style>
.result-display {
  margin-left: 15 px;
  font-size: 125%;
}

.error {
  color: #FF0000;
}
.hidden {
  display: none;
}
.google-visualization-controls-label {
  margin-left: 20px;  
}

#dashboard-div {
  width: 800px;
}
#control-div {
  margin: 70px;
}
#charts-div {
  margin: 10px;
}
#slider-div {
  float:left;
}
#selector-div {
}
#piechart-div {
  width: 50%;
  height: 250px;
  float:left;
}
#table-div {
  width: 50%;
  float:right;
}
</style>
  • Use the `force-printing` scriptlet tags: https://developers.google.com/apps-script/guides/html/templates#force-printing_scriptlets The regular scriptlet tags will escape the output. – tehhowch Apr 03 '18 at 12:16
  • @tehhowch I tried that with my include function ; != include('Stylesheet'); ?> != include('JavaScript'); ?> But the page is still blank – MisterDoesntKnowAnything Apr 03 '18 at 12:25
  • Your `doGet()` works. The HTML _without_ including the custom stylesheet and javascript works (as I don't have the files, no use including them). Try excluding one and then the other to see which is causing an error, then share that file. If it is the javascript file, you may also be able to debug further by looking at the javascript console in your browser when visiting your page. – Diego Apr 03 '18 at 12:32
  • @Diego Thanks for replying. Ive excluded both and the result is the same, a blank page. When running the code without including the CSS or JS it doesnt give an error, but just a blank page. If i go to the javascript console it only shows 4 warnings ; Unrecognized feature : Picture in picture and vibrate. Dont know what that means tho. Could it be that my css is wrong or my js code ? Thanks! – MisterDoesntKnowAnything Apr 03 '18 at 12:54
  • Are your styles and scripts wrapped in style and script tags respectively? – Anton Dementiev Apr 03 '18 at 13:03
  • @AntonDementiev Yes, as far as i know it is wrapped in the correct tags. My CSS is begins with and JS – MisterDoesntKnowAnything Apr 03 '18 at 13:09
  • If you see nothing on the page, then it probably works, because you don’t display anything, at least not in the visible part of the script. What kind of output do you expect? – Anton Dementiev Apr 03 '18 at 13:12
  • @MisterDoesntKnowAnything Interesting. I copied your `doGet()`, `include()`, and HTML. From the HTML, I deleted both `=...?>` lines. Then published the web app, and I can see the "\*\*content here\*\*" – Diego Apr 03 '18 at 13:14
  • @AntonDementiev I wired some columns of my spreadsheet with certain charts (piechart and table) and was expecting that to be visible.. But i do not know if my spreadsheet is compatible with the chart. I have an enormous amount of data/columns and wanted to show 2 columns out of all the data. – MisterDoesntKnowAnything Apr 03 '18 at 13:22
  • @Diego well that is weird.. could you help me with the content ? Maybe thats the part where it's not working ? – MisterDoesntKnowAnything Apr 03 '18 at 13:23
  • @MisterDoesntKnowAnything Try replicating my steps above with the copying of your the code you posted in your question and see if that works. If it does, then you'll need to share the javascript file. – Diego Apr 03 '18 at 13:31
  • @Diego I did exactly the same and I see the "content" text. So i think it's probably the js code.. How do I share this code ? Do I edit my question and include the js ? – MisterDoesntKnowAnything Apr 03 '18 at 13:53
  • @MisterDoesntKnowAnything Yes, or post a [gist](https://gist.github.com/) – Diego Apr 03 '18 at 13:53
  • @Diego ive added my js and css for more detail. As it stands now, i've excluded the js and css. Main code consists out of doGet, Include and getData. – MisterDoesntKnowAnything Apr 03 '18 at 14:03
  • @MisterDoesntKnowAnything Delete the doctype and link tag lines above the opening – Diego Apr 03 '18 at 14:08
  • You can set the onLoad callback in optional parameters of google.load() method https://developers.google.com/loader/#googleload Not sure why you are using an IIFE with jquery selector syntax. Because it's self-executing, it could run when the 'google' object is not yet available in global scope (external APIs are loaded asynchronously). Is there a reason for using the $ notation? – Anton Dementiev Apr 03 '18 at 14:08
  • @AntonDementiev I was following a tutorial on making dashboards and this was included, so thats why i have IIFE with jquery selector syntax. Because i was not certain if it would work without that – MisterDoesntKnowAnything Apr 03 '18 at 14:12
  • @Diego i deleted that, only thing i see is "Loading" from the

    Loading...

    , which is correct i guess.. But no charts or anything
    – MisterDoesntKnowAnything Apr 03 '18 at 14:14
  • @MisterDoesntKnowAnything well, you can log to console and see if the google object exists in global scope when the anonymous function is being executed. – Anton Dementiev Apr 03 '18 at 14:14
  • @AntonDementiev No, i also got the $ from the tutorial. I did not understand everything, so I thought copying everything and figuring it out with my basic knowledge would suffice.. – MisterDoesntKnowAnything Apr 03 '18 at 14:20
  • 1
    @MisterDoesntKnowAnything. Great! That resolves the original question :) You may want edit or post a new question that is more specific to your JS now as this has changed significantly – Diego Apr 03 '18 at 14:21
  • @MisterDoesntKnowAnything Copying and pasting someone else's code almost never works. Try to understand basic concepts behind this tutorial and build your own dashboard from scratch. StackOverflow is for asking specific programming questions, not for debugging some else's code line by line. Also, please read https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – Anton Dementiev Apr 03 '18 at 14:28
  • @AntonDementiev I did try to make it from scratch, but it never worked and when asking questions on this forum it was almost directly put under "try somewhere else" or getting a downvote. I tried to make a dashboard with the information provided from google with their examples, but when it comes to making a dashboard with own data (spreadsheet) the information was limited and I could not figure it out. That's why I turned to a tutorial. But my apologies for trying it this way – MisterDoesntKnowAnything Apr 03 '18 at 14:38
  • @MisterDoesntKnowAnything I can relate to what you said and am sorry to hear your experience asking questions has not been positive. Previously, you wrote you were not sure why the code is written the way it is, which leads me to believe you are not very comfortable with using JavaScript just yet. So, the solution is either 1) break the problem into multiple questions and take it one issue at a time, or 2) hire someone to do the job for you. – Anton Dementiev Apr 03 '18 at 14:48
  • @AntonDementiev Thanks for the advise. I'm gonna start all over from scratch and just take one problem at a time. – MisterDoesntKnowAnything Apr 05 '18 at 09:43

0 Answers0