hwilson1
hwilson1

Reputation: 499

Tableau Javascript API workbook.activeSheet()

I'm probably missing something very obvious here but why does the following not write anything to the log? (jsfiddle here - http://jsfiddle.net/hiwilson1/qk9cjuh2/ - although it's not working for me)

<html>
<head>
<style>
#tableauViz {
    width: 1500px;
    height: 500px;
}
input[type="button"] {
    margin: 5px;
}
</style>
<script type="text/javascript" src="https://online.tableau.com/javascripts/api/tableau-2.0.0.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
window.onload = function() {

    // append visualisation container
    d3.select("body").append("div").attr("id", "tableauViz")

    // function to build visualisation.
    function initializeViz() {
        var placeholderDiv = document.getElementById("tableauViz");
        var url = "http://public.tableau.com/views/WorldIndicators/GDPpercapita";
        var options = {
            width: placeholderDiv.offsetWidth,
            height: placeholderDiv.offsetHeight,
            hideTabs: false,
            hideToolbar: true,
            onFirstInteractive: function () {
                workbook = viz.getWorkbook();
                activeSheet = workbook.getActiveSheet();
            }
        };
        viz = new tableau.Viz(placeholderDiv, url, options);
    }  

    initializeViz();

    // switch worksheet function.
    function switchTab(val) {
        workbook.activateSheetAsync(val);
    }    

    d3.select("body").append("input")
        .attr("type", "button")
        .attr("value", "click me")
        .on("click", function() {switchTab("GDP per capita map")})

    console.log(workbook.activeSheet)
}
</script>
</head>

<body>

</body>
</html>

I've built the visualization and then rigged up a button to run a small function that changes worksheet. I can access the workbook object from within that function but there's nothing special about the function that gives it the right to access that workbook object. Why can I not also output info about the workbook object to the log?

The error I receive is 'Reference Error: Workbook is not defined'. If it's not defined how can the function use it?!

Upvotes: 0

Views: 4208

Answers (1)

RichieHindle
RichieHindle

Reputation: 281835

Your workbook variable is not created until the onFirstInteractive callback is called, but your call to console.log() is immediately after initializing your tableau.Viz. The act of initializing that object won't cause the callback to be called - that will happen later.

Depending on the reason you're calling console.log like that, it might make sense to move that call to the end of the onFirstInteractive callback.

Upvotes: 2

Related Questions