UNPKG

@sheetxl/io

Version:

IO - Import/Export Libraries for SheetXL.

49 lines (48 loc) 2.23 kB
/** * @license @sheetxl/io - IO - Import/Export Libraries for SheetXL. - v0.7.5 * * (C) 2025-present SheetXL Inc. & Michael T. Ford * License: The license can be found at https://www.sheetxl.com/license. */ import { Workbook as e, IRange as t } from "@sheetxl/sdk"; const n = async (e2, t2) => { const { trimLeadingBlankRow: n2 = true, trimLeadingBlankColumn: o2 = true, excludeHidden: r = false, sheetKey: s = null, papaParseConfig: a = {} } = t2 || {}, i = await import("papaparse"); if (!e2) throw new Error("workbook must be specified"); let l = null; if (null === s ? l = e2.getSelectedSheet() : "number" == typeof s ? l = e2.getSheets().getItems()[s] : "string" == typeof s && (l = e2.getSheets().getByName(s)), !l) throw new Error("invalid sheetKey specified"); let p = null; const d = []; let c = null, g = l.getUsedRange({ excludeHidden: r }); if (!g) throw new Error("Nothing to export"); if (!n2 || !o2) { const e3 = n2 ? g.getRowStart() : 0, t3 = o2 ? g.getColumnStart() : 0; g = g.getOffsetTo(e3, t3); } g.forEach((e3, t3) => { const n3 = t3.getCoords(), o3 = t3.getCell(); p !== n3.rowIndex && (c && d.push(c), c = [], p = n3.rowIndex); const r2 = o3.toTextUnformatted() ?? ""; c.push(r2); }, { includeEmpty: true, address: null }), c && d.push(c); const u = { ...a }, m = i.unparse(d, u); return new TextEncoder().encode(m).buffer; }, o = async (n2, o2) => { const { papaParseConfig: r, setValuesOptions: s, createWorkbookOptions: a } = o2 ?? {}, i = o2?.progress, l = i?.onStart?.(o2?.name ?? "Parsing CSV"); await Promise.resolve(l); const p = i?.onProgress, d = await import("papaparse"), c = new TextDecoder().decode(n2); let g = -1; const u = new e(a), m = u.getSelectedSheet().getEntireRange().startIncrementalUpdates({ orientation: t.Orientation.Row }); let f = 0; const w = { ...r, transform: (e2, t2) => { r?.transform && (e2 = r.transform(e2, t2)); const n3 = t2; 0 === n3 && g++; let o3 = e2 ?? ""; return m.pushAt(g, n3, o3), f++, f % 1e5 == 0 && p?.(f), i?.onComplete?.(f), e2; } }; return d.parse(c, w).data, m.apply({ textParser: true, ...s }), u; }; export { o as fromBufferCSV, n as toBufferCSV };