Last active
March 2, 2023 02:39
-
-
Save darshit-shah/0cda3feb94cf55ddcb70e0117b1b127c to your computer and use it in GitHub Desktop.
Get Sheet data from Tableau Visualization link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
td, | |
th { | |
padding: 0px 30px; | |
border: 2px solid white; | |
} | |
a { | |
color: blue; | |
} | |
.ui-tabs { | |
overflow: auto; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Tableau GetData Test</title> | |
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> | |
<!-- <link rel="stylesheet" href="/resources/demos/style.css"> --> | |
<link rel="stylesheet" type="text/css" href="index.css"> | |
</head> | |
<body> | |
<div class="page-header"> | |
<h3>This demo will help to get underlying data from tableau visualization. You can get public dashboards from <a href="https://public.tableau.com/en-us/s/gallery" target="_blank">here.</a></h3> | |
</div> | |
URL of tableau dashboard: | |
<input id="tableauURL" type="text" value="" style="width:100%" /> | |
<div id="tabs" style="display:none"> | |
<ul> | |
<li><a href="#tabs-1">Visualization</a></li> | |
<li><a href="#tabs-2">Data</a></li> | |
</ul> | |
<div id="tabs-1"> | |
<div id="vizContainer"></div> | |
</div> | |
<div id="tabs-2"> | |
Worksheet Name : | |
<select id="menu"></select> | |
<div id="dataContainer"> | |
</div> | |
</div> | |
</div> | |
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> | |
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script> | |
<script src="https://public.tableau.com/javascripts/api/tableau-2.js"></script> | |
<script type="text/javascript" src="index.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function() { | |
$("#tabs").show(); | |
$("#tabs").tabs(); | |
$("#vizContainer").height(window.innerHeight - 200) | |
$("#dataContainer").height(window.innerHeight - 250) | |
$("#tableauURL").change(function() { | |
initViz(); | |
}); | |
$("#menu").change(function() { | |
fetchSheetData(); | |
}); | |
}); | |
function initViz() { | |
$("#vizContainer").empty(); | |
$("#dataContainer").empty(); | |
$("#menu").empty(); | |
$("#vizContainer").append("<div id='tableau_visualization'></div>"); | |
var containerDiv = document.getElementById("tableau_visualization"), | |
url = $("#tableauURL").val(), // "https://public.tableau.com/views/hierarchy_demo/demo", | |
//this url shows how it works with a dashboard. Comment the above and uncomment below to switch. | |
//url = "https://public.tableau.com/views/hierarchy_demo/hierarchy_demo", | |
options = { | |
hideTabs: true, | |
hideToolbar: true, | |
onFirstInteractive: function() { | |
workbook = viz.getWorkbook(); | |
getVizData(); | |
} | |
}; | |
viz = new tableau.Viz(containerDiv, url, options); | |
} | |
//when viz is loaded (onFirstInteractive), request data | |
function getVizData() { | |
options = { | |
maxRows: 0, // Max rows to return. Use 0 to return all rows | |
ignoreAliases: false, | |
ignoreSelection: true, | |
includeAllColumns: false | |
}; | |
sheet = viz.getWorkbook().getActiveSheet(); | |
//if active tab is a worksheet, get data from that sheet | |
if (sheet.getSheetType() === 'worksheet') { | |
$('#menu').append('<option>' + sheet.getName() + '</option>'); | |
if ($('#menu option').length === 1) { | |
sheet.getUnderlyingDataAsync(options).then(function(t) { | |
prepareTable(sheet.getName(), t); | |
}); | |
} | |
//if active sheet is a dashboard get data from a specified sheet | |
} else { | |
// alert("SheetType = " + sheet.getSheetType()); | |
// debugger; | |
worksheetArray = viz.getWorkbook().getActiveSheet().getWorksheets(); | |
for (var i = 0; i < worksheetArray.length; i++) { | |
sheet = worksheetArray[i]; | |
if (sheet.getSheetType() == 'worksheet') { | |
$('#menu').append('<option>' + sheet.getName() + '</option>'); | |
} | |
} | |
fetchSheetData(); | |
} | |
} | |
function fetchSheetData() { | |
$('#dataContainer').empty(); | |
worksheetArray = viz.getWorkbook().getActiveSheet().getWorksheets(); | |
for (var i = 0; i < worksheetArray.length; i++) { | |
sheet = worksheetArray[i]; | |
if (sheet.getSheetType() == 'worksheet' && sheet.getName() === $("#menu").val()) { | |
var sheetName =sheet.getName(); | |
sheet.getUnderlyingDataAsync(options).then(function(t) { | |
prepareTable(sheetName, t); | |
}); | |
} | |
} | |
} | |
//restructure the data and build something with it | |
function prepareTable(name, table) { | |
var rowNum = 500; | |
//the data returned from the tableau API | |
var columns = table.getColumns(); | |
var data = table.getData(); | |
// //convert to field:values convention | |
// function reduceToObjects(cols, data) { | |
// var fieldNameMap = $.map(cols, function(col) { | |
// return col.getFieldName(); | |
// }); | |
// var dataToReturn = $.map(data, function(d) { | |
// return d.reduce(function(memo, value, idx) { | |
// memo[fieldNameMap[idx]] = value.value; | |
// return memo; | |
// }, {}); | |
// }); | |
// return dataToReturn; | |
// } | |
// var niceData = reduceToObjects(columns, data); | |
var table = $("<table>"); | |
var tr = $("<tr>") | |
columns.forEach(function(col, i) { | |
var th = $("<th>") | |
th.text(col.getFieldName()); | |
tr.append(th); | |
}); | |
table.append(tr); | |
data.every(function(row, i) { | |
if (i >= rowNum) { | |
return false; | |
} | |
tr = $("<tr>") | |
row.forEach(function(col) { | |
var td = $("<td>") | |
td.text(col.formattedValue); | |
tr.append(td); | |
}); | |
table.append(tr); | |
return true; | |
}); | |
var tableContainer = $("<div class='tableContainer'>"); | |
var tableHeading = $("<p>" + (data.length > rowNum ? " (showing only " + rowNum + "/" + data.length + " rows)" : " (showing " + data.length + " rows)")+"</p>") | |
tableContainer.append(tableHeading); | |
tableContainer.append(table); | |
$('#dataContainer').append(tableContainer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello~ I cannot get data from the below URL. All worksheets in part 2 failed to get. Could you help me? Thanks a lot!
https://public.tableau.com/views/SalesOverview_16591884572210/SalesOverview?%3Adisplay_static_image=y&%3AbootstrapWhenNotified=true&%3Aembed=true&%3Alanguage=zh-CN&:embed=y&:showVizHome=n&:apiID=host0#navType=0&navSrc=Parse,