@noy-db/as-xlsx 0.2.0-pre.20 → 0.2.0-pre.23

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.d.ts CHANGED
@@ -39,7 +39,39 @@ import { DictEntry, VaultDiff, Vault } from '@noy-db/hub';
39
39
  *
40
40
  * @module
41
41
  */
42
- /** One row in a sheet. Values are coerced per type at emit time. */
42
+ /**
43
+ * A formula cell. Emits `<f>` so the spreadsheet recomputes live; `v` is an
44
+ * optional cached result (what the formula currently evaluates to) so the value
45
+ * shows immediately on open and survives a round-trip read. Build via
46
+ * {@link formula}. The formula string must NOT include a leading `=`.
47
+ */
48
+ interface XlsxFormulaCell {
49
+ readonly __xlsxFormula: string;
50
+ readonly v?: string | number | boolean;
51
+ }
52
+ /** Build a {@link XlsxFormulaCell}. `f` is the formula body without a leading `=`. */
53
+ declare function formula(f: string, cachedValue?: string | number | boolean): XlsxFormulaCell;
54
+ /**
55
+ * A value cell carrying a number-format code (e.g. `'#,##0.00'` for currency,
56
+ * `'yyyy-mm-dd'` for dates). Build via {@link styled}. The format only renders
57
+ * meaningfully on numeric values.
58
+ */
59
+ interface XlsxStyledCell {
60
+ readonly __xlsxStyle: string;
61
+ readonly v: string | number | boolean | null;
62
+ }
63
+ /** Build a {@link XlsxStyledCell} — a value plus an Excel number-format code. */
64
+ declare function styled(value: string | number | boolean | null, numberFormat: string): XlsxStyledCell;
65
+ /** A data-validation rule (dropdown) over a cell range. */
66
+ interface XlsxValidation {
67
+ /** Target range in A1 notation, e.g. `'B2:B100'`. */
68
+ readonly sqref: string;
69
+ /** Inline allowed values → a `"a,b,c"` list. Mutually exclusive with `formula1`. */
70
+ readonly values?: readonly string[];
71
+ /** Explicit `formula1` (e.g. a range ref `Clients!$A$2:$A$999`). */
72
+ readonly formula1?: string;
73
+ }
74
+ /** One row in a sheet. A cell is a primitive value or an {@link XlsxFormulaCell}. */
43
75
  type XlsxRow = ReadonlyArray<unknown>;
44
76
  /** One sheet in a workbook. */
45
77
  interface XlsxSheet {
@@ -60,12 +92,19 @@ interface XlsxSheet {
60
92
  * for "auto" columns and a number for explicit widths.
61
93
  */
62
94
  readonly widths?: ReadonlyArray<number | undefined>;
95
+ /** Data-validation dropdowns applied to ranges on this sheet. */
96
+ readonly validations?: readonly XlsxValidation[];
63
97
  }
64
98
  /**
65
99
  * Build a complete `.xlsx` byte stream from the supplied sheet data.
66
100
  * Pure — no I/O beyond the internal zip concatenation.
67
101
  */
68
- declare function writeXlsx(sheets: readonly XlsxSheet[]): Promise<Uint8Array>;
102
+ declare function writeXlsx(sheets: readonly XlsxSheet[], options?: {
103
+ definedNames?: ReadonlyArray<{
104
+ readonly name: string;
105
+ readonly ref: string;
106
+ }>;
107
+ }): Promise<Uint8Array>;
69
108
  /**
70
109
  * Convert a 1-based column index to Excel A1 letter notation.
71
110
  * 1 → A, 26 → Z, 27 → AA, 702 → ZZ, 703 → AAA.
@@ -115,6 +154,30 @@ interface ReadXlsxResult {
115
154
  */
116
155
  declare function readXlsx(bytes: Uint8Array): Promise<ReadXlsxResult>;
117
156
 
157
+ type InferredType = 'string' | 'number' | 'boolean' | 'date';
158
+ interface InferredField {
159
+ readonly type: InferredType;
160
+ /** Target collection (sheet) name when this field looks like a foreign key. */
161
+ readonly references?: string;
162
+ }
163
+ interface InferredCollection {
164
+ readonly idField: string;
165
+ readonly fields: Record<string, InferredField>;
166
+ }
167
+ interface InferredSchema {
168
+ readonly collections: Record<string, InferredCollection>;
169
+ }
170
+ /**
171
+ * Infer a {@link InferredSchema} from an `.xlsx` byte stream. Sheets whose name
172
+ * starts with `_` (our smart-export meta sheets) are skipped; pass `skipSheets`
173
+ * to exclude more (e.g. summary sheets).
174
+ */
175
+ declare function inferSchema(bytes: Uint8Array, options?: {
176
+ skipSheets?: readonly string[];
177
+ }): Promise<InferredSchema>;
178
+ /** Emit a Zod schema snippet (guidance) from an {@link InferredSchema}. */
179
+ declare function zodSourceFor(schema: InferredSchema): string;
180
+
118
181
  /**
119
182
  * **@noy-db/as-xlsx** — Excel spreadsheet plaintext export for noy-db.
120
183
  *
@@ -179,11 +242,84 @@ interface AsXlsxSheetOptions {
179
242
  * mirrors `XlsxSheet.widths`, which it threads through.
180
243
  */
181
244
  readonly widths?: ReadonlyArray<number | undefined>;
245
+ /**
246
+ * Smart mode only: per-field Excel number-format codes (e.g.
247
+ * `{ amount: '#,##0.00' }` for currency). The value is coerced to a number so
248
+ * the format renders. Money has no introspection signal, so currency
249
+ * formatting is opt-in here.
250
+ */
251
+ readonly numberFormats?: Record<string, string>;
252
+ /**
253
+ * Smart mode only: per-field explicit dropdown value lists. Overrides the
254
+ * auto-detected enum/ref dropdown for that field.
255
+ */
256
+ readonly dropdowns?: Record<string, readonly string[]>;
257
+ /**
258
+ * Smart mode only: fields whose stored value is a multi-locale i18n map
259
+ * (`{ en: '…', th: '…' }`). Each becomes per-locale columns plus a display
260
+ * column resolved **live by the global LANG cell** (change LANG → every label
261
+ * re-renders). Read raw, so open the export vault without an active locale.
262
+ */
263
+ readonly i18nFields?: readonly string[];
264
+ /**
265
+ * Smart mode only: map a dict-backed (code) field to its dictionary name, e.g.
266
+ * `{ status: 'status' }`. Emits a hidden `_Lookups_<dict>` sheet (code +
267
+ * per-locale labels) and a `<field>__label` display column resolved by the
268
+ * global LANG cell via `VLOOKUP(code, …, MATCH(LANG, …))`.
269
+ */
270
+ readonly dictFields?: Record<string, string>;
271
+ }
272
+ /** One aggregate column in a smart-mode summary sheet (#414 P3). */
273
+ interface AsXlsxSummaryAggregate {
274
+ /** Column header for this aggregate. */
275
+ readonly label: string;
276
+ readonly op: 'sum' | 'count' | 'avg';
277
+ /** Field to aggregate — required for `sum`/`avg`, ignored for `count`. */
278
+ readonly field?: string;
279
+ }
280
+ /**
281
+ * A groupBy summary sheet (#414 P3): groups the source collection's sheet by
282
+ * `groupBy` and emits live SUMIFS/COUNTIFS/AVERAGEIFS columns (values cached at
283
+ * export). The source value columns must be numeric for the live formulas to
284
+ * compute (apply `numberFormats` to money fields).
285
+ */
286
+ interface AsXlsxSummarySpec {
287
+ /** Summary sheet name. */
288
+ readonly name: string;
289
+ /** Source collection (must be exported as a sheet). */
290
+ readonly from: string;
291
+ /** Field to group by. */
292
+ readonly groupBy: string;
293
+ readonly aggregates: readonly AsXlsxSummaryAggregate[];
182
294
  }
183
295
  /** Single-collection convenience — passed where a sheet-list is accepted. */
184
296
  interface AsXlsxOptions {
185
297
  /** One or more sheets. At least one required. */
186
298
  readonly sheets: readonly AsXlsxSheetOptions[];
299
+ /** Smart mode only: groupBy summary sheets (live SUMIFS/COUNTIFS/AVERAGEIFS). */
300
+ readonly summaries?: readonly AsXlsxSummarySpec[];
301
+ /**
302
+ * Smart mode only: summary formula dialect. `'excel'` (default) emits
303
+ * cross-compatible per-row SUMIFS/COUNTIFS/AVERAGEIFS. `'sheets'` emits a
304
+ * single Google-Sheets `QUERY` formula per summary — **Sheets-only** (QUERY
305
+ * errors in Excel); use when the target is Google Sheets.
306
+ */
307
+ readonly dialect?: 'excel' | 'sheets';
308
+ /**
309
+ * Smart-workbook mode (#414). Emits a relational workbook instead of a flat
310
+ * dump:
311
+ * - every sheet is **id-first** (record `id` in column A);
312
+ * - each foreign-key field (auto-detected via `vault.dumpSchema()`) gets a
313
+ * `<field>__label` column — a cross-sheet `VLOOKUP` that resolves the
314
+ * reference to the target's first field, carrying a **cached** label so it
315
+ * shows immediately and recomputes live on edit;
316
+ * - a `_manifest` index sheet lists every collection, its row count, and
317
+ * its refs.
318
+ *
319
+ * Requires unique sheet names ≤ 31 chars (so cross-sheet refs resolve) and an
320
+ * `id` field on records. Defaults to the existing flat export when omitted.
321
+ */
322
+ readonly smart?: boolean;
187
323
  }
188
324
  /** Options for `download()` — adds optional filename. */
189
325
  interface AsXlsxDownloadOptions extends AsXlsxOptions {
@@ -267,6 +403,14 @@ interface AsXlsxImportOptions {
267
403
  * Unknown labels (no match in any locale) pass through as-is.
268
404
  */
269
405
  readonly dicts?: Readonly<Record<string, readonly DictEntry[]>>;
406
+ /**
407
+ * Read a sheet produced by smart export (#414 P4). Reverses the smart layout:
408
+ * reconstructs i18n fields from their per-locale columns (`<f>__<loc>` →
409
+ * `{ loc: value }`), and drops derived columns — the i18n display column and
410
+ * every `<f>__label` (FK/dict) formula column. Code columns (the real values)
411
+ * pass through. Maps onto the existing collection schema (Mode A).
412
+ */
413
+ readonly smart?: boolean;
270
414
  }
271
415
  interface AsXlsxImportPlan {
272
416
  readonly plan: VaultDiff;
@@ -297,4 +441,4 @@ interface AsXlsxImportPlan {
297
441
  */
298
442
  declare function fromBytes(vault: Vault, bytes: Uint8Array, options: AsXlsxImportOptions): Promise<AsXlsxImportPlan>;
299
443
 
300
- export { type AsXlsxDownloadOptions, type AsXlsxImportOptions, type AsXlsxImportPlan, type AsXlsxOptions, type AsXlsxSheetOptions, type AsXlsxWriteOptions, type ImportPolicy, type ReadXlsxResult, type ReadXlsxRow, type ReadXlsxSheet, XlsxDictAmbiguityError, type XlsxRow, type XlsxSheet, colLetter, download, fromBytes, readXlsx, toBytes, toBytesFromCollection, write, writeXlsx };
444
+ export { type AsXlsxDownloadOptions, type AsXlsxImportOptions, type AsXlsxImportPlan, type AsXlsxOptions, type AsXlsxSheetOptions, type AsXlsxSummaryAggregate, type AsXlsxSummarySpec, type AsXlsxWriteOptions, type ImportPolicy, type InferredCollection, type InferredField, type InferredSchema, type InferredType, type ReadXlsxResult, type ReadXlsxRow, type ReadXlsxSheet, XlsxDictAmbiguityError, type XlsxFormulaCell, type XlsxRow, type XlsxSheet, type XlsxStyledCell, type XlsxValidation, colLetter, download, formula, fromBytes, inferSchema, readXlsx, styled, toBytes, toBytesFromCollection, write, writeXlsx, zodSourceFor };