@ricsam/excel-to-formula-engine 0.2.1 → 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/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 styleIndex = numAttr(cell, "s");
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 && styleIndex === 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
- if (boolAttr(xf, "applyFont", true)) {
634
- const font = fonts[numAttr(xf, "fontId") ?? -1];
635
- if (font) {
636
- applyFont(style, font, theme, indexedColors);
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
- if (font["b"] !== undefined && isOn(font["b"])) {
683
- style.bold = true;
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 && isOn(font["i"])) {
686
- style.italic = true;
702
+ if (font["i"] !== undefined) {
703
+ style.italic = isOn(font["i"]);
687
704
  }
688
- if (font["u"] !== undefined && isOn(font["u"])) {
689
- style.underline = true;
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
@@ -1860,4 +1878,4 @@ export {
1860
1878
  translateFormula
1861
1879
  };
1862
1880
 
1863
- //# debugId=261D0E01F4A42BBA64756E2164756E21
1881
+ //# debugId=1D4DFA6797056BB064756E2164756E21