I want my users to be able to download a default spreadsheet file with some fields having drop-down values. I know how to do this with Excel/Google Sheets but I’m unable to do this programmatically with React (using a package like Sheets).
Normally, I would manually create the xlsx. file, but the dropdown fields are fairly dynamic. So, I need to generate the spreadsheet template with an up-to-date version of the dropdown fields (after fetching the dropdown data from an external API).
I found someone on Stackoverflow trying to do something similar but no solutions
const handleDownloadCsvTemplate = () => {
if (!selectedCategoryId) return;
const selectedCategory = categories.find(cat => cat.id === selectedCategoryId);
if (!selectedCategory) return;
const headers = selectedCategory.specification.map(spec => spec.name);
const csvContent = 'data:text/csv;charset=utf-8,' + headers.join(',') + 'n';
const encodedUri = encodeURI(csvContent);
const link = document.createElement('a');
link.setAttribute('href', encodedUri);
link.setAttribute('download', `${selectedCategory.name}_template.csv`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
Codewith_Edison is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.