@allyhub/sdk 0.5.0 → 0.7.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/define.d.ts +4 -0
- package/dist/define.js +1 -1
- package/dist/node_modules/@allyhub-next/audio/dist/node/index.d.ts +1 -1
- package/dist/node_modules/@allyhub-next/audio/dist/node/index.js +1 -1
- package/dist/node_modules/@allyhub-next/audio/dist/node/master.d.ts +4 -0
- package/dist/node_modules/@allyhub-next/audio/dist/node/master.js +1 -1
- package/dist/node_modules/@allyhub-next/audio/package.json +1 -1
- package/dist/node_modules/@allyhub-next/chart/dist/chart-xml.js +2 -2
- package/dist/node_modules/@allyhub-next/chart/dist/chart.d.ts +14 -5
- package/dist/node_modules/@allyhub-next/chart/dist/chart.js +1 -1
- package/dist/node_modules/@allyhub-next/chart/package.json +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/data/deck-code.d.ts +205 -0
- package/dist/node_modules/@allyhub-next/formats/dist/data/deck-code.js +2 -0
- package/dist/node_modules/@allyhub-next/formats/dist/data/script.d.ts +4 -1
- package/dist/node_modules/@allyhub-next/formats/dist/data/script.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/data/sheet.d.ts +248 -18
- package/dist/node_modules/@allyhub-next/formats/dist/data/sheet.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/data/slides.d.ts +1114 -91
- package/dist/node_modules/@allyhub-next/formats/dist/data/slides.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/data/website.d.ts +20 -0
- package/dist/node_modules/@allyhub-next/formats/dist/data/website.js +2 -0
- package/dist/node_modules/@allyhub-next/formats/dist/index.d.ts +4 -0
- package/dist/node_modules/@allyhub-next/formats/dist/index.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/items.d.ts +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/items.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/media-type.d.ts +3 -0
- package/dist/node_modules/@allyhub-next/formats/dist/media-type.js +2 -0
- package/dist/node_modules/@allyhub-next/formats/dist/schemas.d.ts +625 -30
- package/dist/node_modules/@allyhub-next/formats/dist/schemas.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/strict.d.ts +2 -0
- package/dist/node_modules/@allyhub-next/formats/dist/strict.js +1 -1
- package/dist/node_modules/@allyhub-next/formats/dist/zip.d.ts +17 -0
- package/dist/node_modules/@allyhub-next/formats/dist/zip.js +2 -0
- package/dist/node_modules/@allyhub-next/formats/package.json +2 -1
- package/dist/node_modules/@allyhub-next/ooxml/LICENSE +241 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/brightness.d.ts +31 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/brightness.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/color.d.ts +33 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/color.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/enums.d.ts +13 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/enums.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/ext.d.ts +79 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/ext.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/generated/enums.d.ts +132 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/generated/enums.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/generated/presets.d.ts +28 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/generated/presets.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/generated/spec.d.ts +4708 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/generated/spec.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/geometry/index.d.ts +57 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/geometry/index.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/index.d.ts +10 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/index.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/mce.d.ts +12 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/mce.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/node.d.ts +111 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/node.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/ns.d.ts +45 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/ns.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/opc.d.ts +48 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/opc.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/report.d.ts +45 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/report.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/xml.d.ts +28 -0
- package/dist/node_modules/@allyhub-next/ooxml/dist/xml.js +2 -0
- package/dist/node_modules/@allyhub-next/ooxml/package.json +26 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/account.d.ts +338 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/account.js +2 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/auth.d.ts +12 -13
- package/dist/node_modules/@allyhub-next/protocol/dist/auth.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/blobs.d.ts +35 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/blobs.js +2 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/cloud.d.ts +101 -32
- package/dist/node_modules/@allyhub-next/protocol/dist/cloud.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/editor.d.ts +89 -5
- package/dist/node_modules/@allyhub-next/protocol/dist/editor.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/files.d.ts +2 -2
- package/dist/node_modules/@allyhub-next/protocol/dist/index.d.ts +6 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/index.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/notes.d.ts +23 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/notes.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/open.d.ts +13 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/open.js +2 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/registry.d.ts +73 -59
- package/dist/node_modules/@allyhub-next/protocol/dist/registry.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/routes.d.ts +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/routes.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/shares.d.ts +115 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/shares.js +2 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/sites.d.ts +293 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/sites.js +2 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/snapshots.d.ts +377 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/snapshots.js +2 -0
- package/dist/node_modules/@allyhub-next/protocol/dist/teams.d.ts +27 -9
- package/dist/node_modules/@allyhub-next/protocol/dist/teams.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/dist/tools.d.ts +53 -14
- package/dist/node_modules/@allyhub-next/protocol/dist/tools.js +1 -1
- package/dist/node_modules/@allyhub-next/protocol/package.json +2 -1
- package/dist/node_modules/@allyhub-next/runtimes/package.json +1 -1
- package/dist/node_modules/@allyhub-next/runtimes/schemas/runtimes.schema.json +25 -5
- package/dist/node_modules/@allyhub-next/scenery/package.json +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/api/capabilities.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/api/index.d.ts +1 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/api/index.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/api/page.d.ts +83 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/api/page.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/api/workbook.d.ts +40 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/api/workbook.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/analyse.d.ts +9 -3
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/analyse.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/coerce.d.ts +5 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/coerce.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/engine.d.ts +60 -13
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/engine.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/aggregate.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/convert.d.ts +4 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/convert.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/criteria.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/database.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/date.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/define.d.ts +27 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/define.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/delimiters.d.ts +16 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/delimiters.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/dynamic.d.ts +18 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/dynamic.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/finance.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/index.d.ts +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/index.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/info.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/kernels.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/logical.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/lookup.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/maths.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/modern.d.ts +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/modern.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/functions/text.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/graph.d.ts +38 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/graph.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/indexes.d.ts +19 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/indexes.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/remote.d.ts +49 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/remote.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/server.d.ts +82 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/server.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/types.d.ts +6 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/calc/types.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/chart.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/check.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/diff.d.ts +15 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/diff.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/export.d.ts +29 -8
- package/dist/node_modules/@allyhub-next/sheet/dist/export.js +3 -4
- package/dist/node_modules/@allyhub-next/sheet/dist/file.js +2 -2
- package/dist/node_modules/@allyhub-next/sheet/dist/format.d.ts +8 -2
- package/dist/node_modules/@allyhub-next/sheet/dist/format.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/html.d.ts +52 -4
- package/dist/node_modules/@allyhub-next/sheet/dist/html.js +43 -11
- package/dist/node_modules/@allyhub-next/sheet/dist/index.d.ts +7 -3
- package/dist/node_modules/@allyhub-next/sheet/dist/index.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/inspect.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/paginate.d.ts +98 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/paginate.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/structure.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/table-style.d.ts +32 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/table-style.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/table-styles.d.ts +3 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/table-styles.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/values.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/workbook.d.ts +10 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/workbook.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/charts.d.ts +12 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/charts.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/formula.d.ts +14 -4
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/formula.js +2 -2
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/functions.d.ts +11 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/functions.js +36 -16
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/import.d.ts +6 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/import.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/read.d.ts +44 -2
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/read.js +2 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/sax.d.ts +23 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/sax.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/styles.d.ts +2 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/styles.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/write.d.ts +6 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/write.js +1 -1
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/zip.d.ts +40 -0
- package/dist/node_modules/@allyhub-next/sheet/dist/xlsx/zip.js +2 -0
- package/dist/node_modules/@allyhub-next/sheet/package.json +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/animation.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/api.d.ts +121 -44
- package/dist/node_modules/@allyhub-next/slides-core/dist/api.js +2 -2
- package/dist/node_modules/@allyhub-next/slides-core/dist/capabilities.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/chart-api.d.ts +50 -13
- package/dist/node_modules/@allyhub-next/slides-core/dist/chart-api.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/chart-file.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/check.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/code.d.ts +33 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/code.js +2 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/components.d.ts +31 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/components.js +2 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/enums.d.ts +188 -5
- package/dist/node_modules/@allyhub-next/slides-core/dist/enums.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/file.d.ts +43 -21
- package/dist/node_modules/@allyhub-next/slides-core/dist/file.js +3 -3
- package/dist/node_modules/@allyhub-next/slides-core/dist/identity.d.ts +17 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/identity.js +2 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/index.d.ts +5 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/index.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/lang.d.ts +6 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/lang.js +2 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/measure.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/merge.d.ts +58 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/merge.js +2 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/model.d.ts +38 -4
- package/dist/node_modules/@allyhub-next/slides-core/dist/office.d.ts +27 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/office.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/resolve.d.ts +26 -5
- package/dist/node_modules/@allyhub-next/slides-core/dist/resolve.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/script.d.ts +18 -16
- package/dist/node_modules/@allyhub-next/slides-core/dist/script.js +2 -2
- package/dist/node_modules/@allyhub-next/slides-core/dist/stage.d.ts +51 -53
- package/dist/node_modules/@allyhub-next/slides-core/dist/strict.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/units.d.ts +27 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/units.js +1 -1
- package/dist/node_modules/@allyhub-next/slides-core/dist/values.d.ts +7 -0
- package/dist/node_modules/@allyhub-next/slides-core/dist/values.js +2 -0
- package/dist/node_modules/@allyhub-next/slides-core/package.json +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/captions.js +6 -6
- package/dist/node_modules/@allyhub-next/video-core/dist/fact.d.ts +47 -8
- package/dist/node_modules/@allyhub-next/video-core/dist/fact.js +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/index.d.ts +1 -0
- package/dist/node_modules/@allyhub-next/video-core/dist/index.js +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/marks.d.ts +14 -0
- package/dist/node_modules/@allyhub-next/video-core/dist/marks.js +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/music.d.ts +2 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/piece.d.ts +11 -2
- package/dist/node_modules/@allyhub-next/video-core/dist/problem.d.ts +4 -0
- package/dist/node_modules/@allyhub-next/video-core/dist/problem.js +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/schema.d.ts +2 -2
- package/dist/node_modules/@allyhub-next/video-core/dist/schema.js +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/script.d.ts +2 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/script.js +1 -1
- package/dist/node_modules/@allyhub-next/video-core/dist/source.d.ts +7 -0
- package/dist/node_modules/@allyhub-next/video-core/dist/source.js +2 -0
- package/dist/node_modules/@allyhub-next/video-core/dist/timeline.d.ts +10 -33
- package/dist/node_modules/@allyhub-next/video-core/package.json +1 -1
- package/dist/sheets.d.ts +1 -1
- package/dist/sheets.js +1 -1
- package/dist/slides.d.ts +3 -1
- package/dist/slides.js +1 -1
- package/docs/README.md +1 -0
- package/docs/contract.md +1 -1
- package/docs/defaults.md +25 -2
- package/docs/layers.md +1 -1
- package/docs/project.md +3 -1
- package/docs/sheets.md +29 -3
- package/docs/slides.md +18 -14
- package/docs/sound.md +1 -1
- package/docs/websites.md +57 -0
- package/package.json +3 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { type SheetConditional, type SheetFile, type SheetFilesColumn, type SheetTable, type SheetWorksheet } from "@allyhub-next/formats";
|
|
1
|
+
import { type SheetNote, type SheetPicture, type SheetPrint, type SheetConditional, type SheetFile, type SheetFilesColumn, type SheetTable, type SheetWorksheet } from "@allyhub-next/formats";
|
|
2
|
+
import { Comment, HeaderFooter, PageMargins, PageSetup, SheetProtection, type Image } from "./page.js";
|
|
2
3
|
import { Calc } from "../calc/engine.js";
|
|
3
4
|
import { type ParsedCsv } from "../csv.js";
|
|
4
5
|
import type { SheetProblem } from "../file.js";
|
|
@@ -36,6 +37,12 @@ export declare class Cell {
|
|
|
36
37
|
get computed(): CellValue;
|
|
37
38
|
get data_type(): string;
|
|
38
39
|
get is_date(): boolean;
|
|
40
|
+
/** What it opens: an address on the web, or `#Sheet!A1`. */
|
|
41
|
+
get hyperlink(): string | undefined;
|
|
42
|
+
set hyperlink(to: string | undefined);
|
|
43
|
+
/** Its note: Excel's Note, openpyxl's Comment. */
|
|
44
|
+
get comment(): Comment | undefined;
|
|
45
|
+
set comment(note: Comment | undefined);
|
|
39
46
|
get number_format(): string;
|
|
40
47
|
set number_format(code: string);
|
|
41
48
|
private _setLook;
|
|
@@ -55,8 +62,12 @@ export declare class ColumnDimension {
|
|
|
55
62
|
_width: number | undefined;
|
|
56
63
|
_hidden: boolean | undefined;
|
|
57
64
|
_files: SheetFilesColumn | undefined;
|
|
65
|
+
_level: number | undefined;
|
|
58
66
|
_ws: Worksheet;
|
|
59
67
|
constructor(ws: Worksheet, letter: string);
|
|
68
|
+
/** How deep it is grouped, Excel's outline, 1 to 7. */
|
|
69
|
+
get outlineLevel(): number | undefined;
|
|
70
|
+
set outlineLevel(level: number | undefined);
|
|
60
71
|
/** Its cells name files, not an openpyxl field: true, or how they join and how tall a picture is. */
|
|
61
72
|
get files(): SheetFilesColumn | undefined;
|
|
62
73
|
set files(files: SheetFilesColumn | undefined);
|
|
@@ -70,8 +81,12 @@ export declare class RowDimension {
|
|
|
70
81
|
readonly index: number;
|
|
71
82
|
_height: number | undefined;
|
|
72
83
|
_hidden: boolean | undefined;
|
|
84
|
+
_level: number | undefined;
|
|
73
85
|
_ws: Worksheet;
|
|
74
86
|
constructor(ws: Worksheet, row: number);
|
|
87
|
+
/** How deep it is grouped, Excel's outline, 1 to 7. */
|
|
88
|
+
get outlineLevel(): number | undefined;
|
|
89
|
+
set outlineLevel(level: number | undefined);
|
|
75
90
|
/** In points, as openpyxl's; the file holds pixels. */
|
|
76
91
|
get height(): number | undefined;
|
|
77
92
|
set height(points: number | undefined);
|
|
@@ -149,6 +164,11 @@ export declare class Worksheet {
|
|
|
149
164
|
_filter: AutoFilter;
|
|
150
165
|
/** Its charts, openpyxl's private list. */
|
|
151
166
|
_charts: ChartBase[];
|
|
167
|
+
_print: SheetPrint;
|
|
168
|
+
_links: Record<string, string>;
|
|
169
|
+
_notes: Record<string, SheetNote>;
|
|
170
|
+
_pictures: SheetPicture[];
|
|
171
|
+
_protect: boolean;
|
|
152
172
|
constructor(parent: Workbook, title: string, model?: ModelSheet);
|
|
153
173
|
_spec(): SheetWorksheet | undefined;
|
|
154
174
|
/** What sheet.yaml says of its rules and filter. */
|
|
@@ -210,6 +230,25 @@ export declare class Worksheet {
|
|
|
210
230
|
add_data_validation(data_validation: DataValidation): void;
|
|
211
231
|
/** The sheet's AutoFilter: set its ref to have one. A table has its own. */
|
|
212
232
|
get auto_filter(): AutoFilter;
|
|
233
|
+
_changed(): void;
|
|
234
|
+
/** `print`, `links`, `notes`, `pictures` and `protect` as sheet.yaml holds them. */
|
|
235
|
+
_heldPage(spec: SheetWorksheet | undefined): void;
|
|
236
|
+
get page_setup(): PageSetup;
|
|
237
|
+
get page_margins(): PageMargins;
|
|
238
|
+
get oddHeader(): HeaderFooter;
|
|
239
|
+
get oddFooter(): HeaderFooter;
|
|
240
|
+
get protection(): SheetProtection;
|
|
241
|
+
/** The range that prints. */
|
|
242
|
+
get print_area(): string | undefined;
|
|
243
|
+
set print_area(area: string | undefined);
|
|
244
|
+
/** Rows repeated on every page, as `1:2`. */
|
|
245
|
+
get print_title_rows(): string | undefined;
|
|
246
|
+
set print_title_rows(rows: string | undefined);
|
|
247
|
+
/** Columns repeated on every page, as `A:B`. */
|
|
248
|
+
get print_title_cols(): string | undefined;
|
|
249
|
+
set print_title_cols(columns: string | undefined);
|
|
250
|
+
/** A picture over the cells, at a cell: its anchor unless it says. */
|
|
251
|
+
add_image(image: Image, anchor?: string): void;
|
|
213
252
|
/** A chart at a cell: its anchor unless it says. */
|
|
214
253
|
add_chart(chart: ChartBase, anchor?: string): void;
|
|
215
254
|
add_table(table: Table): void;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{SHEET_DATA as H,SHEET_FILE as z,strict as g}from"@allyhub-next/formats";import{columnIndex as E,columnLetter as N,cellName as C,contains as L,rangeName as x,readAddress as b,readArea as k}from"../address.js";import{Calc as U}from"../calc/engine.js";import{checkWorkbook as G}from"../check.js";import{readCsv as V,writeCsv as Y}from"../csv.js";import{GENERATED as q}from"../generated.js";import{moveFormula as Q,relativeKey as X}from"../formula/move.js";import{parseFormula as F}from"../formula/parse.js";import{printFormula as A}from"../formula/print.js";import{rewriteFormula as Z}from"../formula/rewrite.js";import{isDateCode as S}from"../format.js";import{patchCsv as ee}from"../patch.js";import{CellError as P,dateOfSerial as te,isoOfSerial as se,readTyped as M,serialOfDate as oe,writeTyped as ne}from"../values.js";import{FormulaCell as re,readWorkbook as B,validName as W}from"../workbook.js";import{restructure as ie,tableShape as le}from"../structure.js";import{patchYaml as ae}from"../write.js";import{anchorOf as he,chartOfSpec as fe}from"./charts.js";import{HINT as w}from"./hint.js";import{rectangles as T}from"./rectangles.js";import{AutoFilter as I,ConditionalFormattingList as ce,DataValidationList as ue,validationOf as _e,validationSpec as de}from"./rules.js";import{alignmentLook as me,alignmentOf as pe,borderLook as we,borderOf as ge,fillLook as ke,fillOf as ye,fontLook as be,fontOf as ve}from"./styles.js";const K=()=>process.getBuiltinModule("node:fs"),$=(...l)=>process.getBuiltinModule("node:path").join(...l);export const BLOCK_CELLS=100;const xe=l=>Math.round(l*7+5),Ee=l=>(l-5)/7,Se=l=>l*4/3,$e=l=>l*3/4;function Ce(l,e){const t={},s=l,o=e;for(const n of new Set([...Object.keys(s),...Object.keys(o)])){const r=o[n];JSON.stringify(r??!1)!==JSON.stringify(s[n]??!1)&&(t[n]=r??!1)}return t}const p=l=>Object.keys(l).length===0,y=(l,e)=>{if(!Number.isInteger(l)||l<1)throw new RangeError(`A ${e} is a whole number from 1: ${String(l)}.`);return l},j=(l,e)=>Object.fromEntries(Object.entries(l).filter(([t])=>t!==e));export class Cell{row;column;_ws;_value;_formula=void 0;_look;_changed=!1;_styled=!1;_fresh=!1;constructor(e,t,s){this._ws=e,this.row=t,this.column=s;const o=t-1,n=s-1,r=e._model?.at(o,n);return this._value=null,r instanceof re?this._formula=r.text:r!==void 0&&(this._value=r),this._look=e._model?.lookAt(o,n)??{},this._fresh=r===void 0,g(this,"Cell",w)}get parent(){return this._ws}get coordinate(){return C({row:this.row-1,col:this.column-1})}get column_letter(){return N(this.column-1)}get col_idx(){return this.column}get value(){return this._ws.parent.data_only?this.computed:this._formula!==void 0?this._formula:this._shown(this._value)}set value(e){if(e===void 0)throw new TypeError(`${this.coordinate}: a value is not undefined.`);if(this._formula=void 0,this._changed=!0,this._ws.parent._pending=!0,e instanceof Date){if(Number.isNaN(e.getTime()))throw new TypeError(`${this.coordinate}: the date is not valid.`);this._value=oe(e),S(this._look.format)||(this._look={...this._look,format:Number.isInteger(this._value)?"yyyy-mm-dd":"yyyy-mm-dd hh:mm"});return}if(typeof e=="string"&&e.length>1&&e.startsWith("=")){this._formula=e,this._value=null;return}this._value=e}_shown(e){return typeof e=="number"&&S(this._look.format)?te(e)??e:e instanceof P?e.code:e}get computed(){const{calc:e,model:t}=this._ws.parent._computed(),s=t.sheet(this._ws.title);return s?this._shown(e.value(s,this.row-1,this.column-1)):null}get data_type(){if(this._formula!==void 0)return"f";const e=this._value;return e===null?"n":typeof e=="number"?S(this._look.format)?"d":"n":typeof e=="boolean"?"b":e instanceof P?"e":"s"}get is_date(){return this.data_type==="d"}get number_format(){return this._look.format??"General"}set number_format(e){this._setLook({format:e})}_setLook(e){this._look={...this._look,...e},this._styled=!0,this._ws.parent._pending=!0}get font(){return ve(this._look)}set font(e){this._setLook(be(e))}get fill(){return ye(this._look)}set fill(e){this._setLook(ke(e))}get border(){return ge(this._look)}set border(e){this._setLook(we(e))}get alignment(){return pe(this._look)}set alignment(e){this._setLook(me(e))}get has_style(){return!p(this._look)}offset(e=0,t=0){return this._ws.cell(this.row+e,this.column+t)}}export class ColumnDimension{index;_width;_hidden;_files;_ws;constructor(e,t){this._ws=e,this.index=t;const s=e._spec()?.columns?.[t];return this._width=s?.width,this._hidden=s?.hidden,this._files=s?.files,g(this,"ColumnDimension",w)}get files(){return this._files}set files(e){this._files=e,this._ws._dimensions=!0}get width(){return this._width===void 0?void 0:Ee(this._width)}set width(e){this._width=e===void 0?void 0:xe(e),this._ws._dimensions=!0}get hidden(){return this._hidden??!1}set hidden(e){this._hidden=e,this._ws._dimensions=!0}}export class RowDimension{index;_height;_hidden;_ws;constructor(e,t){this._ws=e,this.index=t;const s=e._spec()?.rows?.[String(t)];return this._height=s?.height,this._hidden=s?.hidden,g(this,"RowDimension",w)}get height(){return this._height===void 0?void 0:$e(this._height)}set height(e){this._height=e===void 0?void 0:Se(e),this._ws._dimensions=!0}get hidden(){return this._hidden??!1}set hidden(e){this._hidden=e,this._ws._dimensions=!0}}export class Table{displayName;ref;key;_spec;autoFilter;constructor(e){for(const t of Object.keys(e))if(!["displayName","ref","key"].includes(t))throw new TypeError(`Table takes displayName, ref and key; it has no ${t}. ${w}`);if(!W(e.displayName))throw new TypeError(`${e.displayName} cannot be a table's name.`);if(!k(e.ref))throw new TypeError(`${e.ref} is not a range.`);return this.displayName=e.displayName,this.ref=e.ref,this.key=e.key,this.autoFilter=new I(e.ref,()=>{}),g(this,"Table",w)}get name(){return this.displayName}}export class TableList{_items=new Map;constructor(){return g(this,"TableList",w,{key:e=>this._items.get(e)})}add(e){this._items.set(e.name,e)}get(e){return this._items.get(e)}keys(){return[...this._items.keys()]}values(){return[...this._items.values()]}items(){return[...this._items.entries()]}pop(e){const t=this._items.get(e);return this._items.delete(e),t}}export class DefinedName{name;attr_text;constructor(e,t){if(!W(e))throw new TypeError(`${e} cannot be a name.`);return this.name=e,this.attr_text=t.attr_text.replace(/^=/u,""),g(this,"DefinedName",w)}get value(){return this.attr_text}set value(e){this.attr_text=e.replace(/^=/u,"")}}export class DefinedNameDict{_items=new Map;constructor(){return g(this,"DefinedNameDict",w,{key:e=>this._items.get(e),setKey:(e,t)=>t instanceof DefinedName?(this._items.set(e,t),!0):!1})}add(e){this._items.set(e.name,e)}get(e){return this._items.get(e)}keys(){return[...this._items.keys()]}values(){return[...this._items.values()]}items(){return[...this._items.entries()]}pop(e){const t=this._items.get(e);return this._items.delete(e),t}}const v=l=>{if(l._formula!==void 0)return l._formula;const e=l._value;return typeof e=="number"&&S(l._look.format)?se(e):ne(e)};function J(l){if(l._formula!==void 0)return l._formula;const e=l._value;return e===null?null:typeof e=="number"&&!S(l._look.format)||typeof e=="boolean"?e:v(l)}const Te=l=>l.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/gu,"")||"rows";export class Worksheet{_parent;_title;_was;_model;_cells=new Map;_columns=new Map;_rows=new Map;_tables=new TableList;_freeze;_merges=[];_state="visible";_dimensions=!1;_lastRow=0;_rules=[];_validations=[];_filter;_charts=[];constructor(e,t,s){if(this._parent=e,this._title=t,this._filter=new I(void 0,()=>{this._parent._pending=!0}),s){this._model=s,this._was=t,this._freeze=s.spec.freeze,this._merges=[...s.spec.merges??[]],this._state=s.spec.hidden===!0?"hidden":"visible",this._lastRow=(s.used?.bottom??-1)+1,this._heldRules(s.spec);for(const o of s.tables){const n=new Table({displayName:o.name,ref:x(o.area),...o.spec.key===void 0?{}:{key:o.spec.key}});n._spec=o.spec;const{filter:r}=o.spec;if(r)for(const[a,f]of Object.entries(r.columns)){const u=o.column(a);u&&n.autoFilter._columns.set(u.index,f)}this._tables.add(n)}}return g(this,"Worksheet",w,{index:o=>this._rowCells(o),key:o=>this._at(o),setKey:(o,n)=>{const r=b(o);return r?(this.cell(r.row+1,r.col+1).value=n,!0):!1}})}_spec(){return this._parent._file.sheets.find(e=>e.name===(this._was??this._title))}_heldRules(e){this._rules=[...e?.conditional??[]],this._validations=(e?.validations??[]).map(_e),this._charts=(e?.charts??[]).map(fe),this._filter.ref=e?.filter?.range,this._filter._columns.clear();const t=e?.filter?k(e.filter.range):void 0;for(const[s,o]of Object.entries(e?.filter?.columns??{})){const n=E(s);t&&n!==void 0&&this._filter._columns.set(n-t.left,o)}}_at(e){const t=b(e);if(t)return this.cell(t.row+1,t.col+1);const s=k(e);if(s)return this._area(s);const o=E(e);if(o!==void 0){const n=[];for(let r=1;r<=this.max_row;r++)n.push(this.cell(r,o+1));return n}}_rowCells(e){const t=[];for(let s=1;s<=this.max_column;s++)t.push(this.cell(e,s));return t}_area(e){const t=[];for(let s=e.top;s<=e.bottom;s++){const o=[];for(let n=e.left;n<=e.right;n++)o.push(this.cell(s+1,n+1));t.push(o)}return t}get parent(){return this._parent}get title(){return this._title}set title(e){const t=this._title;e!==t&&this._parent._renameSheet(this,t,e)}get sheet_state(){return this._state}set sheet_state(e){this._state=e}cell(e,t,s){if(e<1||t<1)throw new RangeError(`Row and column values must be at least 1: ${String(e)}, ${String(t)}.`);const o=(e-1)*16384+t-1;let n=this._cells.get(o);return n||(n=new Cell(this,e,t),this._cells.set(o,n)),s!==void 0&&(n.value=s),e>this._lastRow&&(this._lastRow=e),n}append(e){const t=this._lastRow+1,s=(o,n)=>{n!=null&&this.cell(t,o,n)};if(Array.isArray(e))e.forEach((o,n)=>{s(n+1,o)});else for(const[o,n]of Object.entries(e)){const r=/^\d+$/u.test(o)?Number(o)-1:E(o);if(r===void 0)throw new TypeError(`${o} is not a column.`);s(r+1,n)}this._lastRow=t}get max_row(){return Math.max(1,this._lastRow)}get max_column(){let e=this._model?.columnsUsed??0;for(const t of this._cells.values())(t._value!==null||t._formula!==void 0)&&t.column>e&&(e=t.column);return Math.max(1,e)}min_row=1;min_column=1;get dimensions(){return x({top:0,left:0,bottom:this.max_row-1,right:this.max_column-1})}iter_rows(e={}){const t=this._area({top:(e.min_row??1)-1,left:(e.min_col??1)-1,bottom:(e.max_row??this.max_row)-1,right:(e.max_col??this.max_column)-1});return e.values_only===!0?t.map(s=>s.map(o=>o.value)):t}iter_cols(e={}){const t=this.iter_rows(e);return(t[0]??[]).map((o,n)=>t.map(r=>r[n]))}get rows(){return this.iter_rows()}get columns(){return this.iter_cols()}get values(){return this.iter_rows({values_only:!0})}insert_rows(e,t=1){this._parent._restructure({op:"insert-rows",sheet:this._title,at:y(e,"row")-1,count:y(t,"amount")})}delete_rows(e,t=1){this._parent._restructure({op:"delete-rows",sheet:this._title,at:y(e,"row")-1,count:y(t,"amount")})}insert_cols(e,t=1){this._parent._restructure({op:"insert-columns",sheet:this._title,at:y(e,"column")-1,count:y(t,"amount")})}delete_cols(e,t=1){this._parent._restructure({op:"delete-columns",sheet:this._title,at:y(e,"column")-1,count:y(t,"amount")})}merge_cells(e){if(!k(e))throw new TypeError(`${e} is not a range.`);this._merges.includes(e)||this._merges.push(e),this._parent._pending=!0}unmerge_cells(e){this._merges=this._merges.filter(t=>t!==e),this._parent._pending=!0}get merged_cells(){return[...this._merges]}get freeze_panes(){return this._freeze}set freeze_panes(e){const t=e instanceof Cell?e.coordinate:e;if(t!==void 0&&!b(t))throw new TypeError(`${t} is not a cell.`);this._freeze=t,this._parent._pending=!0}get column_dimensions(){return new Proxy({},{get:(e,t)=>{if(typeof t!="string")return;const s=t.toUpperCase();if(E(s)===void 0)throw new TypeError(`${t} is not a column.`);let o=this._columns.get(s);return o||(o=new ColumnDimension(this,s),this._columns.set(s,o)),o}})}get row_dimensions(){return new Proxy({},{get:(e,t)=>{if(typeof t!="string"||!/^[1-9]\d*$/u.test(t))throw new TypeError(`${String(t)} is not a row.`);const s=Number(t);let o=this._rows.get(s);return o||(o=new RowDimension(this,s),this._rows.set(s,o)),o}})}get tables(){return this._tables}get conditional_formatting(){return new ce(this._rules,()=>{this._parent._pending=!0})}get data_validations(){return new ue(this._validations,()=>{this._parent._pending=!0})}add_data_validation(e){this._validations.push(e),this._parent._pending=!0}get auto_filter(){return this._filter}add_chart(e,t){e.anchor=he(t)??e.anchor,this._charts.push(e),this._parent._pending=!0}add_table(e){if(this._parent._tableNamed(e.name))throw new Error(`There is already a table named ${e.name}.`);this._tables.add(e),this._parent._pending=!0}}export class Workbook{worksheets=[];defined_names=new DefinedNameDict;data_only;title;path;block_cells=BLOCK_CELLS;_file={sheets:[]};_text="";_disk={text:"",csv:new Map};_csv=new Map;_model;_names=new Map;_active=0;_pending=!1;_making=!1;_calc=void 0;constructor(e={}){this.data_only=e.data_only??!1;const t=g(this,"Workbook",w,{key:s=>this.worksheets.find(o=>o.title===s),index:s=>this.worksheets[s]});return t.worksheets=[new Worksheet(t,"Sheet")],t}static open(e,t={}){const s=new Workbook(t);return s.path=e,s._read(e),s}_read(e){const{readFileSync:t}=K(),s=t($(e,z),"utf8"),o=new Map,n=r=>{try{const a=t($(e,r),"utf8");return o.set(r,a),a}catch{return}};this._load(s,n);for(const[r,a]of o)this._keepCsv(r,a);this._disk={text:s,csv:o}}_keepCsv(e,t){const s=V(t);this._csv.set(e,{parsed:s,rows:s.records.map(o=>o.end===o.start?[]:[...o.fields])})}_load(e,t){const{workbook:s,problems:o}=B({text:e,read:t});if(!s)throw new Error(`sheet.yaml cannot be opened: ${o.map(n=>n.message).join("; ")}`);this._text=e,this._file=structuredClone(s.file),this._model=s,this.title=s.title,this.worksheets=s.sheets.map(n=>new Worksheet(this,n.name,n));for(const n of s.names.values()){if(n.source.path?.[0]!=="names")continue;const r=n.formula?n.formula.text:n.value??null;this._names.set(n.name,r),this.defined_names.add(new DefinedName(n.name,{attr_text:String(r)}))}}get active(){const e=this.worksheets[this._active]??this.worksheets[0];if(!e)throw new Error("The workbook has no sheets.");return e}set active(e){this._active=this.worksheets.indexOf(e)}get sheetnames(){return this.worksheets.map(e=>e.title)}get_sheet_by_name(e){return this.worksheets.find(t=>t.title===e)}index(e){return this.worksheets.indexOf(e)}create_sheet(e,t){let s=e??"Sheet";for(let n=1;this.worksheets.some(r=>r.title===s);n++)s=`${e??"Sheet"}${String(n)}`;const o=new Worksheet(this,s);return this.worksheets.splice(t??this.worksheets.length,0,o),this._pending=!0,o}remove(e){this.worksheets=this.worksheets.filter(t=>t!==e),this._pending=!0}move_sheet(e,t=0){const s=this.worksheets.indexOf(e);this.worksheets.splice(s,1),this.worksheets.splice(s+t,0,e),this._pending=!0}_tableNamed(e){for(const t of this.worksheets){const s=t._tables.get(e);if(s)return s}}_renameSheet(e,t,s){if(this.worksheets.some(n=>n!==e&&n.title===s))throw new Error(`There is already a sheet named ${s}.`);this._apply(),this._rewrite({op:"rename-sheet",from:t,to:s}),e._title=s,e._was=s;const o=this._file.sheets.find(n=>n.name===t);o&&(o.name=s),this._apply()}_restructure(e){this._apply();const t=ie({file:this._file,rows:new Map([...this._csv].map(([o,n])=>[o,n.rows]))},e);if(t.refused!==void 0)throw new Error(t.refused==="table-header"?"A table's header row is not deleted: delete the table's rows, or all of it.":"That sheet is not there.");this._file=t.file;for(const[o,n]of t.rows){const r=this._csv.get(o);r&&(r.rows=n.map(a=>[...a]))}for(const o of this.worksheets){const n=this._file.sheets.find(r=>r.name===o._title);o._cells.clear(),o._columns.clear(),o._rows.clear(),o._merges=[...n?.merges??[]],o._freeze=n?.freeze,o._heldRules(n)}const s=this._build();this._rebase(s.text,s.files);for(const o of this.worksheets){const n=this._model?.sheet(o._title);if(n){o._lastRow=(n.used?.bottom??-1)+1;for(const r of n.tables){const a=o._tables.get(r.name);a&&(a.ref=x(r.area))}}}}_rewrite(e){const t=(o,n)=>{const r=F(o);if(!r.ok)return o;const a=Z(r.node,e,n);return a===r.node?o:A(a)},{_file:s}=this;if(s.names){for(const[o,n]of Object.entries(s.names))if(typeof n=="string"&&n.startsWith("=")){const r=t(n,s.sheets[0]?.name??"");s.names[o]=r,this._names.set(o,r);const a=this.defined_names.get(o);a&&(a.attr_text=r.slice(1))}}for(const o of s.checks??[])o.formula=t(o.formula,s.sheets[0]?.name??"");for(const o of s.sheets){for(const[n,r]of Object.entries(o.cells??{}))typeof r=="string"&&r.startsWith("=")?o.cells[n]=t(r,o.name):r!==null&&typeof r=="object"&&r.formula!==void 0&&(r.formula=t(r.formula,o.name));for(const n of o.tables??[]){for(const r of Object.values(n.columns??{}))r.formula!==void 0&&(r.formula=t(r.formula,o.name));this._rewriteCsv(n.rows,o.name,t)}for(const n of o.blocks??[])this._rewriteCsv(n.rows,o.name,t)}}_rewriteCsv(e,t,s){const o=this._csv.get(e);o&&(o.rows=o.rows.map(n=>n.map(r=>M(r).formula!==void 0?s(r,t):r)))}_sheetEntry(e){const t=e._was??e._title;let s=this._file.sheets.find(o=>o.name===t);return s||(s={name:e._title},this._file.sheets.push(s)),s.name=e._title,s}_dataPath(e){const t=`${H}/${Te(e)}`;let s=`${t}.csv`;const o=n=>this._csv.has(n)||this._file.sheets.some(r=>r.tables?.some(a=>a.rows===n)??r.blocks?.some(a=>a.rows===n));for(let n=2;o(s);n++)s=`${t}-${String(n)}.csv`;return s}_writeNewTable(e,t,s){const o=k(s.ref);if(!o)throw new Error(`${s.ref} is not a range.`);const n=[];for(let i=o.left;i<=o.right;i++){const h=e.cell(o.top+1,i+1).value;if(h===null||h==="")throw new Error(`Table ${s.name}'s header has no name at ${C({row:o.top,col:i})}.`);n.push(String(h))}const r={},a=[];n.forEach((i,h)=>{const c=o.left+h,d=this._columnFormula(e,o,c);d===void 0?a.push(h):r[i]={formula:d};const m=this._columnFormat(e,o,c);m!==void 0&&(r[i]={...r[i],format:m})}),this._columnsStyled(e,o);const f=[a.map(i=>n[i]??"")];for(let i=o.top+1;i<=o.bottom;i++)f.push(a.map(h=>v(e.cell(i+1,o.left+h+1))));t.cells&&(t.cells=Object.fromEntries(Object.entries(t.cells).filter(([i])=>{const h=b(i);return!h||!L(o,h.row,h.col)})));const u=this._dataPath(s.name);this._csv.set(u,{parsed:void 0,rows:f});const _={name:s.name,at:C({row:o.top,col:o.left}),rows:u,...s.key===void 0?{}:{key:s.key},...p(r)?{}:{columns:r}};(t.tables??=[]).push(_),s._spec=_}_columnsStyled(e,t){for(let s=t.left;s<=t.right;s++)for(let o=t.top+1;o<=t.bottom;o++){const n=e.cell(o+1,s+1),{format:r,...a}=this._ownLook(n);r!==void 0&&p(a)&&(n._styled=!1)}}_columnFormula(e,t,s){if(t.bottom<=t.top)return;const o=e.cell(t.top+2,s+1),n=D(o);if(n!==void 0){for(let r=t.top+2;r<=t.bottom;r++)if(D(e.cell(r+1,s+1))!==n)return;return o._formula}}_columnFormat(e,t,s){if(t.bottom<=t.top)return;const o=r=>this._ownLook(e.cell(r+1,s+1)).format,n=o(t.top+1);for(let r=t.top+2;r<=t.bottom;r++)if(o(r)!==n)return;return n}_held(e){const t=this._csv.get(e);if(!t)throw new Error(`${e} was not read.`);return t}_setCsvCell(e,t,s,o){const{rows:n}=this._held(e);for(;n.length<=t;)n.push([]);const r=n[t];if(r){for(;r.length<=s;)r.push("");r[s]=o}}_recordOf(e,t){let s=0;for(let o=0;o<t;){if(s+=1,s>=e.length)return s+t-o-1;e[s]?.length!==0&&(o+=1)}return s}_writeTableCell(e,t,s){const o=this._held(e.rows),n=o.rows[0]??[],r=s.column-1-t.left,a=this._recordOf(o.rows,s.row-1-t.top),f=Object.keys(e.columns??{}).filter(c=>!n.some(d=>d.trim().toLowerCase()===c.toLowerCase())),_=[...n.map(c=>c.trim()),...f][r];if(_===void 0)return;if(a===0){r<n.length&&(n[r]=v(s));return}const i=e.columns?.[_]?.formula;if(i!==void 0){const c=this._movedColumn(i,t,s);if(s._formula===c){const d=n.findIndex(m=>m.trim()===_);d>=0&&this._setCsvCell(e.rows,a,d,"");return}}let h=n.findIndex(c=>c.trim()===_);h<0&&(h=n.length,this._setCsvCell(e.rows,0,h,_)),this._setCsvCell(e.rows,a,h,v(s))}_movedColumn(e,t,s){const o=F(e);if(!o.ok)return e;const n=s.row-1-(t.top+1);return n===0?e:A(Q(o.node,n,0))}_apply(){this.title===void 0?delete this._file.title:this._file.title=this.title;const e=new Set(this.worksheets.map(s=>s._was??s._title));this._file.sheets=this._file.sheets.filter(s=>e.has(s.name));for(const s of this.worksheets)this._applySheet(s);this._file.sheets=this.worksheets.map(s=>this._sheetEntry(s)),this._applyNames();const t=this._build();this._rebase(t.text,t.files)}_applyNames(){const e={};for(const t of this.defined_names.values()){const s=M(t.attr_text),o=this._names.get(t.name);e[t.name]=o!==void 0&&String(o).replace(/^=/u,"")===t.attr_text?o:typeof s.value=="number"||typeof s.value=="boolean"?s.value:/^"[^"]*"$/u.test(t.attr_text)?t.attr_text.slice(1,-1):`=${t.attr_text}`}p(e)?delete this._file.names:this._file.names=e}_applySheet(e){const t=this._sheetEntry(e);e._freeze===void 0?delete t.freeze:t.freeze=e._freeze,e._merges.length===0?delete t.merges:t.merges=[...e._merges],e._state==="hidden"?t.hidden=!0:delete t.hidden,e._rules.length===0?delete t.conditional:t.conditional=[...e._rules],e._validations.length===0?delete t.validations:t.validations=e._validations.map(de),e._charts.length===0?delete t.charts:t.charts=e._charts.map(o=>o._spec(e._title)),this._applyFilter(e,t);const s=new Set(t.tables?.map(o=>o.name.toLowerCase()));for(const o of e._tables.values())s.has(o.name.toLowerCase())||this._writeNewTable(e,t,o);this._applyTableFilters(e,t),this._applyCells(e,t),this._applyDimensions(e,t),t.cells&&p(t.cells)&&delete t.cells,t.columns&&p(t.columns)&&delete t.columns,t.rows&&p(t.rows)&&delete t.rows,t.format?.length===0&&delete t.format,t.tables?.length===0&&delete t.tables,t.blocks?.length===0&&delete t.blocks}_applyFilter(e,t){const{ref:s,_columns:o}=e._filter;if(s===void 0){delete t.filter;return}const n=k(s);if(!n)throw new TypeError(`${s} is not a range.`);const r={};for(const[a,f]of o)r[N(n.left+a)]=f;t.filter={range:s,...p(r)?{}:{columns:r}}}_applyTableFilters(e,t){for(const s of t.tables??[]){const o=e._tables.get(s.name);if(!o||o.autoFilter._columns.size===0)continue;const n=this._csv.get(s.rows)?.rows??[],r=le(s,n)?.names??[],a={};for(const[f,u]of o.autoFilter._columns){const _=r[f];if(_===void 0)throw new RangeError(`Table ${s.name} has no column at ${String(f)}.`);a[_]=u}s.filter={columns:a}}}_applyDimensions(e,t){if(e._dimensions){for(const[s,o]of e._columns){const n={...t.columns?.[s]};o._width===void 0?delete n.width:n.width=o._width,o._hidden===void 0?delete n.hidden:n.hidden=o._hidden,o._files===void 0?delete n.files:n.files=o._files;const r=j(t.columns??{},s);t.columns=p(n)?r:{...r,[s]:n}}for(const[s,o]of e._rows){const n={...t.rows?.[String(s)]};o._height===void 0?delete n.height:n.height=o._height,o._hidden===void 0?delete n.hidden:n.hidden=o._hidden;const r=j(t.rows??{},String(s));t.rows=p(n)?r:{...r,[String(s)]:n}}e._dimensions=!1}}_applyCells(e,t){const s=(t.tables??[]).flatMap(r=>{const a=b(r.at),f=e._tables.get(r.name),u=f?k(f.ref):void 0;return a&&u?[{entry:r,area:u}]:[]}),o=(t.blocks??[]).flatMap(r=>{const a=e._model?.blocks.find(f=>f.file===r.rows);return a?[{entry:r,area:a.area}]:[]}),n=[];for(const r of e._cells.values()){if(!r._changed&&!r._styled)continue;const a=r.row-1,f=r.column-1,u=s.find(i=>L(i.area,a,f)),_=o.find(i=>L(i.area,a,f));u?(r._changed&&this._writeTableCell(u.entry,u.area,r),r._styled&&this._styleByFormat(t,r)):_?(r._changed&&this._setCsvCell(_.entry.rows,a-_.area.top,f-_.area.left,v(r)),r._styled&&this._styleByFormat(t,r)):r._fresh&&!e._model?.keys.has(a*16384+f)?n.push(r):this._writeLoose(e,t,r),r._changed=!1,r._styled=!1}this._consolidate(e,t,n),Oe(t)}_styleByFormat(e,t){(e.format??=[]).push({range:t.coordinate,...this._ownLook(t)})}_ownLook(e){const t=e._ws._model?.lookAt(e.row-1,e.column-1)??{},s=Ce(t,e._look);return s.format!==void 0&&e._formula===void 0&&M(v(e)).format===s.format&&delete s.format,s}_writeLoose(e,t,s){const o=s.coordinate,n=t.cells??={},r=J(s),a=this._ownLook(s),f=n[o],_={...f!==null&&typeof f=="object"?Object.fromEntries(Object.entries(f).filter(([c])=>c!=="value"&&c!=="formula"&&c!=="name")):{},...a},i=f!==null&&typeof f=="object"&&f.name!==void 0?{name:f.name}:{},h=e._model?.at(s.row-1,s.column-1)!==void 0;if(r===null&&p(_)&&!h){t.cells=j(n,o);return}n[o]=p(_)&&p(i)?r:{...typeof r=="string"&&r.startsWith("=")?{formula:r}:r===null?{}:{value:r},...i,..._}}_consolidate(e,t,s){const o=s.filter(i=>i._value!==null||i._formula!==void 0),n=new Map;for(const i of s){const h=this._ownLook(i);if(p(h))continue;const c=JSON.stringify(h),d=n.get(c);d?d.push(i):n.set(c,[i])}const r=new Map(o.map(i=>[`${String(i.row)}:${String(i.column)}`,i])),a=i=>r.get(`${String(i.row+1)}:${String(i.col+1)}`)??e.cell(i.row+1,i.col+1),f=new Map,u=[];for(const i of o){const h={row:i.row-1,col:i.column-1},c=i._formula===void 0?void 0:D(i);if(c===void 0){u.push(h);continue}const d=f.get(c);d?d.push(h):f.set(c,[h])}const _=t.cells??={};for(const i of f.values())for(const h of T(i))_[x(h)]=a({row:h.top,col:h.left})._formula??null;for(const i of T(u))if((i.bottom-i.top+1)*(i.right-i.left+1)>=this.block_cells){const c=[];for(let m=i.top;m<=i.bottom;m++){const R=[];for(let O=i.left;O<=i.right;O++)R.push(v(a({row:m,col:O})));c.push(R)}const d=this._dataPath(e._title);this._csv.set(d,{parsed:void 0,rows:c}),(t.blocks??=[]).push({at:C({row:i.top,col:i.left}),rows:d})}else for(let c=i.top;c<=i.bottom;c++)for(let d=i.left;d<=i.right;d++){const m=a({row:c,col:d});_[m.coordinate]=J(m)}for(const i of n.values()){const[h]=i;if(!h)continue;const c=this._ownLook(h),d=i.map(m=>({row:m.row-1,col:m.column-1}));for(const m of T(d))(t.format??=[]).push({range:x(m),...c})}}_build(){const e=new Map;for(const[t,s]of this._csv)e.set(t,s.parsed?ee(s.parsed,s.rows):Y(s.rows));return{text:ae(this._text,this._file),files:e}}_rebase(e,t){const{workbook:s,problems:o}=B({text:e,read:n=>t.get(n)});if(!s)throw new Error(`The workbook is not one: ${o.map(n=>n.message).join("; ")}`);this._text=e,this._file=structuredClone(s.file),this._model=s;for(const[n,r]of t)this._keepCsv(n,r);this._calc=void 0,this._pending=!1;for(const n of this.worksheets){n._was=n._title;const r=s.sheet(n._title);r&&(n._model=r);for(const a of n._cells.values())a._fresh=r?.at(a.row-1,a.column-1)===void 0}this._names=new Map;for(const n of s.names.values())n.source.path?.[0]==="names"&&this._names.set(n.name,n.formula?n.formula.text:n.value??null)}_computed(){const e=[...this.defined_names.values()];if((this._pending||e.length!==this._names.size||e.some(t=>String(this._names.get(t.name)).replace(/^=/u,"")!==t.attr_text))&&this._apply(),!this._calc){const t=this._model;if(!t)throw new Error("The workbook has no sheets.");this._calc={calc:new U(t).calculate(),model:t}}return this._calc}check(){this._apply();const{text:e,files:t}=this._build();return G({text:e,read:s=>t.get(s)}).problems}_generated(){if(this.worksheets.length===0)return this._applyNames(),{sheets:[],csv:{},...this._file.names?{names:this._file.names}:{}};this._apply();const e=structuredClone(this._file.sheets),t=o=>`${q}${o}`;for(const o of e){for(const n of o.tables??[])n.rows=t(n.rows);for(const n of o.blocks??[])n.rows=t(n.rows)}const s={};for(const[o,n]of this._build().files)s[t(o)]=n;return{sheets:e,csv:s,...this._file.names?{names:this._file.names}:{}}}save(e){if(this._making)throw new Error("sheet.ts makes sheets and saves nothing: what it adds to wb is what it makes.");const t=e??this.path;if(t===void 0)throw new TypeError("A new workbook is saved to a folder: wb.save('content/budget').");if(this.worksheets.length===0)throw new Error("A workbook has at least one sheet.");const{text:s,csv:o}=this._disk;this._apply();const n=new Set(this._file.sheets.flatMap(i=>[...(i.tables??[]).map(h=>h.rows),...(i.blocks??[]).map(h=>h.rows)])),r=[...this._csv.keys()].filter(i=>!n.has(i));for(const i of r)this._csv.delete(i);const{mkdirSync:a,rmSync:f,writeFileSync:u}=K(),_=process.getBuiltinModule("node:path");a(t,{recursive:!0}),(this._text!==s||e!==void 0)&&u($(t,z),this._text);for(const[i,h]of this._csv){const c=h.parsed?.source??"";if(e===void 0&&o.get(i)===c)continue;const d=$(t,i);a(_.dirname(d),{recursive:!0}),u(d,c)}if(e===void 0||t===this.path)for(const i of r)f($(t,i),{force:!0});this._disk={text:this._text,csv:new Map([...this._csv].map(([i,h])=>[i,h.parsed?.source??""]))},this.path=t}}function D(l){if(l._formula===void 0)return;const e=F(l._formula);if(e.ok)return X(e.node,l.row-1,l.column-1)}function Oe(l){const e=l.format;if(!e||e.length<2)return;const t=[],s=new Map;for(const o of e){const{range:n,...r}=o,a=b(n);if(!a){t.push(o);continue}const f=JSON.stringify(r),u=s.get(f);u?u.places.push(a):s.set(f,{look:r,places:[a]})}for(const{look:o,places:n}of s.values())for(const r of T(n))t.push({range:x(r),...o});l.format=t}export function load_workbook(l,e={}){return Workbook.open(l,e)}export const get_column_letter=l=>N(l-1);export function column_index_from_string(l){const e=E(l);if(e===void 0)throw new TypeError(`${l} is not a column.`);return e+1}export function coordinate_to_tuple(l){const e=b(l);if(!e)throw new TypeError(`${l} is not a cell.`);return[e.row+1,e.col+1]}export function range_boundaries(l){const e=k(l);if(!e)throw new TypeError(`${l} is not a range.`);return[e.left+1,e.top+1,e.right+1,e.bottom+1]}
|
|
2
|
+
import{SHEET_DATA as G,SHEET_FILE as P,strict as g}from"@allyhub-next/formats";import{Comment as z,HeaderFooter as A,PageMargins as V,PageSetup as Y,SheetProtection as q}from"./page.js";import{columnIndex as E,columnLetter as L,cellName as C,contains as N,rangeName as x,readAddress as y,readArea as k}from"../address.js";import{Calc as Q}from"../calc/engine.js";import{checkWorkbook as X}from"../check.js";import{readCsv as Z,writeCsv as ee}from"../csv.js";import{GENERATED as te}from"../generated.js";import{moveFormula as se,relativeKey as ne}from"../formula/move.js";import{parseFormula as F}from"../formula/parse.js";import{printFormula as B}from"../formula/print.js";import{rewriteFormula as oe}from"../formula/rewrite.js";import{isDateCode as S}from"../format.js";import{patchCsv as re}from"../patch.js";import{CellError as W,dateOfSerial as ie,isoOfSerial as le,readTyped as M,serialOfDate as ae,writeTyped as he}from"../values.js";import{FormulaCell as fe,readWorkbook as I,validName as H}from"../workbook.js";import{restructure as _e,tableShape as ce}from"../structure.js";import{patchYaml as ue}from"../write.js";import{anchorOf as de,chartOfSpec as me}from"./charts.js";import{HINT as w}from"./hint.js";import{rectangles as T}from"./rectangles.js";import{AutoFilter as K,ConditionalFormattingList as pe,DataValidationList as we,validationOf as ge,validationSpec as ke}from"./rules.js";import{alignmentLook as ye,alignmentOf as be,borderLook as ve,borderOf as xe,fillLook as Ee,fillOf as Se,fontLook as $e,fontOf as Ce}from"./styles.js";const J=()=>process.getBuiltinModule("node:fs"),$=(...l)=>process.getBuiltinModule("node:path").join(...l);export const BLOCK_CELLS=100;const Te=l=>Math.round(l*7+5),Oe=l=>(l-5)/7,Le=l=>l*4/3,Ne=l=>l*3/4;function Fe(l,e){const t={},s=l,n=e;for(const o of new Set([...Object.keys(s),...Object.keys(n)])){const r=n[o];JSON.stringify(r??!1)!==JSON.stringify(s[o]??!1)&&(t[o]=r??!1)}return t}const m=l=>Object.keys(l).length===0,b=(l,e)=>{if(!Number.isInteger(l)||l<1)throw new RangeError(`A ${e} is a whole number from 1: ${String(l)}.`);return l},j=(l,e)=>Object.fromEntries(Object.entries(l).filter(([t])=>t!==e));export class Cell{row;column;_ws;_value;_formula=void 0;_look;_changed=!1;_styled=!1;_fresh=!1;constructor(e,t,s){this._ws=e,this.row=t,this.column=s;const n=t-1,o=s-1,r=e._model?.at(n,o);return this._value=null,r instanceof fe?this._formula=r.text:r!==void 0&&(this._value=r),this._look=e._model?.lookAt(n,o)??{},this._fresh=r===void 0,g(this,"Cell",w)}get parent(){return this._ws}get coordinate(){return C({row:this.row-1,col:this.column-1})}get column_letter(){return L(this.column-1)}get col_idx(){return this.column}get value(){return this._ws.parent.data_only?this.computed:this._formula!==void 0?this._formula:this._shown(this._value)}set value(e){if(e===void 0)throw new TypeError(`${this.coordinate}: a value is not undefined.`);if(this._formula=void 0,this._changed=!0,this._ws.parent._pending=!0,e instanceof Date){if(Number.isNaN(e.getTime()))throw new TypeError(`${this.coordinate}: the date is not valid.`);this._value=ae(e),S(this._look.format)||(this._look={...this._look,format:Number.isInteger(this._value)?"yyyy-mm-dd":"yyyy-mm-dd hh:mm"});return}if(typeof e=="string"&&e.length>1&&e.startsWith("=")){this._formula=e,this._value=null;return}this._value=e}_shown(e){return typeof e=="number"&&S(this._look.format)?ie(e)??e:e instanceof W?e.code:e}get computed(){const{calc:e,model:t}=this._ws.parent._computed(),s=t.sheet(this._ws.title);return s?this._shown(e.value(s,this.row-1,this.column-1)):null}get data_type(){if(this._formula!==void 0)return"f";const e=this._value;return e===null?"n":typeof e=="number"?S(this._look.format)?"d":"n":typeof e=="boolean"?"b":e instanceof W?"e":"s"}get is_date(){return this.data_type==="d"}get hyperlink(){return this._ws._links[this.coordinate]}set hyperlink(e){e===void 0?Reflect.deleteProperty(this._ws._links,this.coordinate):this._ws._links[this.coordinate]=e,this._ws._changed()}get comment(){const e=this._ws._notes[this.coordinate];if(e!==void 0)return typeof e=="string"?new z(e):new z(e.text,e.author)}set comment(e){e===void 0?Reflect.deleteProperty(this._ws._notes,this.coordinate):this._ws._notes[this.coordinate]=e._file(),this._ws._changed()}get number_format(){return this._look.format??"General"}set number_format(e){this._setLook({format:e})}_setLook(e){this._look={...this._look,...e},this._styled=!0,this._ws.parent._pending=!0}get font(){return Ce(this._look)}set font(e){this._setLook($e(e))}get fill(){return Se(this._look)}set fill(e){this._setLook(Ee(e))}get border(){return xe(this._look)}set border(e){this._setLook(ve(e))}get alignment(){return be(this._look)}set alignment(e){this._setLook(ye(e))}get has_style(){return!m(this._look)}offset(e=0,t=0){return this._ws.cell(this.row+e,this.column+t)}}export class ColumnDimension{index;_width;_hidden;_files;_level;_ws;constructor(e,t){this._ws=e,this.index=t;const s=e._spec()?.columns?.[t];return this._level=s?.level,this._width=s?.width,this._hidden=s?.hidden,this._files=s?.files,g(this,"ColumnDimension",w)}get outlineLevel(){return this._level}set outlineLevel(e){this._level=e,this._ws._dimensions=!0}get files(){return this._files}set files(e){this._files=e,this._ws._dimensions=!0}get width(){return this._width===void 0?void 0:Oe(this._width)}set width(e){this._width=e===void 0?void 0:Te(e),this._ws._dimensions=!0}get hidden(){return this._hidden??!1}set hidden(e){this._hidden=e,this._ws._dimensions=!0}}export class RowDimension{index;_height;_hidden;_level;_ws;constructor(e,t){this._ws=e,this.index=t;const s=e._spec()?.rows?.[String(t)];return this._level=s?.level,this._height=s?.height,this._hidden=s?.hidden,g(this,"RowDimension",w)}get outlineLevel(){return this._level}set outlineLevel(e){this._level=e,this._ws._dimensions=!0}get height(){return this._height===void 0?void 0:Ne(this._height)}set height(e){this._height=e===void 0?void 0:Le(e),this._ws._dimensions=!0}get hidden(){return this._hidden??!1}set hidden(e){this._hidden=e,this._ws._dimensions=!0}}export class Table{displayName;ref;key;_spec;autoFilter;constructor(e){for(const t of Object.keys(e))if(!["displayName","ref","key"].includes(t))throw new TypeError(`Table takes displayName, ref and key; it has no ${t}. ${w}`);if(!H(e.displayName))throw new TypeError(`${e.displayName} cannot be a table's name.`);if(!k(e.ref))throw new TypeError(`${e.ref} is not a range.`);return this.displayName=e.displayName,this.ref=e.ref,this.key=e.key,this.autoFilter=new K(e.ref,()=>{}),g(this,"Table",w)}get name(){return this.displayName}}export class TableList{_items=new Map;constructor(){return g(this,"TableList",w,{key:e=>this._items.get(e)})}add(e){this._items.set(e.name,e)}get(e){return this._items.get(e)}keys(){return[...this._items.keys()]}values(){return[...this._items.values()]}items(){return[...this._items.entries()]}pop(e){const t=this._items.get(e);return this._items.delete(e),t}}export class DefinedName{name;attr_text;constructor(e,t){if(!H(e))throw new TypeError(`${e} cannot be a name.`);return this.name=e,this.attr_text=t.attr_text.replace(/^=/u,""),g(this,"DefinedName",w)}get value(){return this.attr_text}set value(e){this.attr_text=e.replace(/^=/u,"")}}export class DefinedNameDict{_items=new Map;constructor(){return g(this,"DefinedNameDict",w,{key:e=>this._items.get(e),setKey:(e,t)=>t instanceof DefinedName?(this._items.set(e,t),!0):!1})}add(e){this._items.set(e.name,e)}get(e){return this._items.get(e)}keys(){return[...this._items.keys()]}values(){return[...this._items.values()]}items(){return[...this._items.entries()]}pop(e){const t=this._items.get(e);return this._items.delete(e),t}}const v=l=>{if(l._formula!==void 0)return l._formula;const e=l._value;return typeof e=="number"&&S(l._look.format)?le(e):he(e)};function U(l){if(l._formula!==void 0)return l._formula;const e=l._value;return e===null?null:typeof e=="number"&&!S(l._look.format)||typeof e=="boolean"?e:v(l)}const Me=l=>l.toLowerCase().replace(/[^\p{L}\p{N}]+/gu,"-").replace(/^-+|-+$/gu,"")||"rows";export class Worksheet{_parent;_title;_was;_model;_cells=new Map;_columns=new Map;_rows=new Map;_tables=new TableList;_freeze;_merges=[];_state="visible";_dimensions=!1;_lastRow=0;_rules=[];_validations=[];_filter;_charts=[];_print={};_links={};_notes={};_pictures=[];_protect=!1;constructor(e,t,s){if(this._parent=e,this._title=t,this._filter=new K(void 0,()=>{this._parent._pending=!0}),s){this._model=s,this._was=t,this._freeze=s.spec.freeze,this._heldPage(s.spec),this._merges=[...s.spec.merges??[]],this._state=s.spec.hidden===!0?"hidden":"visible",this._lastRow=(s.used?.bottom??-1)+1,this._heldRules(s.spec);for(const n of s.tables){const o=new Table({displayName:n.name,ref:x(n.area),...n.spec.key===void 0?{}:{key:n.spec.key}});o._spec=n.spec;const{filter:r}=n.spec;if(r)for(const[a,f]of Object.entries(r.columns)){const c=n.column(a);c&&o.autoFilter._columns.set(c.index,f)}this._tables.add(o)}}return g(this,"Worksheet",w,{index:n=>this._rowCells(n),key:n=>this._at(n),setKey:(n,o)=>{const r=y(n);return r?(this.cell(r.row+1,r.col+1).value=o,!0):!1}})}_spec(){return this._parent._file.sheets.find(e=>e.name===(this._was??this._title))}_heldRules(e){this._rules=[...e?.conditional??[]],this._validations=(e?.validations??[]).map(ge),this._charts=(e?.charts??[]).map(me),this._filter.ref=e?.filter?.range,this._filter._columns.clear();const t=e?.filter?k(e.filter.range):void 0;for(const[s,n]of Object.entries(e?.filter?.columns??{})){const o=E(s);t&&o!==void 0&&this._filter._columns.set(o-t.left,n)}}_at(e){const t=y(e);if(t)return this.cell(t.row+1,t.col+1);const s=k(e);if(s)return this._area(s);const n=E(e);if(n!==void 0){const o=[];for(let r=1;r<=this.max_row;r++)o.push(this.cell(r,n+1));return o}}_rowCells(e){const t=[];for(let s=1;s<=this.max_column;s++)t.push(this.cell(e,s));return t}_area(e){const t=[];for(let s=e.top;s<=e.bottom;s++){const n=[];for(let o=e.left;o<=e.right;o++)n.push(this.cell(s+1,o+1));t.push(n)}return t}get parent(){return this._parent}get title(){return this._title}set title(e){const t=this._title;e!==t&&this._parent._renameSheet(this,t,e)}get sheet_state(){return this._state}set sheet_state(e){this._state=e}cell(e,t,s){if(e<1||t<1)throw new RangeError(`Row and column values must be at least 1: ${String(e)}, ${String(t)}.`);const n=(e-1)*16384+t-1;let o=this._cells.get(n);return o||(o=new Cell(this,e,t),this._cells.set(n,o)),s!==void 0&&(o.value=s),e>this._lastRow&&(this._lastRow=e),o}append(e){const t=this._lastRow+1,s=(n,o)=>{o!=null&&this.cell(t,n,o)};if(Array.isArray(e))e.forEach((n,o)=>{s(o+1,n)});else for(const[n,o]of Object.entries(e)){const r=/^\d+$/u.test(n)?Number(n)-1:E(n);if(r===void 0)throw new TypeError(`${n} is not a column.`);s(r+1,o)}this._lastRow=t}get max_row(){return Math.max(1,this._lastRow)}get max_column(){let e=this._model?.columnsUsed??0;for(const t of this._cells.values())(t._value!==null||t._formula!==void 0)&&t.column>e&&(e=t.column);return Math.max(1,e)}min_row=1;min_column=1;get dimensions(){return x({top:0,left:0,bottom:this.max_row-1,right:this.max_column-1})}iter_rows(e={}){const t=this._area({top:(e.min_row??1)-1,left:(e.min_col??1)-1,bottom:(e.max_row??this.max_row)-1,right:(e.max_col??this.max_column)-1});return e.values_only===!0?t.map(s=>s.map(n=>n.value)):t}iter_cols(e={}){const t=this.iter_rows(e);return(t[0]??[]).map((n,o)=>t.map(r=>r[o]))}get rows(){return this.iter_rows()}get columns(){return this.iter_cols()}get values(){return this.iter_rows({values_only:!0})}insert_rows(e,t=1){this._parent._restructure({op:"insert-rows",sheet:this._title,at:b(e,"row")-1,count:b(t,"amount")})}delete_rows(e,t=1){this._parent._restructure({op:"delete-rows",sheet:this._title,at:b(e,"row")-1,count:b(t,"amount")})}insert_cols(e,t=1){this._parent._restructure({op:"insert-columns",sheet:this._title,at:b(e,"column")-1,count:b(t,"amount")})}delete_cols(e,t=1){this._parent._restructure({op:"delete-columns",sheet:this._title,at:b(e,"column")-1,count:b(t,"amount")})}merge_cells(e){if(!k(e))throw new TypeError(`${e} is not a range.`);this._merges.includes(e)||this._merges.push(e),this._parent._pending=!0}unmerge_cells(e){this._merges=this._merges.filter(t=>t!==e),this._parent._pending=!0}get merged_cells(){return[...this._merges]}get freeze_panes(){return this._freeze}set freeze_panes(e){const t=e instanceof Cell?e.coordinate:e;if(t!==void 0&&!y(t))throw new TypeError(`${t} is not a cell.`);this._freeze=t,this._parent._pending=!0}get column_dimensions(){return new Proxy({},{get:(e,t)=>{if(typeof t!="string")return;const s=t.toUpperCase();if(E(s)===void 0)throw new TypeError(`${t} is not a column.`);let n=this._columns.get(s);return n||(n=new ColumnDimension(this,s),this._columns.set(s,n)),n}})}get row_dimensions(){return new Proxy({},{get:(e,t)=>{if(typeof t!="string"||!/^[1-9]\d*$/u.test(t))throw new TypeError(`${String(t)} is not a row.`);const s=Number(t);let n=this._rows.get(s);return n||(n=new RowDimension(this,s),this._rows.set(s,n)),n}})}get tables(){return this._tables}get conditional_formatting(){return new pe(this._rules,()=>{this._parent._pending=!0})}get data_validations(){return new we(this._validations,()=>{this._parent._pending=!0})}add_data_validation(e){this._validations.push(e),this._parent._pending=!0}get auto_filter(){return this._filter}_changed(){this._parent._pending=!0}_heldPage(e){this._print=structuredClone(e?.print??{}),this._links={...e?.links},this._notes={...e?.notes},this._pictures=[...e?.pictures??[]],this._protect=e?.protect===!0}get page_setup(){return new Y(this)}get page_margins(){return new V(this)}get oddHeader(){return new A(this,"header")}get oddFooter(){return new A(this,"footer")}get protection(){return new q(()=>this._protect,e=>{this._protect=e,this._changed()})}get print_area(){return this._print.area}set print_area(e){if(e===void 0)delete this._print.area;else if(k(e))this._print.area=e;else throw new TypeError(`${e} is not a range.`);this._changed()}get print_title_rows(){return this._print.title_rows}set print_title_rows(e){e===void 0?delete this._print.title_rows:this._print.title_rows=e,this._changed()}get print_title_cols(){return this._print.title_cols}set print_title_cols(e){e===void 0?delete this._print.title_cols:this._print.title_cols=e,this._changed()}add_image(e,t){const s=t??e.anchor??"A1";if(!y(s))throw new TypeError(`${s} is not a cell.`);this._pictures.push({file:e.path,at:s,...e.width===void 0?{}:{width:e.width},...e.height===void 0?{}:{height:e.height}}),this._changed()}add_chart(e,t){e.anchor=de(t)??e.anchor,this._charts.push(e),this._parent._pending=!0}add_table(e){if(this._parent._tableNamed(e.name))throw new Error(`There is already a table named ${e.name}.`);this._tables.add(e),this._parent._pending=!0}}export class Workbook{worksheets=[];defined_names=new DefinedNameDict;data_only;title;path;block_cells=BLOCK_CELLS;_file={sheets:[]};_text="";_disk={text:"",csv:new Map};_csv=new Map;_model;_names=new Map;_active=0;_pending=!1;_making=!1;_calc=void 0;constructor(e={}){this.data_only=e.data_only??!1;const t=g(this,"Workbook",w,{key:s=>this.worksheets.find(n=>n.title===s),index:s=>this.worksheets[s]});return t.worksheets=[new Worksheet(t,"Sheet")],t}static open(e,t={}){const s=new Workbook(t);return s.path=e,s._read(e),s}_read(e){const{readFileSync:t}=J(),s=t($(e,P),"utf8"),n=new Map,o=r=>{try{const a=t($(e,r),"utf8");return n.set(r,a),a}catch{return}};this._load(s,o);for(const[r,a]of n)this._keepCsv(r,a);this._disk={text:s,csv:n}}_keepCsv(e,t){const s=Z(t);this._csv.set(e,{parsed:s,rows:s.records.map(n=>n.end===n.start?[]:[...n.fields])})}_load(e,t){const{workbook:s,problems:n}=I({text:e,read:t});if(!s)throw new Error(`sheet.yaml cannot be opened: ${n.map(o=>o.message).join("; ")}`);this._text=e,this._file=structuredClone(s.file),this._model=s,this.title=s.title,this.worksheets=s.sheets.map(o=>new Worksheet(this,o.name,o));for(const o of s.names.values()){if(o.source.path?.[0]!=="names")continue;const r=o.formula?o.formula.text:o.value??null;this._names.set(o.name,r),this.defined_names.add(new DefinedName(o.name,{attr_text:String(r)}))}}get active(){const e=this.worksheets[this._active]??this.worksheets[0];if(!e)throw new Error("The workbook has no sheets.");return e}set active(e){this._active=this.worksheets.indexOf(e)}get sheetnames(){return this.worksheets.map(e=>e.title)}get_sheet_by_name(e){return this.worksheets.find(t=>t.title===e)}index(e){return this.worksheets.indexOf(e)}create_sheet(e,t){let s=e??"Sheet";for(let o=1;this.worksheets.some(r=>r.title===s);o++)s=`${e??"Sheet"}${String(o)}`;const n=new Worksheet(this,s);return this.worksheets.splice(t??this.worksheets.length,0,n),this._pending=!0,n}remove(e){this.worksheets=this.worksheets.filter(t=>t!==e),this._pending=!0}move_sheet(e,t=0){const s=this.worksheets.indexOf(e);this.worksheets.splice(s,1),this.worksheets.splice(s+t,0,e),this._pending=!0}_tableNamed(e){for(const t of this.worksheets){const s=t._tables.get(e);if(s)return s}}_renameSheet(e,t,s){if(this.worksheets.some(o=>o!==e&&o.title===s))throw new Error(`There is already a sheet named ${s}.`);this._apply(),this._rewrite({op:"rename-sheet",from:t,to:s}),e._title=s,e._was=s;const n=this._file.sheets.find(o=>o.name===t);n&&(n.name=s),this._apply()}_restructure(e){this._apply();const t=_e({file:this._file,rows:new Map([...this._csv].map(([n,o])=>[n,o.rows]))},e);if(t.refused!==void 0)throw new Error(t.refused==="table-header"?"A table's header row is not deleted: delete the table's rows, or all of it.":"That sheet is not there.");this._file=t.file;for(const[n,o]of t.rows){const r=this._csv.get(n);r&&(r.rows=o.map(a=>[...a]))}for(const n of this.worksheets){const o=this._file.sheets.find(r=>r.name===n._title);n._cells.clear(),n._columns.clear(),n._rows.clear(),n._merges=[...o?.merges??[]],n._freeze=o?.freeze,n._heldPage(o),n._heldRules(o)}const s=this._build();this._rebase(s.text,s.files);for(const n of this.worksheets){const o=this._model?.sheet(n._title);if(o){n._lastRow=(o.used?.bottom??-1)+1;for(const r of o.tables){const a=n._tables.get(r.name);a&&(a.ref=x(r.area))}}}}_rewrite(e){const t=(n,o)=>{const r=F(n);if(!r.ok)return n;const a=oe(r.node,e,o);return a===r.node?n:B(a)},{_file:s}=this;if(s.names){for(const[n,o]of Object.entries(s.names))if(typeof o=="string"&&o.startsWith("=")){const r=t(o,s.sheets[0]?.name??"");s.names[n]=r,this._names.set(n,r);const a=this.defined_names.get(n);a&&(a.attr_text=r.slice(1))}}for(const n of s.checks??[])n.formula=t(n.formula,s.sheets[0]?.name??"");for(const n of s.sheets){for(const[o,r]of Object.entries(n.cells??{}))typeof r=="string"&&r.startsWith("=")?n.cells[o]=t(r,n.name):r!==null&&typeof r=="object"&&r.formula!==void 0&&(r.formula=t(r.formula,n.name));for(const o of n.tables??[]){for(const r of Object.values(o.columns??{}))r.formula!==void 0&&(r.formula=t(r.formula,n.name));this._rewriteCsv(o.rows,n.name,t)}for(const o of n.blocks??[])this._rewriteCsv(o.rows,n.name,t)}}_rewriteCsv(e,t,s){const n=this._csv.get(e);n&&(n.rows=n.rows.map(o=>o.map(r=>M(r).formula!==void 0?s(r,t):r)))}_sheetEntry(e){const t=e._was??e._title;let s=this._file.sheets.find(n=>n.name===t);return s||(s={name:e._title},this._file.sheets.push(s)),s.name=e._title,s}_dataPath(e){const t=`${G}/${Me(e)}`;let s=`${t}.csv`;const n=o=>this._csv.has(o)||this._file.sheets.some(r=>r.tables?.some(a=>a.rows===o)??r.blocks?.some(a=>a.rows===o));for(let o=2;n(s);o++)s=`${t}-${String(o)}.csv`;return s}_writeNewTable(e,t,s){const n=k(s.ref);if(!n)throw new Error(`${s.ref} is not a range.`);const o=[];for(let i=n.left;i<=n.right;i++){const h=e.cell(n.top+1,i+1).value;if(h===null||h==="")throw new Error(`Table ${s.name}'s header has no name at ${C({row:n.top,col:i})}.`);o.push(String(h))}const r={},a=[];o.forEach((i,h)=>{const _=n.left+h,d=this._columnFormula(e,n,_);d===void 0?a.push(h):r[i]={formula:d};const p=this._columnFormat(e,n,_);p!==void 0&&(r[i]={...r[i],format:p})}),this._columnsStyled(e,n);const f=[a.map(i=>o[i]??"")];for(let i=n.top+1;i<=n.bottom;i++)f.push(a.map(h=>v(e.cell(i+1,n.left+h+1))));t.cells&&(t.cells=Object.fromEntries(Object.entries(t.cells).filter(([i])=>{const h=y(i);return!h||!N(n,h.row,h.col)})));const c=this._dataPath(s.name);this._csv.set(c,{parsed:void 0,rows:f});const u={name:s.name,at:C({row:n.top,col:n.left}),rows:c,...s.key===void 0?{}:{key:s.key},...m(r)?{}:{columns:r}};(t.tables??=[]).push(u),s._spec=u}_columnsStyled(e,t){for(let s=t.left;s<=t.right;s++)for(let n=t.top+1;n<=t.bottom;n++){const o=e.cell(n+1,s+1),{format:r,...a}=this._ownLook(o);r!==void 0&&m(a)&&(o._styled=!1)}}_columnFormula(e,t,s){if(t.bottom<=t.top)return;const n=e.cell(t.top+2,s+1),o=R(n);if(o!==void 0){for(let r=t.top+2;r<=t.bottom;r++)if(R(e.cell(r+1,s+1))!==o)return;return n._formula}}_columnFormat(e,t,s){if(t.bottom<=t.top)return;const n=r=>this._ownLook(e.cell(r+1,s+1)).format,o=n(t.top+1);for(let r=t.top+2;r<=t.bottom;r++)if(n(r)!==o)return;return o}_held(e){const t=this._csv.get(e);if(!t)throw new Error(`${e} was not read.`);return t}_setCsvCell(e,t,s,n){const{rows:o}=this._held(e);for(;o.length<=t;)o.push([]);const r=o[t];if(r){for(;r.length<=s;)r.push("");r[s]=n}}_recordOf(e,t){let s=0;for(let n=0;n<t;){if(s+=1,s>=e.length)return s+t-n-1;e[s]?.length!==0&&(n+=1)}return s}_writeTableCell(e,t,s){const n=this._held(e.rows),o=n.rows[0]??[],r=s.column-1-t.left,a=this._recordOf(n.rows,s.row-1-t.top),f=Object.keys(e.columns??{}).filter(_=>!o.some(d=>d.trim().toLowerCase()===_.toLowerCase())),u=[...o.map(_=>_.trim()),...f][r];if(u===void 0)return;if(a===0){r<o.length&&(o[r]=v(s));return}const i=e.columns?.[u]?.formula;if(i!==void 0){const _=this._movedColumn(i,t,s);if(s._formula===_){const d=o.findIndex(p=>p.trim()===u);d>=0&&this._setCsvCell(e.rows,a,d,"");return}}let h=o.findIndex(_=>_.trim()===u);h<0&&(h=o.length,this._setCsvCell(e.rows,0,h,u)),this._setCsvCell(e.rows,a,h,v(s))}_movedColumn(e,t,s){const n=F(e);if(!n.ok)return e;const o=s.row-1-(t.top+1);return o===0?e:B(se(n.node,o,0))}_apply(){this.title===void 0?delete this._file.title:this._file.title=this.title;const e=new Set(this.worksheets.map(s=>s._was??s._title));this._file.sheets=this._file.sheets.filter(s=>e.has(s.name));for(const s of this.worksheets)this._applySheet(s);this._file.sheets=this.worksheets.map(s=>this._sheetEntry(s)),this._applyNames();const t=this._build();this._rebase(t.text,t.files)}_applyNames(){const e={};for(const t of this.defined_names.values()){const s=M(t.attr_text),n=this._names.get(t.name);e[t.name]=n!==void 0&&String(n).replace(/^=/u,"")===t.attr_text?n:typeof s.value=="number"||typeof s.value=="boolean"?s.value:/^"[^"]*"$/u.test(t.attr_text)?t.attr_text.slice(1,-1):`=${t.attr_text}`}m(e)?delete this._file.names:this._file.names=e}_applySheet(e){const t=this._sheetEntry(e);e._freeze===void 0?delete t.freeze:t.freeze=e._freeze,m(e._print)?delete t.print:t.print=e._print,m(e._links)?delete t.links:t.links=e._links,m(e._notes)?delete t.notes:t.notes=e._notes,e._pictures.length===0?delete t.pictures:t.pictures=e._pictures,e._protect?t.protect=!0:delete t.protect,e._merges.length===0?delete t.merges:t.merges=[...e._merges],e._state==="hidden"?t.hidden=!0:delete t.hidden,e._rules.length===0?delete t.conditional:t.conditional=[...e._rules],e._validations.length===0?delete t.validations:t.validations=e._validations.map(ke),e._charts.length===0?delete t.charts:t.charts=e._charts.map(n=>n._spec(e._title)),this._applyFilter(e,t);const s=new Set(t.tables?.map(n=>n.name.toLowerCase()));for(const n of e._tables.values())s.has(n.name.toLowerCase())||this._writeNewTable(e,t,n);this._applyTableFilters(e,t),this._applyCells(e,t),this._applyDimensions(e,t),t.cells&&m(t.cells)&&delete t.cells,t.columns&&m(t.columns)&&delete t.columns,t.rows&&m(t.rows)&&delete t.rows,t.format?.length===0&&delete t.format,t.tables?.length===0&&delete t.tables,t.blocks?.length===0&&delete t.blocks}_applyFilter(e,t){const{ref:s,_columns:n}=e._filter;if(s===void 0){delete t.filter;return}const o=k(s);if(!o)throw new TypeError(`${s} is not a range.`);const r={};for(const[a,f]of n)r[L(o.left+a)]=f;t.filter={range:s,...m(r)?{}:{columns:r}}}_applyTableFilters(e,t){for(const s of t.tables??[]){const n=e._tables.get(s.name);if(!n||n.autoFilter._columns.size===0)continue;const o=this._csv.get(s.rows)?.rows??[],r=ce(s,o)?.names??[],a={};for(const[f,c]of n.autoFilter._columns){const u=r[f];if(u===void 0)throw new RangeError(`Table ${s.name} has no column at ${String(f)}.`);a[u]=c}s.filter={columns:a}}}_applyDimensions(e,t){if(e._dimensions){for(const[s,n]of e._columns){const o={...t.columns?.[s]};n._width===void 0?delete o.width:o.width=n._width,n._hidden===void 0?delete o.hidden:o.hidden=n._hidden,n._files===void 0?delete o.files:o.files=n._files,n._level===void 0?delete o.level:o.level=n._level;const r=j(t.columns??{},s);t.columns=m(o)?r:{...r,[s]:o}}for(const[s,n]of e._rows){const o={...t.rows?.[String(s)]};n._height===void 0?delete o.height:o.height=n._height,n._hidden===void 0?delete o.hidden:o.hidden=n._hidden,n._level===void 0?delete o.level:o.level=n._level;const r=j(t.rows??{},String(s));t.rows=m(o)?r:{...r,[String(s)]:o}}e._dimensions=!1}}_applyCells(e,t){const s=(t.tables??[]).flatMap(r=>{const a=y(r.at),f=e._tables.get(r.name),c=f?k(f.ref):void 0;return a&&c?[{entry:r,area:c}]:[]}),n=(t.blocks??[]).flatMap(r=>{const a=e._model?.blocks.find(f=>f.file===r.rows);return a?[{entry:r,area:a.area}]:[]}),o=[];for(const r of e._cells.values()){if(!r._changed&&!r._styled)continue;const a=r.row-1,f=r.column-1,c=s.find(i=>N(i.area,a,f)),u=n.find(i=>N(i.area,a,f));c?(r._changed&&this._writeTableCell(c.entry,c.area,r),r._styled&&this._styleByFormat(t,r)):u?(r._changed&&this._setCsvCell(u.entry.rows,a-u.area.top,f-u.area.left,v(r)),r._styled&&this._styleByFormat(t,r)):r._fresh&&!e._model?.keys.has(a*16384+f)?o.push(r):this._writeLoose(e,t,r),r._changed=!1,r._styled=!1}this._consolidate(e,t,o),je(t)}_styleByFormat(e,t){(e.format??=[]).push({range:t.coordinate,...this._ownLook(t)})}_ownLook(e){const t=e._ws._model?.lookAt(e.row-1,e.column-1)??{},s=Fe(t,e._look);return s.format!==void 0&&e._formula===void 0&&M(v(e)).format===s.format&&delete s.format,s}_writeLoose(e,t,s){const n=s.coordinate,o=t.cells??={},r=U(s),a=this._ownLook(s),f=o[n],u={...f!==null&&typeof f=="object"?Object.fromEntries(Object.entries(f).filter(([_])=>_!=="value"&&_!=="formula"&&_!=="name")):{},...a},i=f!==null&&typeof f=="object"&&f.name!==void 0?{name:f.name}:{},h=e._model?.at(s.row-1,s.column-1)!==void 0;if(r===null&&m(u)&&!h){t.cells=j(o,n);return}o[n]=m(u)&&m(i)?r:{...typeof r=="string"&&r.startsWith("=")?{formula:r}:r===null?{}:{value:r},...i,...u}}_consolidate(e,t,s){const n=s.filter(i=>i._value!==null||i._formula!==void 0),o=new Map;for(const i of s){const h=this._ownLook(i);if(m(h))continue;const _=JSON.stringify(h),d=o.get(_);d?d.push(i):o.set(_,[i])}const r=new Map(n.map(i=>[`${String(i.row)}:${String(i.column)}`,i])),a=i=>r.get(`${String(i.row+1)}:${String(i.col+1)}`)??e.cell(i.row+1,i.col+1),f=new Map,c=[];for(const i of n){const h={row:i.row-1,col:i.column-1},_=i._formula===void 0?void 0:R(i);if(_===void 0){c.push(h);continue}const d=f.get(_);d?d.push(h):f.set(_,[h])}const u=t.cells??={};for(const i of f.values())for(const h of T(i))u[x(h)]=a({row:h.top,col:h.left})._formula??null;for(const i of T(c))if((i.bottom-i.top+1)*(i.right-i.left+1)>=this.block_cells){const _=[];for(let p=i.top;p<=i.bottom;p++){const D=[];for(let O=i.left;O<=i.right;O++)D.push(v(a({row:p,col:O})));_.push(D)}const d=this._dataPath(e._title);this._csv.set(d,{parsed:void 0,rows:_}),(t.blocks??=[]).push({at:C({row:i.top,col:i.left}),rows:d})}else for(let _=i.top;_<=i.bottom;_++)for(let d=i.left;d<=i.right;d++){const p=a({row:_,col:d});u[p.coordinate]=U(p)}for(const i of o.values()){const[h]=i;if(!h)continue;const _=this._ownLook(h),d=i.map(p=>({row:p.row-1,col:p.column-1}));for(const p of T(d))(t.format??=[]).push({range:x(p),..._})}}_build(){const e=new Map;for(const[t,s]of this._csv)e.set(t,s.parsed?re(s.parsed,s.rows):ee(s.rows));return{text:ue(this._text,this._file),files:e}}_rebase(e,t){const{workbook:s,problems:n}=I({text:e,read:o=>t.get(o)});if(!s)throw new Error(`The workbook is not one: ${n.map(o=>o.message).join("; ")}`);this._text=e,this._file=structuredClone(s.file),this._model=s;for(const[o,r]of t)this._keepCsv(o,r);this._calc=void 0,this._pending=!1;for(const o of this.worksheets){o._was=o._title;const r=s.sheet(o._title);r&&(o._model=r);for(const a of o._cells.values())a._fresh=r?.at(a.row-1,a.column-1)===void 0}this._names=new Map;for(const o of s.names.values())o.source.path?.[0]==="names"&&this._names.set(o.name,o.formula?o.formula.text:o.value??null)}_computed(){const e=[...this.defined_names.values()];if((this._pending||e.length!==this._names.size||e.some(t=>String(this._names.get(t.name)).replace(/^=/u,"")!==t.attr_text))&&this._apply(),!this._calc){const t=this._model;if(!t)throw new Error("The workbook has no sheets.");this._calc={calc:new Q(t).calculate(),model:t}}return this._calc}check(){this._apply();const{text:e,files:t}=this._build();return X({text:e,read:s=>t.get(s)}).problems}_generated(){if(this.worksheets.length===0)return this._applyNames(),{sheets:[],csv:{},...this._file.names?{names:this._file.names}:{}};this._apply();const e=structuredClone(this._file.sheets),t=n=>`${te}${n}`;for(const n of e){for(const o of n.tables??[])o.rows=t(o.rows);for(const o of n.blocks??[])o.rows=t(o.rows)}const s={};for(const[n,o]of this._build().files)s[t(n)]=o;return{sheets:e,csv:s,...this._file.names?{names:this._file.names}:{}}}save(e){if(this._making)throw new Error("sheet.ts makes sheets and saves nothing: what it adds to wb is what it makes.");const t=e??this.path;if(t===void 0)throw new TypeError("A new workbook is saved to a folder: wb.save('content/budget').");if(this.worksheets.length===0)throw new Error("A workbook has at least one sheet.");const{text:s,csv:n}=this._disk;this._apply();const o=new Set(this._file.sheets.flatMap(i=>[...(i.tables??[]).map(h=>h.rows),...(i.blocks??[]).map(h=>h.rows)])),r=[...this._csv.keys()].filter(i=>!o.has(i));for(const i of r)this._csv.delete(i);const{mkdirSync:a,rmSync:f,writeFileSync:c}=J(),u=process.getBuiltinModule("node:path");a(t,{recursive:!0}),(this._text!==s||e!==void 0)&&c($(t,P),this._text);for(const[i,h]of this._csv){const _=h.parsed?.source??"";if(e===void 0&&n.get(i)===_)continue;const d=$(t,i);a(u.dirname(d),{recursive:!0}),c(d,_)}if(e===void 0||t===this.path)for(const i of r)f($(t,i),{force:!0});this._disk={text:this._text,csv:new Map([...this._csv].map(([i,h])=>[i,h.parsed?.source??""]))},this.path=t}}function R(l){if(l._formula===void 0)return;const e=F(l._formula);if(e.ok)return ne(e.node,l.row-1,l.column-1)}function je(l){const e=l.format;if(!e||e.length<2)return;const t=[],s=new Map;for(const n of e){const{range:o,...r}=n,a=y(o);if(!a){t.push(n);continue}const f=JSON.stringify(r),c=s.get(f);c?c.places.push(a):s.set(f,{look:r,places:[a]})}for(const{look:n,places:o}of s.values())for(const r of T(o))t.push({range:x(r),...n});l.format=t}export function load_workbook(l,e={}){return Workbook.open(l,e)}export const get_column_letter=l=>L(l-1);export function column_index_from_string(l){const e=E(l);if(e===void 0)throw new TypeError(`${l} is not a column.`);return e+1}export function coordinate_to_tuple(l){const e=y(l);if(!e)throw new TypeError(`${l} is not a cell.`);return[e.row+1,e.col+1]}export function range_boundaries(l){const e=k(l);if(!e)throw new TypeError(`${l} is not a range.`);return[e.left+1,e.top+1,e.right+1,e.bottom+1]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RefNode, StructuredNode } from "../formula/ast.js";
|
|
2
|
-
import type { SharedFormula, Workbook } from "../workbook.js";
|
|
2
|
+
import type { SharedFormula, Workbook, Worksheet } from "../workbook.js";
|
|
3
3
|
export interface Analysis {
|
|
4
4
|
reads: (RefNode | StructuredNode)[];
|
|
5
5
|
/** Defined names, in lower case. */
|
|
@@ -9,13 +9,19 @@ export interface Analysis {
|
|
|
9
9
|
unknownNames: string[];
|
|
10
10
|
/** Functions that are nothing here. */
|
|
11
11
|
unknownFunctions: string[];
|
|
12
|
-
/** Functions given too few or too many arguments. */
|
|
12
|
+
/** Functions given too few or too many arguments, or a repeating group of them left short (`group`, its size). */
|
|
13
13
|
arguments: {
|
|
14
14
|
name: string;
|
|
15
15
|
given: number;
|
|
16
16
|
min: number;
|
|
17
17
|
max: number;
|
|
18
|
+
group?: number;
|
|
18
19
|
}[];
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
+
/**
|
|
22
|
+
* A formula's analysis. `sheet`: the sheet it is on, whose own names come
|
|
23
|
+
* before the workbook's; without it every sheet's name of a spelling counts,
|
|
24
|
+
* so what a formula can read is never less than it reads.
|
|
25
|
+
*/
|
|
26
|
+
export declare function analyse(wb: Workbook, shared: SharedFormula, sheet?: Worksheet): Analysis;
|
|
21
27
|
//# sourceMappingURL=analyse.d.ts.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{FUNCTIONS as
|
|
2
|
+
import{FUNCTIONS as p}from"./functions/index.js";import{fillsGroup as w}from"./functions/define.js";export function analyse(i,c,l){const a={reads:[],names:new Set,volatile:!1,unknownNames:[],unknownFunctions:[],arguments:[]},d=new Set,g=(e,t,n)=>{const r=e.toLowerCase();if(n===void 0&&t.has(r))return!0;const u=n===void 0?l:i.sheet(n),o=u?[i.name(r,u)]:i.definitions(r);if(o.some(m=>m!==void 0)){a.names.add(r);const m=`${n??""}!${r}`;if(!d.has(m)){d.add(m);for(const h of o)h?.formula?.node&&s(h.formula.node,new Set)}return!0}if(p.has(e.toUpperCase()))return!0;const f=i.table(r);return f?(a.reads.push({kind:"structured",table:f.name,sections:[],columns:[],thisRow:!1}),!0):!1},s=(e,t)=>{switch(e.kind){case"ref":case"structured":a.reads.push(e);return;case"name":g(e.name,t,e.sheet)||a.unknownNames.push(e.name);return;case"unary":case"postfix":case"paren":s(e.arg,t);return;case"binary":s(e.left,t),s(e.right,t);return;case"array":return;case"invoke":s(e.callee,t);for(const n of e.args)s(n,t);return;case"call":{const n=p.get(e.name);if(e.name==="LET"||e.name==="LAMBDA"){const r=new Set(t),u=e.args[e.args.length-1];e.args.forEach((o,f)=>{if(o!==u){if(e.name==="LET"&&f%2===1){s(o,r);return}o.kind==="name"&&r.add(o.name.toLowerCase())}}),u&&s(u,r);return}if(!n)g(e.written,t)||a.unknownFunctions.push(e.name);else{n.volatile===!0&&(a.volatile=!0);const r=n.kind==="raw"&&!w(n.params,e.args.length);(e.args.length<n.min||e.args.length>n.max||r)&&a.arguments.push({name:e.name,given:e.args.length,min:n.min,max:n.max,...r?{group:n.params.group.length}:{}})}for(const r of e.args)s(r,t);return}default:return}};return c.node&&s(c.node,new Set),a}
|
|
@@ -4,7 +4,11 @@ export declare function numberOfText(text: string): number | undefined;
|
|
|
4
4
|
export declare function toNumber(v: Scalar | undefined): number | CellError;
|
|
5
5
|
export declare function toText(v: Scalar | undefined): string | CellError;
|
|
6
6
|
export declare function toBool(v: Scalar | undefined): boolean | CellError;
|
|
7
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Equal to 15 significant digits, as Excel compares numbers. Two numbers that
|
|
9
|
+
* round to the same 15 digits are within 1e-13 of each other relatively, so
|
|
10
|
+
* any further apart are told apart without rounding either.
|
|
11
|
+
*/
|
|
8
12
|
export declare const sameNumber: (a: number, b: number) => boolean;
|
|
9
13
|
/** Excel's order of two values that are not errors: numbers, then text, then TRUE and FALSE. */
|
|
10
14
|
export declare function compare(a: Scalar, b: Scalar): number;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{parseValue as d}from"numfmt";import{CellError as f,ERR as s,Matrix as x,numberText as h,readTyped as
|
|
2
|
+
import{parseValue as d}from"numfmt";import{CellError as f,ERR as s,Matrix as x,numberText as h,readTyped as M}from"../values.js";export function numberOfText(e){if(!/\d/u.test(e))return;const r=M(e.trim());if(typeof r.value=="number")return r.value;if(e.trim()==="")return;const t=d(e);return typeof t?.value=="number"?t.value:void 0}export function toNumber(e){return typeof e=="number"?e:e==null?0:typeof e=="boolean"?e?1:0:e instanceof f?e:numberOfText(e)??s.VALUE}export function toText(e){return typeof e=="string"?e:e==null?"":typeof e=="number"?h(e):typeof e=="boolean"?e?"TRUE":"FALSE":e}export function toBool(e){if(typeof e=="boolean")return e;if(e==null)return!1;if(typeof e=="number")return e!==0;if(e instanceof f)return e;const r=e.trim().toUpperCase();return r==="TRUE"?!0:r==="FALSE"?!1:s.VALUE}const m=e=>typeof e=="number"?0:typeof e=="string"?1:2;export const sameNumber=(e,r)=>e===r?!0:Math.abs(e-r)>1e-13*Math.max(Math.abs(e),Math.abs(r))?!1:Number(e.toPrecision(15))===Number(r.toPrecision(15));export function compare(e,r){const t=e??(typeof r=="string"?"":typeof r=="boolean"?!1:0),n=r??(typeof e=="string"?"":typeof e=="boolean"?!1:0),o=m(t),c=m(n);if(o!==c)return o<c?-1:1;if(typeof t=="number"&&typeof n=="number")return sameNumber(t,n)?0:t<n?-1:1;if(typeof t=="string"&&typeof n=="string"){const a=t.toUpperCase(),u=n.toUpperCase();return a===u?0:a<u?-1:1}return t===n?0:t===!1?-1:1}export const finite=e=>Number.isFinite(e)?e:s.NUM;export function snap(e,r,t){return Math.abs(e)<=Math.max(Math.abs(r),Math.abs(t))*89e-17?0:e}export function arithmetic(e,r,t){const n=toNumber(r);if(n instanceof f)return n;const o=toNumber(t);if(o instanceof f)return o;switch(e){case"+":return finite(snap(n+o,n,o));case"-":return finite(snap(n-o,n,o));case"*":return finite(n*o);case"/":return o===0?s.DIV0:finite(n/o);default:return n===0&&o===0?s.NUM:n===0&&o<0?s.DIV0:finite(n**o)}}export function binary(e,r,t){if(r instanceof f)return r;if(t instanceof f)return t;switch(e){case"&":{const n=toText(r);if(n instanceof f)return n;const o=toText(t);return o instanceof f?o:`${n}${o}`}case"=":return compare(r,t)===0;case"<>":return compare(r,t)!==0;case"<":return compare(r,t)<0;case"<=":return compare(r,t)<=0;case">":return compare(r,t)>0;case">=":return compare(r,t)>=0;default:return arithmetic(e,r,t)}}export function mapBoth(e,r,t){const n=Math.max(e.rows,r.rows),o=Math.max(e.cols,r.cols),c=new Array(n*o),a=(u,i,l)=>{const p=u.rows===1?0:i,y=u.cols===1?0:l;return p<u.rows&&y<u.cols?u.at(p,y):void 0};for(let u=0;u<n;u++)for(let i=0;i<o;i++){const l=a(e,u,i),p=a(r,u,i);c[u*o+i]=l===void 0||p===void 0?s.NA:t(l,p)}return new x(n,o,c)}
|
|
@@ -1,12 +1,27 @@
|
|
|
1
1
|
import type { Node, RefNode, StructuredNode } from "../formula/ast.js";
|
|
2
2
|
import { CellError, Matrix, type Scalar } from "../values.js";
|
|
3
3
|
import { FormulaCell, SharedFormula, type Slot, type Workbook, type Worksheet } from "../workbook.js";
|
|
4
|
+
import { type Computed } from "./remote.js";
|
|
5
|
+
import { RangeCache, type Index } from "./indexes.js";
|
|
6
|
+
import { type FunctionDef } from "./functions/index.js";
|
|
4
7
|
import { Lambda, Reference, Union, type Frame, type Value } from "./types.js";
|
|
5
8
|
export interface CalcOptions {
|
|
6
9
|
/** The moment NOW and TODAY read. Default: the clock. */
|
|
7
10
|
now?: Date;
|
|
8
11
|
/** What RAND reads, values from 0 up to 1. Default: Math.random. */
|
|
9
12
|
random?: () => number;
|
|
13
|
+
/**
|
|
14
|
+
* Keeps what each pass settles for `takeComputed`: a calculation in a
|
|
15
|
+
* worker, which sends it to the model the page reads.
|
|
16
|
+
*/
|
|
17
|
+
report?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Computes nothing: a model the page reads while a calculation elsewhere
|
|
20
|
+
* works. What is typed shows at once; what formulas give arrives by
|
|
21
|
+
* `receive`, and a formula reads as it last did until then. A formula
|
|
22
|
+
* given to `evaluate` (a rule's, a validation's) is worked out over that.
|
|
23
|
+
*/
|
|
24
|
+
replica?: boolean;
|
|
10
25
|
}
|
|
11
26
|
/**
|
|
12
27
|
* Random numbers that are the same for the same seed, for a result that must
|
|
@@ -31,12 +46,15 @@ export declare class Calc {
|
|
|
31
46
|
private readonly stack;
|
|
32
47
|
private readonly inCycle;
|
|
33
48
|
private readonly namesAt;
|
|
34
|
-
|
|
35
|
-
private readonly
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
private readonly
|
|
39
|
-
|
|
49
|
+
/** Who reads whom: made the first time something changes. */
|
|
50
|
+
private readonly graph;
|
|
51
|
+
private graphMade;
|
|
52
|
+
/** Keeps what each pass settles, for `takeComputed`. */
|
|
53
|
+
private readonly reports;
|
|
54
|
+
/** Computes nothing; results arrive by `receive`. */
|
|
55
|
+
readonly replica: boolean;
|
|
56
|
+
/** What passes settled since `takeComputed` was last asked. */
|
|
57
|
+
private readonly settled;
|
|
40
58
|
/** What spills changed since it was last looked at. */
|
|
41
59
|
private moved;
|
|
42
60
|
/** Formula cells whose spill's size turned out to depend on itself. */
|
|
@@ -44,8 +62,10 @@ export declare class Calc {
|
|
|
44
62
|
private readonly hiddenBy;
|
|
45
63
|
/** What reads what no reference shows (OFFSET, INDIRECT) or changes by itself (NOW, RAND): computed with every change, as Excel's are. */
|
|
46
64
|
private volatiles;
|
|
47
|
-
/** Lookup indexes of ranges,
|
|
48
|
-
readonly indexes:
|
|
65
|
+
/** Lookup and criteria indexes of ranges, each kept until its range changes. */
|
|
66
|
+
readonly indexes: RangeCache<Index>;
|
|
67
|
+
/** Criteria functions' results over ranges, each kept until a range it read changes. */
|
|
68
|
+
readonly results: RangeCache<Scalar>;
|
|
49
69
|
readonly wb: Workbook;
|
|
50
70
|
constructor(wb: Workbook, o?: CalcOptions);
|
|
51
71
|
private allCells;
|
|
@@ -65,6 +85,8 @@ export declare class Calc {
|
|
|
65
85
|
/** What an empty place reads: nothing, or the value a formula spilled into it. */
|
|
66
86
|
private ghost;
|
|
67
87
|
private circular;
|
|
88
|
+
/** What was worked out over ranges that hold changed cells is worked out again. */
|
|
89
|
+
private forget;
|
|
68
90
|
private run;
|
|
69
91
|
/**
|
|
70
92
|
* What reads a spill's cells computed again until nothing moves: who reads
|
|
@@ -79,6 +101,8 @@ export declare class Calc {
|
|
|
79
101
|
private analysisOf;
|
|
80
102
|
/** What a formula cell reads, by what evaluating its references gives. */
|
|
81
103
|
private readsOf;
|
|
104
|
+
/** What a formula reads evaluated at a place, an entry a reference (each place of a union its own); undefined where one gives no place. */
|
|
105
|
+
private readsAt;
|
|
82
106
|
/** What a formula that is in no cell reads: a chart's, a rule's, a check's. */
|
|
83
107
|
readsOfFormula(shared: SharedFormula, sheet: Worksheet): Reference[];
|
|
84
108
|
/** `seeds`, and what they read that is to be computed, each after what it reads. */
|
|
@@ -103,6 +127,7 @@ export declare class Calc {
|
|
|
103
127
|
private bound;
|
|
104
128
|
resolveRef(n: RefNode, f: Frame): Reference | Union | CellError;
|
|
105
129
|
structured(n: StructuredNode, f: Frame): Reference | Union | CellError;
|
|
130
|
+
/** A name's value: a LET's or a LAMBDA's, else the sheet's own (`sheet` when it is written Sheet1!name), else the workbook's. */
|
|
106
131
|
private nameValue;
|
|
107
132
|
private intersect;
|
|
108
133
|
private unary;
|
|
@@ -115,17 +140,31 @@ export declare class Calc {
|
|
|
115
140
|
/** A LAMBDA given its arguments' values. */
|
|
116
141
|
apply(l: Lambda, args: readonly Value[]): Value;
|
|
117
142
|
private call;
|
|
118
|
-
/**
|
|
119
|
-
|
|
143
|
+
/**
|
|
144
|
+
* A function given its arguments' values: every function call goes through
|
|
145
|
+
* here, a function's own call of another (AGGREGATE's) too, so each is
|
|
146
|
+
* lifted over arrays as its parameters say.
|
|
147
|
+
*/
|
|
148
|
+
applyFunction(def: FunctionDef, values: readonly Value[], f: Frame): Value;
|
|
149
|
+
/**
|
|
150
|
+
* A function over its arguments, as Excel lifts one: where a single value
|
|
151
|
+
* goes and an array or a range of several cells is given, the function is
|
|
152
|
+
* applied over each of its cells and the results are an array. Arrays of
|
|
153
|
+
* different sizes are stretched along a side of one, and give #N/A past the
|
|
154
|
+
* end of a shorter one. Every argument taken as a single value is given as
|
|
155
|
+
* one (a Scalar, or Missing when left empty).
|
|
156
|
+
*/
|
|
157
|
+
private over;
|
|
158
|
+
/** One cell of a lifted function's array: what one call gave, as a single value. */
|
|
159
|
+
private element;
|
|
120
160
|
/** One scalar from a value: a range in a place for one gives the cell in the formula's row or column. */
|
|
121
161
|
scalar(v: Value, f: Frame): Scalar;
|
|
122
162
|
/** Lets NOW, TODAY, RAND and whatever else is volatile change on the next pass. */
|
|
123
163
|
recalculate(): this;
|
|
124
|
-
private key;
|
|
125
|
-
private addEdges;
|
|
126
|
-
private removeEdges;
|
|
127
164
|
/** The graph of who reads whom: made the first time something changes. */
|
|
128
165
|
private build;
|
|
166
|
+
private addEdges;
|
|
167
|
+
private removeEdges;
|
|
129
168
|
/** Who reads any place of an area. */
|
|
130
169
|
private readersIn;
|
|
131
170
|
/** Who reads a place. */
|
|
@@ -146,6 +185,14 @@ export declare class Calc {
|
|
|
146
185
|
change(cells: readonly CellChange[]): void;
|
|
147
186
|
/** Sets a defined name's constant (a scenario's) and computes what reads it. */
|
|
148
187
|
setName(name: string, value: Scalar): void;
|
|
188
|
+
/** What passes settled since this was last asked, for the model the page reads (`receive`). */
|
|
189
|
+
takeComputed(): Computed;
|
|
190
|
+
/**
|
|
191
|
+
* What a calculation elsewhere computed, put in this model: each formula
|
|
192
|
+
* cell's value and spill. A cell no longer a formula, or another formula
|
|
193
|
+
* now, is left as it is: what was typed there since is newer.
|
|
194
|
+
*/
|
|
195
|
+
receive(computed: Computed): void;
|
|
149
196
|
/** Numbers of a value, for code outside a formula. */
|
|
150
197
|
numberOf(v: Scalar): number | CellError;
|
|
151
198
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{MAX_COLUMNS as A,MAX_ROWS as M,overlaps as L}from"../address.js";import{CellError as m,ERR as f,Matrix as d,serialOfDate as U}from"../values.js";import{filteredRows as N}from"../filter.js";import{FormulaCell as b,SharedFormula as F,placeKey as _}from"../workbook.js";import{analyse as V}from"./analyse.js";import{binary as k,mapBoth as I,toNumber as T}from"./coerce.js";import{FUNCTIONS as B}from"./functions/index.js";import{Lambda as S,MISSING as O,Missing as x,Reference as u,Union as g}from"./types.js";export function seededRandom(h){let t=2166136261;for(let e=0;e<h.length;e++)t=Math.imul(t^h.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const $=new Set,W=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,D=2e3,z=32,R=12,G=(h,t)=>!h||!t?h===t:h.rows!==t.rows||h.cols!==t.cols?!1:h.data.every((e,s)=>e===t.data[s]),H=(h,t)=>h.row<t.row||h.row===t.row&&h.col<t.col;class P{cells=new Set;reference;constructor(t){this.reference=t}}export class Calc{cycles=[];computed=0;nowSerial;random;pass=0;stack=[];inCycle=new Set;namesAt=new Set;dependents=new Map;watches=new Map;watchesAt=new Map;wideWatches=[];graph=!1;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new Map;wb;constructor(t,e={}){this.wb=t;const{now:s,random:n}=e;this.nowSerial=()=>U(s??new Date),this.random=n??Math.random;for(const o of t.sheets)o.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??$}calculate(){this.hiddenBy.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.run(t),this.applyFilters(t),this}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(n=>n.columns.size===0))continue;const s=N(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>W.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const n=t.at(e,s);return n===void 0?this.ghost(t,e,s):typeof n!="object"||n instanceof m?n:(n.state===1?this.compute(n):n.state===2&&this.circular(n),n.value)}ghost(t,e,s){const n=t.spillAt(e,s);if(!n)return null;if(n.state===1)this.compute(n);else if(n.state===2)return this.circular(n),null;const{array:o}=n;return o&&t.spillAt(e,s)===n?o.at(e-n.row,s-n.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);s.some(n=>this.inCycle.has(n))||this.cycles.push(s);for(const n of s)this.inCycle.add(n)}run(t){this.indexes.clear(),this.computed=0;for(const e of this.order(t))e.state===1&&this.compute(e);this.spread()}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=R*2)break;if(t>=R)for(const{sheet:o,area:r}of e)for(const i of o.spillsIn(r))this.unsettled.add(i);this.build();const s=new Set,n=[...this.unsettled];for(const{sheet:o,area:r}of e)n.push(...this.readersIn(o,r),...o.blockedBy(r));for(;;){const o=n.pop();if(!o)break;s.has(o)||(s.add(o),n.push(...this.readers(o.sheet,o.row,o.col)))}if(s.size!==0){for(const o of s)o.state=1,this.inCycle.delete(o);this.cycles=this.cycles.filter(o=>!o.some(r=>s.has(r))),this.indexes.clear();for(const o of this.order([...s]))o.state===1&&this.compute(o)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:n,col:o}=t,r={top:n,left:o,bottom:n+e.rows-1,right:o+e.cols-1},i=s.spillsIn(r).filter(c=>c!==t);if(r.bottom>=M||r.right>=A||s.holds(r,t)||s.tables.some(c=>L(c.area,r))||s.merges.some(c=>L(c,r))||i.some(c=>H(c,t)))return s.blockSpill(t,r),!1;for(const c of i)this.evict(c,r);return s.placeSpill(t,r),!0}evict(t,e){const{sheet:s,spill:n}=t;n&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=f.SPILL,this.moved.push({sheet:s,area:n}))}analysisOf(t){return t.analysis??=V(this.wb,t)}readsOf(t){const e=[],s=v(t);for(const n of this.analysisOf(t.shared).reads){const o=n.kind==="ref"?this.resolveRef(n,s):this.structured(n,s);o instanceof u?e.push(o):o instanceof g&&e.push(...o.parts)}return e}readsOfFormula(t,e){const s=[],n={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const o of this.analysisOf(t).reads){const r=o.kind==="ref"?this.resolveRef(o,n):this.structured(o,n);r instanceof u?s.push(r):r instanceof g&&s.push(...r.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],n=new Set,o=[],r=i=>{i.mark=e,o.push({cell:i,reads:this.pending(i,n),next:0})};for(const i of t)if(i.mark!==e)for(r(i);;){const a=o[o.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),o.pop()):c.mark!==e&&c.state===1&&r(c)}return s}pending(t,e){const s=[];for(const n of this.readsOf(t)){const{sheet:o}=n;if(n.single){const i=o.at(n.top,n.left);i instanceof b&&s.push(i);continue}if(e.has(n.key))continue;e.add(n.key);const r=Math.min(n.right,o.columnsUsed-1);for(let i=n.left;i<=r;i++){if(!o.hasFormulas(i))continue;const[a,c]=o.span(i,n.top,n.bottom);for(let l=a;l<=c;l++){const p=o.at(l,i);p instanceof b&&s.push(p)}}}return s}compute(t){if(this.stack.length>=D){t.value=f.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=e?this.top(this.eval(e,v(t))):f.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0}settleResult(t,e){const{sheet:s}=t,n=t.spill,o=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof d){const i=j(e);i.rows*i.cols<=1||this.spillOut(t,i)?(t.array=i,t.value=i.at(0,0)??0):t.value=f.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=f.CYCLE,t.array=void 0);const r=t.spill;(!G(o,t.array)||!q(n,r))&&(n&&this.moved.push({sheet:s,area:n}),r&&this.moved.push({sheet:s,area:r}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof u?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof g?f.VALUE:t instanceof S?f.CALC:t instanceof x?null:t}matrix(t){const e=t.bottom===M-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===A-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,n=Math.max(0,e-t.top+1),o=Math.max(0,s-t.left+1),r=new Array(n*o);for(let i=0;i<n;i++)for(let a=0;a<o;a++)r[i*o+a]=this.read(t.sheet,t.top+i,t.left+a);return new d(n,o,r)}each(t,e){const{sheet:s}=t,n=Math.min(t.right,s.columnsUsed-1);for(let o=t.left;o<=n;o++){const[r,i]=s.span(o,t.top,t.bottom);for(let a=r;a<=i;a++){const c=s.at(a,o);if(c===void 0){s.spillAt(a,o)&&e(this.read(s,a,o),a,o);continue}e(typeof c=="object"&&!(c instanceof m)?this.read(s,a,o):c,a,o)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):f.NAME}evaluateAt(t,e,s,n){const{node:o}=t;return o?this.top(this.eval(o,{sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col})):f.NAME}bound(t,e,s){const n=t.abs?t.index:t.index+e;return n<0||n>=s?void 0:n}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return f.REF;let n=0,o=A-1,r=0,i=M-1;if(t.col){const a=this.bound(t.col,e.dc,A),c=this.bound(t.col2??t.col,e.dc,A);if(a===void 0||c===void 0)return f.REF;n=Math.min(a,c),o=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,M),c=this.bound(t.row2??t.row,e.dr,M);if(a===void 0||c===void 0)return f.REF;r=Math.min(a,c),i=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return f.REF;const[c,l]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new g(this.wb.sheets.slice(c,l+1).map(p=>new u(p,r,n,i,o)))}return new u(s,r,n,i,o)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?X(s,t,e):t.table===void 0?f.REF:f.NAME}nameValue(t,e){const s=t.toLowerCase();for(let r=e.scope;r;r=r.parent){const i=r.names.get(s);if(i!==void 0)return i}const n=this.wb.name(t);if(n){if(!n.formula)return n.value??null;const{node:r}=n.formula;if(!r)return f.NAME;if(this.namesAt.has(s))return f.CYCLE;this.namesAt.add(s);try{return this.eval(r,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(s)}}const o=this.wb.table(t);if(o){const{top:r,left:i,bottom:a,right:c}=o.data;return new u(o.sheet,r,i,a,c)}return f.NAME}intersect(t,e){return!(t instanceof u)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new u(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new u(t.sheet,t.top,e.col,t.top,e.col):f.VALUE}unary(t,e){const s=this.flat(t);return s instanceof d?new d(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const n=this.flat(e),o=this.flat(s);if(n instanceof d||o instanceof d){const r=(i,a)=>k(t,i,a);return I(n instanceof d?n:new d(1,1,[n]),o instanceof d?o:new d(1,1,[o]),r)}return k(t,n,o)}rangeOf(t,e){return t instanceof m?t:e instanceof m?e:!(t instanceof u)||!(e instanceof u)||t.sheet!==e.sheet?f.REF:new u(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof m)return t;if(e instanceof m)return e;if(!(t instanceof u)||!(e instanceof u))return f.REF;const s=Math.max(t.top,e.top),n=Math.max(t.left,e.left),o=Math.min(t.bottom,e.bottom),r=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>o||n>r?f.NULL:new u(t.sheet,s,n,o,r)}union(t,e){if(t instanceof m)return t;if(e instanceof m)return e;const s=o=>o instanceof u?[o]:o instanceof g?o.parts:[],n=[...s(t),...s(e)];return n.length===0?f.VALUE:new g(n)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return m.of(t.code);case"missing":return O;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,n=>k("*",n,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,n=>k("/",n,100));if(s instanceof u&&s.single){const n=s.sheet.at(s.top,s.left);if(n instanceof b){this.read(s.sheet,s.top,s.left);const{spill:o}=n;if(o)return new u(s.sheet,o.top,o.left,o.bottom,o.right);if(n.array)return s}}return f.REF}case"binary":{const s=this.eval(t.left,e),n=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,n):t.op===" "?this.intersection(s,n):t.op===","?this.union(s,n):this.operate(t.op,s,n)}case"array":return new d(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(n=>{const o=this.eval(n,e);return o instanceof d||o instanceof u||o instanceof g||o instanceof S||o instanceof x?f.VALUE:o})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof S?this.invoke(s,t.args,e):s instanceof m?s:f.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(n=>this.eval(n,s)))}apply(t,e){if(e.length>t.params.length)return f.VALUE;const s=new Map;return t.params.forEach((n,o)=>{s.set(n,e[o]??O)}),this.eval(t.body,{...t.frame,scope:{names:s,...t.frame.scope?{parent:t.frame.scope}:{}}})}call(t,e,s){const n=B.get(t);if(!n){const i=this.nameValue(t,s);return i instanceof S?this.invoke(i,e,s):f.NAME}const o={calc:this,frame:s};if(e.length<n.min||e.length>n.max)return f.VALUE;if(n.kind==="lazy")return n.call(e,o);const r=e.map(i=>this.eval(i,s));return n.kind==="raw"?n.call(r,o):this.lifted(n.call,n.errors===!0,r)}lifted(t,e,s){const n=s.map(l=>l instanceof x?void 0:this.flat(l)),o=l=>{if(!e){for(const p of l)if(p instanceof m)return p}return t(...l)};if(!n.some(l=>l instanceof d))return o(n);const r=n.filter(l=>l instanceof d),i=Math.max(...r.map(l=>l.rows)),a=Math.max(...r.map(l=>l.cols)),c=new Array(i*a);for(let l=0;l<i;l++)for(let p=0;p<a;p++)c[l*a+p]=o(n.map(w=>{if(!(w instanceof d))return w;const E=w.rows===1?0:l,y=w.cols===1?0:p;return E<w.rows&&y<w.cols?w.at(E,y):f.NA}));return new d(i,a,c)}scalar(t,e){if(t instanceof x)return null;if(t instanceof u){const s=this.intersect(t,e);return s instanceof u?this.read(s.sheet,s.top,s.left):f.VALUE}return t instanceof d?t.at(0,0):t instanceof g||t instanceof S?f.VALUE:t}recalculate(){const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}key(t,e,s){return`${String(t.index)}:${String(_(e,s))}`}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t);for(const e of this.readsOf(t))if(e.single){const s=this.key(e.sheet,e.top,e.left);let n=this.dependents.get(s);n||(n=new Set,this.dependents.set(s,n)),n.add(t)}else{let s=this.watches.get(e.key);if(!s)if(s=new P(e),this.watches.set(e.key,s),e.cols>z)this.wideWatches.push(s);else for(let n=e.left;n<=e.right;n++){const o=`${String(e.sheet.index)}:${String(n)}`,r=this.watchesAt.get(o);r?r.push(s):this.watchesAt.set(o,[s])}s.cells.add(t)}}removeEdges(t){this.volatiles.delete(t);for(const e of this.readsOf(t))e.single?this.dependents.get(this.key(e.sheet,e.top,e.left))?.delete(t):this.watches.get(e.key)?.cells.delete(t)}build(){if(!this.graph){this.graph=!0;for(const t of this.allCells())this.addEdges(t)}}readersIn(t,e){const s=new Set,n=e.right;for(let i=e.top;i<=e.bottom;i++)for(let a=e.left;a<=n;a++)for(const c of this.dependents.get(this.key(t,i,a))??[])s.add(c);const o=new Set,r=i=>{if(o.has(i))return;o.add(i);const{reference:a,cells:c}=i;if(a.sheet===t&&L(a,e))for(const l of c)s.add(l)};this.wideWatches.forEach(r);for(let i=e.left;i<=n;i++)this.watchesAt.get(`${String(t.index)}:${String(i)}`)?.forEach(r);return[...s]}readers(t,e,s){this.build();const n=new Set(this.dependents.get(this.key(t,e,s))),o=`${String(t.index)}:${String(s)}`,r=this.watchesAt.get(o)??[];for(const i of[r,this.wideWatches])for(const{reference:a,cells:c}of i)if(a.sheet===t&&e>=a.top&&e<=a.bottom&&s>=a.left&&s<=a.right)for(const l of c)n.add(l);return[...n]}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const n=s.pop();if(!n)break;for(const o of this.readers(n.sheet,n.row,n.col))e.has(o)||(e.add(o),s.push(o))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,n){const o=typeof n=="string"&&n.length>1&&n.startsWith("=")?new b(new F(n,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:o??n??void 0}])}prepare(){this.build()}change(t){this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();for(const{sheet:n,row:o,col:r,slot:i}of t){const a=n.at(o,r);a instanceof b&&(this.removeEdges(a),e.delete(a),a.spill&&this.moved.push({sheet:n,area:a.spill}),n.clearSpill(a));const c=n.spillAt(o,r);c&&s.push(c),s.push(...n.blockedBy({top:o,left:r,bottom:o,right:r})),n.replace(o,r,i),i instanceof b&&(this.addEdges(i),e.add(i))}for(const{sheet:n,row:o,col:r}of t)s.push(...this.readers(n,o,r));for(s.push(...this.volatiles);;){const n=s.pop();if(!n)break;e.has(n)||(e.add(n),s.push(...this.readers(n.sheet,n.row,n.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const n=t.toLowerCase();this.mark(this.allCells().filter(o=>this.analysisOf(o.shared).names.has(n)))}numberOf(t){return T(t)}}const q=(h,t)=>h?.top===t?.top&&h?.left===t?.left&&h?.bottom===t?.bottom&&h?.right===t?.right;function j(h){return h.data.some(t=>t===null)?new d(h.rows,h.cols,h.data.map(t=>t??0)):h}function v(h){return{sheet:h.sheet,row:h.row,col:h.col,dr:h.row-h.shared.row,dc:h.col-h.shared.col}}function X(h,t,e){const{sheet:s}=h,n=h.area;let o=n.left,r=n.right;const[i,a]=t.columns;if(i!==void 0){const y=h.column(i),C=a===void 0?y:h.column(a);if(!y||!C)return f.REF;o=n.left+Math.min(y.index,C.index),r=n.left+Math.max(y.index,C.index)}if(t.thisRow)return e.sheet!==s||e.row<=n.top||e.row>n.bottom?f.VALUE:new u(s,e.row,o,e.row,r);const c=t.sections;if(c.includes("#Totals"))return f.REF;const l=c.includes("#All")||c.includes("#Headers"),p=c.length===0||c.includes("#All")||c.includes("#Data"),w=l?n.top:n.top+1,E=p?n.bottom:n.top;return new u(s,w,o,E,r)}
|
|
2
|
+
import{MAX_COLUMNS as E,MAX_ROWS as k,overlaps as O}from"../address.js";import{CellError as m,ERR as h,Matrix as p,serialOfDate as _}from"../values.js";import{filteredRows as B}from"../filter.js";import{FormulaCell as y,SharedFormula as I}from"../workbook.js";import{analyse as z}from"./analyse.js";import{Graph as T}from"./graph.js";import{fromWire as G,matrixFromWire as D,matrixToWire as W,toWire as P}from"./remote.js";import{RangeCache as U}from"./indexes.js";import{binary as S,mapBoth as H,toNumber as q}from"./coerce.js";import{FUNCTIONS as v}from"./functions/index.js";import{fillsGroup as j,paramAt as X}from"./functions/define.js";import{Lambda as M,MISSING as F,Missing as x,Reference as u,Union as b}from"./types.js";export function seededRandom(l){let t=2166136261;for(let e=0;e<l.length;e++)t=Math.imul(t^l.charCodeAt(e),16777619);return()=>{t=t+1831565813|0;let e=Math.imul(t^t>>>15,1|t);return e=e+Math.imul(e^e>>>7,61|e)^e,((e^e>>>14)>>>0)/4294967296}}const Y=new Set,Z=/\b(?:SUBTOTAL|AGGREGATE)\(/iu,$=2e3,N=12,J=(l,t)=>!l||!t?l===t:l.rows!==t.rows||l.cols!==t.cols?!1:l.data.every((e,s)=>e===t.data[s]),K=(l,t)=>l.row<t.row||l.row===t.row&&l.col<t.col;export class Calc{cycles=[];computed=0;nowSerial;random;pass=0;stack=[];inCycle=new Set;namesAt=new Set;graph=new T({readsOf:t=>this.readsOf(t),readsAt:(t,e,s,n)=>this.readsAt(t,e,s,n)});graphMade=!1;reports;replica;settled=new Set;moved=[];unsettled=new Set;hiddenBy=new Map;volatiles=new Set;indexes=new U;results=new U;wb;constructor(t,e={}){this.wb=t;const{now:s,random:n}=e;this.reports=e.report===!0,this.replica=e.replica===!0,this.nowSerial=()=>_(s??new Date),this.random=n??Math.random;for(const o of t.sheets)o.expand()}allCells(){return this.wb.sheets.flatMap(t=>[...t.formulas])}filtered(t){return this.hiddenBy.get(t)??Y}calculate(){if(this.replica){for(const e of this.wb.sheets)e.clearSpills();for(const e of this.allCells())e.state=0;return this}this.hiddenBy.clear(),this.indexes.clear(),this.results.clear();for(const e of this.wb.sheets)e.clearSpills();this.moved=[],this.unsettled.clear();const t=this.allCells();for(const e of t)e.state=1;return this.cycles=[],this.inCycle.clear(),this.run(t),this.applyFilters(t),this}applyFilters(t){for(const e of this.wb.sheets){if(e.filters.every(n=>n.columns.size===0))continue;const s=B(e,this);s.size>0&&this.hiddenBy.set(e,s)}if(this.hiddenBy.size!==0&&t.some(e=>Z.test(e.shared.text))){for(const e of t)e.state=1;this.cycles=[],this.inCycle.clear(),this.run([...t])}}value(t,e,s){return this.read(t,e,s)}read(t,e,s){const n=t.at(e,s);return n===void 0?this.ghost(t,e,s):typeof n!="object"||n instanceof m?n:(n.state===1?this.compute(n):n.state===2&&this.circular(n),n.value)}ghost(t,e,s){const n=t.spillAt(e,s);if(!n)return null;if(n.state===1)this.compute(n);else if(n.state===2)return this.circular(n),null;const{array:o}=n;return o&&t.spillAt(e,s)===n?o.at(e-n.row,s-n.col):null}circular(t){const e=this.stack.indexOf(t),s=e<0?[t]:this.stack.slice(e);s.some(n=>this.inCycle.has(n))||this.cycles.push(s);for(const n of s)this.inCycle.add(n)}forget(t,e){this.indexes.changed(t,e),this.results.changed(t,e)}run(t){for(const e of t){const{sheet:s,row:n,col:o}=e;this.forget(s,{top:n,left:o,bottom:n,right:o}),e.spill&&this.forget(s,e.spill)}this.computed=0;for(const e of this.order(t))e.state===1&&this.compute(e);this.spread()}spread(){for(let t=0;this.moved.length>0;t++){const e=this.moved;if(this.moved=[],t>=N*2)break;if(t>=N)for(const{sheet:o,area:r}of e)for(const i of o.spillsIn(r))this.unsettled.add(i);this.build();const s=new Set,n=[...this.unsettled];for(const{sheet:o,area:r}of e)n.push(...this.readersIn(o,r),...o.blockedBy(r));for(;;){const o=n.pop();if(!o)break;s.has(o)||(s.add(o),n.push(...this.readers(o.sheet,o.row,o.col)))}if(s.size!==0){for(const o of s)o.state=1,this.inCycle.delete(o);this.cycles=this.cycles.filter(o=>!o.some(r=>s.has(r))),this.indexes.clear(),this.results.clear();for(const o of this.order([...s]))o.state===1&&this.compute(o)}}for(const t of this.wb.sheets)t.tighten()}spillOut(t,e){const{sheet:s,row:n,col:o}=t,r={top:n,left:o,bottom:n+e.rows-1,right:o+e.cols-1},i=s.spillsIn(r).filter(c=>c!==t);if(r.bottom>=k||r.right>=E||s.holds(r,t)||s.tables.some(c=>O(c.area,r))||s.merges.some(c=>O(c,r))||i.some(c=>K(c,t)))return s.blockSpill(t,r),!1;for(const c of i)this.evict(c,r);return s.placeSpill(t,r),!0}evict(t,e){const{sheet:s,spill:n}=t;n&&(s.clearSpill(t),s.blockSpill(t,e),t.array=void 0,t.value=h.SPILL,this.moved.push({sheet:s,area:n}),this.reports&&this.settled.add(t))}analysisOf(t){return t.analysis??=z(this.wb,t)}readsOf(t){const e=[],s=V(t);for(const n of this.analysisOf(t.shared).reads){const o=n.kind==="ref"?this.resolveRef(n,s):this.structured(n,s);o instanceof u?e.push(o):o instanceof b&&e.push(...o.parts)}return e}readsAt(t,e,s,n){const o={sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col},r=[];for(const i of this.analysisOf(t).reads){const a=i.kind==="ref"?this.resolveRef(i,o):this.structured(i,o);a instanceof u?r.push(a):a instanceof b?r.push(...a.parts):r.push(void 0)}return r}readsOfFormula(t,e){const s=[],n={sheet:e,row:t.row,col:t.col,dr:0,dc:0};for(const o of this.analysisOf(t).reads){const r=o.kind==="ref"?this.resolveRef(o,n):this.structured(o,n);r instanceof u?s.push(r):r instanceof b&&s.push(...r.parts)}return s}order(t){this.pass+=1;const{pass:e}=this,s=[],n=new Set,o=[],r=i=>{i.mark=e,o.push({cell:i,reads:this.pending(i,n),next:0})};for(const i of t)if(i.mark!==e)for(r(i);;){const a=o[o.length-1];if(!a)break;const c=a.reads[a.next];a.next+=1,c===void 0?(s.push(a.cell),o.pop()):c.mark!==e&&c.state===1&&r(c)}return s}pending(t,e){const s=[];for(const n of this.readsOf(t)){const{sheet:o}=n;if(n.single){const i=o.at(n.top,n.left);i instanceof y&&s.push(i);continue}if(e.has(n.key))continue;e.add(n.key);const r=Math.min(n.right,o.columnsUsed-1);for(let i=n.left;i<=r;i++){if(!o.hasFormulas(i))continue;const[a,c]=o.span(i,n.top,n.bottom);for(let f=a;f<=c;f++){const d=o.at(f,i);d instanceof y&&s.push(d)}}}return s}compute(t){if(this.stack.length>=$){t.value=h.CALC,t.state=0;return}t.state=2,this.stack.push(t),this.computed+=1;const{node:e}=t.shared,s=e?this.top(this.eval(e,V(t))):h.NAME;this.stack.pop(),this.unsettled.has(t)&&!this.inCycle.has(t)&&(this.inCycle.add(t),this.cycles.push([t])),this.settleResult(t,s),t.state=0,this.reports&&this.settled.add(t)}settleResult(t,e){const{sheet:s}=t,n=t.spill,o=t.array;if(s.clearSpill(t),t.array=void 0,e instanceof p){const i=tt(e);i.rows*i.cols<=1||this.spillOut(t,i)?(t.array=i,t.value=i.at(0,0)??0):t.value=h.SPILL}else t.value=e;this.inCycle.has(t)&&(s.clearSpill(t),t.value=h.CYCLE,t.array=void 0);const r=t.spill;(!J(o,t.array)||!Q(n,r))&&(n&&this.moved.push({sheet:s,area:n}),r&&this.moved.push({sheet:s,area:r}))}top(t){const e=this.flat(t);return e===null?0:e}flat(t){return t instanceof u?t.single?this.read(t.sheet,t.top,t.left):this.matrix(t):t instanceof b?h.VALUE:t instanceof M?h.CALC:t instanceof x?null:t}matrix(t){const e=t.bottom===k-1?Math.min(t.bottom,t.sheet.lastRow):t.bottom,s=t.right===E-1?Math.min(t.right,t.sheet.columnsUsed-1):t.right,n=Math.max(0,e-t.top+1),o=Math.max(0,s-t.left+1),r=new Array(n*o);for(let i=0;i<n;i++)for(let a=0;a<o;a++)r[i*o+a]=this.read(t.sheet,t.top+i,t.left+a);return new p(n,o,r)}each(t,e){const{sheet:s}=t,n=Math.min(t.right,s.columnsUsed-1);for(let o=t.left;o<=n;o++){const[r,i]=s.span(o,t.top,t.bottom);for(let a=r;a<=i;a++){const c=s.at(a,o);if(c===void 0){s.spillAt(a,o)&&e(this.read(s,a,o),a,o);continue}e(typeof c=="object"&&!(c instanceof m)?this.read(s,a,o):c,a,o)}}}evaluate(t,e){const{node:s}=t;return s?this.top(this.eval(s,{sheet:e,row:t.row,col:t.col,dr:0,dc:0})):h.NAME}evaluateAt(t,e,s,n){const{node:o}=t;return o?this.top(this.eval(o,{sheet:e,row:s,col:n,dr:s-t.row,dc:n-t.col})):h.NAME}bound(t,e,s){const n=t.abs?t.index:t.index+e;return n<0||n>=s?void 0:n}resolveRef(t,e){const s=t.sheet===void 0?e.sheet:this.wb.sheet(t.sheet);if(!s)return h.REF;let n=0,o=E-1,r=0,i=k-1;if(t.col){const a=this.bound(t.col,e.dc,E),c=this.bound(t.col2??t.col,e.dc,E);if(a===void 0||c===void 0)return h.REF;n=Math.min(a,c),o=Math.max(a,c)}if(t.row){const a=this.bound(t.row,e.dr,k),c=this.bound(t.row2??t.row,e.dr,k);if(a===void 0||c===void 0)return h.REF;r=Math.min(a,c),i=Math.max(a,c)}if(t.lastSheet!==void 0){const a=this.wb.sheet(t.lastSheet);if(!a)return h.REF;const[c,f]=[Math.min(s.index,a.index),Math.max(s.index,a.index)];return new b(this.wb.sheets.slice(c,f+1).map(d=>new u(d,r,n,i,o)))}return new u(s,r,n,i,o)}structured(t,e){const s=t.table===void 0?e.sheet.tableAt(e.row,e.col):this.wb.table(t.table);return s?et(s,t,e):t.table===void 0?h.REF:h.NAME}nameValue(t,e,s){const n=t.toLowerCase();if(s===void 0)for(let c=e.scope;c;c=c.parent){const f=c.names.get(n);if(f!==void 0)return f}const o=s===void 0?e.sheet:this.wb.sheet(s);if(!o)return h.REF;const r=this.wb.name(t,o);if(r){if(!r.formula)return r.value??null;const{node:c}=r.formula;if(!c)return h.NAME;if(this.namesAt.has(n))return h.CYCLE;this.namesAt.add(n);try{return this.eval(c,{sheet:e.sheet,row:0,col:0,dr:0,dc:0})}finally{this.namesAt.delete(n)}}const i=this.wb.table(t);if(i){const{top:c,left:f,bottom:d,right:g}=i.data;return new u(i.sheet,c,f,d,g)}const a=v.get(t.toUpperCase());return a&&a.kind!=="lazy"?st(a,t,e):h.NAME}intersect(t,e){return!(t instanceof u)||t.single?t:t.cols===1&&e.row>=t.top&&e.row<=t.bottom?new u(t.sheet,e.row,t.left,e.row,t.left):t.rows===1&&e.col>=t.left&&e.col<=t.right?new u(t.sheet,t.top,e.col,t.top,e.col):h.VALUE}unary(t,e){const s=this.flat(t);return s instanceof p?new p(s.rows,s.cols,s.data.map(e)):e(s)}operate(t,e,s){const n=this.flat(e),o=this.flat(s);if(n instanceof p||o instanceof p){const r=(i,a)=>S(t,i,a);return H(n instanceof p?n:new p(1,1,[n]),o instanceof p?o:new p(1,1,[o]),r)}return S(t,n,o)}rangeOf(t,e){return t instanceof m?t:e instanceof m?e:!(t instanceof u)||!(e instanceof u)||t.sheet!==e.sheet?h.REF:new u(t.sheet,Math.min(t.top,e.top),Math.min(t.left,e.left),Math.max(t.bottom,e.bottom),Math.max(t.right,e.right))}intersection(t,e){if(t instanceof m)return t;if(e instanceof m)return e;if(!(t instanceof u)||!(e instanceof u))return h.REF;const s=Math.max(t.top,e.top),n=Math.max(t.left,e.left),o=Math.min(t.bottom,e.bottom),r=Math.min(t.right,e.right);return t.sheet!==e.sheet||s>o||n>r?h.NULL:new u(t.sheet,s,n,o,r)}union(t,e){if(t instanceof m)return t;if(e instanceof m)return e;const s=o=>o instanceof u?[o]:o instanceof b?o.parts:[],n=[...s(t),...s(e)];return n.length===0?h.VALUE:new b(n)}eval(t,e){switch(t.kind){case"number":case"string":case"boolean":return t.value;case"error":return m.of(t.code);case"missing":return F;case"ref":return this.resolveRef(t,e);case"structured":return this.structured(t,e);case"name":return this.nameValue(t.name,e,t.sheet);case"paren":return this.eval(t.arg,e);case"unary":{const s=this.eval(t.arg,e);return t.op==="@"?this.intersect(s,e):t.op==="+"?s:this.unary(s,n=>S("*",n,-1))}case"postfix":{const s=this.eval(t.arg,e);if(t.op==="%")return this.unary(s,n=>S("/",n,100));if(s instanceof u&&s.single){const n=s.sheet.at(s.top,s.left);if(n instanceof y){this.read(s.sheet,s.top,s.left);const{spill:o}=n;if(o)return new u(s.sheet,o.top,o.left,o.bottom,o.right);if(n.array)return s}}return h.REF}case"binary":{const s=this.eval(t.left,e),n=this.eval(t.right,e);return t.op===":"?this.rangeOf(s,n):t.op===" "?this.intersection(s,n):t.op===","?this.union(s,n):this.operate(t.op,s,n)}case"array":return new p(t.rows.length,t.rows[0]?.length??0,t.rows.flatMap(s=>s.map(n=>{const o=this.eval(n,e);return o instanceof p||o instanceof u||o instanceof b||o instanceof M||o instanceof x?h.VALUE:o})));case"invoke":{const s=this.eval(t.callee,e);return s instanceof M?this.invoke(s,t.args,e):s instanceof m?s:h.VALUE}case"call":return this.call(t.name,t.args,e)}}invoke(t,e,s){return this.apply(t,e.map(n=>this.eval(n,s)))}apply(t,e){if(t.named!==void 0){const n=v.get(t.named);return!n||n.kind==="lazy"?h.NAME:e.length<n.min||e.length>n.max?h.VALUE:this.applyFunction(n,e,t.frame)}if(e.length>t.params.length)return h.VALUE;const s=new Map;return t.params.forEach((n,o)=>{s.set(n,e[o]??F)}),this.eval(t.body,{...t.frame,scope:{names:s,...t.frame.scope?{parent:t.frame.scope}:{}}})}call(t,e,s){const n=v.get(t);if(!n){const o=this.nameValue(t,s);return o instanceof M?this.invoke(o,e,s):h.NAME}return e.length<n.min||e.length>n.max?h.VALUE:n.kind==="lazy"?n.call(e,{calc:this,frame:s}):this.applyFunction(n,e.map(o=>this.eval(o,s)),s)}applyFunction(t,e,s){const n={calc:this,frame:s};if(t.kind==="lazy")return h.VALUE;if(t.kind==="raw")return j(t.params,e.length)?this.over(e,r=>X(t.params,r)==="value",r=>t.call(r,n)):h.VALUE;const o=t.errors===!0;return this.over(e,()=>!0,r=>{const i=r.map(a=>a instanceof x?void 0:a);if(!o){for(const a of i)if(a instanceof m)return a}return t.call(...i)})}over(t,e,s){const n=[],o=new Map;for(const[f,d]of t.entries()){if(!e(f)||d instanceof x){n.push(d);continue}const g=this.flat(d);g instanceof p&&o.set(f,g),n.push(g)}if(o.size===0)return s(n);const r=[...o.values()],i=Math.max(...r.map(f=>f.rows)),a=Math.max(...r.map(f=>f.cols));if(i===0||a===0)return h.CALC;const c=new Array(i*a);for(let f=0;f<i;f++)for(let d=0;d<a;d++){const g=n.map((C,A)=>{const w=o.get(A);if(!w)return C;const L=w.rows===1?0:f,R=w.cols===1?0:d;return L<w.rows&&R<w.cols?w.at(L,R):h.NA});c[f*a+d]=this.element(s(g))}return new p(i,a,c)}element(t){const e=this.flat(t);return e instanceof p?e.rows>0&&e.cols>0?e.at(0,0):h.CALC:e}scalar(t,e){if(t instanceof x)return null;if(t instanceof u){const s=this.intersect(t,e);return s instanceof u?this.read(s.sheet,s.top,s.left):h.VALUE}return t instanceof p?t.at(0,0):t instanceof b||t instanceof M?h.VALUE:t}recalculate(){if(this.replica)return this;const t=this.allCells().filter(e=>this.analysisOf(e.shared).volatile);return this.mark(t),this}build(){if(this.graphMade)return;this.graphMade=!0;const t=this.allCells();for(const e of t)this.analysisOf(e.shared).volatile&&this.volatiles.add(e);this.graph.build(t)}addEdges(t){this.analysisOf(t.shared).volatile&&this.volatiles.add(t),this.graph.add(t)}removeEdges(t){this.volatiles.delete(t),this.graph.remove(t)}readersIn(t,e){return this.graph.readersIn(t,e)}readers(t,e,s){return this.build(),this.graph.readers(t,e,s)}precedents(t){return this.readsOf(t)}mark(t){this.build();const e=new Set(t),s=[...t];for(;;){const n=s.pop();if(!n)break;for(const o of this.readers(n.sheet,n.row,n.col))e.has(o)||(e.add(o),s.push(o))}this.settle(e)}settle(t){for(const e of t)e.state=1,this.inCycle.delete(e);this.cycles=this.cycles.filter(e=>!e.some(s=>t.has(s))),this.run([...t])}set(t,e,s,n){const o=typeof n=="string"&&n.length>1&&n.startsWith("=")?new y(new I(n,e,s,{file:""}),t,e,s):void 0;this.change([{sheet:t,row:e,col:s,slot:o??n??void 0}])}prepare(){this.build()}change(t){if(this.replica){for(const{sheet:o,row:r,col:i,slot:a}of t){const c=o.at(r,i);c instanceof y&&o.clearSpill(c),o.replace(r,i,a),a instanceof y&&(a.state=0)}return}this.build();const e=new Set,s=[...this.unsettled];this.unsettled.clear();const n=new Set;for(const{sheet:o,row:r,col:i,slot:a}of t){const c=o.at(r,i);c instanceof y&&(n.add(c),this.inCycle.delete(c),this.removeEdges(c),e.delete(c),c.spill&&this.moved.push({sheet:o,area:c.spill}),o.clearSpill(c));const f=o.spillAt(r,i);f&&s.push(f),s.push(...o.blockedBy({top:r,left:i,bottom:r,right:i})),o.replace(r,i,a),this.forget(o,{top:r,left:i,bottom:r,right:i}),a instanceof y&&(this.addEdges(a),e.add(a))}n.size>0&&(this.cycles=this.cycles.filter(o=>!o.some(r=>n.has(r))));for(const{sheet:o,row:r,col:i}of t)s.push(...this.readers(o,r,i));for(s.push(...this.volatiles);;){const o=s.pop();if(!o)break;e.has(o)||(e.add(o),s.push(...this.readers(o.sheet,o.row,o.col)))}this.settle(e)}setName(t,e){const s=this.wb.name(t);if(!s)throw new Error(`There is no name ${t}.`);s.value=e,delete s.formula;const n=t.toLowerCase();this.mark(this.allCells().filter(o=>this.analysisOf(o.shared).names.has(n)))}takeComputed(){const t=[];for(const e of this.settled)t.push({sheet:e.sheet.index,row:e.row,col:e.col,value:P(e.value),...e.array?{array:W(e.array)}:{},...e.spill?{spill:e.spill}:{},...e.blocked?{blocked:e.blocked}:{}});return this.settled.clear(),{cells:t,cycles:this.cycles.map(e=>e.map(s=>({sheet:s.sheet.index,row:s.row,col:s.col}))),hidden:[...this.hiddenBy].map(([e,s])=>({sheet:e.index,rows:[...s]}))}}receive(t){for(const s of t.cells){const n=this.wb.sheets[s.sheet],o=n?.at(s.row,s.col);!n||!(o instanceof y)||(n.clearSpill(o),o.value=G(s.value),o.array=s.array?D(s.array):void 0,s.spill&&n.placeSpill(o,s.spill),s.blocked&&n.blockSpill(o,s.blocked),o.state=0)}this.cycles=t.cycles.flatMap(s=>{const n=s.flatMap(o=>{const r=this.wb.sheets[o.sheet]?.at(o.row,o.col);return r instanceof y?[r]:[]});return n.length>0?[n]:[]});const e=new Map(this.hiddenBy);this.hiddenBy.clear();for(const{sheet:s,rows:n}of t.hidden){const o=this.wb.sheets[s];if(!o)continue;const r=e.get(o),i=r?.size===n.length&&n.every(a=>r.has(a));this.hiddenBy.set(o,i?r:new Set(n))}for(const s of this.wb.sheets)s.tighten()}numberOf(t){return q(t)}}const Q=(l,t)=>l?.top===t?.top&&l?.left===t?.left&&l?.bottom===t?.bottom&&l?.right===t?.right;function tt(l){return l.data.some(t=>t===null)?new p(l.rows,l.cols,l.data.map(t=>t??0)):l}function V(l){return{sheet:l.sheet,row:l.row,col:l.col,dr:l.row-l.shared.row,dc:l.col-l.shared.col}}function et(l,t,e){const{sheet:s}=l,n=l.area;let o=n.left,r=n.right;const[i,a]=t.columns;if(i!==void 0){const A=l.column(i),w=a===void 0?A:l.column(a);if(!A||!w)return h.REF;o=n.left+Math.min(A.index,w.index),r=n.left+Math.max(A.index,w.index)}if(t.thisRow)return e.sheet!==s||e.row<=n.top||e.row>n.bottom?h.VALUE:new u(s,e.row,o,e.row,r);const c=t.sections;if(c.includes("#Totals"))return h.REF;const f=c.includes("#All")||c.includes("#Headers"),d=c.length===0||c.includes("#All")||c.includes("#Data"),g=f?n.top:n.top+1,C=d?n.bottom:n.top;return new u(s,g,o,C,r)}function st(l,t,e){return new M([],{kind:"call",name:l.name,written:t,args:[]},e,l.name)}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
-
import{MAX_ROWS as
|
|
2
|
+
import{MAX_ROWS as L}from"../../address.js";import{CellError as s,ERR as u,Matrix as V}from"../../values.js";import{FormulaCell as b}from"../../workbook.js";import{finite as S,snap as g}from"../coerce.js";import{Reference as w,Union as D}from"../types.js";import{FUNCTIONS as I,cellsOf as y,eachArg as E,numbersOf as h,numbersOfA as O,raw as i}from"./define.js";function M(n){let t=0;for(const e of n)t=g(t+e,t,e);return t}i("SUM","(r)",(n,t)=>{let e=0,o;return E(n,t,(f,c)=>{if(!o){if(typeof f=="number")e=g(e+f,e,f);else if(f instanceof s)o=f;else if(c){const l=h([f],t);l instanceof s?o=l:e+=l[0]??0}}}),o??S(e)}),i("AVERAGE","(r)",(n,t)=>{const e=h(n,t);return e instanceof s?e:e.length===0?u.DIV0:M(e)/e.length}),i("AVERAGEA","(r)",(n,t)=>{const e=O(n,t);return e instanceof s?e:e.length===0?u.DIV0:M(e)/e.length}),i("COUNT","(r)",(n,t)=>{let e=0;return E(n,t,(o,f)=>{typeof o=="number"?e+=1:f&&!(o instanceof s)&&o!==null&&(h([o],t)instanceof s||(e+=1))}),e}),i("COUNTA","(r)",(n,t)=>{let e=0;return E(n,t,()=>{e+=1}),e}),i("COUNTBLANK","r",(n,t)=>{const e=n[0];if(e instanceof s)return e;if(e instanceof w){let f=0;return t.calc.each(e,c=>{c!==""&&(f+=1)}),e.rows*e.cols-f}const o=t.calc.flat(e);return o instanceof V?o.data.filter(f=>f===null||f==="").length:o===null||o===""?1:0});const R=n=>t=>{if(t instanceof s)return t;if(t.length===0)return 0;let e=t[0]??0;for(const o of t)e=n(e,o);return e},C=R((n,t)=>t<n?t:n),N=R((n,t)=>t>n?t:n);i("MIN","(r)",(n,t)=>C(h(n,t))),i("MAX","(r)",(n,t)=>N(h(n,t))),i("MINA","(r)",(n,t)=>C(O(n,t))),i("MAXA","(r)",(n,t)=>N(O(n,t))),i("PRODUCT","(r)",(n,t)=>{const e=h(n,t);return e instanceof s?e:e.length===0?0:S(e.reduce((o,f)=>o*f,1))}),i("SUMSQ","(r)",(n,t)=>{const e=h(n,t);return e instanceof s?e:S(M(e.map(o=>o*o)))}),i("SUMPRODUCT","(r)",(n,t)=>{const e=n.map(r=>y(r,t)),o=e[0];if(o===void 0||o instanceof s)return o??u.VALUE;let{rows:f}=o;const{cols:c}=o,l=r=>r.reference&&r.reference.bottom<L-1?r.reference.rows:r.rows;for(const r of e){if(r instanceof s)return r;if(r.cols!==c||l(r)!==l(o))return u.VALUE;f=Math.min(f,r.rows)}let A=0;for(let r=0;r<f;r++)for(let d=0;d<c;d++){let p=1;for(const U of e){const m=U.at(r,d);if(m instanceof s)return m;p*=typeof m=="number"?m:0}A+=p}return S(A)});const P={1:"AVERAGE",2:"COUNT",3:"COUNTA",4:"MAX",5:"MIN",6:"PRODUCT",7:"STDEV.S",8:"STDEV.P",9:"SUM",10:"VAR.S",11:"VAR.P"};function k(n){if(!(n instanceof b))return!1;const{node:t}=n.shared;return t?.kind==="call"&&t.name==="SUBTOTAL"}i("SUBTOTAL","v(r)",(n,t)=>{const e=t.calc.scalar(n[0],t.frame);if(e instanceof s)return e;if(typeof e!="number")return u.VALUE;const o=e<100,f=P[o?e:e-100],c=f===void 0?void 0:I.get(f);if(c?.kind!=="raw")return u.VALUE;const l=[],A=r=>{const d=a=>r.sheet.spec.rows?.[String(a+1)]?.hidden===!0,p=Math.min(r.bottom,r.sheet.lastRow),U=[],m=t.calc.filtered(r.sheet);for(let a=r.top;a<=p;a++)if(!m.has(a)&&!(!o&&d(a)))for(let T=r.left;T<=r.right;T++)U.push(k(r.sheet.at(a,T))?null:t.calc.read(r.sheet,a,T));l.push(new V(U.length,1,U))};for(const r of n.slice(1)){if(r instanceof s)return r;if(r instanceof w)A(r);else if(r instanceof D)r.parts.forEach(A);else return u.VALUE}return c.call(l,t)});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { CellError } from "../../values.js";
|
|
2
|
+
/** A number from one unit to another; #N/A for an unknown unit or two of different quantities. */
|
|
3
|
+
export declare function convert(value: number, from: string, to: string): number | CellError;
|
|
4
|
+
//# sourceMappingURL=convert.d.ts.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/*! AllyHub. Copyright (c) 2026 AllyHub, Inc. All rights reserved. Use is subject to LICENSE.md. */
|
|
2
|
+
import{CellError as b,ERR as l}from"../../values.js";import{toNumber as I,toText as v}from"../coerce.js";import{scalar as w}from"./define.js";const u=.0254,s=12*u,d=3*s,_=5280*s,g=1852,h=9460730472580800,k=u/72,f=453.59237,E=9.80665,P=f/1e3*E,K=s*P,q=231*u**3/128,R=.00454609,T=101325,O=550*K,N=new Map;function t(a,i){for(const o of a)N.set(o,i)}const e=(a,i)=>({quantity:a,factor:i,prefix:1});t(["g"],e("mass",1)),t(["sg"],{quantity:"mass",factor:P*1e3/s}),t(["lbm"],{quantity:"mass",factor:f}),t(["u"],e("mass",1660538782e-33)),t(["ozm"],{quantity:"mass",factor:f/16}),t(["grain"],{quantity:"mass",factor:f/7e3}),t(["cwt","shweight"],{quantity:"mass",factor:100*f}),t(["uk_cwt","lcwt","hweight"],{quantity:"mass",factor:112*f}),t(["stone"],{quantity:"mass",factor:14*f}),t(["ton"],{quantity:"mass",factor:2e3*f}),t(["uk_ton","LTON","brton"],{quantity:"mass",factor:2240*f}),t(["m"],e("distance",1)),t(["mi"],{quantity:"distance",factor:_}),t(["Nmi"],{quantity:"distance",factor:g}),t(["in"],{quantity:"distance",factor:u}),t(["ft"],{quantity:"distance",factor:s}),t(["yd"],{quantity:"distance",factor:d}),t(["ang"],e("distance",1e-10)),t(["ell"],{quantity:"distance",factor:45*u}),t(["ly"],e("distance",h)),t(["parsec","pc"],e("distance",648e3/Math.PI*149597870700)),t(["Picapt","Pica"],{quantity:"distance",factor:k}),t(["pica"],{quantity:"distance",factor:u/6}),t(["survey_mi"],{quantity:"distance",factor:6336e3/3937}),t(["yr"],{quantity:"time",factor:365.25*86400}),t(["day","d"],{quantity:"time",factor:86400}),t(["hr"],{quantity:"time",factor:3600}),t(["mn","min"],{quantity:"time",factor:60}),t(["sec","s"],e("time",1)),t(["Pa","p"],e("pressure",1)),t(["atm","at"],e("pressure",T)),t(["mmHg"],e("pressure",T/760)),t(["psi"],{quantity:"pressure",factor:P/u**2}),t(["Torr"],{quantity:"pressure",factor:T/760}),t(["N"],e("force",1)),t(["dyn","dy"],e("force",1e-5)),t(["lbf"],{quantity:"force",factor:P}),t(["pond"],e("force",E/1e3)),t(["J"],e("energy",1)),t(["e"],e("energy",1e-7)),t(["c"],e("energy",4.184)),t(["cal"],e("energy",4.1868)),t(["eV","ev"],e("energy",1602176487e-28)),t(["HPh","hh"],{quantity:"energy",factor:O*3600}),t(["Wh","wh"],e("energy",3600)),t(["flb"],{quantity:"energy",factor:K}),t(["BTU","btu"],{quantity:"energy",factor:1055.05585262}),t(["HP","h"],{quantity:"power",factor:O}),t(["PS"],{quantity:"power",factor:75*E}),t(["W","w"],e("power",1)),t(["T"],e("magnetism",1)),t(["ga"],e("magnetism",1e-4));const y=(a,i,o=!1)=>{t(a,{quantity:"volume",factor:i**3,...o?{prefix:3}:{}})};t(["tsp"],{quantity:"volume",factor:q/6}),t(["tspm"],{quantity:"volume",factor:5e-6}),t(["tbs"],{quantity:"volume",factor:q/2}),t(["oz"],{quantity:"volume",factor:q}),t(["cup"],{quantity:"volume",factor:8*q}),t(["pt","us_pt"],{quantity:"volume",factor:16*q}),t(["uk_pt"],{quantity:"volume",factor:R/8}),t(["qt"],{quantity:"volume",factor:32*q}),t(["uk_qt"],{quantity:"volume",factor:R/4}),t(["gal"],{quantity:"volume",factor:128*q}),t(["uk_gal"],{quantity:"volume",factor:R}),t(["l","L","lt"],e("volume",.001)),t(["barrel"],{quantity:"volume",factor:5376*q}),t(["bushel"],{quantity:"volume",factor:2150.42*u**3}),t(["GRT","regton"],{quantity:"volume",factor:100*s**3}),t(["MTON"],{quantity:"volume",factor:40*s**3}),y(["m3","m^3"],1,!0),y(["ang3","ang^3"],1e-10,!0),y(["ft3","ft^3"],s),y(["in3","in^3"],u),y(["ly3","ly^3"],h),y(["mi3","mi^3"],_),y(["yd3","yd^3"],d),y(["Nmi3","Nmi^3"],g),y(["Pica3","Picapt3","Pica^3","Picapt^3"],k);const m=(a,i,o=!1)=>{t(a,{quantity:"area",factor:i**2,...o?{prefix:2}:{}})};t(["uk_acre"],{quantity:"area",factor:4840*d**2}),t(["us_acre"],{quantity:"area",factor:43560*(1200/3937)**2}),t(["ar"],e("area",100)),t(["ha"],{quantity:"area",factor:1e4}),t(["Morgen"],{quantity:"area",factor:2500}),m(["m2","m^2"],1,!0),m(["ang2","ang^2"],1e-10,!0),m(["ft2","ft^2"],s),m(["in2","in^2"],u),m(["ly2","ly^2"],h),m(["mi2","mi^2"],_),m(["yd2","yd^2"],d),m(["Nmi2","Nmi^2"],g),m(["Pica2","Picapt2","Pica^2","Picapt^2"],k),t(["bit"],{quantity:"information",factor:1,prefix:1,binary:!0}),t(["byte"],{quantity:"information",factor:8,prefix:1,binary:!0}),t(["admkn"],{quantity:"speed",factor:6080*s/3600}),t(["kn"],{quantity:"speed",factor:g/3600}),t(["m/h","m/hr"],e("speed",1/3600)),t(["m/s","m/sec"],e("speed",1)),t(["mph"],{quantity:"speed",factor:_/3600});const U={Y:1e24,Z:1e21,E:1e18,P:1e15,T:1e12,G:1e9,M:1e6,k:1e3,h:100,da:10,e:10,d:.1,c:.01,m:.001,u:1e-6,n:1e-9,p:1e-12,f:1e-15,a:1e-18,z:1e-21,y:1e-24},L={Yi:2**80,Zi:2**70,Ei:2**60,Pi:2**50,Ti:2**40,Gi:2**30,Mi:2**20,ki:2**10},A={C:{toK:a=>a+273.15,fromK:a=>a-273.15},F:{toK:a=>(a-32)*5/9+273.15,fromK:a=>(a-273.15)*9/5+32},K:{toK:a=>a,fromK:a=>a},Rank:{toK:a=>a*5/9,fromK:a=>a*9/5},Reau:{toK:a=>a*5/4+273.15,fromK:a=>(a-273.15)*4/5}},M={C:"C",cel:"C",F:"F",fah:"F",K:"K",kel:"K",Rank:"Rank",Reau:"Reau"};function C(a){const i=M[a];if(i!==void 0)return{kind:"temperature",scale:i,times:1};const o=N.get(a);if(o)return{kind:"unit",quantity:o.quantity,factor:o.factor};for(const[n,r]of Object.entries(U)){if(!a.startsWith(n))continue;const c=a.slice(n.length);if(M[c]==="K")return{kind:"temperature",scale:"K",times:r};const p=N.get(c);if(p?.prefix!==void 0)return{kind:"unit",quantity:p.quantity,factor:p.factor*r**p.prefix}}for(const[n,r]of Object.entries(L)){if(!a.startsWith(n))continue;const c=N.get(a.slice(n.length));if(c?.binary===!0)return{kind:"unit",quantity:c.quantity,factor:c.factor*r}}}export function convert(a,i,o){const n=C(i),r=C(o);if(!n||!r)return l.NA;if(n.kind==="temperature"||r.kind==="temperature"){if(n.kind!=="temperature"||r.kind!=="temperature")return l.NA;const c=A[n.scale],p=A[r.scale];return!c||!p?l.NA:p.fromK(c.toK(a*n.times))/r.times}return n.quantity!==r.quantity?l.NA:a*n.factor/r.factor}w("CONVERT",3,3,(a,i,o)=>{const n=I(a),r=v(i),c=v(o);return n instanceof b?n:r instanceof b?r:c instanceof b?c:convert(n,r,c)});
|