Actually I've found the answer myself. activeSheet variable was actually pointing to a dashboard. Since we cannot act directly on the dashboard, all I have to do was:
activeSheet.getWorksheets().get("Sample1").applyFilterAsync("Region", "A", tableauSoftware.FilterUpdateType.REPLACE);
And it works fine now.
Hey Nay -
The problem here is that it looks like you're attempting to filter your dashboard, rather than the sheet WITHIN your dashboard. Here's an example that selects marks in a sheet inside a dashboard. It should give you an idea of how to get a reference to the sheet in the dashboard so you can filter it directly.
// First, access the workbook. Ask for the active sheet (getActiveSheet), which will be sheet // "Dashboard" // (assuming that the Dashboard sheet has focus). Ask the Dashboard sheet for the the collection // of worksheets it contains (getWorksheets). Finally, pull "Profit v. Sales" from that collection and assign it // to a variable mainWorkbook = mainViz.getWorkbook(); var worksheet = mainViz.getWorkbook().getActiveSheet().getWorksheets().get("Profit v Sales"); worksheet.selectMarksAsync("Category", "Technology", "REPLACE");
Thanks Russell. I managed to figure it out myself and posted the response as a reply to myself (just 11mins before your post--you had already begun typing by then I guess).
Have you ever had more than one viz (from 2 entirely different projects/environments) and both have YEAR field.
Now I would like to use TABLEAU JS to display both vizes next to each other (inline-block) at the same time
using a single <select year tag onclick call FilterYear and apply year filter to viz1 and then to viz2...
I can't get this to work...
There is no simple way to make this work - you'll have to manage applying the filter to both vizzes yourself. There are probably a couple ways to do this:
- Show the filter from one/or the other viz and then put an event listener on it. When the user changes the value (impacting workbook1), your event listener will fire and then you'll have additional code which reads what the filter was changed to and apply that value to the second viz.
- Use a 3rd party "year picker" and use the JS API to apply the selection to both sheets.