@ricsam/excel-to-formula-engine 0.2.1 → 0.3.0

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) {
@@ -1048,6 +1065,7 @@ import {
1048
1065
  } from "@ricsam/formula-engine";
1049
1066
  function translateConditionalFormat(rule, options) {
1050
1067
  const diagnostics = [];
1068
+ const ranges = rule.ranges.map((reference) => reference.replace(/\$/g, ""));
1051
1069
  const areas = [];
1052
1070
  for (const reference of rule.ranges) {
1053
1071
  const range = rangeReferenceToSpreadsheetRange(reference);
@@ -1059,14 +1077,16 @@ function translateConditionalFormat(rule, options) {
1059
1077
  return { diagnostics };
1060
1078
  }
1061
1079
  const anchor = rule.ranges[0]?.split(":")[0]?.replace(/\$/g, "") ?? "A1";
1062
- if (rule.type === "colorScale") {
1063
- return translateColorScale(rule, areas, options, diagnostics);
1064
- }
1065
- const condition = buildFormulaCondition(rule, anchor, options, diagnostics);
1066
- if (!condition) {
1067
- return { diagnostics };
1080
+ const result = rule.type === "colorScale" ? translateColorScale(rule, areas, options, diagnostics).result : withCondition(areas, buildFormulaCondition(rule, anchor, options, diagnostics));
1081
+ for (const diagnostic of diagnostics) {
1082
+ if (diagnostic.ranges === undefined && ranges.length > 0) {
1083
+ diagnostic.ranges = ranges;
1084
+ }
1068
1085
  }
1069
- return { result: { areas, condition }, diagnostics };
1086
+ return { result, diagnostics };
1087
+ }
1088
+ function withCondition(areas, condition) {
1089
+ return condition ? { areas, condition } : undefined;
1070
1090
  }
1071
1091
  function buildFormulaCondition(rule, anchor, options, diagnostics) {
1072
1092
  const color = ruleColor(rule, options);
@@ -1379,6 +1399,7 @@ async function convertWorkbook(source, options = {}) {
1379
1399
  cellStyles: cellStyles.length > 0 ? cellStyles : undefined,
1380
1400
  conditionalStyles: conditionalStyles.length > 0 ? conditionalStyles : undefined,
1381
1401
  workbookMetadata: {
1402
+ defaultCellStyle: importStyles ? styleTable.formats[0]?.style : undefined,
1382
1403
  fileName: options.fileName,
1383
1404
  sheetOrder: sheetEntries.map((entry) => entry.name),
1384
1405
  diagnostics: diagnostics.length > 0 ? diagnostics : undefined
@@ -1860,4 +1881,4 @@ export {
1860
1881
  translateFormula
1861
1882
  };
1862
1883
 
1863
- //# debugId=261D0E01F4A42BBA64756E2164756E21
1884
+ //# debugId=442BD7CE06012CE864756E2164756E21