I have a custom sidebar generated via Apps Script on a Google Sheet which has both a View Option "filter" (radio buttons which shows/hides certain columns and rows) together with an Export CSV button.
The intended functionality is for the user to select their desired View Option and click the Export CSV button and a CSV file will be added to their download queue. The resulting CSV file will only include the rows & columns visible on the screen (due to the View Option).
The following code successfully downloads the CSV file, but includes visible and hidden rows/columns:
HTML CSV Export button
<h4>Export as CSV</h4>
<form id="thisSheetForm">
<button class="red" onclick="download('csv')">Export as CSV</button>
</form>
JS Function Handler in sidebar.html
<script>
function download(type) {
google.script.run
.withSuccessHandler(({ data, filename }) => {
if (data && filename) {
const a = document.createElement("a");
document.body.appendChild(a);
a.download = filename;
a.href = data;
a.click();
}
})
.createDataUrl(type);
}
</script>
Function in code.gs
function createDataUrl(type) {
const mimeTypes = { csv: MimeType.CSV, pdf: MimeType.PDF };
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
let url = null;
if (type == "csv") {
url = `https://docs.google.com/spreadsheets/d/${ss.getId()}/export?format=csv&gid=${sheet.getSheetId()}`;
} else if (type == "pdf") {
url = `https://docs.google.com/spreadsheets/d/${ss.getId()}/export?format=pdf&gid=${sheet.getSheetId()}`;
}
if (url) {
const blob = UrlFetchApp.fetch(url, {
headers: { authorization: `Bearer ${ScriptApp.getOAuthToken()}` },
}).getBlob();
return {
data:
`data:${mimeTypes[type]};base64,` +
Utilities.base64Encode(blob.getBytes()),
filename: `${sheet.getSheetName()}.${type}`,
};
}
return { data: null, filename: null };
}
The above createDataUrl() function was the only way I had any success downloading the CSV file. All the other options I tried either brought up a javascript error re: security or just downloaded it to Drive instead of adding to the users browser download queue.
But I'm now struggling to see how I can manipulate the function so the resulting CSV data only includes the columns and rows I want. Is there a way to modify the url to only pull off certain columns/rows or is what I'm trying to do simply not possible?
Any help would be hugely appreciated.