UNPKG

@obliczeniowo/elementary

Version:
64 lines (60 loc) 2.26 kB
import * as XLSX from 'xlsx'; /** * Save array to xlsx file * @param arr array to export * @param name file name * @param sheet base excel sheet name (will create multiple of sheet's if maxRow is less then number of array rows) * @param properties XLSX properties and maxRows, headers properties */ function arrayToXlsx(arr, name, sheet, properties, bookType) { arr = [...arr]; const maxRows = 1048576; const minRows = 10; if (properties?.maxRows) { if (properties.maxRows < minRows) { console.warn(`Minimum value for maxRows is 10, but ${properties.maxRows} was given. Override to ${minRows}`); properties.maxRows = minRows; } else if (properties.maxRows > maxRows) { console.warn(`Maximum value for maxRows is ${maxRows}, but ${properties.maxRows} was given. Override to ${maxRows}`); properties.maxRows = maxRows; } } const splitted = []; const max = properties?.maxRows || maxRows; while (arr.length > (properties?.headers && max - 1 || max)) { if (properties?.headers) { splitted.push([properties.headers, ...arr.splice(0, max - 1)]); } else { splitted.push(arr.splice(0, max)); } } if (arr.length) { splitted.push([properties?.headers || [], ...arr]); } const wb = XLSX.utils.book_new(); splitted.forEach((t, index) => { const table = document.createElement('table'); t.forEach((row, index) => { const tRow = document.createElement('tr'); row.forEach(ceil => { const tCeil = document.createElement(properties?.headers && index === 0 && 'th' || 'td'); tCeil.textContent = ceil.toString(); tRow.appendChild(tCeil); }); table.appendChild(tRow); }); const ws = XLSX.utils.table_to_sheet(table); XLSX.utils.book_append_sheet(wb, ws, index === 0 && sheet || `${sheet} ${index}`); }); XLSX.writeFile(wb, name, { Props: properties, bookType: bookType || 'xlsx' }); } /** * Generated bundle index. Do not edit. */ export { arrayToXlsx }; //# sourceMappingURL=obliczeniowo-elementary-xlsx.mjs.map