@sheetxl/io
Version:
IO - Import/Export Libraries for SheetXL.
47 lines (46 loc) • 2.11 kB
JavaScript
/**
* @license @sheetxl/io - IO - Import/Export Libraries for SheetXL. - v0.4.0
*
* (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: r2 = true, excludeHidden: o = false, sheetKey: s = null, papaParseConfig: a = {} } = t2 || {}, l = await import("papaparse");
if (!e2) throw new Error("workbook must be specified");
let i = null;
if (null === s ? i = e2.getSelectedSheet() : "number" == typeof s ? i = e2.getSheets().search()[s] : "string" == typeof s && (i = e2.getSheets().getByName(s)), !i) throw new Error("invalid sheetKey specified");
let p = null;
const c = [];
let d = null, u = i.getUsedRange({ excludeHidden: o });
if (!u) throw new Error("Nothing to export");
if (!n2 || !r2) {
const e3 = n2 ? u.getRowStart() : 0, t3 = r2 ? u.getColumnStart() : 0;
u = u.getOffsetTo(e3, t3);
}
u.forEach((e3, t3) => {
const n3 = t3.getCoords(), r3 = t3.getCell();
p !== n3.rowIndex && (d && c.push(d), d = [], p = n3.rowIndex);
const o2 = r3.toTextUnformatted() ?? "";
d.push(o2);
}, { includeEmpty: true, address: null }), d && c.push(d);
const g = { ...a }, f = l.unparse(c, g);
return new TextEncoder().encode(f).buffer;
}, r = async (n2, r2) => {
const { papaParseConfig: o, setValuesOptions: s, constructorOptions: a } = r2 ?? {}, l = await import("papaparse"), i = new TextDecoder().decode(n2);
let p = -1;
const c = new e(a), d = c.getSelectedSheet().getEntireRange().startIncrementalUpdates({ orientation: t.Orientation.Row }), u = r2?.onProgress;
let g = 0;
const f = { ...o, transform: (e2, t2) => {
o?.transform && (e2 = o.transform(e2, t2));
const n3 = t2;
0 === n3 && p++;
let r3 = e2 ?? "";
return d.pushAt(p, n3, r3), g++, g % 1e5 == 0 && u?.(g), e2;
} };
return l.parse(i, f).data, d.apply({ textParser: true, ...s }), c;
};
export {
r as fromBufferCSV,
n as toBufferCSV
};