javascript-excel 2.0.0-alpha.4 → 2.0.0-alpha.6

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.
@@ -386,6 +386,11 @@ function mapColumns(columns, schema, mapping) {
386
386
  };
387
387
  }
388
388
  function applyMapping(dataset, mapping, schema) {
389
+ const { sources: _s, ...result } = applyMappingDetailed(dataset, mapping, schema);
390
+ return result;
391
+ }
392
+ /** @internal applyMapping plus each source column key's key in the rows (columns the mapping drops are absent). */
393
+ function applyMappingDetailed(dataset, mapping, schema) {
389
394
  const fields = fieldsOf(schema);
390
395
  const byKey = new Map(dataset.columns.map((c, i) => [c.key, {
391
396
  ...c,
@@ -456,29 +461,35 @@ function applyMapping(dataset, mapping, schema) {
456
461
  }
457
462
  }
458
463
  const required = fields.filter(([name, spec]) => spec.required && plan.some((p) => p.out === name && p.from !== null)).map(([name]) => name);
464
+ const data = dataset.data.map((row, i) => {
465
+ const out = Object.create(null);
466
+ for (const { out: k, from } of plan) out[k] = from === null ? null : row[from] ?? null;
467
+ for (const name of required) {
468
+ const v = out[name];
469
+ if (v === null || v === void 0 || typeof v === "string" && v.trim() === "") {
470
+ const from = plan.find((p) => p.out === name).from;
471
+ const letter = byKey.get(from)?.letter;
472
+ issues.push({
473
+ row: rowNumbers[i],
474
+ field: name,
475
+ column: from,
476
+ ...letter ? { ref: `${letter}${rowNumbers[i]}` } : {},
477
+ code: "REQUIRED",
478
+ message: `Row ${rowNumbers[i]}: "${name}" is required.`,
479
+ severity: "error"
480
+ });
481
+ }
482
+ }
483
+ return out;
484
+ });
485
+ const sources = /* @__PURE__ */ new Map();
486
+ for (const p of plan) if (p.from !== null) sources.set(p.from, p.out);
459
487
  return {
460
488
  columns,
461
- data: dataset.data.map((row, i) => {
462
- const out = Object.create(null);
463
- for (const { out: k, from } of plan) out[k] = from === null ? null : row[from] ?? null;
464
- for (const name of required) {
465
- const v = out[name];
466
- if (v === null || v === void 0 || typeof v === "string" && v.trim() === "") {
467
- const letter = byKey.get(plan.find((p) => p.out === name).from)?.letter;
468
- issues.push({
469
- row: rowNumbers[i],
470
- field: name,
471
- ...letter ? { ref: `${letter}${rowNumbers[i]}` } : {},
472
- code: "REQUIRED",
473
- message: `Row ${rowNumbers[i]}: "${name}" is required.`,
474
- severity: "error"
475
- });
476
- }
477
- }
478
- return out;
479
- }),
489
+ data,
480
490
  rowNumbers,
481
- issues
491
+ issues,
492
+ sources
482
493
  };
483
494
  }
484
495
  // src/internal/import/types.ts
@@ -740,6 +751,7 @@ function coerceColumns(dataset, types, options = {}) {
740
751
  issues.push({
741
752
  row: rowNumber,
742
753
  field: column.key,
754
+ column: column.key,
743
755
  ...column.letter ? { ref: `${column.letter}${rowNumber}` } : {},
744
756
  code: "TYPE",
745
757
  message: `${column.letter ? `${column.letter}${rowNumber}` : `Row ${rowNumber}`}: ${JSON.stringify(String(textOf(row[column.key]) ?? row[column.key]))} is not a ${spec.type}.`,
@@ -783,6 +795,30 @@ function readCap(options) {
783
795
  const h = options.header ?? "auto";
784
796
  return (h === false ? 0 : typeof h === "number" ? h + 1 : typeof h === "object" ? (h.row === void 0 ? 25 : h.row) + h.rows : 25) + rows + 1;
785
797
  }
798
+ /**
799
+ * Issues after mapping: `field` becomes the key in the rows, issues on
800
+ * columns the mapping drops go, and a cell with a TYPE issue gets no
801
+ * REQUIRED issue as well (its value didn't convert; it wasn't empty).
802
+ */
803
+ function renameIssues(issues, sources) {
804
+ const cell = (row, column) => `${row}\u0000${column}`;
805
+ const typed = new Set(issues.filter((i) => i.code === "TYPE" && i.column !== void 0).map((i) => cell(i.row, i.column)));
806
+ const out = [];
807
+ for (const i of issues) {
808
+ if (i.column === void 0) {
809
+ out.push(i);
810
+ continue;
811
+ }
812
+ const field = sources.get(i.column);
813
+ if (field === void 0) continue;
814
+ if (i.code === "REQUIRED" && typed.has(cell(i.row, i.column))) continue;
815
+ out.push(i.field === field ? i : {
816
+ ...i,
817
+ field
818
+ });
819
+ }
820
+ return out;
821
+ }
786
822
  async function importDataset(input, options = {}) {
787
823
  const o = options;
788
824
  checkEnum("format", o.format, [
@@ -997,6 +1033,7 @@ async function importDataset(input, options = {}) {
997
1033
  issues.push({
998
1034
  row: rowNumbers[i],
999
1035
  field: c.key,
1036
+ column: c.key,
1000
1037
  ref,
1001
1038
  code: "ERROR_CELL",
1002
1039
  message: `${ref} holds the error ${v.error}; read as empty.`,
@@ -1047,6 +1084,7 @@ async function importDataset(input, options = {}) {
1047
1084
  if (inf.ambiguousDates) issues.push({
1048
1085
  row: 0,
1049
1086
  field: inf.key,
1087
+ column: inf.key,
1050
1088
  code: "DATE_ORDER_AMBIGUOUS",
1051
1089
  message: `Column "${inf.key}" looks like dates, but the day/month order can't be told from the data; set locale.dateOrder.`,
1052
1090
  severity: "warning"
@@ -1067,13 +1105,14 @@ async function importDataset(input, options = {}) {
1067
1105
  progress("dataset", .97);
1068
1106
  let result;
1069
1107
  if (mapped) {
1070
- const m = applyMapping({
1108
+ const { sources, ...m } = applyMappingDetailed({
1071
1109
  columns: typedColumns,
1072
1110
  data: coerced.data,
1073
1111
  rowNumbers
1074
1112
  }, mapped.mapping, o.schema);
1075
1113
  issues.push(...m.issues);
1076
1114
  result = m;
1115
+ issues.splice(0, issues.length, ...renameIssues(issues, sources));
1077
1116
  meta.mapping = mapped.mapping;
1078
1117
  meta.unmappedColumns = mapped.unmapped;
1079
1118
  } else result = {
package/dist/render.cjs CHANGED
@@ -95,6 +95,7 @@ function createRenderSheet(workbook, sheet, options = {}) {
95
95
  usedRows,
96
96
  usedCols,
97
97
  rowTops,
98
+ defaultRowPx,
98
99
  colLefts,
99
100
  merges,
100
101
  buckets,
@@ -176,6 +177,45 @@ function colWidth(s, col) {
176
177
  function isRowHidden(s, row) {
177
178
  return rowHeight(s, row) === 0;
178
179
  }
180
+ /** Excel's largest row height, 409 pt, in CSS px. */
181
+ const MAX_ROW_PX = 39264 / 72;
182
+ /** True when the row's height comes from the file (`rowProps[row].height`); only other rows may be auto-fitted (§5.8.1). */
183
+ function hasStoredRowHeight(s, row) {
184
+ return state(s).sh.rowProps?.[row]?.height !== void 0;
185
+ }
186
+ /**
187
+ * Sets measured heights, in CSS px at zoom 100 %, for rows without a stored height: the viewer's
188
+ * auto-fit (§5.8.1). Rows with a stored height, hidden rows and rows outside the extent are ignored.
189
+ * Heights are clamped to [default row height, 409 pt], so auto-fit only grows rows. The offset table
190
+ * is rebuilt from the first changed row onwards. Returns true when anything changed.
191
+ * (An array, not an Iterable, so the declaration works with ES5 lib typings; see Consumer TS.)
192
+ */
193
+ function setRowHeights(s, heights) {
194
+ const st = state(s);
195
+ const tops = st.rowTops;
196
+ const updates = /* @__PURE__ */ new Map();
197
+ let first = Number.POSITIVE_INFINITY;
198
+ for (const [row, px] of heights) {
199
+ if (!(Number.isInteger(row) && row >= 0 && row < st.rows) || !Number.isFinite(px)) continue;
200
+ const props = st.sh.rowProps?.[row];
201
+ if (props?.hidden || props?.height !== void 0) continue;
202
+ const current = tops[row + 1] - tops[row];
203
+ if (current === 0) continue;
204
+ const next = Math.min(MAX_ROW_PX, Math.max(st.defaultRowPx, px));
205
+ if (next !== current) {
206
+ updates.set(row, next);
207
+ if (row < first) first = row;
208
+ }
209
+ }
210
+ if (updates.size === 0) return false;
211
+ let oldTop = tops[first];
212
+ for (let r = first; r < st.rows; r++) {
213
+ const oldNext = tops[r + 1];
214
+ tops[r + 1] = tops[r] + (updates.get(r) ?? oldNext - oldTop);
215
+ oldTop = oldNext;
216
+ }
217
+ return true;
218
+ }
179
219
  function isColHidden(s, col) {
180
220
  return colWidth(s, col) === 0;
181
221
  }
@@ -735,6 +775,7 @@ exports.colLeft = colLeft;
735
775
  exports.colWidth = colWidth;
736
776
  exports.createRenderSheet = createRenderSheet;
737
777
  exports.edge = edge;
778
+ exports.hasStoredRowHeight = hasStoredRowHeight;
738
779
  exports.isColHidden = isColHidden;
739
780
  exports.isRowHidden = isRowHidden;
740
781
  exports.lastUsedCell = lastUsedCell;
@@ -746,6 +787,7 @@ exports.renderCell = renderCell;
746
787
  exports.rowAt = rowAt;
747
788
  exports.rowHeight = rowHeight;
748
789
  exports.rowTop = rowTop;
790
+ exports.setRowHeights = setRowHeights;
749
791
  exports.sheetExtent = sheetExtent;
750
792
  exports.sheetSettings = sheetSettings;
751
793
  exports.step = step;
package/dist/render.d.cts CHANGED
@@ -186,6 +186,16 @@ export declare function rowHeight(s: RenderSheet, row: number): number;
186
186
  export declare function colLeft(s: RenderSheet, col: number): number;
187
187
  export declare function colWidth(s: RenderSheet, col: number): number;
188
188
  export declare function isRowHidden(s: RenderSheet, row: number): boolean;
189
+ /** True when the row's height comes from the file (`rowProps[row].height`); only other rows may be auto-fitted (§5.8.1). */
190
+ export declare function hasStoredRowHeight(s: RenderSheet, row: number): boolean;
191
+ /**
192
+ * Sets measured heights, in CSS px at zoom 100 %, for rows without a stored height: the viewer's
193
+ * auto-fit (§5.8.1). Rows with a stored height, hidden rows and rows outside the extent are ignored.
194
+ * Heights are clamped to [default row height, 409 pt], so auto-fit only grows rows. The offset table
195
+ * is rebuilt from the first changed row onwards. Returns true when anything changed.
196
+ * (An array, not an Iterable, so the declaration works with ES5 lib typings; see Consumer TS.)
197
+ */
198
+ export declare function setRowHeights(s: RenderSheet, heights: ReadonlyArray<readonly [row: number, px: number]>): boolean;
189
199
  export declare function isColHidden(s: RenderSheet, col: number): boolean;
190
200
  export declare function rowAt(s: RenderSheet, y: number): number;
191
201
  export declare function colAt(s: RenderSheet, x: number): number;
package/dist/render.d.ts CHANGED
@@ -186,6 +186,16 @@ export declare function rowHeight(s: RenderSheet, row: number): number;
186
186
  export declare function colLeft(s: RenderSheet, col: number): number;
187
187
  export declare function colWidth(s: RenderSheet, col: number): number;
188
188
  export declare function isRowHidden(s: RenderSheet, row: number): boolean;
189
+ /** True when the row's height comes from the file (`rowProps[row].height`); only other rows may be auto-fitted (§5.8.1). */
190
+ export declare function hasStoredRowHeight(s: RenderSheet, row: number): boolean;
191
+ /**
192
+ * Sets measured heights, in CSS px at zoom 100 %, for rows without a stored height: the viewer's
193
+ * auto-fit (§5.8.1). Rows with a stored height, hidden rows and rows outside the extent are ignored.
194
+ * Heights are clamped to [default row height, 409 pt], so auto-fit only grows rows. The offset table
195
+ * is rebuilt from the first changed row onwards. Returns true when anything changed.
196
+ * (An array, not an Iterable, so the declaration works with ES5 lib typings; see Consumer TS.)
197
+ */
198
+ export declare function setRowHeights(s: RenderSheet, heights: ReadonlyArray<readonly [row: number, px: number]>): boolean;
189
199
  export declare function isColHidden(s: RenderSheet, col: number): boolean;
190
200
  export declare function rowAt(s: RenderSheet, y: number): number;
191
201
  export declare function colAt(s: RenderSheet, x: number): number;
package/dist/render.js CHANGED
@@ -94,6 +94,7 @@ function createRenderSheet(workbook, sheet, options = {}) {
94
94
  usedRows,
95
95
  usedCols,
96
96
  rowTops,
97
+ defaultRowPx,
97
98
  colLefts,
98
99
  merges,
99
100
  buckets,
@@ -175,6 +176,45 @@ function colWidth(s, col) {
175
176
  function isRowHidden(s, row) {
176
177
  return rowHeight(s, row) === 0;
177
178
  }
179
+ /** Excel's largest row height, 409 pt, in CSS px. */
180
+ const MAX_ROW_PX = 39264 / 72;
181
+ /** True when the row's height comes from the file (`rowProps[row].height`); only other rows may be auto-fitted (§5.8.1). */
182
+ function hasStoredRowHeight(s, row) {
183
+ return state(s).sh.rowProps?.[row]?.height !== void 0;
184
+ }
185
+ /**
186
+ * Sets measured heights, in CSS px at zoom 100 %, for rows without a stored height: the viewer's
187
+ * auto-fit (§5.8.1). Rows with a stored height, hidden rows and rows outside the extent are ignored.
188
+ * Heights are clamped to [default row height, 409 pt], so auto-fit only grows rows. The offset table
189
+ * is rebuilt from the first changed row onwards. Returns true when anything changed.
190
+ * (An array, not an Iterable, so the declaration works with ES5 lib typings; see Consumer TS.)
191
+ */
192
+ function setRowHeights(s, heights) {
193
+ const st = state(s);
194
+ const tops = st.rowTops;
195
+ const updates = /* @__PURE__ */ new Map();
196
+ let first = Number.POSITIVE_INFINITY;
197
+ for (const [row, px] of heights) {
198
+ if (!(Number.isInteger(row) && row >= 0 && row < st.rows) || !Number.isFinite(px)) continue;
199
+ const props = st.sh.rowProps?.[row];
200
+ if (props?.hidden || props?.height !== void 0) continue;
201
+ const current = tops[row + 1] - tops[row];
202
+ if (current === 0) continue;
203
+ const next = Math.min(MAX_ROW_PX, Math.max(st.defaultRowPx, px));
204
+ if (next !== current) {
205
+ updates.set(row, next);
206
+ if (row < first) first = row;
207
+ }
208
+ }
209
+ if (updates.size === 0) return false;
210
+ let oldTop = tops[first];
211
+ for (let r = first; r < st.rows; r++) {
212
+ const oldNext = tops[r + 1];
213
+ tops[r + 1] = tops[r] + (updates.get(r) ?? oldNext - oldTop);
214
+ oldTop = oldNext;
215
+ }
216
+ return true;
217
+ }
178
218
  function isColHidden(s, col) {
179
219
  return colWidth(s, col) === 0;
180
220
  }
@@ -729,4 +769,4 @@ function rangeStats(s, range, limit = 1e6) {
729
769
  truncated
730
770
  };
731
771
  }
732
- export { colAt, colLeft, colWidth, createRenderSheet, edge, isColHidden, isRowHidden, lastUsedCell, mergeAt, rangeAsHtml, rangeAsTsv, rangeStats, renderCell, rowAt, rowHeight, rowTop, sheetExtent, sheetSettings, step };
772
+ export { colAt, colLeft, colWidth, createRenderSheet, edge, hasStoredRowHeight, isColHidden, isRowHidden, lastUsedCell, mergeAt, rangeAsHtml, rangeAsTsv, rangeStats, renderCell, rowAt, rowHeight, rowTop, setRowHeights, sheetExtent, sheetSettings, step };
package/dist/worker.cjs CHANGED
@@ -2,7 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_csv = require("./csv-DyVH9y7S.cjs");
3
3
  const require_errors = require("./errors-DOPrrGS4.cjs");
4
4
  const require_read = require("./read-CZEWV4rK.cjs");
5
- const require_pipeline = require("./pipeline-BV2q-khO.cjs");
5
+ const require_pipeline = require("./pipeline-DwJjPL7r.cjs");
6
6
  const require_write = require("./write-f9kcYf5K.cjs");
7
7
  // src/worker.ts
8
8
  /** Marks the library's messages, so an app can use the same worker for its own messages too. */
package/dist/worker.d.cts CHANGED
@@ -25,7 +25,7 @@ import { r as BinaryInput } from "./warnings-B1v58nCZ.cjs";
25
25
  import { CsvParseOptions, CsvParseResult, CsvReadOptions, CsvWriteOptions } from "./csv.cjs";
26
26
  import { D as Sheet, j as Workbook, s as CellInput } from "./model-0JikLfuM.cjs";
27
27
  import { ReadOptions, ReadResult } from "./read.cjs";
28
- import { n as ImportDatasetResult, t as ImportDatasetOptions } from "./pipeline-CctEa86r.cjs";
28
+ import { n as ImportDatasetResult, t as ImportDatasetOptions } from "./pipeline-DM7n0aRI.cjs";
29
29
  import { n as WriteOptions, t as Progress } from "./write-DtjfdIFm.cjs";
30
30
  import { t as AbortSignalLike } from "./signal-Dz0ZYe6d.cjs";
31
31
  /** The side of a message channel the library uses: a `Worker`, the worker's `self`, or a `MessagePort`. */
package/dist/worker.d.ts CHANGED
@@ -25,7 +25,7 @@ import { r as BinaryInput } from "./warnings-B1v58nCZ.js";
25
25
  import { CsvParseOptions, CsvParseResult, CsvReadOptions, CsvWriteOptions } from "./csv.js";
26
26
  import { D as Sheet, j as Workbook, s as CellInput } from "./model-0JikLfuM.js";
27
27
  import { ReadOptions, ReadResult } from "./read.js";
28
- import { n as ImportDatasetResult, t as ImportDatasetOptions } from "./pipeline-I8SLmV9S.js";
28
+ import { n as ImportDatasetResult, t as ImportDatasetOptions } from "./pipeline-BuG_nn_S.js";
29
29
  import { n as WriteOptions, t as Progress } from "./write-DM6ieuQY.js";
30
30
  import { t as AbortSignalLike } from "./signal-Dz0ZYe6d.js";
31
31
  /** The side of a message channel the library uses: a `Worker`, the worker's `self`, or a `MessagePort`. */
package/dist/worker.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { i as toCsv, r as readCsv } from "./csv-pXfaputs.js";
2
2
  import { a as LimitError, i as FormatError, n as EnvironmentError, o as ValidationError, r as ExcelError, s as isExcelError, t as CsvError } from "./errors-CvGlfw0E.js";
3
3
  import { r as read } from "./read-LWti17lz.js";
4
- import { t as importDataset } from "./pipeline-Snu8wBXd.js";
4
+ import { t as importDataset } from "./pipeline-DyWvM9t2.js";
5
5
  import { r as toUint8Array } from "./write-DjgWy1PK.js";
6
6
  // src/worker.ts
7
7
  /** Marks the library's messages, so an app can use the same worker for its own messages too. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "javascript-excel",
3
- "version": "2.0.0-alpha.4",
3
+ "version": "2.0.0-alpha.6",
4
4
  "private": false,
5
5
  "description": "Read and write .xlsx and .csv files in browsers, Web Workers and Node. 2.0 is a from-scratch rewrite in progress.",
6
6
  "keywords": [