-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
92 lines (76 loc) · 3.38 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
document.addEventListener('DOMContentLoaded', () => {
console.log("Initializing Tableau Extension...");
tableau.extensions.initializeAsync().then(() => {
console.log("Tableau API initialized successfully!");
populateWorksheetDropdown();
}).catch(err => {
console.error("Error initializing Tableau API:", err);
});
// Attach click event to the export button
document.getElementById('exportBtn').addEventListener('click', exportToCSV);
});
async function populateWorksheetDropdown() {
try {
const dashboard = tableau.extensions.dashboardContent.dashboard;
const worksheetSelect = document.getElementById('worksheetSelect');
if (dashboard.worksheets.length === 0) {
alert("No worksheets available in this dashboard.");
return;
}
dashboard.worksheets.forEach(ws => {
const option = document.createElement('option');
option.value = ws.name;
option.text = ws.name;
worksheetSelect.appendChild(option);
});
document.getElementById('exportBtn').disabled = false;
console.log("Worksheet dropdown populated.");
} catch (error) {
console.error("Error populating worksheets:", error);
}
}
async function exportToCSV() {
try {
const dashboard = tableau.extensions.dashboardContent.dashboard;
const selectedWorksheetName = document.getElementById('worksheetSelect').value;
const worksheet = dashboard.worksheets.find(ws => ws.name === selectedWorksheetName);
if (!worksheet) {
alert("Selected worksheet not found.");
return;
}
console.log(`Exporting data from: ${worksheet.name}`);
// Use summary data instead of underlying data
const options = {
maxRows: 1000000, // Increase limit as needed
ignoreSelection: true
};
const dataTable = await worksheet.getSummaryDataAsync(options);
if (dataTable.data.length === 0) {
alert("No data available for export.");
return;
}
const columns = dataTable.columns.map(col => col.fieldName);
// Sort rows by the number of populated values (most populated fields first)
const sortedData = dataTable.data.sort((a, b) => {
const populatedCountA = a.filter(cell => cell.formattedValue !== "Null" && cell.formattedValue.trim() !== "").length;
const populatedCountB = b.filter(cell => cell.formattedValue !== "Null" && cell.formattedValue.trim() !== "").length;
return populatedCountB - populatedCountA; // Descending order (most populated first)
});
// Build CSV content
let csvContent = columns.join(",") + "\n";
sortedData.forEach(row => {
const rowData = row.map(cell => `"${cell.formattedValue}"`);
csvContent += rowData.join(",") + "\n";
});
// Trigger file download
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = `${worksheet.name}_export.csv`;
link.click();
console.log("CSV export completed!");
} catch (error) {
console.error("Error during CSV export:", error);
alert("CSV export failed. See console for details.");
}
}