@ricsam/excel-to-formula-engine 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/dist/index.cjs +43 -14
- package/dist/index.cjs.map +6 -6
- package/dist/index.mjs +43 -14
- package/dist/index.mjs.map +6 -6
- package/dist/types/src/types.d.ts +6 -1
- package/package.json +3 -2
package/dist/index.mjs
CHANGED
|
@@ -216,6 +216,11 @@ function parseWorksheet(pkg, partPath, sharedStrings) {
|
|
|
216
216
|
function parseCells(worksheet, sharedStrings, result) {
|
|
217
217
|
const sheetData = worksheet["sheetData"];
|
|
218
218
|
const shared = new Map;
|
|
219
|
+
const columnStyles = asArray(worksheet["cols"]?.["col"]).map((col) => ({
|
|
220
|
+
min: numAttr(col, "min"),
|
|
221
|
+
max: numAttr(col, "max"),
|
|
222
|
+
style: numAttr(col, "style")
|
|
223
|
+
})).filter((col) => col.min !== undefined && col.max !== undefined && col.style !== undefined);
|
|
219
224
|
for (const rawRow of asArray(sheetData?.["row"])) {
|
|
220
225
|
const row = rawRow;
|
|
221
226
|
const rowNumber = numAttr(row, "r");
|
|
@@ -233,11 +238,14 @@ function parseCells(worksheet, sharedStrings, result) {
|
|
|
233
238
|
if (!address) {
|
|
234
239
|
continue;
|
|
235
240
|
}
|
|
236
|
-
const
|
|
241
|
+
const explicitStyle = numAttr(cell, "s");
|
|
242
|
+
const rowStyle = boolAttr(row, "customFormat") ? numAttr(row, "s") : undefined;
|
|
243
|
+
const columnStyle = columnStyles.findLast((col) => address.colIndex + 1 >= col.min && address.colIndex + 1 <= col.max)?.style;
|
|
244
|
+
const styleIndex = explicitStyle ?? rowStyle ?? columnStyle ?? 0;
|
|
237
245
|
const type = attr(cell, "t") ?? "n";
|
|
238
246
|
const formula = readFormula(cell, address, shared);
|
|
239
247
|
const { kind, value } = readValue(cell, type, sharedStrings);
|
|
240
|
-
if (kind === "empty" && formula === undefined &&
|
|
248
|
+
if (kind === "empty" && formula === undefined && explicitStyle === undefined && rowStyle === undefined && columnStyle === undefined) {
|
|
241
249
|
continue;
|
|
242
250
|
}
|
|
243
251
|
result.cells.push({
|
|
@@ -627,14 +635,19 @@ function parseStyles(pkg) {
|
|
|
627
635
|
const fills = asArray(styleSheet["fills"]?.["fill"]);
|
|
628
636
|
const borders = asArray(styleSheet["borders"]?.["border"]);
|
|
629
637
|
const cellXfs = asArray(styleSheet["cellXfs"]?.["xf"]);
|
|
638
|
+
const styleXfs = asArray(styleSheet["cellStyleXfs"]?.["xf"]);
|
|
630
639
|
const formats = cellXfs.map((rawXf) => {
|
|
631
640
|
const xf = rawXf;
|
|
632
641
|
const style = {};
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
642
|
+
const baseXf = styleXfs[numAttr(xf, "xfId") ?? -1];
|
|
643
|
+
const baseFontId = boolAttr(baseXf, "applyFont", true) ? numAttr(baseXf, "fontId") ?? 0 : 0;
|
|
644
|
+
const fontId = boolAttr(xf, "applyFont", true) ? numAttr(xf, "fontId") ?? baseFontId : baseFontId;
|
|
645
|
+
const font = fonts[fontId];
|
|
646
|
+
if (font) {
|
|
647
|
+
style.bold = false;
|
|
648
|
+
style.italic = false;
|
|
649
|
+
style.underline = false;
|
|
650
|
+
applyFont(style, font, theme, indexedColors);
|
|
638
651
|
}
|
|
639
652
|
if (boolAttr(xf, "applyFill", true)) {
|
|
640
653
|
const fill = fills[numAttr(xf, "fillId") ?? -1];
|
|
@@ -679,14 +692,18 @@ function parseStyles(pkg) {
|
|
|
679
692
|
return { formats, differentialFormats };
|
|
680
693
|
}
|
|
681
694
|
function applyFont(style, font, theme, indexedColors) {
|
|
682
|
-
|
|
683
|
-
|
|
695
|
+
const fontFamily = attr(font["name"], "val");
|
|
696
|
+
if (fontFamily) {
|
|
697
|
+
style.fontFamily = fontFamily;
|
|
698
|
+
}
|
|
699
|
+
if (font["b"] !== undefined) {
|
|
700
|
+
style.bold = isOn(font["b"]);
|
|
684
701
|
}
|
|
685
|
-
if (font["i"] !== undefined
|
|
686
|
-
style.italic =
|
|
702
|
+
if (font["i"] !== undefined) {
|
|
703
|
+
style.italic = isOn(font["i"]);
|
|
687
704
|
}
|
|
688
|
-
if (font["u"] !== undefined
|
|
689
|
-
style.underline =
|
|
705
|
+
if (font["u"] !== undefined) {
|
|
706
|
+
style.underline = attr(font["u"], "val") !== "none";
|
|
690
707
|
}
|
|
691
708
|
const size = numAttr(font["sz"], "val");
|
|
692
709
|
if (size !== undefined) {
|
|
@@ -1379,6 +1396,7 @@ async function convertWorkbook(source, options = {}) {
|
|
|
1379
1396
|
cellStyles: cellStyles.length > 0 ? cellStyles : undefined,
|
|
1380
1397
|
conditionalStyles: conditionalStyles.length > 0 ? conditionalStyles : undefined,
|
|
1381
1398
|
workbookMetadata: {
|
|
1399
|
+
defaultCellStyle: importStyles ? styleTable.formats[0]?.style : undefined,
|
|
1382
1400
|
fileName: options.fileName,
|
|
1383
1401
|
sheetOrder: sheetEntries.map((entry) => entry.name),
|
|
1384
1402
|
diagnostics: diagnostics.length > 0 ? diagnostics : undefined
|
|
@@ -1800,11 +1818,22 @@ async function spreadsheetToFormulaEngine(source, options) {
|
|
|
1800
1818
|
}
|
|
1801
1819
|
async function spreadsheetToFormulaEngineDetailed(source, options) {
|
|
1802
1820
|
const bytes = await toBytes2(source);
|
|
1821
|
+
const fileName = options?.fileName ?? (typeof File !== "undefined" && source instanceof File ? source.name : undefined);
|
|
1803
1822
|
if (looksLikeZip(bytes)) {
|
|
1804
1823
|
return convertWorkbook(bytes, withFileName(source, options));
|
|
1805
1824
|
}
|
|
1825
|
+
if (fileName && /\.(xlsx|xlsm)$/i.test(fileName)) {
|
|
1826
|
+
throw new Error(`${fileName} is named as an Excel workbook but is not one — the file is ` + `corrupt, or it is a legacy .xls saved under the wrong extension.`);
|
|
1827
|
+
}
|
|
1828
|
+
assertLooksLikeText(bytes, fileName);
|
|
1806
1829
|
return csvToFormulaEngineDetailed(bytes, withCsvFileName(source, options));
|
|
1807
1830
|
}
|
|
1831
|
+
function assertLooksLikeText(bytes, fileName) {
|
|
1832
|
+
const sample = bytes.subarray(0, 8192);
|
|
1833
|
+
if (sample.indexOf(0) !== -1) {
|
|
1834
|
+
throw new Error(`${fileName ?? "The file"} is not a spreadsheet: it contains binary data ` + `rather than delimited text.`);
|
|
1835
|
+
}
|
|
1836
|
+
}
|
|
1808
1837
|
function looksLikeZip(bytes) {
|
|
1809
1838
|
return bytes.length >= 4 && bytes[0] === 80 && bytes[1] === 75 && bytes[2] === 3 && bytes[3] === 4;
|
|
1810
1839
|
}
|
|
@@ -1849,4 +1878,4 @@ export {
|
|
|
1849
1878
|
translateFormula
|
|
1850
1879
|
};
|
|
1851
1880
|
|
|
1852
|
-
//# debugId=
|
|
1881
|
+
//# debugId=1D4DFA6797056BB064756E2164756E21
|