0

I'm trying to use this StackOverflow question How to pass url parameter value into string filter input in Google visualization to pass URL parameters to a Google Dashboard I created using Mogsdad's excellent writeup.

The dashboard works great, but I can't pass a parameter, e.g. ?state=Texas from the URL to use as a content filter. As soon as I call the function in my JavaScript.html file, the dashboard fails to draw:

var stateFilter = stateurl('state'); //if I use this ANYWHERE in the html file, the dashboard fails

    function stateurl(variable){ //in tests this works fine on its own
     var url = ScriptApp.getService().getUrl(); 
     var vars = url.split("&");
     for (var i=0;i<vars.length;i++) {
      var pair = vars[i].split("=");
      if(pair[0] == variable){
       return pair[1];
      }else return 'Texas'
     }
    }

The goal of doing this is to enable my coworkers to create shortcuts to the most commonly used filters / refresh easily.

1
  • I'm not that familiar with the Google Apps Script API but you might want to check if the value returned by ScriptApp.getService().getUrl() contains the ? character. If it does then I recommend taking a look at the conditional in your if statement. Commented Dec 30, 2017 at 3:36

1 Answer 1

0

Okay, I figured out a workaround using HTML (which I'm even worse at than javascript!) so it is probably not the best, BUT it works. In the doGet function I added these two lines (switched to city versus state):

function doGet(e) {

  var template = HtmlService.createTemplateFromFile('Index');

  var city = e.parameter.city; //grab city parameter

  template.city = city; //build template, then proceed as usual

  // Build and return HTML in IFRAME sandbox mode.
  return template.evaluate()
      .setTitle("KPIs")
      .setSandboxMode(HtmlService.SandboxMode.IFRAME);
}

Then I add this simple div to the Index.html (and for me I set the css style to display: none)

<div id="cityname"><?=city?></div>

Then in my dashboard function, I refer to the div:

var city = document.getElementById('cityname').innerHTML;
if (city=="undefined"){
var city = "Austin";}

This answers it for me, but others may have a better JS fix of course. I used this strategy to add other possible filters, just duplicate the two lines in doGet for other parameters.

For more, see:

Pass URL parameter into Web App script

How to pass url parameter value into string filter input in Google visualization

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.