Skip to content

Commit

Permalink
Merge pull request #461 from SalesforceLabs/455-export-not-working
Browse files Browse the repository at this point in the history
Resolving issue on firefox #455
  • Loading branch information
VinceFINET authored Nov 20, 2024
2 parents 16eab33 + 635f6e5 commit ebe4158
Showing 1 changed file with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,51 @@ export default class OrgcheckExportButton extends LightningElement {

handleClickExportXLS() {
try {
// create workbook
const workbook = this.#api.utils.book_new();
this.source.forEach(item => {
if (!item.rows || item.rows.length === 0) return; // skip if no rows or empty rows
const isTypeSimple = typeof item.columns[0] === 'string';
const datasheet_columns = isTypeSimple ? item.columns : (item.columns.map(c => c?.label));
const datasheet_rows = isTypeSimple ? item.rows : item.rows.map(r => item.columns.map(c => r[c.field]));
const datasheet = [ datasheet_columns ].concat(datasheet_rows);
const worksheet = this.#api.utils.aoa_to_sheet(datasheet);
worksheet['!cols'] = item.columns.map((c, i) => {
const maxWidth = datasheet.reduce((prev, curr) => {
if (typeof curr[i] === 'string') return Math.max(prev, curr[i]?.length);
return prev;
}, 10);
return maxWidth ? { wch: maxWidth } : {};
});
this.#api.utils.book_append_sheet(workbook, worksheet, `${item.header} (${item.rows.length})`);
});
const workfile = this.#api.write(workbook, { bookType: 'xlsx', type: 'base64' });
const workbook = this._createTheWorkBook();
const url = this._generateTheFileAsURL(workbook);

// Generate the link to download
const a = this.template.querySelector('a')
a.href = `data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64, ${workfile}`;
const a = this.template.querySelector('a');
a.href = url;
a.download = `${this.basename}.xlsx`; // Filename Here
a.click(); // Downloaded file
a.click(); // Downloaded file*/

this._releaseTheURL(url);

} catch (error) {
console.error(error);
}
}

_createTheWorkBook() {
const workbook = this.#api.utils.book_new();
this.source.forEach(item => {
if (!item.rows || item.rows.length === 0) return; // skip if no rows or empty rows
const isTypeSimple = typeof item.columns[0] === 'string';
const datasheet_columns = isTypeSimple ? item.columns : (item.columns.map(c => c?.label));
const datasheet_rows = isTypeSimple ? item.rows : item.rows.map(r => item.columns.map(c => r[c.field]));
const datasheet = [ datasheet_columns ].concat(datasheet_rows);
const worksheet = this.#api.utils.aoa_to_sheet(datasheet);
worksheet['!cols'] = item.columns.map((c, i) => {
const maxWidth = datasheet.reduce((prev, curr) => {
if (typeof curr[i] === 'string') return Math.max(prev, curr[i]?.length);
return prev;
}, 10);
return maxWidth ? { wch: maxWidth } : {};
});
this.#api.utils.book_append_sheet(workbook, worksheet, `${item.header} (${item.rows.length})`);
});
return workbook;
}

_generateTheFileAsURL(workbook) {
const workfile = this.#api.write(workbook, { bookType: 'xlsx', type: 'binary' });
const workfileAsBuffer = new ArrayBuffer(workfile.length)
const workfileAsBufferView = new Uint8Array(workfileAsBuffer)
for (let i = 0; i < workfile.length; i++) workfileAsBufferView[i] = workfile.charCodeAt(i) & 0xff
return URL.createObjectURL(new Blob([workfileAsBuffer], { type: 'application/octet-stream' }));
}

_releaseTheURL(url) {
URL.revokeObjectURL(url);
}
}

0 comments on commit ebe4158

Please sign in to comment.