UNPKG

@sheetxl/io

Version:

IO - Import/Export Libraries for SheetXL.

47 lines (46 loc) 2.13 kB
/** * @license @sheetxl/io - IO - Import/Export Libraries for SheetXL. - v0.7.27 * * (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 r = async (e2, t2) => { const { trimLeadingBlankRow: r2 = true, trimLeadingBlankColumn: n2 = true, ignoreHidden: o = 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({ ignoreHidden: o }); if (!g) throw new Error("Nothing to export"); if (!r2 || !n2) { const e3 = r2 ? g.getRowStart() : 0, t3 = n2 ? g.getColumnStart() : 0; g = g.getOffsetTo(e3, t3); } g.forEach((e3, t3) => { const r3 = t3.getCoords(), n3 = t3.getCell(); p !== r3.rowIndex && (c && d.push(c), c = [], p = r3.rowIndex); const o2 = n3.toTextUnformatted() ?? ""; c.push(o2); }, { includeEmpty: true, address: null }), c && d.push(c); const u = { ...a }, f = i.unparse(d, u); return new TextEncoder().encode(f).buffer; }, n = async (r2, n2) => { const { papaParseConfig: o, setValuesOptions: s, createWorkbookOptions: a } = n2 ?? {}, i = n2?.progress, l = i?.onProgress, p = await import("papaparse"), d = new TextDecoder().decode(r2); let c = -1; const g = new e(a), u = g.getSelectedSheet().getEntireRange().startIncrementalUpdates({ orientation: t.Orientation.Row }); let f = 0; const m = { ...o, transform: (e2, t2) => { o?.transform && (e2 = o.transform(e2, t2)); const r3 = t2; 0 === r3 && c++; let n3 = e2 ?? ""; return u.pushAt(c, r3, n3), f++, f % 1e5 == 0 && l?.(f), e2; } }; return p.parse(d, m).data, u.apply({ textParser: true, ...s }), g; }; export { n as readBufferCSV, r as writeBufferCSV };