cocheck 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +104 -2
- package/THIRD_PARTY_NOTICES.md +279 -0
- package/bin/cocheck.mjs +8 -0
- package/dist/agent-transcripts.js +541 -0
- package/dist/attribution/basis.js +31 -0
- package/dist/attribution/evidence.js +311 -0
- package/dist/attribution/hooks/cocheck-preload.cjs +254 -0
- package/dist/attribution/hooks/sitecustomize.py +245 -0
- package/dist/attribution/names.js +163 -0
- package/dist/attribution/reconstruct.js +128 -0
- package/dist/attribution/tag.js +246 -0
- package/dist/attribution/write-scope-win.js +615 -0
- package/dist/attribution/write-scope.js +1668 -0
- package/dist/build-info.json +1 -0
- package/dist/cli-main.js +8 -0
- package/dist/cli.js +193 -0
- package/dist/codex-config.js +107 -0
- package/dist/connector/after-gap.js +56 -0
- package/dist/connector/after-snapshot.js +244 -0
- package/dist/connector/before-snapshot.js +168 -0
- package/dist/connector/builtins.js +93 -0
- package/dist/connector/changes-v1.js +276 -0
- package/dist/connector/claimed-state.js +40 -0
- package/dist/connector/claimed.js +86 -0
- package/dist/connector/conformance.js +458 -0
- package/dist/connector/diff-object.js +81 -0
- package/dist/connector/diff-samples.js +64 -0
- package/dist/connector/diff.js +273 -0
- package/dist/connector/doc-level.js +20 -0
- package/dist/connector/enabled-types.js +87 -0
- package/dist/connector/export-view.js +13 -0
- package/dist/connector/file-capture.js +265 -0
- package/dist/connector/frozen.js +213 -0
- package/dist/connector/installed-manifests.js +72 -0
- package/dist/connector/installed-match.js +161 -0
- package/dist/connector/local-process.js +119 -0
- package/dist/connector/nothing-changed-rule.js +23 -0
- package/dist/connector/old-rows.js +66 -0
- package/dist/connector/png-pixels.js +4 -0
- package/dist/connector/registry.js +78 -0
- package/dist/connector/requires.js +30 -0
- package/dist/connector/row-doc.js +54 -0
- package/dist/connector/run-argv.js +25 -0
- package/dist/connector/runner.js +325 -0
- package/dist/connector/schema.js +242 -0
- package/dist/connector/subject.js +8 -0
- package/dist/connector/subjects.js +113 -0
- package/dist/connector/validate.js +200 -0
- package/dist/connector-auth.js +101 -0
- package/dist/connector-create.js +55 -0
- package/dist/connector-doctor.js +167 -0
- package/dist/connector-test-cli.js +627 -0
- package/dist/connector-types-cli.js +43 -0
- package/dist/connectors/microsoft-excel/cocheck-connector.json +13 -0
- package/dist/connectors/microsoft-excel/connector.mjs +11 -0
- package/dist/connectors/microsoft-powerpoint/cocheck-connector.json +13 -0
- package/dist/connectors/microsoft-powerpoint/connector.mjs +11 -0
- package/dist/connectors/microsoft-word/cocheck-connector.json +13 -0
- package/dist/connectors/microsoft-word/connector.mjs +11 -0
- package/dist/copies.js +151 -0
- package/dist/copilot-helpers.js +246 -0
- package/dist/coverage.js +277 -0
- package/dist/credit-guard.js +330 -0
- package/dist/display.js +78 -0
- package/dist/file-lock.js +145 -0
- package/dist/fingerprint.js +25 -0
- package/dist/formats/files/align.js +84 -0
- package/dist/formats/files/csv.js +78 -0
- package/dist/formats/files/layer.js +274 -0
- package/dist/formats/files/scan.js +86 -0
- package/dist/formats/files/textlike.js +21 -0
- package/dist/formats/layer.js +48 -0
- package/dist/helpers/pdfpages +0 -0
- package/dist/home-page.js +173 -0
- package/dist/home-perms.js +59 -0
- package/dist/hook-adapter.js +3415 -0
- package/dist/hook-cli.js +505 -0
- package/dist/hook-journal.js +147 -0
- package/dist/hook.js +25 -0
- package/dist/hosts/claude.js +250 -0
- package/dist/hosts/codex-bin-macos.js +2 -0
- package/dist/hosts/codex-bin.js +35 -0
- package/dist/hosts/codex-trust-probe.js +52 -0
- package/dist/hosts/codex-trust.js +46 -0
- package/dist/hosts/codex.js +355 -0
- package/dist/hosts/copilot.js +236 -0
- package/dist/hosts/index.js +52 -0
- package/dist/hosts/session-titles.js +327 -0
- package/dist/hosts/shared.js +31 -0
- package/dist/hosts/types.js +26 -0
- package/dist/html.js +4 -0
- package/dist/install.js +676 -0
- package/dist/items.js +105 -0
- package/dist/ledger.js +2097 -0
- package/dist/narration.js +202 -0
- package/dist/normalize.js +82 -0
- package/dist/office/connector/apps.js +34 -0
- package/dist/office/connector/docx/layer.js +247 -0
- package/dist/office/connector/docx/reader.js +284 -0
- package/dist/office/connector/excel/align.js +284 -0
- package/dist/office/connector/excel/answer.js +352 -0
- package/dist/office/connector/excel/diff.js +1310 -0
- package/dist/office/connector/excel/errors.js +44 -0
- package/dist/office/connector/excel/look.js +121 -0
- package/dist/office/connector/excel/print-boxes.js +432 -0
- package/dist/office/connector/excel/print-grid-previous.js +206 -0
- package/dist/office/connector/excel/print-setup.js +168 -0
- package/dist/office/connector/excel/workbook.js +374 -0
- package/dist/office/connector/export-driver.js +9 -0
- package/dist/office/connector/helpers/cocheck-screen +0 -0
- package/dist/office/connector/issues.js +31 -0
- package/dist/office/connector/mac-guard.js +16 -0
- package/dist/office/connector/main.js +23 -0
- package/dist/office/connector/nothing-changed.js +18 -0
- package/dist/office/connector/office-job.js +416 -0
- package/dist/office/connector/office-macos.js +473 -0
- package/dist/office/connector/office-win-export.js +107 -0
- package/dist/office/connector/office-win-lifecycle.js +194 -0
- package/dist/office/connector/office-win.js +209 -0
- package/dist/office/connector/office-win.ps1 +451 -0
- package/dist/office/connector/open-watch.js +46 -0
- package/dist/office/connector/pictures.js +156 -0
- package/dist/office/connector/pptx/diff.js +589 -0
- package/dist/office/connector/pptx/frames.js +105 -0
- package/dist/office/connector/pptx/layer.js +148 -0
- package/dist/office/connector/pptx/links.js +59 -0
- package/dist/office/connector/pptx/reader.js +744 -0
- package/dist/office/connector/pptx/spans.js +70 -0
- package/dist/office/connector/pptx/tags.js +86 -0
- package/dist/office/connector/pptx/xml.js +96 -0
- package/dist/office/connector/pptx/zip.js +223 -0
- package/dist/office/connector/pptx-boxes.js +238 -0
- package/dist/office/connector/print-copy-cap.js +19 -0
- package/dist/office/connector/print-copy.js +13 -0
- package/dist/office/connector/program.js +291 -0
- package/dist/office/connector/reach.js +27 -0
- package/dist/office/connector/refusal.js +16 -0
- package/dist/office/connector/screen-macos.js +193 -0
- package/dist/office/connector/screen-windows.js +34 -0
- package/dist/office/connector/shared/chart-diff.js +376 -0
- package/dist/office/connector/shared/docprops-noise.js +13 -0
- package/dist/office/connector/shared/lock.js +107 -0
- package/dist/office/connector/shared/ooxml-names.js +347 -0
- package/dist/office/connector/shared/owner.js +57 -0
- package/dist/office/connector/shared/package-media.js +703 -0
- package/dist/office/connector/shared/preflight.js +226 -0
- package/dist/office/connector/shared/structure-check.js +623 -0
- package/dist/office/connector/word-boxes.js +364 -0
- package/dist/office/connector/word-print-copy.js +234 -0
- package/dist/office/connector/xlsx/diff.js +1038 -0
- package/dist/office/connector/xlsx/formula.js +410 -0
- package/dist/office/connector/xlsx/layer.js +621 -0
- package/dist/office/connector/xlsx/memo.js +39 -0
- package/dist/office/connector/xlsx/numbers.js +31 -0
- package/dist/office/connector/xlsx/numfmt.js +913 -0
- package/dist/office/connector/xlsx/parts.js +66 -0
- package/dist/office/connector/xlsx/reader.js +394 -0
- package/dist/office/connector/xlsx/types.js +20 -0
- package/dist/office/connector/xlsx/written.js +446 -0
- package/dist/open-pick.js +99 -0
- package/dist/patch-secrets.js +194 -0
- package/dist/paths.js +65 -0
- package/dist/platform/darwin.js +179 -0
- package/dist/platform/index.js +61 -0
- package/dist/platform/spawn.js +115 -0
- package/dist/platform/stub.js +21 -0
- package/dist/platform/task-folder.js +41 -0
- package/dist/platform/types.js +20 -0
- package/dist/platform/win32-docrender.js +7 -0
- package/dist/platform/win32.js +265 -0
- package/dist/preferences.js +49 -0
- package/dist/reason.js +103 -0
- package/dist/region-delivery.js +148 -0
- package/dist/registry.js +68 -0
- package/dist/reveal.js +45 -0
- package/dist/review-process.js +154 -0
- package/dist/review-refresh-process.js +17 -0
- package/dist/review-refresh.js +120 -0
- package/dist/review-server.js +2079 -0
- package/dist/review-view-server.js +106 -0
- package/dist/review.js +8 -0
- package/dist/seal.js +315 -0
- package/dist/secret-files.js +34 -0
- package/dist/secret-hashes.js +27 -0
- package/dist/secret-rules.generated.js +3519 -0
- package/dist/secret-scan.js +127 -0
- package/dist/session-context.js +18 -0
- package/dist/signoff.js +419 -0
- package/dist/skill.js +110 -0
- package/dist/states.js +192 -0
- package/dist/tasks.js +1001 -0
- package/dist/timeline/attribution.js +207 -0
- package/dist/timeline/broken.js +55 -0
- package/dist/timeline/describe.js +413 -0
- package/dist/timeline/generic.js +195 -0
- package/dist/timeline/load.js +464 -0
- package/dist/timeline/loading.js +49 -0
- package/dist/timeline/notes.js +208 -0
- package/dist/timeline/page.js +178 -0
- package/dist/timeline/picture.js +303 -0
- package/dist/timeline/rail/assets/cocheck-icon.svg +1 -0
- package/dist/timeline/rail/assets/files-view.js +198 -0
- package/dist/timeline/rail/assets/loaders/icon-intro.json +1 -0
- package/dist/timeline/rail/assets/loaders/loader-orbit.json +1 -0
- package/dist/timeline/rail/assets/loaders/loader-spin.json +1 -0
- package/dist/timeline/rail/assets/review.css +992 -0
- package/dist/timeline/rail/assets/review.js +3365 -0
- package/dist/timeline/rail/assets/view-table.js +683 -0
- package/dist/timeline/rail/failure-line.js +26 -0
- package/dist/timeline/rail/files-model.js +45 -0
- package/dist/timeline/rail/live.js +49 -0
- package/dist/timeline/rail/made-pictures.js +48 -0
- package/dist/timeline/rail/mcp-card.js +307 -0
- package/dist/timeline/rail/model.js +1317 -0
- package/dist/timeline/rail/page.js +167 -0
- package/dist/timeline/rail/passes.js +106 -0
- package/dist/timeline/rail/regions.js +26 -0
- package/dist/timeline/rail/send-back.js +154 -0
- package/dist/timeline/rail/stop-status.js +164 -0
- package/dist/timeline/read-signal.js +743 -0
- package/dist/timeline/render/claimed.js +13 -0
- package/dist/timeline/render/fallback.js +36 -0
- package/dist/timeline/render/pdf-helper.js +71 -0
- package/dist/timeline/render/pdf.js +410 -0
- package/dist/timeline/render/pixel-pages.js +168 -0
- package/dist/timeline/render/png-crop.js +281 -0
- package/dist/timeline/render/registry.js +7 -0
- package/dist/timeline/render/text.js +366 -0
- package/dist/timeline/render/types.js +1 -0
- package/dist/timeline/render/view-v1.js +148 -0
- package/dist/timeline/render/whole.js +6 -0
- package/dist/timeline/rollout-failures.js +40 -0
- package/dist/timeline/runs.js +836 -0
- package/dist/timeline/stored-image.js +34 -0
- package/dist/timeline/subject-display.js +44 -0
- package/dist/timeline/timeline.js +1114 -0
- package/dist/timeline/write-signal.js +403 -0
- package/dist/tool-label.js +201 -0
- package/dist/tracking.js +87 -0
- package/dist/vendor/pdfjs/LICENSE +177 -0
- package/dist/vendor/pdfjs/pdf.min.mjs +27 -0
- package/dist/vendor/pdfjs/pdf.worker.min.mjs +27 -0
- package/package.json +66 -4
- package/public-kit/GUIDE.md +189 -0
- package/public-kit/README.md +10 -0
- package/public-kit/conformance/README.md +13 -0
- package/public-kit/conformance/cases/C1.json +12 -0
- package/public-kit/conformance/cases/C10.json +15 -0
- package/public-kit/conformance/cases/C11.json +16 -0
- package/public-kit/conformance/cases/C12.json +18 -0
- package/public-kit/conformance/cases/C13.json +23 -0
- package/public-kit/conformance/cases/C14.json +16 -0
- package/public-kit/conformance/cases/C16.json +15 -0
- package/public-kit/conformance/cases/C17.json +25 -0
- package/public-kit/conformance/cases/C2.json +13 -0
- package/public-kit/conformance/cases/C3.json +44 -0
- package/public-kit/conformance/cases/C4.json +13 -0
- package/public-kit/conformance/cases/C5.json +16 -0
- package/public-kit/conformance/cases/C6.json +58 -0
- package/public-kit/conformance/generate-large.mjs +15 -0
- package/public-kit/examples/toy-export/cocheck-connector.json +10 -0
- package/public-kit/examples/toy-export/connector.mjs +28 -0
- package/public-kit/examples/toy-helpers/cocheck-connector.json +10 -0
- package/public-kit/examples/toy-helpers/connector.mjs +69 -0
- package/public-kit/examples/toy-level-1/cocheck-connector.json +10 -0
- package/public-kit/examples/toy-level-1/connector.mjs +61 -0
- package/public-kit/examples/toy-level-3/cocheck-connector.json +10 -0
- package/public-kit/examples/toy-level-3/connector.mjs +82 -0
- package/public-kit/examples/toy-tracker/cocheck-connector.json +14 -0
- package/public-kit/examples/toy-tracker/connector.mjs +46 -0
- package/public-kit/examples/toy-tracker/samples/history.json +10 -0
- package/public-kit/examples/toy-tracker/samples/samples.json +7 -0
- package/public-kit/examples/toy-vendor/cocheck-connector.json +15 -0
- package/public-kit/examples/toy-vendor/connector.mjs +62 -0
- package/public-kit/helpers/pdf-text-layout.d.mts +35 -0
- package/public-kit/helpers/pdf-text-layout.mjs +602 -0
- package/public-kit/helpers/pdf-text.d.mts +56 -0
- package/public-kit/helpers/pdf-text.mjs +375 -0
- package/public-kit/helpers/pdf.d.mts +61 -0
- package/public-kit/helpers/pdf.mjs +251 -0
- package/public-kit/helpers/png.d.mts +15 -0
- package/public-kit/helpers/png.mjs +88 -0
- package/public-kit/helpers/spec.d.mts +4 -0
- package/public-kit/helpers/spec.mjs +2 -0
- package/public-kit/scaffold/README.md +23 -0
- package/public-kit/scaffold/template/README.md +13 -0
- package/public-kit/scaffold/template/cocheck-connector.json +10 -0
- package/public-kit/scaffold/template/connector.mjs +50 -0
- package/public-kit/spec/README.md +408 -0
- package/public-kit/spec/v1.md +598 -0
- package/public-kit/toy-product/README.md +7 -0
- package/public-kit/toy-product/notes-store.mjs +30 -0
- package/public-kit/toy-product/states/create.after.json +16 -0
- package/public-kit/toy-product/states/create.before.json +12 -0
- package/public-kit/toy-product/states/delete.after.json +8 -0
- package/public-kit/toy-product/states/delete.before.json +12 -0
- package/public-kit/toy-product/states/edit.after.json +12 -0
- package/public-kit/toy-product/states/edit.before.json +12 -0
- package/public-kit/toy-product/states/malformed.after.json +1 -0
- package/public-kit/toy-product/states/malformed.before.json +12 -0
- package/public-kit/toy-product/states/no-change.after.json +12 -0
- package/public-kit/toy-product/states/no-change.before.json +12 -0
- package/scripts/hook-launch.mjs +58 -0
- package/scripts/hook-launch.sh +52 -0
|
@@ -0,0 +1,1310 @@
|
|
|
1
|
+
// The Excel connector's change list and print plan for one before/after pair of workbooks. Pure: bytes in, changes and areas out.
|
|
2
|
+
//
|
|
3
|
+
// The XML alone decides what changed and where (no pixel comparison). Steps:
|
|
4
|
+
// sheets matched by name, then by content (a rename); package parts (charts, images, comments, names, macros) diffed as changes;
|
|
5
|
+
// per matched worksheet: columns aligned by content, rows aligned on the cells' constants (anchors on unique rows, LCS in gaps, a
|
|
6
|
+
// similarity pass, reorder detection); cells compared under the maps (a formula is remapped through the maps before comparing, so an
|
|
7
|
+
// insert never reads as a formula edit); each difference classed input, derived (formula unchanged, value recalculated), or format;
|
|
8
|
+
// runs of the same change down a column collapse to one change with its exceptions as child changes; single edits of the same kind in
|
|
9
|
+
// one column are grouped; derived cells hang under one summary per cause; changes in hidden rows, columns or sheets get a reason and no
|
|
10
|
+
// area; the rest cluster into blocks (gap 4 rows, 2 columns; at most 60 x 15), ranked by stakes and capped (6 per sheet, 24 per file),
|
|
11
|
+
// everything past the cap listed with the reason. Each pictured block becomes print areas on both sides: the block with 3 rows and 1
|
|
12
|
+
// column of context (at least 10 x 5), tall uniform stretches folded to a top and a bottom piece, wide blocks folded to a left and a right
|
|
13
|
+
// piece, before and after windows images of each other under the row map.
|
|
14
|
+
import { extractFormulaRefs, numToCol, remapFormulaRefs, toRelativeSignature } from "../xlsx/formula.js";
|
|
15
|
+
import { xlsxChartDetail } from "../shared/package-media.js";
|
|
16
|
+
import { align } from "./align.js";
|
|
17
|
+
import { introducedErrors } from "./errors.js";
|
|
18
|
+
import { heightPt, readWorkbook } from "./workbook.js";
|
|
19
|
+
/** The sheet behind a SheetPrint (for sizes), kept out of the plan's data so the plan's output is unchanged. */
|
|
20
|
+
const sheetOfPrint = new WeakMap();
|
|
21
|
+
export const sheetOf = (sp) => sheetOfPrint.get(sp);
|
|
22
|
+
export const LIMITS = { gapRows: 4, gapCols: 2, maxBlockRows: 60, maxBlockCols: 15, ctxRows: 3, ctxCols: 1, minRows: 10, minCols: 5, maxAreaRows: 40, foldKeep: 8, foldMin: 20, perSheet: 6, perFile: 24, maxPieces: 6, listChildren: 200, minZoom: 40 };
|
|
23
|
+
// ---------------------------------------------------------------- words
|
|
24
|
+
const quoteSheet = (n) => (/^[A-Za-z_][A-Za-z0-9_.]*$/.test(n) && !/^[A-Z]{1,3}\d+$/i.test(n) ? n : `'${n.replace(/'/g, "''")}'`);
|
|
25
|
+
export const addr = (r, c) => `${numToCol(c)}${r}`;
|
|
26
|
+
export const rangeText = (x) => (x.r1 === x.r2 && x.c1 === x.c2 ? addr(x.r1, x.c1) : `${addr(x.r1, x.c1)}:${addr(x.r2, x.c2)}`);
|
|
27
|
+
const at = (sheet, x) => `${quoteSheet(sheet)}!${rangeText(x)}`;
|
|
28
|
+
const n = (x) => x.toLocaleString("en-US");
|
|
29
|
+
const rowsText = (a, b) => (a === b ? `row ${n(a)}` : `rows ${n(a)}-${n(b)}`);
|
|
30
|
+
function valueWords(v) {
|
|
31
|
+
if (v === null)
|
|
32
|
+
return "empty";
|
|
33
|
+
if (typeof v === "object")
|
|
34
|
+
return v.error;
|
|
35
|
+
if (typeof v === "boolean")
|
|
36
|
+
return v ? "TRUE" : "FALSE";
|
|
37
|
+
return String(v);
|
|
38
|
+
}
|
|
39
|
+
function cellWords(c) {
|
|
40
|
+
if (c === undefined || (c.f === null && c.v === null))
|
|
41
|
+
return undefined;
|
|
42
|
+
return c.f !== null ? `=${c.f}` : valueWords(c.v);
|
|
43
|
+
}
|
|
44
|
+
const valEq = (a, b) => {
|
|
45
|
+
if (a === null || b === null)
|
|
46
|
+
return a === b;
|
|
47
|
+
if (typeof a === "object" || typeof b === "object")
|
|
48
|
+
return typeof a === "object" && typeof b === "object" && a.error === b.error;
|
|
49
|
+
return a === b;
|
|
50
|
+
};
|
|
51
|
+
const isErr = (v) => v !== null && typeof v === "object";
|
|
52
|
+
const VOLATILE = /\b(RAND|RANDBETWEEN|RANDARRAY|NOW|TODAY|OFFSET|INDIRECT)\s*\(/i;
|
|
53
|
+
function cellSig(s) {
|
|
54
|
+
const out = new Set();
|
|
55
|
+
for (const [r, row] of s.rows)
|
|
56
|
+
for (const [c, cell] of row) {
|
|
57
|
+
out.add(`${r},${c}=${cell.f !== null ? "=" + cell.f : valueWords(cell.v)}`);
|
|
58
|
+
if (out.size > 20000)
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
function jaccard(x, y) {
|
|
64
|
+
if (x.size === 0 && y.size === 0)
|
|
65
|
+
return 1;
|
|
66
|
+
let i = 0;
|
|
67
|
+
for (const k of x)
|
|
68
|
+
if (y.has(k))
|
|
69
|
+
i++;
|
|
70
|
+
return i / (x.size + y.size - i);
|
|
71
|
+
}
|
|
72
|
+
function matchSheets(A, B) {
|
|
73
|
+
const pairs = [];
|
|
74
|
+
const bByName = new Map(B.sheets.map((s) => [s.name, s]));
|
|
75
|
+
const usedB = new Set();
|
|
76
|
+
const leftA = [];
|
|
77
|
+
for (const a of A.sheets) {
|
|
78
|
+
const b = bByName.get(a.name);
|
|
79
|
+
if (b && b.kind === a.kind) {
|
|
80
|
+
pairs.push({ a, b });
|
|
81
|
+
usedB.add(b);
|
|
82
|
+
}
|
|
83
|
+
else
|
|
84
|
+
leftA.push(a);
|
|
85
|
+
}
|
|
86
|
+
const leftB = B.sheets.filter((s) => !usedB.has(s));
|
|
87
|
+
// renames: content similarity at least 0.6, same kind (formulas pointing at the renamed sheet change their text, so compare loosely)
|
|
88
|
+
for (const a of leftA) {
|
|
89
|
+
let best, bv = 0.6;
|
|
90
|
+
const sa = cellSig(a);
|
|
91
|
+
for (const b of leftB)
|
|
92
|
+
if (!usedB.has(b) && b.kind === a.kind) {
|
|
93
|
+
const v = a.kind === "chartsheet" ? (a.drawings[0]?.sha === b.drawings[0]?.sha ? 1 : 0) : jaccard(sa, cellSig(b));
|
|
94
|
+
if (v >= bv) {
|
|
95
|
+
bv = v;
|
|
96
|
+
best = b;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (best) {
|
|
100
|
+
pairs.push({ a, b: best, renamed: true });
|
|
101
|
+
usedB.add(best);
|
|
102
|
+
}
|
|
103
|
+
else
|
|
104
|
+
pairs.push({ a });
|
|
105
|
+
}
|
|
106
|
+
for (const b of leftB)
|
|
107
|
+
if (!usedB.has(b))
|
|
108
|
+
pairs.push({ b });
|
|
109
|
+
// keep the after order, then removed sheets
|
|
110
|
+
const pos = (p) => (p.b ? B.sheets.indexOf(p.b) : 1000 + A.sheets.indexOf(p.a));
|
|
111
|
+
return pairs.sort((x, y) => pos(x) - pos(y));
|
|
112
|
+
}
|
|
113
|
+
/** Sheet qualifiers in a formula rewritten to the pair's token, so a formula pointing at a renamed sheet reads the same on both sides. */
|
|
114
|
+
export function canonNames(f, map) {
|
|
115
|
+
if (!map)
|
|
116
|
+
return f;
|
|
117
|
+
let out = f;
|
|
118
|
+
for (const [nm, tok] of map) {
|
|
119
|
+
const esc = nm.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
120
|
+
out = out.replace(new RegExp(`(?<![A-Za-z0-9_.'])(?:'${esc.replace(/'/g, "''")}'|${esc})!`, "g"), `${tok}!`);
|
|
121
|
+
}
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
function r1c1(sheet, r, c, cell, cache, names) {
|
|
125
|
+
if (cell.f === null)
|
|
126
|
+
return "";
|
|
127
|
+
const key = `${r},${c}`;
|
|
128
|
+
let s = cache.get(key);
|
|
129
|
+
if (s === undefined) {
|
|
130
|
+
try {
|
|
131
|
+
s = canonNames(toRelativeSignature(cell.f, addr(r, c), sheet.name), names).replace(/\bR(\d+)/g, "R#");
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
s = cell.f;
|
|
135
|
+
}
|
|
136
|
+
cache.set(key, s);
|
|
137
|
+
}
|
|
138
|
+
return s;
|
|
139
|
+
}
|
|
140
|
+
const constKey = (c) => (c === undefined ? "" : c.f !== null ? "ƒ" : c.v === null ? "" : typeof c.v === "object" ? `#${c.v.error}` : `${typeof c.v === "number" ? "n" : typeof c.v === "boolean" ? "b" : "s"}${String(c.v)}`);
|
|
141
|
+
function alignSheet(a, b, namesA, namesB) {
|
|
142
|
+
const ra = new Map(), rb = new Map();
|
|
143
|
+
const full = (s, cache, r, c) => {
|
|
144
|
+
const cell = s.rows.get(r)?.get(c);
|
|
145
|
+
if (cell === undefined)
|
|
146
|
+
return "";
|
|
147
|
+
return cell.f !== null ? `=${r1c1(s, r, c, cell, cache, s === a ? namesA : namesB)}` : constKey(cell);
|
|
148
|
+
};
|
|
149
|
+
// both sides span the same extent, including what drawings cover, so a chart beside the data has columns and rows to sit in
|
|
150
|
+
const ext = (s) => s.drawings.reduce((m, d) => (d.anchor ? { r: Math.max(m.r, d.anchor.r2), c: Math.max(m.c, d.anchor.c2) } : m), { r: 0, c: 0 });
|
|
151
|
+
const ea = ext(a), eb = ext(b);
|
|
152
|
+
// columns: both sides to the same count (a column filled where the other side has none pairs with it); rows: only up to what drawings cover
|
|
153
|
+
const maxC = Math.max(a.maxCol, b.maxCol, ea.c, eb.c);
|
|
154
|
+
const extR = Math.max(ea.r, eb.r), rowsA = Math.max(a.maxRow, extR), rowsB = Math.max(b.maxRow, extR);
|
|
155
|
+
// columns: by the multiset of what they hold
|
|
156
|
+
const colBag = (s, cache) => {
|
|
157
|
+
const bags = [];
|
|
158
|
+
for (let c = 1; c <= maxC; c++)
|
|
159
|
+
bags.push(new Map());
|
|
160
|
+
for (const [r, row] of s.rows)
|
|
161
|
+
for (const [c] of row) {
|
|
162
|
+
const k = full(s, cache, r, c);
|
|
163
|
+
if (k === "")
|
|
164
|
+
continue;
|
|
165
|
+
const m = bags[c - 1];
|
|
166
|
+
m.set(k, (m.get(k) ?? 0) + 1);
|
|
167
|
+
}
|
|
168
|
+
return bags;
|
|
169
|
+
};
|
|
170
|
+
const ba = colBag(a, ra), bb = colBag(b, rb);
|
|
171
|
+
const bagKey = (m) => [...m].sort((x, y) => (x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0)).map(([k, v]) => `${k}*${v}`).join("\u0001");
|
|
172
|
+
const bagSim = (x, y) => {
|
|
173
|
+
let inter = 0, tx = 0, ty = 0;
|
|
174
|
+
for (const v of x.values())
|
|
175
|
+
tx += v;
|
|
176
|
+
for (const v of y.values())
|
|
177
|
+
ty += v;
|
|
178
|
+
if (tx === 0 && ty === 0)
|
|
179
|
+
return 1;
|
|
180
|
+
for (const [k, v] of x)
|
|
181
|
+
inter += Math.min(v, y.get(k) ?? 0);
|
|
182
|
+
return inter / (tx + ty - inter);
|
|
183
|
+
};
|
|
184
|
+
const cols0 = align(ba.map(bagKey), bb.map(bagKey), (i, j) => bagSim(ba[i], bb[j]));
|
|
185
|
+
// a column filled where the other side's same column is empty and unmatched is new cells, not an inserted column
|
|
186
|
+
for (let i = 0; i < maxC; i++) {
|
|
187
|
+
if (cols0.aToB[i] === null && cols0.bToA[i] === null && (ba[i].size === 0 || bb[i].size === 0)) {
|
|
188
|
+
const before = cols0.aToB.slice(0, i).filter((x) => x !== null), last = before.length ? Math.max(...before) : -1;
|
|
189
|
+
const nextA = cols0.aToB.slice(i + 1).find((x) => x !== null);
|
|
190
|
+
if (last < i && (nextA === undefined || nextA > i)) {
|
|
191
|
+
cols0.aToB[i] = i;
|
|
192
|
+
cols0.bToA[i] = i;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
const cols = cols0.reordered ? cols0 : { ...cols0, ops: opsFrom(cols0.aToB, cols0.bToA) };
|
|
197
|
+
const colPairs = [];
|
|
198
|
+
cols.aToB.forEach((j, i) => { if (j !== null)
|
|
199
|
+
colPairs.push([i + 1, j + 1]); });
|
|
200
|
+
// rows: keyed by constants over the matched columns (formulas count as present, not by text: a formula edit is a modification)
|
|
201
|
+
const rowKey = (s, r, side) => {
|
|
202
|
+
const row = s.rows.get(r);
|
|
203
|
+
if (!row)
|
|
204
|
+
return "";
|
|
205
|
+
let k = "";
|
|
206
|
+
for (const p of colPairs)
|
|
207
|
+
k += constKey(row.get(p[side])) + "\u0001";
|
|
208
|
+
return k;
|
|
209
|
+
};
|
|
210
|
+
const ka = [], kb = [];
|
|
211
|
+
for (let r = 1; r <= rowsA; r++)
|
|
212
|
+
ka.push(rowKey(a, r, 0));
|
|
213
|
+
for (let r = 1; r <= rowsB; r++)
|
|
214
|
+
kb.push(rowKey(b, r, 1));
|
|
215
|
+
const sim = (i, j) => {
|
|
216
|
+
let same = 0, any = 0;
|
|
217
|
+
for (const [ca, cb] of colPairs) {
|
|
218
|
+
const x = full(a, ra, i + 1, ca), y = full(b, rb, j + 1, cb);
|
|
219
|
+
if (x === "" && y === "")
|
|
220
|
+
continue;
|
|
221
|
+
any++;
|
|
222
|
+
if (x === y)
|
|
223
|
+
same++;
|
|
224
|
+
}
|
|
225
|
+
return any === 0 ? 1 : same / any;
|
|
226
|
+
};
|
|
227
|
+
const rows = align(ka, kb, sim);
|
|
228
|
+
const out = { rows, cols, kA: [], kB: [], mA: [], mB: [], aRowK: new Map(), bRowK: new Map(), aColM: new Map(), bColM: new Map() };
|
|
229
|
+
rows.ops.forEach((o, k) => { out.kA.push(o.a === null ? null : o.a + 1); out.kB.push(o.b === null ? null : o.b + 1); if (o.a !== null)
|
|
230
|
+
out.aRowK.set(o.a + 1, k); if (o.b !== null)
|
|
231
|
+
out.bRowK.set(o.b + 1, k); });
|
|
232
|
+
cols.ops.forEach((o, m) => { out.mA.push(o.a === null ? null : o.a + 1); out.mB.push(o.b === null ? null : o.b + 1); if (o.a !== null)
|
|
233
|
+
out.aColM.set(o.a + 1, m); if (o.b !== null)
|
|
234
|
+
out.bColM.set(o.b + 1, m); });
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
/** One sheet that exists on one side only: rows and columns map to themselves. */
|
|
238
|
+
/** How far a sheet reaches: its cells, and the drawings anchored to them (a sheet that holds only charts has no cells and still has something to show). */
|
|
239
|
+
const reach = (s) => s.drawings.reduce((m, d) => (d.anchor ? { r: Math.max(m.r, d.anchor.r2), c: Math.max(m.c, d.anchor.c2) } : m), { r: s.maxRow, c: s.maxCol });
|
|
240
|
+
function oneSided(s, side) {
|
|
241
|
+
const ext = reach(s);
|
|
242
|
+
const ops = Array.from({ length: ext.r }, (_, i) => (side === "a" ? { a: i, b: null } : { a: null, b: i }));
|
|
243
|
+
const cops = Array.from({ length: ext.c }, (_, i) => (side === "a" ? { a: i, b: null } : { a: null, b: i }));
|
|
244
|
+
const mk = (o) => ({ aToB: [], bToA: [], ops: o, reordered: false, moved: 0 });
|
|
245
|
+
const out = { rows: mk(ops), cols: mk(cops), kA: [], kB: [], mA: [], mB: [], aRowK: new Map(), bRowK: new Map(), aColM: new Map(), bColM: new Map() };
|
|
246
|
+
ops.forEach((o, k) => { out.kA.push(o.a === null ? null : o.a + 1); out.kB.push(o.b === null ? null : o.b + 1); if (o.a !== null)
|
|
247
|
+
out.aRowK.set(o.a + 1, k); if (o.b !== null)
|
|
248
|
+
out.bRowK.set(o.b + 1, k); });
|
|
249
|
+
cops.forEach((o, m) => { out.mA.push(o.a === null ? null : o.a + 1); out.mB.push(o.b === null ? null : o.b + 1); if (o.a !== null)
|
|
250
|
+
out.aColM.set(o.a + 1, m); if (o.b !== null)
|
|
251
|
+
out.bColM.set(o.b + 1, m); });
|
|
252
|
+
return out;
|
|
253
|
+
}
|
|
254
|
+
function opsFrom(aToB, bToA) {
|
|
255
|
+
const ops = [];
|
|
256
|
+
let i = 0, j = 0;
|
|
257
|
+
while (i < aToB.length || j < bToA.length) {
|
|
258
|
+
if (i < aToB.length && aToB[i] === null) {
|
|
259
|
+
ops.push({ a: i++, b: null });
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
if (j < bToA.length && bToA[j] === null) {
|
|
263
|
+
ops.push({ a: null, b: j++ });
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
if (i < aToB.length && j < bToA.length) {
|
|
267
|
+
ops.push({ a: i++, b: j++ });
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
if (i < aToB.length)
|
|
271
|
+
ops.push({ a: i++, b: null });
|
|
272
|
+
else
|
|
273
|
+
ops.push({ a: null, b: j++ });
|
|
274
|
+
}
|
|
275
|
+
return ops;
|
|
276
|
+
}
|
|
277
|
+
/** The sides a change exists on, from its mark: added after only, removed before only, anything else both. */
|
|
278
|
+
const sidesOfMark = (mark) => ({ before: mark !== "added", after: mark !== "removed" });
|
|
279
|
+
const newId = (ctx) => `c${++ctx.next}`;
|
|
280
|
+
function push(ctx, ch) {
|
|
281
|
+
const full = { id: newId(ctx), ...ch };
|
|
282
|
+
ctx.changes.push(full);
|
|
283
|
+
return full;
|
|
284
|
+
}
|
|
285
|
+
function unplace(ch, why, side = "after") {
|
|
286
|
+
const un = ch.private?.unplaced ?? {};
|
|
287
|
+
if (side === "both" || side === "before")
|
|
288
|
+
un.before = why;
|
|
289
|
+
if (side === "both" || side === "after")
|
|
290
|
+
un.after = why;
|
|
291
|
+
ch.private = { ...(ch.private ?? {}), unplaced: un };
|
|
292
|
+
const line = `Not in the pictures: ${why}.`;
|
|
293
|
+
ch.lines = [...(ch.lines ?? []).filter((l) => l !== line), line];
|
|
294
|
+
}
|
|
295
|
+
const sheetLabel = (p) => quoteSheet(p.b?.name ?? p.a.name);
|
|
296
|
+
function diffSheet(ctx, p, nameMapA, nameMapB, mapsBySheetA) {
|
|
297
|
+
const a = p.a, b = p.b;
|
|
298
|
+
const al = mapsBySheetA.get(a.name);
|
|
299
|
+
const items = [];
|
|
300
|
+
const sname = b.name;
|
|
301
|
+
const hiddenWhy = (r, c, s) => {
|
|
302
|
+
if (s.state !== "visible")
|
|
303
|
+
return `on the ${s.state === "veryHidden" ? "very hidden" : "hidden"} sheet ${s.name}`;
|
|
304
|
+
if (r !== null && s.hiddenRows.has(r))
|
|
305
|
+
return `in hidden row ${n(r)}`;
|
|
306
|
+
if (c !== null && s.hiddenCols.has(c))
|
|
307
|
+
return `in hidden column ${numToCol(c)}`;
|
|
308
|
+
if (r !== null && s.autoFilter && r > s.autoFilter.r1 && r <= s.autoFilter.r2 && s.hiddenRows.has(r))
|
|
309
|
+
return `in row ${n(r)}, filtered out`;
|
|
310
|
+
return undefined;
|
|
311
|
+
};
|
|
312
|
+
// a sheet whose cells were all removed: one change, its old top pictured, the after side says it is empty
|
|
313
|
+
if (b.rows.size === 0 && a.rows.size > 0) {
|
|
314
|
+
const ch = push(ctx, { mark: "removed", label: `Sheet ${sheetLabel(p)}`, text: `Sheet ${sname} is now empty: its ${n(a.maxRow)} rows by ${n(a.maxCol)} columns were cleared`, kind: "sheet-emptied", private: { sheet: sname } });
|
|
315
|
+
if (a.state !== "visible")
|
|
316
|
+
unplace(ch, hiddenWhy(null, null, a), "before");
|
|
317
|
+
else
|
|
318
|
+
items.push({ change: ch, k0: 0, k1: Math.min(al.kA.length, 30) - 1, m0: 0, m1: Math.min(al.mA.length, 12) - 1, score: 100, uniform: false, ...{ emptied: true } });
|
|
319
|
+
return { items, pair: p, al };
|
|
320
|
+
}
|
|
321
|
+
// reorder
|
|
322
|
+
if (al.rows.reordered) {
|
|
323
|
+
const ch = push(ctx, { mark: "moved", label: `${sheetLabel(p)} rows`, text: `${n(al.rows.moved)} rows reordered (sorted); their values did not change except where listed`, kind: "reorder" });
|
|
324
|
+
unplace(ch, "a reorder moves rows; the pictures show the changed cells", "both");
|
|
325
|
+
}
|
|
326
|
+
// structural runs: rows
|
|
327
|
+
const runs = (ops, side) => {
|
|
328
|
+
const out = [];
|
|
329
|
+
let s = -1;
|
|
330
|
+
for (let k = 0; k <= ops.length; k++) {
|
|
331
|
+
const hit = k < ops.length && (side === "a" ? ops[k].b === null && ops[k].a !== null : ops[k].a === null && ops[k].b !== null);
|
|
332
|
+
if (hit && s < 0)
|
|
333
|
+
s = k;
|
|
334
|
+
if (!hit && s >= 0) {
|
|
335
|
+
out.push([s, k - 1]);
|
|
336
|
+
s = -1;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return out;
|
|
340
|
+
};
|
|
341
|
+
const lastMatchedB = (() => { let m = 0; for (const o of al.rows.ops)
|
|
342
|
+
if (o.a !== null && o.b !== null)
|
|
343
|
+
m = Math.max(m, o.b + 1); return m; })();
|
|
344
|
+
const lastMatchedA = (() => { let m = 0; for (const o of al.rows.ops)
|
|
345
|
+
if (o.a !== null && o.b !== null)
|
|
346
|
+
m = Math.max(m, o.a + 1); return m; })();
|
|
347
|
+
const usedCols = (s, r1, r2, colM) => {
|
|
348
|
+
let lo = Infinity, hi = -Infinity;
|
|
349
|
+
for (let r = r1; r <= r2; r++)
|
|
350
|
+
for (const c of s.rows.get(r)?.keys() ?? []) {
|
|
351
|
+
const m = colM.get(c);
|
|
352
|
+
if (m !== undefined) {
|
|
353
|
+
lo = Math.min(lo, m);
|
|
354
|
+
hi = Math.max(hi, m);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
return lo === Infinity ? [0, Math.max(0, colM.size - 1)] : [lo, hi];
|
|
358
|
+
};
|
|
359
|
+
const emptyRows = (s, r1, r2) => { for (let r = r1; r <= r2; r++)
|
|
360
|
+
if (s.rows.get(r)?.size)
|
|
361
|
+
return false; return true; };
|
|
362
|
+
for (const [k0, k1] of runs(al.rows.ops, "b")) {
|
|
363
|
+
const r1 = al.kB[k0], r2 = al.kB[k1];
|
|
364
|
+
if (emptyRows(b, r1, r2))
|
|
365
|
+
continue;
|
|
366
|
+
const count = r2 - r1 + 1;
|
|
367
|
+
const end = r1 > lastMatchedB;
|
|
368
|
+
const before = r1 - 1;
|
|
369
|
+
// what the inserted rows hold, by column: a formula filled down reads as one line
|
|
370
|
+
const lines = [];
|
|
371
|
+
for (let c = 1; c <= b.maxCol && lines.length < 6; c++) {
|
|
372
|
+
const first = b.rows.get(r1)?.get(c);
|
|
373
|
+
if (!first || first.f === null)
|
|
374
|
+
continue;
|
|
375
|
+
const cache = new Map();
|
|
376
|
+
const sig = r1c1(b, r1, c, first, cache);
|
|
377
|
+
let same = 0;
|
|
378
|
+
for (let r = r1; r <= r2; r++) {
|
|
379
|
+
const x = b.rows.get(r)?.get(c);
|
|
380
|
+
if (x && x.f !== null && r1c1(b, r, c, x, cache) === sig)
|
|
381
|
+
same++;
|
|
382
|
+
}
|
|
383
|
+
lines.push(same === count ? `Column ${numToCol(c)}: =${first.f} filled down` : `Column ${numToCol(c)}: formulas in ${n(same)} of ${n(count)} rows, starting =${first.f}`);
|
|
384
|
+
}
|
|
385
|
+
const ch = push(ctx, { mark: "added", label: `${sheetLabel(p)}!${r1}:${r2}`, text: end ? `${n(count)} row${count === 1 ? "" : "s"} added at the end (${rowsText(r1, r2)})` : `${n(count)} row${count === 1 ? "" : "s"} inserted after row ${n(before)} (now ${rowsText(r1, r2)})`, ...(lines.length ? { lines } : {}), kind: "rows-inserted", private: { sheet: sname, rows: [r1, r2] } });
|
|
386
|
+
const [m0, m1] = usedCols(b, r1, r2, al.bColM);
|
|
387
|
+
const hid = [...Array(count).keys()].every((i) => b.hiddenRows.has(r1 + i));
|
|
388
|
+
if (b.state !== "visible" || hid) {
|
|
389
|
+
unplace(ch, hiddenWhy(r1, null, b) ?? "hidden");
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
items.push({ change: ch, k0, k1, m0, m1, score: 30, uniform: true });
|
|
393
|
+
}
|
|
394
|
+
for (const [k0, k1] of runs(al.rows.ops, "a")) {
|
|
395
|
+
const r1 = al.kA[k0], r2 = al.kA[k1];
|
|
396
|
+
if (emptyRows(a, r1, r2))
|
|
397
|
+
continue;
|
|
398
|
+
const count = r2 - r1 + 1;
|
|
399
|
+
const end = r1 > lastMatchedA;
|
|
400
|
+
const ch = push(ctx, { mark: "removed", label: `${quoteSheet(a.name)}!${r1}:${r2}`, text: end ? `${n(count)} row${count === 1 ? "" : "s"} deleted at the end (were ${rowsText(r1, r2)})` : `${n(count)} row${count === 1 ? "" : "s"} deleted (were ${rowsText(r1, r2)})`, kind: "rows-deleted", private: { sheet: a.name, rows: [r1, r2] } });
|
|
401
|
+
const [m0, m1] = usedCols(a, r1, r2, al.aColM);
|
|
402
|
+
if (a.state !== "visible") {
|
|
403
|
+
unplace(ch, hiddenWhy(r1, null, a), "before");
|
|
404
|
+
continue;
|
|
405
|
+
}
|
|
406
|
+
items.push({ change: ch, k0, k1, m0, m1, score: 30, uniform: true });
|
|
407
|
+
}
|
|
408
|
+
// structural runs: columns
|
|
409
|
+
for (const [m0, m1] of runs(al.cols.ops, "b")) {
|
|
410
|
+
const c1 = al.mB[m0], c2 = al.mB[m1];
|
|
411
|
+
if ([...Array(c2 - c1 + 1).keys()].every((i) => ![...b.rows.values()].some((row) => row.has(c1 + i))))
|
|
412
|
+
continue;
|
|
413
|
+
const ch = push(ctx, { mark: "added", label: `${sheetLabel(p)}!${numToCol(c1)}:${numToCol(c2)}`, text: `${c2 - c1 + 1 === 1 ? "Column" : `${c2 - c1 + 1} columns`} ${c1 === c2 ? numToCol(c1) : `${numToCol(c1)}-${numToCol(c2)}`} inserted`, kind: "cols-inserted" });
|
|
414
|
+
items.push({ change: ch, k0: 0, k1: Math.min(al.kB.length - 1, LIMITS.maxAreaRows), m0, m1, score: 30, uniform: true });
|
|
415
|
+
}
|
|
416
|
+
for (const [m0, m1] of runs(al.cols.ops, "a")) {
|
|
417
|
+
const c1 = al.mA[m0], c2 = al.mA[m1];
|
|
418
|
+
if ([...Array(c2 - c1 + 1).keys()].every((i) => ![...a.rows.values()].some((row) => row.has(c1 + i))))
|
|
419
|
+
continue;
|
|
420
|
+
const ch = push(ctx, { mark: "removed", label: `${quoteSheet(a.name)}!${numToCol(c1)}:${numToCol(c2)}`, text: `${c2 - c1 + 1 === 1 ? "Column" : `${c2 - c1 + 1} columns`} ${c1 === c2 ? numToCol(c1) : `${numToCol(c1)}-${numToCol(c2)}`} deleted`, kind: "cols-deleted" });
|
|
421
|
+
items.push({ change: ch, k0: 0, k1: Math.min(al.kA.length - 1, LIMITS.maxAreaRows), m0, m1, score: 30, uniform: true });
|
|
422
|
+
}
|
|
423
|
+
// cells under the maps
|
|
424
|
+
const cacheA = new Map(), cacheB = new Map();
|
|
425
|
+
const rowMapOf = (sheetName) => {
|
|
426
|
+
const m = mapsBySheetA.get(sheetName);
|
|
427
|
+
if (!m)
|
|
428
|
+
return (r) => r;
|
|
429
|
+
return (r) => { const k = m.aRowK.get(r); if (k === undefined)
|
|
430
|
+
return r + (m.kB.length - m.kA.length > 0 && r > (m.kA.length) ? m.kB.length - m.kA.length : 0); for (let x = k; x < m.kB.length; x++) {
|
|
431
|
+
const rb = m.kB[x];
|
|
432
|
+
if (rb !== null && rb !== undefined)
|
|
433
|
+
return rb;
|
|
434
|
+
} return r; };
|
|
435
|
+
};
|
|
436
|
+
const colMapOf = (sheetName) => {
|
|
437
|
+
const m = mapsBySheetA.get(sheetName);
|
|
438
|
+
if (!m)
|
|
439
|
+
return (c) => c;
|
|
440
|
+
return (c) => { const x = m.aColM.get(c); if (x === undefined)
|
|
441
|
+
return c; for (let y = x; y < m.mB.length; y++) {
|
|
442
|
+
const cb = m.mB[y];
|
|
443
|
+
if (cb !== null && cb !== undefined)
|
|
444
|
+
return cb;
|
|
445
|
+
} return c; };
|
|
446
|
+
};
|
|
447
|
+
const remapped = (f) => {
|
|
448
|
+
try {
|
|
449
|
+
return remapFormulaRefs(f, a.name, (sh, r) => rowMapOf(sh)(r), (sh, c) => colMapOf(sh)(c)).text;
|
|
450
|
+
}
|
|
451
|
+
catch {
|
|
452
|
+
return f;
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
const diffs = [];
|
|
456
|
+
for (let k = 0; k < al.kA.length; k++) {
|
|
457
|
+
const ra = al.kA[k], rb = al.kB[k];
|
|
458
|
+
if (ra === null || rb === null || ra === undefined || rb === undefined)
|
|
459
|
+
continue;
|
|
460
|
+
const rowA = a.rows.get(ra), rowB = b.rows.get(rb);
|
|
461
|
+
if (!rowA && !rowB)
|
|
462
|
+
continue;
|
|
463
|
+
for (let m = 0; m < al.mA.length; m++) {
|
|
464
|
+
const ca = al.mA[m], cb = al.mB[m];
|
|
465
|
+
if (ca === null || cb === null || ca === undefined || cb === undefined)
|
|
466
|
+
continue;
|
|
467
|
+
const x = rowA?.get(ca), y = rowB?.get(cb);
|
|
468
|
+
if (x === undefined && y === undefined)
|
|
469
|
+
continue;
|
|
470
|
+
let kind;
|
|
471
|
+
if (x?.f != null && y?.f != null) {
|
|
472
|
+
const fa = canonNames(remapped(x.f), nameMapA), fb = canonNames(y.f, nameMapB);
|
|
473
|
+
if (fa.replace(/\s+/g, "") !== fb.replace(/\s+/g, ""))
|
|
474
|
+
kind = "input";
|
|
475
|
+
else if (x.cached && y.cached && !valEq(x.v, y.v))
|
|
476
|
+
kind = "derived";
|
|
477
|
+
}
|
|
478
|
+
else if ((x?.f ?? null) !== null || (y?.f ?? null) !== null)
|
|
479
|
+
kind = "input";
|
|
480
|
+
else if (!valEq(x?.v ?? null, y?.v ?? null))
|
|
481
|
+
kind = "input";
|
|
482
|
+
if (kind === undefined && (x?.look ?? "") !== (y?.look ?? "") && !(x === undefined || y === undefined))
|
|
483
|
+
kind = "format";
|
|
484
|
+
if (kind === undefined)
|
|
485
|
+
continue;
|
|
486
|
+
const sa = x ? (x.f !== null ? `=${r1c1(a, ra, ca, x, cacheA, nameMapA)}` : `c:${x.v === null ? "empty" : typeof x.v}`) : "none";
|
|
487
|
+
const sb = y ? (y.f !== null ? `=${r1c1(b, rb, cb, y, cacheB, nameMapB)}` : `c:${y.v === null ? "empty" : typeof y.v}`) : "none";
|
|
488
|
+
const sig = kind === "format" ? `fmt|${x?.look}|${y?.look}` : `${kind}|${sb}|${sa}|${y && isErr(y.v) ? "err" : ""}`;
|
|
489
|
+
diffs.push({ k, m, ra, rb, ca, cb, ...(x ? { a: x } : {}), ...(y ? { b: y } : {}), kind, sig });
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
// derived cells: one summary per cause, after the input changes exist
|
|
493
|
+
const inputs = diffs.filter((d) => d.kind !== "derived");
|
|
494
|
+
const derived = diffs.filter((d) => d.kind === "derived");
|
|
495
|
+
// patterns per column
|
|
496
|
+
const byCol = new Map();
|
|
497
|
+
for (const d of inputs) {
|
|
498
|
+
const l = byCol.get(d.m);
|
|
499
|
+
if (l)
|
|
500
|
+
l.push(d);
|
|
501
|
+
else
|
|
502
|
+
byCol.set(d.m, [d]);
|
|
503
|
+
}
|
|
504
|
+
const owner = new Map();
|
|
505
|
+
const singles = [];
|
|
506
|
+
const describeCell = (d) => {
|
|
507
|
+
const bw = cellWords(d.a), aw = cellWords(d.b);
|
|
508
|
+
const mark = bw === undefined ? "added" : aw === undefined ? "removed" : "changed";
|
|
509
|
+
const lines = [];
|
|
510
|
+
if (d.kind === "format")
|
|
511
|
+
lines.push("Formatting changed; the value did not.");
|
|
512
|
+
if (d.b && isErr(d.b.v))
|
|
513
|
+
lines.push(`Shows the error ${valueWords(d.b.v)}.`);
|
|
514
|
+
if (d.b?.f && VOLATILE.test(d.b.f))
|
|
515
|
+
lines.push("Uses a function whose value differs on every open.");
|
|
516
|
+
return { mark, label: `${quoteSheet(sname)}!${addr(d.rb, d.cb)}`, ...(bw !== undefined && mark !== "added" ? { before: bw } : {}), ...(aw !== undefined && mark !== "removed" ? { after: aw } : {}), ...(lines.length ? { lines } : {}), kind: d.kind === "format" ? "format" : d.a?.f || d.b?.f ? "formula" : "value", private: { sheet: sname, cell: addr(d.rb, d.cb), beforeCell: addr(d.ra, d.ca) } };
|
|
517
|
+
};
|
|
518
|
+
const cellScore = (d) => {
|
|
519
|
+
if (d.b && isErr(d.b.v) && !(d.a && isErr(d.a.v)))
|
|
520
|
+
return 80;
|
|
521
|
+
if (d.kind === "format")
|
|
522
|
+
return 5;
|
|
523
|
+
const f = d.b?.f ?? d.a?.f;
|
|
524
|
+
let s = f ? 40 : 20;
|
|
525
|
+
if (f && /\b(SUM|SUBTOTAL|AVERAGE|COUNT|MAX|MIN)\s*\(/i.test(f))
|
|
526
|
+
s += 60;
|
|
527
|
+
const x = d.a?.v, y = d.b?.v;
|
|
528
|
+
if (typeof x === "number" && typeof y === "number" && Math.abs(y - x) > 0.5 * Math.max(Math.abs(x), 1e-9))
|
|
529
|
+
s += 10;
|
|
530
|
+
return s;
|
|
531
|
+
};
|
|
532
|
+
for (const [m, list] of byCol) {
|
|
533
|
+
list.sort((x, y) => x.k - y.k);
|
|
534
|
+
const segs = [];
|
|
535
|
+
for (const d of list) {
|
|
536
|
+
const last = segs[segs.length - 1];
|
|
537
|
+
if (last && last.sig === d.sig && last.cells[last.cells.length - 1].k === d.k - 1)
|
|
538
|
+
last.cells.push(d);
|
|
539
|
+
else
|
|
540
|
+
segs.push({ sig: d.sig, cells: [d], except: [] });
|
|
541
|
+
}
|
|
542
|
+
// merge a run, a short run of exceptions (up to 3 rows), and a run with the same change again
|
|
543
|
+
for (let i = 0; i + 2 < segs.length;) {
|
|
544
|
+
const x = segs[i], e = segs[i + 1], y = segs[i + 2];
|
|
545
|
+
const contiguous = x.cells[x.cells.length - 1].k + 1 === e.cells[0].k && e.cells[e.cells.length - 1].k + 1 === y.cells[0].k;
|
|
546
|
+
if (x.sig === y.sig && contiguous && e.cells.length + e.except.length <= 3 && x.cells.length + y.cells.length >= 3) {
|
|
547
|
+
segs.splice(i, 3, { sig: x.sig, cells: [...x.cells, ...y.cells], except: [...x.except, ...e.cells, ...e.except, ...y.except] });
|
|
548
|
+
}
|
|
549
|
+
else
|
|
550
|
+
i++;
|
|
551
|
+
}
|
|
552
|
+
// a run followed by a short tail of exceptions to the end (boundary rows): attach them
|
|
553
|
+
for (let i = 0; i + 1 < segs.length; i++) {
|
|
554
|
+
const x = segs[i], e = segs[i + 1];
|
|
555
|
+
if (x.cells.length >= 3 && e.cells.length === 1 && e.except.length === 0 && x.cells[x.cells.length - 1].k + 1 === e.cells[0].k && (i + 2 >= segs.length || segs[i + 2].cells[0].k > e.cells[0].k + 1)) {
|
|
556
|
+
x.except.push(...e.cells);
|
|
557
|
+
segs.splice(i + 1, 1);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
for (const s of segs) {
|
|
561
|
+
if (s.cells.length < 3) {
|
|
562
|
+
singles.push(...s.cells, ...s.except);
|
|
563
|
+
continue;
|
|
564
|
+
}
|
|
565
|
+
const first = s.cells[0], last = s.cells[s.cells.length - 1];
|
|
566
|
+
const allK = [...s.cells, ...s.except].map((d) => d.k);
|
|
567
|
+
const k0 = Math.min(...allK), k1 = Math.max(...allK);
|
|
568
|
+
const r1 = al.kB[k0], r2 = al.kB[k1], c = first.cb;
|
|
569
|
+
const fb = cellWords(first.b), fa = cellWords(first.a);
|
|
570
|
+
const isFormula = !!(first.b?.f || first.a?.f);
|
|
571
|
+
const what = first.kind === "format" ? "Formatting changed" : !first.a ? (isFormula ? "Formula filled in" : "Values entered in") : !first.b ? "Cleared" : isFormula ? "Formula changed in" : "Values changed in";
|
|
572
|
+
const exc = s.except.map((d) => al.kB[d.k]).sort((x, y) => x - y);
|
|
573
|
+
const text = `${what} ${n(s.cells.length)} cells, ${rowsText(al.kB[first.k], al.kB[last.k])}${exc.length ? `, except ${exc.length === 1 ? "row" : "rows"} ${exc.map(n).join(", ")}` : ""}`;
|
|
574
|
+
const lastB = cellWords(last.b), lastA = cellWords(last.a);
|
|
575
|
+
const sideWords = (w, wl) => (w === undefined ? undefined : isFormula ? `${w} (filled down)` : wl !== undefined && wl !== w ? `${w} \u2026 ${wl}` : w);
|
|
576
|
+
const parent = push(ctx, { mark: !first.a ? "added" : !first.b ? "removed" : "changed", label: `${quoteSheet(sname)}!${numToCol(c)}${r1}:${numToCol(c)}${r2}`, ...(first.a && sideWords(fa, lastA) !== undefined ? { before: sideWords(fa, lastA) } : {}), ...(first.b && sideWords(fb, lastB) !== undefined ? { after: sideWords(fb, lastB) } : {}), text, kind: "pattern", private: { sheet: sname, range: `${numToCol(c)}${r1}:${numToCol(c)}${r2}`, count: s.cells.length, constant: !isFormula, rows: [r1, r2], col: c } });
|
|
577
|
+
for (const d of s.cells)
|
|
578
|
+
owner.set(d, parent);
|
|
579
|
+
const keep = [];
|
|
580
|
+
let sc = Math.max(...s.cells.slice(0, 1).map(cellScore));
|
|
581
|
+
for (const d of s.except) {
|
|
582
|
+
const ch = push(ctx, { ...describeCell(d), parent: parent.id, lines: [...(describeCell(d).lines ?? []), "Breaks the pattern of the cells around it."] });
|
|
583
|
+
owner.set(d, ch);
|
|
584
|
+
ctx.exceptions.set(ch.id, { before: { r1: d.ra, r2: d.ra, c1: d.ca, c2: d.ca }, after: { r1: d.rb, r2: d.rb, c1: d.cb, c2: d.cb } });
|
|
585
|
+
keep.push(d.k);
|
|
586
|
+
sc = Math.max(sc, cellScore(d));
|
|
587
|
+
const why = hiddenWhy(d.rb, d.cb, b);
|
|
588
|
+
if (why)
|
|
589
|
+
unplace(ch, why);
|
|
590
|
+
}
|
|
591
|
+
const hidden = hiddenWhy(null, c, b);
|
|
592
|
+
if (hidden || b.state !== "visible") {
|
|
593
|
+
unplace(parent, hidden ?? hiddenWhy(null, null, b));
|
|
594
|
+
continue;
|
|
595
|
+
}
|
|
596
|
+
items.push({ change: parent, k0, k1, m0: m, m1: m, score: sc, uniform: true, keep });
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
// constant runs side by side over the same rows: one rectangle of values
|
|
600
|
+
const constItems = items.filter((it) => it.change.kind === "pattern" && it.change.private.constant === true && !(it.keep ?? []).length);
|
|
601
|
+
constItems.sort((x, y) => x.k0 - y.k0 || x.m0 - y.m0);
|
|
602
|
+
for (let i = 0; i < constItems.length; i++) {
|
|
603
|
+
const x = constItems[i];
|
|
604
|
+
if (!items.includes(x))
|
|
605
|
+
continue;
|
|
606
|
+
let m1 = x.m1, count = x.change.private.count;
|
|
607
|
+
const absorbed = [];
|
|
608
|
+
for (const y of constItems)
|
|
609
|
+
if (y !== x && items.includes(y) && y.k0 === x.k0 && y.k1 === x.k1 && y.m0 === m1 + 1 && y.change.mark === x.change.mark) {
|
|
610
|
+
m1 = y.m1;
|
|
611
|
+
count += y.change.private.count;
|
|
612
|
+
absorbed.push(y);
|
|
613
|
+
}
|
|
614
|
+
if (!absorbed.length)
|
|
615
|
+
continue;
|
|
616
|
+
const c1 = al.mB[x.m0], c2 = al.mB[m1], r1 = al.kB[x.k0], r2 = al.kB[x.k1];
|
|
617
|
+
for (const y of absorbed) {
|
|
618
|
+
items.splice(items.indexOf(y), 1);
|
|
619
|
+
ctx.changes.splice(ctx.changes.indexOf(y.change), 1);
|
|
620
|
+
}
|
|
621
|
+
Object.assign(x.change, { label: `${quoteSheet(sname)}!${numToCol(c1)}${r1}:${numToCol(c2)}${r2}`, text: `Values ${x.change.mark === "added" ? "entered in" : x.change.mark === "removed" ? "cleared from" : "changed in"} ${n(count)} cells, ${numToCol(c1)}${r1}:${numToCol(c2)}${r2}`, private: { ...x.change.private, range: `${numToCol(c1)}${r1}:${numToCol(c2)}${r2}`, count } });
|
|
622
|
+
delete x.change.before;
|
|
623
|
+
delete x.change.after;
|
|
624
|
+
x.m1 = m1;
|
|
625
|
+
}
|
|
626
|
+
// single edits along one row (an edit across many columns): one run
|
|
627
|
+
const byRow = new Map();
|
|
628
|
+
for (const d of singles) {
|
|
629
|
+
const l = byRow.get(d.k);
|
|
630
|
+
if (l)
|
|
631
|
+
l.push(d);
|
|
632
|
+
else
|
|
633
|
+
byRow.set(d.k, [d]);
|
|
634
|
+
}
|
|
635
|
+
const inRun = new Set();
|
|
636
|
+
for (const [k, list] of byRow) {
|
|
637
|
+
list.sort((x, y) => x.m - y.m);
|
|
638
|
+
let s0 = 0;
|
|
639
|
+
for (let i = 1; i <= list.length; i++) {
|
|
640
|
+
if (i < list.length && list[i].m === list[i - 1].m + 1 && list[i].kind === list[0 + s0].kind && (list[i].a?.f != null) === (list[s0].a?.f != null))
|
|
641
|
+
continue;
|
|
642
|
+
const run = list.slice(s0, i);
|
|
643
|
+
s0 = i;
|
|
644
|
+
if (run.length < 3)
|
|
645
|
+
continue;
|
|
646
|
+
const r = al.kB[k], c1 = run[0].cb, c2 = run[run.length - 1].cb;
|
|
647
|
+
const kindWord = run[0].kind === "format" ? "Formatting changed in" : run.some((d) => d.a?.f || d.b?.f) ? "Formulas changed in" : "Values changed in";
|
|
648
|
+
const ch = push(ctx, { mark: run.every((d) => !d.a) ? "added" : run.every((d) => !d.b) ? "removed" : "changed", label: `${quoteSheet(sname)}!${numToCol(c1)}${r}:${numToCol(c2)}${r}`, text: `${kindWord} ${n(run.length)} cells across row ${n(r)}, ${numToCol(c1)}${r}:${numToCol(c2)}${r}`, before: `${cellWords(run[0].a) ?? "empty"} \u2026 ${cellWords(run[run.length - 1].a) ?? "empty"}`, after: `${cellWords(run[0].b) ?? "empty"} \u2026 ${cellWords(run[run.length - 1].b) ?? "empty"}`, kind: "row-run", private: { sheet: sname, range: `${numToCol(c1)}${r}:${numToCol(c2)}${r}`, count: run.length } });
|
|
649
|
+
for (const d of run) {
|
|
650
|
+
inRun.add(d);
|
|
651
|
+
owner.set(d, ch);
|
|
652
|
+
}
|
|
653
|
+
const why = hiddenWhy(r, null, b);
|
|
654
|
+
if (why) {
|
|
655
|
+
unplace(ch, why);
|
|
656
|
+
continue;
|
|
657
|
+
}
|
|
658
|
+
items.push({ change: ch, k0: k, k1: k, m0: run[0].m, m1: run[run.length - 1].m, score: Math.max(...run.map(cellScore)), uniform: true });
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
// single edits: grouped when many share a column and a kind of edit
|
|
662
|
+
const singleByColSig = new Map();
|
|
663
|
+
for (const d of singles) {
|
|
664
|
+
if (inRun.has(d))
|
|
665
|
+
continue;
|
|
666
|
+
const key = `${d.m}|${d.sig}`;
|
|
667
|
+
const l = singleByColSig.get(key);
|
|
668
|
+
if (l)
|
|
669
|
+
l.push(d);
|
|
670
|
+
else
|
|
671
|
+
singleByColSig.set(key, [d]);
|
|
672
|
+
}
|
|
673
|
+
for (const [, list] of singleByColSig) {
|
|
674
|
+
list.sort((x, y) => x.k - y.k);
|
|
675
|
+
let pictured = list;
|
|
676
|
+
let groupParent;
|
|
677
|
+
if (list.length >= 8) {
|
|
678
|
+
const c = list[0].cb;
|
|
679
|
+
const r1 = al.kB[list[0].k], r2 = al.kB[list[list.length - 1].k];
|
|
680
|
+
groupParent = push(ctx, { mark: "changed", label: `${quoteSheet(sname)}!${numToCol(c)}${r1}:${numToCol(c)}${r2}`, text: `${n(list.length)} single-cell edits in column ${numToCol(c)}, ${rowsText(r1, r2)}; the first 3 are pictured`, kind: "group", private: { sheet: sname, count: list.length } });
|
|
681
|
+
pictured = list.slice(0, 3);
|
|
682
|
+
}
|
|
683
|
+
list.forEach((d, i) => {
|
|
684
|
+
if (groupParent && i >= LIMITS.listChildren)
|
|
685
|
+
return;
|
|
686
|
+
const ch = push(ctx, { ...describeCell(d), ...(groupParent ? { parent: groupParent.id } : {}) });
|
|
687
|
+
owner.set(d, ch);
|
|
688
|
+
const why = hiddenWhy(d.rb, d.cb, b);
|
|
689
|
+
if (why) {
|
|
690
|
+
unplace(ch, why);
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
if (groupParent && !pictured.includes(d)) {
|
|
694
|
+
unplace(ch, "the same kind of edit as the pictured ones in this column");
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
items.push({ change: ch, k0: d.k, k1: d.k, m0: d.m, m1: d.m, score: cellScore(d), uniform: false });
|
|
698
|
+
});
|
|
699
|
+
if (groupParent && list.length > LIMITS.listChildren)
|
|
700
|
+
groupParent.lines = [`${n(list.length - LIMITS.listChildren)} more edits like these are not listed one by one.`];
|
|
701
|
+
}
|
|
702
|
+
// derived summaries, by cause
|
|
703
|
+
if (derived.length > 0) {
|
|
704
|
+
const vol = derived.filter((d) => d.b?.f && VOLATILE.test(d.b.f));
|
|
705
|
+
const real = derived.filter((d) => !vol.includes(d));
|
|
706
|
+
if (vol.length > 0)
|
|
707
|
+
ctx.caveats.push(`${n(vol.length)} cell${vol.length === 1 ? "" : "s"} on ${sname} use${vol.length === 1 ? "s" : ""} a function (RAND, NOW, TODAY, OFFSET, INDIRECT) whose value differs every time the file opens; not listed as changes.`);
|
|
708
|
+
const groups = new Map();
|
|
709
|
+
for (const d of real) {
|
|
710
|
+
let cause = "";
|
|
711
|
+
try {
|
|
712
|
+
const refs = extractFormulaRefs(d.b.f, sname).refs;
|
|
713
|
+
outer: for (const ref of refs)
|
|
714
|
+
for (const ch of ctx.changes) {
|
|
715
|
+
const pv = ch.private;
|
|
716
|
+
if (!pv || ch.kind === "derived" || !pv.sheet)
|
|
717
|
+
continue;
|
|
718
|
+
const sh = pv.sheet;
|
|
719
|
+
if (sh !== ref.sheet && !(ref.sheet === a.name && sh === sname))
|
|
720
|
+
continue;
|
|
721
|
+
const box = pv.cell ? rangeOf(pv.cell) : pv.range ? rangeOf(pv.range) : null;
|
|
722
|
+
if (box && box.r1 <= ref.r2 && box.r2 >= ref.r1 && box.c1 <= ref.c2 && box.c2 >= ref.c1) {
|
|
723
|
+
cause = ch.parent ?? ch.id;
|
|
724
|
+
break outer;
|
|
725
|
+
}
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
catch { /* not resolvable */ }
|
|
729
|
+
const l = groups.get(cause);
|
|
730
|
+
if (l)
|
|
731
|
+
l.push(d);
|
|
732
|
+
else
|
|
733
|
+
groups.set(cause, [d]);
|
|
734
|
+
}
|
|
735
|
+
for (const [cause, list] of groups) {
|
|
736
|
+
const rs = list.map((d) => d.rb), cs = list.map((d) => d.cb);
|
|
737
|
+
const box = { r1: Math.min(...rs), r2: Math.max(...rs), c1: Math.min(...cs), c2: Math.max(...cs) };
|
|
738
|
+
const causeCh = ctx.changes.find((c) => c.id === cause);
|
|
739
|
+
const ch = push(ctx, { mark: "changed", label: at(sname, box), text: `${n(list.length)} cell${list.length === 1 ? "" : "s"} recalculated: formula${list.length === 1 ? "" : "s"} unchanged, value${list.length === 1 ? "" : "s"} changed${causeCh ? ` because of ${causeCh.label}` : ""}`, kind: "derived", ...(cause ? { causedBy: cause } : {}), private: { sheet: sname, count: list.length } });
|
|
740
|
+
unplace(ch, "values Excel recalculated from a changed input; the input is what is pictured", "both");
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
return { items, pair: p, al };
|
|
744
|
+
}
|
|
745
|
+
export function rangeOf(s) {
|
|
746
|
+
const m = /^\$?([A-Z]{1,3})\$?(\d+)(?::\$?([A-Z]{1,3})\$?(\d+))?$/.exec(s.replace(/^.*!/, ""));
|
|
747
|
+
if (!m)
|
|
748
|
+
return null;
|
|
749
|
+
const col = (x) => [...x].reduce((v, ch) => v * 26 + ch.charCodeAt(0) - 64, 0);
|
|
750
|
+
const c1 = col(m[1]), r1 = Number(m[2]), c2 = m[3] ? col(m[3]) : c1, r2 = m[4] ? Number(m[4]) : r1;
|
|
751
|
+
return { r1: Math.min(r1, r2), r2: Math.max(r1, r2), c1: Math.min(c1, c2), c2: Math.max(c1, c2) };
|
|
752
|
+
}
|
|
753
|
+
export function clusterItems(items, L = LIMITS) {
|
|
754
|
+
const blocks = [];
|
|
755
|
+
const sorted = [...items].sort((x, y) => x.k0 - y.k0 || x.m0 - y.m0);
|
|
756
|
+
for (const it of sorted) {
|
|
757
|
+
let joined;
|
|
758
|
+
for (const bl of blocks) {
|
|
759
|
+
const rowGap = Math.max(0, Math.max(it.k0, bl.k0) - Math.min(it.k1, bl.k1) - 1);
|
|
760
|
+
const colGap = Math.max(0, Math.max(it.m0, bl.m0) - Math.min(it.m1, bl.m1) - 1);
|
|
761
|
+
const overlap = it.k0 <= bl.k1 && it.k1 >= bl.k0 && it.m0 <= bl.m1 && it.m1 >= bl.m0;
|
|
762
|
+
if (!overlap && (rowGap > L.gapRows || colGap > L.gapCols))
|
|
763
|
+
continue;
|
|
764
|
+
const k0 = Math.min(bl.k0, it.k0), k1 = Math.max(bl.k1, it.k1), m0 = Math.min(bl.m0, it.m0), m1 = Math.max(bl.m1, it.m1);
|
|
765
|
+
const big = k1 - k0 + 1 > L.maxBlockRows || m1 - m0 + 1 > L.maxBlockCols;
|
|
766
|
+
// an item already past the caps on its own joins only what it overlaps; otherwise the caps hold
|
|
767
|
+
if (big && !overlap)
|
|
768
|
+
continue;
|
|
769
|
+
joined = bl;
|
|
770
|
+
bl.items.push(it);
|
|
771
|
+
bl.k0 = k0;
|
|
772
|
+
bl.k1 = k1;
|
|
773
|
+
bl.m0 = m0;
|
|
774
|
+
bl.m1 = m1;
|
|
775
|
+
bl.score = Math.max(bl.score, it.score) + (bl.items.length > 1 ? 1 : 0);
|
|
776
|
+
break;
|
|
777
|
+
}
|
|
778
|
+
if (!joined)
|
|
779
|
+
blocks.push({ items: [it], k0: it.k0, k1: it.k1, m0: it.m0, m1: it.m1, score: it.score });
|
|
780
|
+
}
|
|
781
|
+
return blocks;
|
|
782
|
+
}
|
|
783
|
+
/** Row windows (merged space) for a block: whole when short, else folded around what must stay visible. */
|
|
784
|
+
export function rowPieces(bl, kMax, L = LIMITS) {
|
|
785
|
+
const h = bl.k1 - bl.k0 + 1;
|
|
786
|
+
if (h + 2 * L.ctxRows <= L.maxAreaRows)
|
|
787
|
+
return { pieces: [win(bl.k0, bl.k1, kMax, L.ctxRows, L.minRows)], folded: [] };
|
|
788
|
+
// rows that must show: every non-uniform item's rows, each uniform item's ends and its exceptions
|
|
789
|
+
const must = [];
|
|
790
|
+
for (const it of bl.items) {
|
|
791
|
+
if (!it.uniform || it.k1 - it.k0 + 1 <= 2 * L.foldKeep + L.foldMin) {
|
|
792
|
+
must.push([it.k0, it.k1]);
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
must.push([it.k0, it.k0 + L.foldKeep - 1], [it.k1 - L.foldKeep + 1, it.k1]);
|
|
796
|
+
for (const k of it.keep ?? [])
|
|
797
|
+
must.push([k, k]);
|
|
798
|
+
}
|
|
799
|
+
must.sort((x, y) => x[0] - y[0]);
|
|
800
|
+
// windows with context, merged when they overlap or the gap between them is too short to fold
|
|
801
|
+
let wins = [];
|
|
802
|
+
for (const [a0, a1] of must) {
|
|
803
|
+
const w = win(a0, a1, kMax, L.ctxRows, 1);
|
|
804
|
+
const last = wins[wins.length - 1];
|
|
805
|
+
if (last && w[0] - last[1] - 1 < L.foldMin)
|
|
806
|
+
last[1] = Math.max(last[1], w[1]);
|
|
807
|
+
else
|
|
808
|
+
wins.push(w);
|
|
809
|
+
}
|
|
810
|
+
// a window that grew past one page splits into page-sized pieces
|
|
811
|
+
wins = wins.flatMap(([w0, w1]) => {
|
|
812
|
+
const out = [];
|
|
813
|
+
for (let s = w0; s <= w1; s += L.maxAreaRows)
|
|
814
|
+
out.push([s, Math.min(w1, s + L.maxAreaRows - 1)]);
|
|
815
|
+
return out;
|
|
816
|
+
});
|
|
817
|
+
wins = wins.map(([w0, w1]) => win(w0 + 0, w1, kMax, 0, L.minRows));
|
|
818
|
+
const folded = [];
|
|
819
|
+
for (let i = 0; i + 1 < wins.length; i++)
|
|
820
|
+
if (wins[i + 1][0] - wins[i][1] > 1)
|
|
821
|
+
folded.push([wins[i][1] + 1, wins[i + 1][0] - 1]);
|
|
822
|
+
return { pieces: wins, folded };
|
|
823
|
+
}
|
|
824
|
+
/** [lo, hi] grown by `ctx` on each side and to at least `min`, clamped to [0, max]; a shortfall at one edge moves to the other side. */
|
|
825
|
+
export function win(lo, hi, max, ctx, min) {
|
|
826
|
+
let a = lo - ctx, b = hi + ctx;
|
|
827
|
+
const need = Math.max(0, min - (b - a + 1));
|
|
828
|
+
a -= Math.floor(need / 2);
|
|
829
|
+
b += Math.ceil(need / 2);
|
|
830
|
+
if (a < 0) {
|
|
831
|
+
b += -a;
|
|
832
|
+
a = 0;
|
|
833
|
+
}
|
|
834
|
+
if (b > max) {
|
|
835
|
+
a -= b - max;
|
|
836
|
+
b = max;
|
|
837
|
+
}
|
|
838
|
+
return [Math.max(0, a), Math.min(max, b)];
|
|
839
|
+
}
|
|
840
|
+
/** Column windows (merged space): one when narrow, else a left and a right piece. */
|
|
841
|
+
export function colPieces(bl, mMax, L = LIMITS) {
|
|
842
|
+
const w = bl.m1 - bl.m0 + 1;
|
|
843
|
+
if (w + 2 * L.ctxCols <= L.maxBlockCols)
|
|
844
|
+
return { pieces: [win(bl.m0, bl.m1, mMax, L.ctxCols, L.minCols)], folded: [] };
|
|
845
|
+
const half = Math.floor((L.maxBlockCols - 1) / 2);
|
|
846
|
+
const left = win(bl.m0, bl.m0 + half - 1, mMax, L.ctxCols, 1), right = win(bl.m1 - half + 1, bl.m1, mMax, L.ctxCols, 1);
|
|
847
|
+
return { pieces: [left, right], folded: right[0] - left[1] > 1 ? [[left[1] + 1, right[0] - 1]] : [] };
|
|
848
|
+
}
|
|
849
|
+
// ---------------------------------------------------------------- the whole workbook
|
|
850
|
+
/** What differs between two sides of a chart, from its XML (title, axis titles, plot type, series), plus a resized frame; undefined when nothing known differs. A moved frame is not listed: its anchor is in each side's own rows and columns, which an insert shifts. */
|
|
851
|
+
function chartDetail(x, y) {
|
|
852
|
+
const bits = [];
|
|
853
|
+
const d = x.xml !== undefined && y.xml !== undefined ? xlsxChartDetail(x.xml, y.xml) : undefined;
|
|
854
|
+
if (d)
|
|
855
|
+
bits.push(d);
|
|
856
|
+
if (x.anchor && y.anchor && (x.anchor.r2 - x.anchor.r1 !== y.anchor.r2 - y.anchor.r1 || x.anchor.c2 - x.anchor.c1 !== y.anchor.c2 - y.anchor.c1))
|
|
857
|
+
bits.push(`size ${x.anchor.c2 - x.anchor.c1 + 1} by ${x.anchor.r2 - x.anchor.r1 + 1} cells \u2192 ${y.anchor.c2 - y.anchor.c1 + 1} by ${y.anchor.r2 - y.anchor.r1 + 1} cells`);
|
|
858
|
+
return bits.length ? bits.join("; ") : undefined;
|
|
859
|
+
}
|
|
860
|
+
const describeDrawing = (d) => `${d.kind === "chart" ? "Chart" : d.kind === "image" ? "Picture" : "Shape"}${d.name ? ` '${d.name}'` : ""}`;
|
|
861
|
+
/** The change list and print plan for a before/after pair. `before` or `after` null: the file did not exist on that side. */
|
|
862
|
+
export function planExcel(beforeBytes, afterBytes, L = LIMITS) {
|
|
863
|
+
const A = beforeBytes ? readWorkbook(beforeBytes) : null;
|
|
864
|
+
const B = afterBytes ? readWorkbook(afterBytes) : null;
|
|
865
|
+
const ctx = { changes: [], caveats: [], next: 0, exceptions: new Map(), charts: new Map() };
|
|
866
|
+
const plan = { changes: ctx.changes, caveats: ctx.caveats, before: [], after: [], groups: [], issues: [] };
|
|
867
|
+
const empty = { sheets: [], names: new Map(), parts: new Map(), fullCalcOnLoad: false, uncached: 0, macros: false };
|
|
868
|
+
const pairs = matchSheets(A ?? empty, B ?? empty);
|
|
869
|
+
// sheet name tokens, so a formula pointing at a renamed sheet compares equal
|
|
870
|
+
const nameMapA = new Map(), nameMapB = new Map();
|
|
871
|
+
pairs.forEach((p, i) => { if (p.a)
|
|
872
|
+
nameMapA.set(p.a.name, `⟦S${i}⟧`); if (p.b)
|
|
873
|
+
nameMapB.set(p.b.name, `⟦S${i}⟧`); });
|
|
874
|
+
const mapsA = new Map();
|
|
875
|
+
for (const p of pairs)
|
|
876
|
+
if (p.a && p.b && p.a.kind === "worksheet")
|
|
877
|
+
mapsA.set(p.a.name, alignSheet(p.a, p.b, nameMapA, nameMapB));
|
|
878
|
+
const scored = [];
|
|
879
|
+
// workbook-level parts and sheets
|
|
880
|
+
for (const p of pairs) {
|
|
881
|
+
if (p.a && !p.b) {
|
|
882
|
+
const ch = push(ctx, { mark: "removed", label: `Sheet ${quoteSheet(p.a.name)}`, text: `Sheet ${p.a.name} deleted${p.a.kind === "worksheet" ? ` (it held ${n(countCells(p.a))} cells)` : ""}`, kind: "sheet-deleted" });
|
|
883
|
+
if (p.a.state !== "visible")
|
|
884
|
+
unplace(ch, `the sheet was ${p.a.state === "veryHidden" ? "very hidden" : "hidden"}`, "before");
|
|
885
|
+
else
|
|
886
|
+
scored.push({ p, al: oneSided(p.a, "a"), block: topBlock(p.a, ch, 100), oneSide: "a", ...(p.a.kind === "chartsheet" ? { chart: true } : {}) });
|
|
887
|
+
}
|
|
888
|
+
else if (p.b && !p.a) {
|
|
889
|
+
const created = A === null;
|
|
890
|
+
const cells = countCells(p.b), formulas = countFormulas(p.b), charts = p.b.drawings.filter((d) => d.kind === "chart").length;
|
|
891
|
+
const ch = push(ctx, { mark: "added", label: `Sheet ${quoteSheet(p.b.name)}`, text: p.b.kind === "worksheet" ? `Sheet ${p.b.name} ${created ? "created" : "added"}: ${n(p.b.maxRow)} rows by ${n(p.b.maxCol)} columns, ${n(cells)} cells${formulas ? ` (${n(formulas)} formulas)` : ""}${charts ? `, ${n(charts)} chart${charts === 1 ? "" : "s"}` : ""}` : `Chart sheet ${p.b.name} ${created ? "created" : "added"}`, kind: "sheet-added" });
|
|
892
|
+
if (p.b.state !== "visible")
|
|
893
|
+
unplace(ch, `the sheet is ${p.b.state === "veryHidden" ? "very hidden" : "hidden"}`);
|
|
894
|
+
else
|
|
895
|
+
scored.push({ p, al: oneSided(p.b, "b"), block: topBlock(p.b, ch, created ? 50 : 100), oneSide: "b", ...(p.b.kind === "chartsheet" ? { chart: true } : {}) });
|
|
896
|
+
}
|
|
897
|
+
else if (p.a && p.b && p.renamed) {
|
|
898
|
+
push(ctx, { mark: "changed", label: `Sheet ${quoteSheet(p.b.name)}`, before: p.a.name, after: p.b.name, text: `Sheet renamed from ${p.a.name} to ${p.b.name}; formulas that point at it were updated with it`, kind: "sheet-renamed" });
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
if (A && B) {
|
|
902
|
+
const orderA = pairs.filter((p) => p.a && p.b).sort((x, y) => A.sheets.indexOf(x.a) - A.sheets.indexOf(y.a)).map((p) => p.b.name);
|
|
903
|
+
const orderB = pairs.filter((p) => p.a && p.b).sort((x, y) => B.sheets.indexOf(x.b) - B.sheets.indexOf(y.b)).map((p) => p.b.name);
|
|
904
|
+
if (orderA.join("\u0001") !== orderB.join("\u0001"))
|
|
905
|
+
push(ctx, { mark: "moved", label: "Sheet order", before: orderA.join(", "), after: orderB.join(", "), text: "The order of the sheets changed", kind: "sheet-order" });
|
|
906
|
+
for (const p of pairs) {
|
|
907
|
+
if (!p.a || !p.b)
|
|
908
|
+
continue;
|
|
909
|
+
if (p.a.state !== p.b.state)
|
|
910
|
+
push(ctx, { mark: "changed", label: `Sheet ${quoteSheet(p.b.name)}`, before: p.a.state, after: p.b.state, text: `Sheet ${p.b.name} is now ${p.b.state === "visible" ? "shown" : p.b.state === "hidden" ? "hidden" : "very hidden"}`, kind: "sheet-state" });
|
|
911
|
+
// drawings: charts, pictures, shapes, matched by content, then by name
|
|
912
|
+
const da = [...p.a.drawings], db = [...p.b.drawings];
|
|
913
|
+
const used = new Set();
|
|
914
|
+
const changedCharts = [];
|
|
915
|
+
for (const x of da) {
|
|
916
|
+
let y = db.find((d) => !used.has(d) && d.kind === x.kind && d.sha === x.sha);
|
|
917
|
+
if (y) {
|
|
918
|
+
used.add(y);
|
|
919
|
+
continue;
|
|
920
|
+
}
|
|
921
|
+
// a chart pairs only on an exact object-name match on this sheet (never on its title); other drawings keep the name match, and a chart with no object name on either side keeps the title match
|
|
922
|
+
y = db.find((d) => !used.has(d) && d.kind === x.kind && (x.kind === "chart" ? (x.objectName !== undefined ? d.objectName === x.objectName : d.objectName === undefined && d.name !== "" && d.name === x.name) : d.name !== "" && d.name === x.name));
|
|
923
|
+
if (y) {
|
|
924
|
+
used.add(y);
|
|
925
|
+
const detail = x.kind === "chart" ? chartDetail(x, y) : undefined;
|
|
926
|
+
const ch = push(ctx, { mark: "changed", label: `${quoteSheet(p.b.name)} (${describeDrawing(x)})`, text: `${describeDrawing(y)} on ${p.b.name} changed${detail ? `: ${detail}` : ""}`, kind: x.kind });
|
|
927
|
+
changedCharts.push({ before: x, after: y, ch });
|
|
928
|
+
continue;
|
|
929
|
+
}
|
|
930
|
+
const ch = push(ctx, { mark: "removed", label: `${quoteSheet(p.b.name)} (${describeDrawing(x)})`, text: `${describeDrawing(x)} removed from sheet ${p.b.name}`, kind: x.kind });
|
|
931
|
+
changedCharts.push({ before: x, ch });
|
|
932
|
+
}
|
|
933
|
+
for (const y of db)
|
|
934
|
+
if (!used.has(y)) {
|
|
935
|
+
const ch = push(ctx, { mark: "added", label: `${quoteSheet(p.b.name)} (${describeDrawing(y)})`, text: `${describeDrawing(y)} added to sheet ${p.b.name}`, kind: y.kind });
|
|
936
|
+
changedCharts.push({ after: y, ch });
|
|
937
|
+
}
|
|
938
|
+
for (const cc of changedCharts) {
|
|
939
|
+
ctx.charts.set(cc.ch.id, { ...(cc.before ? { before: cc.before } : {}), ...(cc.after ? { after: cc.after } : {}) });
|
|
940
|
+
if (p.b.kind === "chartsheet") {
|
|
941
|
+
scored.push({ p, al: oneSided(p.b, "b"), block: { items: [], k0: 0, k1: 0, m0: 0, m1: 0, score: 100 }, chart: true });
|
|
942
|
+
scored[scored.length - 1].block.change = cc.ch;
|
|
943
|
+
continue;
|
|
944
|
+
}
|
|
945
|
+
const anchor = cc.before?.anchor ?? cc.after?.anchor;
|
|
946
|
+
const al = mapsA.get(p.a.name);
|
|
947
|
+
if (!anchor || !al) {
|
|
948
|
+
unplace(cc.ch, "it is not anchored to cells");
|
|
949
|
+
continue;
|
|
950
|
+
}
|
|
951
|
+
const kmap = cc.before?.anchor ? al.aRowK : al.bRowK, mmap = cc.before?.anchor ? al.aColM : al.bColM;
|
|
952
|
+
const k0 = kmap.get(anchor.r1) ?? anchor.r1 - 1, k1 = kmap.get(anchor.r2) ?? anchor.r2 - 1, m0 = mmap.get(anchor.c1) ?? anchor.c1 - 1, m1 = mmap.get(anchor.c2) ?? anchor.c2 - 1;
|
|
953
|
+
const it = { change: cc.ch, k0: Math.min(k0, al.kA.length - 1), k1: Math.min(k1, Math.max(al.kA.length, al.kB.length) - 1), m0: Math.min(m0, Math.max(al.mA.length, al.mB.length) - 1), m1: Math.min(m1, Math.max(al.mA.length, al.mB.length) - 1), score: cc.ch.mark === "removed" ? 100 : 60, uniform: false };
|
|
954
|
+
(cc.ch.private ??= {}).anchor = rangeText(anchor);
|
|
955
|
+
scored.push({ p, al, block: { items: [it], k0: it.k0, k1: Math.max(it.k0, it.k1), m0: it.m0, m1: Math.max(it.m0, it.m1), score: it.score } });
|
|
956
|
+
}
|
|
957
|
+
// comments
|
|
958
|
+
for (const [ref, t] of p.a.comments)
|
|
959
|
+
if (p.b.comments.get(ref) !== t) {
|
|
960
|
+
const ch = push(ctx, { mark: p.b.comments.has(ref) ? "changed" : "removed", label: `${quoteSheet(p.b.name)}!${ref} (comment)`, before: t, ...(p.b.comments.has(ref) ? { after: p.b.comments.get(ref) } : {}), kind: "comment" });
|
|
961
|
+
unplace(ch, "comments do not print", "both");
|
|
962
|
+
}
|
|
963
|
+
for (const [ref, t] of p.b.comments)
|
|
964
|
+
if (!p.a.comments.has(ref)) {
|
|
965
|
+
const ch = push(ctx, { mark: "added", label: `${quoteSheet(p.b.name)}!${ref} (comment)`, after: t, kind: "comment" });
|
|
966
|
+
unplace(ch, "comments do not print", "both");
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
// defined names
|
|
970
|
+
for (const [k, v] of A.names) {
|
|
971
|
+
const w = B.names.get(k);
|
|
972
|
+
if (w !== v) {
|
|
973
|
+
const ch = push(ctx, { mark: w === undefined ? "removed" : "changed", label: `Name ${k.split("|")[0]}`, before: v, ...(w !== undefined ? { after: w } : {}), kind: "name" });
|
|
974
|
+
unplace(ch, "a defined name has no place on a sheet", "both");
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
for (const [k, v] of B.names)
|
|
978
|
+
if (!A.names.has(k)) {
|
|
979
|
+
const ch = push(ctx, { mark: "added", label: `Name ${k.split("|")[0]}`, after: v, kind: "name" });
|
|
980
|
+
unplace(ch, "a defined name has no place on a sheet", "both");
|
|
981
|
+
}
|
|
982
|
+
if (A.macros !== B.macros) {
|
|
983
|
+
const ch = push(ctx, { mark: B.macros ? "added" : "removed", label: "Macros", text: B.macros ? "The workbook now holds macros (a VBA project)" : "The workbook's macros (VBA project) were removed", kind: "macros" });
|
|
984
|
+
unplace(ch, "macros do not print", "both");
|
|
985
|
+
}
|
|
986
|
+
for (const kind of ["tables", "pivotTables", "externalLinks"]) {
|
|
987
|
+
const pa = [...A.parts.keys()].filter((x) => x.includes(`/${kind}/`) && x.endsWith(".xml")).length, pb = [...B.parts.keys()].filter((x) => x.includes(`/${kind}/`) && x.endsWith(".xml")).length;
|
|
988
|
+
if (pa !== pb) {
|
|
989
|
+
const ch = push(ctx, { mark: pb > pa ? "added" : "removed", label: kind === "tables" ? "Tables" : kind === "pivotTables" ? "Pivot tables" : "External links", text: `${kind === "tables" ? "Tables" : kind === "pivotTables" ? "Pivot tables" : "Links to other workbooks"}: ${pa} before, ${pb} after`, kind });
|
|
990
|
+
unplace(ch, "this is a workbook part, not cells", "both");
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
// cells, per matched worksheet
|
|
995
|
+
for (const p of pairs) {
|
|
996
|
+
if (!p.a || !p.b || p.a.kind !== "worksheet" || p.b.kind !== "worksheet")
|
|
997
|
+
continue;
|
|
998
|
+
const r = diffSheet(ctx, p, nameMapA, nameMapB, mapsA);
|
|
999
|
+
if (p.b.state !== "visible")
|
|
1000
|
+
continue;
|
|
1001
|
+
for (const bl of clusterItems(r.items, L))
|
|
1002
|
+
scored.push({ p, al: r.al, block: bl });
|
|
1003
|
+
}
|
|
1004
|
+
if (B && B.uncached > 0)
|
|
1005
|
+
ctx.caveats.push(`${n(B.uncached)} formula${B.uncached === 1 ? " has" : "s have"} no saved value in the after file; Excel calculates ${B.uncached === 1 ? "it" : "them"} when it opens the file, and the pictures show Excel's result.`);
|
|
1006
|
+
if (B?.fullCalcOnLoad)
|
|
1007
|
+
ctx.caveats.push("The after file asks Excel to recalculate everything when it opens; the values shown are Excel's recalculation, which may differ from the values saved in the file.");
|
|
1008
|
+
// rank and cap
|
|
1009
|
+
scored.sort((x, y) => y.block.score - x.block.score);
|
|
1010
|
+
const perSheet = new Map();
|
|
1011
|
+
let total = 0;
|
|
1012
|
+
let gi = 0;
|
|
1013
|
+
const sheetsOut = { before: new Map(), after: new Map() };
|
|
1014
|
+
const pendingBoxes = [];
|
|
1015
|
+
const pendingZoom = [];
|
|
1016
|
+
const omitted = [];
|
|
1017
|
+
for (const s of scored) {
|
|
1018
|
+
const key = s.p.b?.name ?? s.p.a.name;
|
|
1019
|
+
const used = perSheet.get(key) ?? 0;
|
|
1020
|
+
const blockChanges = s.block.items.length ? s.block.items.map((i) => i.change) : [s.block.change].filter((x) => !!x);
|
|
1021
|
+
if (used >= L.perSheet || total >= L.perFile) {
|
|
1022
|
+
omitted.push(...blockChanges);
|
|
1023
|
+
for (const ch of blockChanges)
|
|
1024
|
+
unplace(ch, `past the picture limit (${L.perSheet} areas a sheet, ${L.perFile} a file)`, "both");
|
|
1025
|
+
continue;
|
|
1026
|
+
}
|
|
1027
|
+
perSheet.set(key, used + 1);
|
|
1028
|
+
total++;
|
|
1029
|
+
const group = `g${++gi}`;
|
|
1030
|
+
const info = { id: group, score: s.block.score, changes: blockChanges.map((c) => c.id) };
|
|
1031
|
+
plan.groups.push(info);
|
|
1032
|
+
for (const ch of blockChanges)
|
|
1033
|
+
(ch.private ??= {}).group = group;
|
|
1034
|
+
if (s.chart) {
|
|
1035
|
+
for (const side of ["before", "after"]) {
|
|
1036
|
+
const sh = side === "before" ? s.p.a : s.p.b;
|
|
1037
|
+
if (!sh) {
|
|
1038
|
+
info.placeholder = { ...(info.placeholder ?? {}), [side]: side === "before" ? "This chart sheet did not exist" : "This chart sheet was deleted" };
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1041
|
+
if (sh.state !== "visible")
|
|
1042
|
+
continue;
|
|
1043
|
+
sheetsOut[side].set(sh.name, { name: sh.name, kind: "chartsheet", areas: [], zoom: 100, landscape: true, group });
|
|
1044
|
+
}
|
|
1045
|
+
continue;
|
|
1046
|
+
}
|
|
1047
|
+
// pieces in merged space, then each side's own rows and columns
|
|
1048
|
+
const kMax = s.al.kA.length - 1, mMax = s.al.mA.length - 1;
|
|
1049
|
+
const asOne = s.block.whole === true;
|
|
1050
|
+
const rp = asOne ? { pieces: [[s.block.k0, Math.min(s.block.k1, kMax)]], folded: [] } : rowPieces(s.block, kMax, L);
|
|
1051
|
+
const cp = asOne ? { pieces: [[s.block.m0, Math.min(s.block.m1, mMax)]], folded: [] } : colPieces(s.block, mMax, L);
|
|
1052
|
+
const main = blockChanges[0];
|
|
1053
|
+
for (const [f0, f1] of rp.folded) {
|
|
1054
|
+
const kind = s.block.items.find((i) => i.uniform && i.k0 <= f0 && i.k1 >= f1);
|
|
1055
|
+
const ra = s.al.kB.slice(f0, f1 + 1).filter((x) => x !== null), rb = s.al.kA.slice(f0, f1 + 1).filter((x) => x !== null);
|
|
1056
|
+
const rows = ra.length ? ra : rb;
|
|
1057
|
+
if (!rows.length)
|
|
1058
|
+
continue;
|
|
1059
|
+
const ch = push(ctx, { mark: "described", label: `${quoteSheet(key)}!${rows[0]}:${rows[rows.length - 1]}`, text: `${rowsText(rows[0], rows[rows.length - 1])} not shown${kind ? ` (${kind.change.kind === "rows-inserted" ? "all inserted" : kind.change.kind === "rows-deleted" ? "all deleted" : "the same change throughout"}, no other changes)` : ""}`, parent: main.id, kind: "fold" });
|
|
1060
|
+
unplace(ch, "folded to keep the picture to the changed edges", "both");
|
|
1061
|
+
}
|
|
1062
|
+
for (const [f0, f1] of cp.folded) {
|
|
1063
|
+
const cs = s.al.mB.slice(f0, f1 + 1).filter((x) => x !== null);
|
|
1064
|
+
if (!cs.length)
|
|
1065
|
+
continue;
|
|
1066
|
+
const ch = push(ctx, { mark: "described", label: `${quoteSheet(key)}!${numToCol(cs[0])}:${numToCol(cs[cs.length - 1])}`, text: `Columns ${numToCol(cs[0])}-${numToCol(cs[cs.length - 1])} not shown (the same change continues across them)`, parent: main.id, kind: "fold" });
|
|
1067
|
+
unplace(ch, "folded to keep the picture to the changed edges", "both");
|
|
1068
|
+
}
|
|
1069
|
+
// the cells of each pictured change, per side it exists on, in that side's own rows and columns
|
|
1070
|
+
const boundOf = (side, k0, k1, m0, m1) => {
|
|
1071
|
+
const ks = (side === "before" ? s.al.kA : s.al.kB).slice(k0, k1 + 1).filter((x) => x !== null);
|
|
1072
|
+
const ms = (side === "before" ? s.al.mA : s.al.mB).slice(m0, m1 + 1).filter((x) => x !== null);
|
|
1073
|
+
return ks.length && ms.length ? { r1: Math.min(...ks), r2: Math.max(...ks), c1: Math.min(...ms), c2: Math.max(...ms) } : undefined;
|
|
1074
|
+
};
|
|
1075
|
+
const sheetNames = { ...(s.p.a ? { before: s.p.a.name } : {}), ...(s.p.b ? { after: s.p.b.name } : {}) };
|
|
1076
|
+
for (const it of s.block.items) {
|
|
1077
|
+
const exs = [it.change, ...ctx.changes.filter((c) => c.parent === it.change.id && ctx.exceptions.has(c.id))];
|
|
1078
|
+
for (const ch of exs) {
|
|
1079
|
+
if (ch.private?.unplaced)
|
|
1080
|
+
continue;
|
|
1081
|
+
const on = sidesOfMark(ch.mark), cells = {}, drawing = {}, offset = {};
|
|
1082
|
+
const chart = ctx.charts.get(ch.id), ex = ctx.exceptions.get(ch.id);
|
|
1083
|
+
for (const side of ["before", "after"]) {
|
|
1084
|
+
if (!on[side])
|
|
1085
|
+
continue;
|
|
1086
|
+
const r = chart ? chart[side]?.anchor : ex ? ex[side] : boundOf(side, it.k0, it.k1, it.m0, it.m1);
|
|
1087
|
+
if (!r)
|
|
1088
|
+
continue;
|
|
1089
|
+
cells[side] = { ...r };
|
|
1090
|
+
if (chart?.[side]) {
|
|
1091
|
+
drawing[side] = chart[side].objectName ?? chart[side].name;
|
|
1092
|
+
if (chart[side].anchorOffset)
|
|
1093
|
+
offset[side] = chart[side].anchorOffset;
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
if (cells.before || cells.after)
|
|
1097
|
+
pendingBoxes.push({ change: ch, group, sheet: sheetNames, cells, drawing, offset });
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
let order = 0;
|
|
1101
|
+
const pieces = rp.pieces.slice(0, L.maxPieces);
|
|
1102
|
+
for (const [k0, k1] of pieces)
|
|
1103
|
+
for (const [m0, m1] of cp.pieces) {
|
|
1104
|
+
for (const side of ["before", "after"]) {
|
|
1105
|
+
const ks = side === "before" ? s.al.kA : s.al.kB, ms = side === "before" ? s.al.mA : s.al.mB;
|
|
1106
|
+
const sh = side === "before" ? s.p.a : s.p.b;
|
|
1107
|
+
if (!sh || sh.kind !== "worksheet")
|
|
1108
|
+
continue;
|
|
1109
|
+
// a sheet with no cells has nothing to show, unless drawings sit on it (a sheet of charts)
|
|
1110
|
+
if (side === "after" && sh.rows.size === 0 && !sh.drawings.some((d) => d.anchor !== undefined))
|
|
1111
|
+
continue;
|
|
1112
|
+
let rows = ks.slice(k0, k1 + 1).filter((x) => x !== null);
|
|
1113
|
+
const cols = ms.slice(m0, m1 + 1).filter((x) => x !== null);
|
|
1114
|
+
if (!rows.length || !cols.length)
|
|
1115
|
+
continue;
|
|
1116
|
+
if (Math.max(...rows) - Math.min(...rows) + 1 > 2 * (k1 - k0 + 1)) {
|
|
1117
|
+
// this side's rows are scattered (a reorder): a window around the block's own rows, in this side's own rows
|
|
1118
|
+
const core = ks.slice(s.block.k0, s.block.k1 + 1).filter((x) => x !== null);
|
|
1119
|
+
if (core.length) {
|
|
1120
|
+
const [w0, w1] = win(Math.min(...core) - 1, Math.max(...core) - 1, Math.max(0, sh.maxRow - 1), L.ctxRows, L.minRows);
|
|
1121
|
+
rows = [w0 + 1, w1 + 1];
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
const rect = { r1: Math.min(...rows), r2: Math.max(...rows), c1: Math.min(...cols), c2: Math.max(...cols) };
|
|
1125
|
+
let sp = sheetsOut[side].get(sh.name);
|
|
1126
|
+
if (!sp) {
|
|
1127
|
+
sp = { name: sh.name, kind: "worksheet", areas: [], zoom: 100, landscape: false };
|
|
1128
|
+
sheetsOut[side].set(sh.name, sp);
|
|
1129
|
+
sheetOfPrint.set(sp, sh);
|
|
1130
|
+
pendingZoom.push({ sp, side, sheet: sh, key });
|
|
1131
|
+
}
|
|
1132
|
+
sp.areas.push({ group, order, rect, boxes: [] });
|
|
1133
|
+
}
|
|
1134
|
+
order++;
|
|
1135
|
+
}
|
|
1136
|
+
if (s.oneSide)
|
|
1137
|
+
info.placeholder = s.oneSide === "b" ? { before: A === null ? "The file did not exist yet" : "This sheet did not exist" } : { after: "This sheet was deleted" };
|
|
1138
|
+
if (s.block.items.some((i) => i.emptied))
|
|
1139
|
+
info.placeholder = { after: "The sheet is now empty: nothing to print" };
|
|
1140
|
+
}
|
|
1141
|
+
if (omitted.length)
|
|
1142
|
+
ctx.caveats.push(`${n(omitted.length)} change${omitted.length === 1 ? "" : "s"} past the picture limit ${omitted.length === 1 ? "is" : "are"} listed without a picture.`);
|
|
1143
|
+
// merge areas on one sheet side that overlap or touch (Excel merges touching print ranges itself, and no row should print twice)
|
|
1144
|
+
for (const side of ["before", "after"])
|
|
1145
|
+
for (const sp of sheetsOut[side].values())
|
|
1146
|
+
sp.areas = mergeAreas(sp.areas);
|
|
1147
|
+
// groups whose areas merged on either side become one group; pieces are then numbered top to bottom on each side
|
|
1148
|
+
const up = new Map();
|
|
1149
|
+
const find = (g) => { let x = g; while (up.has(x))
|
|
1150
|
+
x = up.get(x); return x; };
|
|
1151
|
+
for (const side of ["before", "after"])
|
|
1152
|
+
for (const sp of sheetsOut[side].values())
|
|
1153
|
+
for (const ar of sp.areas)
|
|
1154
|
+
for (const o of ar.also ?? []) {
|
|
1155
|
+
const x = find(o.group), y = find(ar.group);
|
|
1156
|
+
if (x !== y)
|
|
1157
|
+
up.set(x, y);
|
|
1158
|
+
}
|
|
1159
|
+
if (up.size > 0) {
|
|
1160
|
+
const keep = [];
|
|
1161
|
+
for (const g of plan.groups) {
|
|
1162
|
+
const r = find(g.id);
|
|
1163
|
+
if (r === g.id) {
|
|
1164
|
+
keep.push(g);
|
|
1165
|
+
continue;
|
|
1166
|
+
}
|
|
1167
|
+
const into = plan.groups.find((x) => x.id === r);
|
|
1168
|
+
into.changes.push(...g.changes);
|
|
1169
|
+
into.score = Math.max(into.score, g.score);
|
|
1170
|
+
for (const id of g.changes) {
|
|
1171
|
+
const ch = ctx.changes.find((c) => c.id === id);
|
|
1172
|
+
if (ch?.private)
|
|
1173
|
+
ch.private.group = r;
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
plan.groups = keep;
|
|
1177
|
+
for (const side of ["before", "after"])
|
|
1178
|
+
for (const sp of sheetsOut[side].values()) {
|
|
1179
|
+
for (const ar of sp.areas)
|
|
1180
|
+
ar.group = find(ar.group);
|
|
1181
|
+
const byGroup = new Map();
|
|
1182
|
+
for (const ar of sp.areas) {
|
|
1183
|
+
const l = byGroup.get(ar.group);
|
|
1184
|
+
if (l)
|
|
1185
|
+
l.push(ar);
|
|
1186
|
+
else
|
|
1187
|
+
byGroup.set(ar.group, [ar]);
|
|
1188
|
+
}
|
|
1189
|
+
for (const l of byGroup.values())
|
|
1190
|
+
l.sort((x, y) => x.rect.r1 - y.rect.r1 || x.rect.c1 - y.rect.c1).forEach((ar, i) => { ar.order = i; });
|
|
1191
|
+
}
|
|
1192
|
+
}
|
|
1193
|
+
for (const side of ["before", "after"])
|
|
1194
|
+
for (const sp of sheetsOut[side].values()) {
|
|
1195
|
+
for (const ar of sp.areas)
|
|
1196
|
+
delete ar.also;
|
|
1197
|
+
sp.areas.sort((x, y) => x.rect.r1 - y.rect.r1 || x.rect.c1 - y.rect.c1);
|
|
1198
|
+
}
|
|
1199
|
+
// boxes: each pictured change on the first area of its group, on each side, that touches its cells
|
|
1200
|
+
for (const pb of pendingBoxes) {
|
|
1201
|
+
const g = find(pb.group);
|
|
1202
|
+
for (const side of ["before", "after"]) {
|
|
1203
|
+
const cells = pb.cells[side], name = pb.sheet[side];
|
|
1204
|
+
if (!cells || name === undefined)
|
|
1205
|
+
continue;
|
|
1206
|
+
const sp = sheetsOut[side].get(name);
|
|
1207
|
+
const ar = sp?.areas.find((a) => a.group === g && a.rect.r1 <= cells.r2 && a.rect.r2 >= cells.r1 && a.rect.c1 <= cells.c2 && a.rect.c2 >= cells.c1);
|
|
1208
|
+
if (ar)
|
|
1209
|
+
ar.boxes.push({ change: pb.change.id, cells, ...(pb.drawing[side] !== undefined ? { drawing: pb.drawing[side] } : {}), ...(pb.offset[side] ? { offset: pb.offset[side] } : {}) });
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
// zoom and orientation, shared by both sides of a sheet so before and after print at the same scale
|
|
1213
|
+
const PAGE = { w: 612 - 36, h: 792 - 36 };
|
|
1214
|
+
const need = new Map();
|
|
1215
|
+
for (const { sp, sheet, key } of pendingZoom) {
|
|
1216
|
+
let w = 0, h = 0;
|
|
1217
|
+
for (const ar of sp.areas) {
|
|
1218
|
+
let aw = 30, ah = 16;
|
|
1219
|
+
for (let c = ar.rect.c1; c <= ar.rect.c2; c++)
|
|
1220
|
+
aw += printedWidthPt(sheet, c);
|
|
1221
|
+
for (let r = ar.rect.r1; r <= ar.rect.r2; r++)
|
|
1222
|
+
ah += heightPt(sheet, r) * PRINT_ROW_K;
|
|
1223
|
+
w = Math.max(w, aw);
|
|
1224
|
+
h = Math.max(h, ah);
|
|
1225
|
+
}
|
|
1226
|
+
const prev = need.get(key) ?? { w: 0, h: 0 };
|
|
1227
|
+
need.set(key, { w: Math.max(prev.w, w), h: Math.max(prev.h, h) });
|
|
1228
|
+
}
|
|
1229
|
+
for (const { sp, key } of pendingZoom) {
|
|
1230
|
+
const nd = need.get(key);
|
|
1231
|
+
const landscape = nd.w > PAGE.w && nd.h <= PAGE.w;
|
|
1232
|
+
const pw = landscape ? PAGE.h : PAGE.w, ph = landscape ? PAGE.w : PAGE.h;
|
|
1233
|
+
sp.landscape = landscape;
|
|
1234
|
+
sp.zoom = Math.max(L.minZoom, Math.min(100, Math.floor(100 * Math.min(pw / nd.w, ph / nd.h))));
|
|
1235
|
+
}
|
|
1236
|
+
plan.before = [...sheetsOut.before.values()];
|
|
1237
|
+
plan.after = [...sheetsOut.after.values()];
|
|
1238
|
+
// the list in priority order: pictured groups by rank, then the rest; children right after their parents
|
|
1239
|
+
const rank = new Map();
|
|
1240
|
+
plan.groups.forEach((g, i) => g.changes.forEach((id) => { if (!rank.has(id))
|
|
1241
|
+
rank.set(id, i); }));
|
|
1242
|
+
const byId = new Map(ctx.changes.map((c) => [c.id, c]));
|
|
1243
|
+
const top = ctx.changes.filter((c) => !c.parent || !byId.has(c.parent));
|
|
1244
|
+
const weight = (c) => (c.kind === "chart" || c.kind === "image" || c.kind === "sheet-deleted") && c.mark === "removed" ? -2 : rank.has(c.id) ? rank.get(c.id) : 1e6;
|
|
1245
|
+
const ordered = [];
|
|
1246
|
+
const kids = new Map();
|
|
1247
|
+
for (const c of ctx.changes)
|
|
1248
|
+
if (c.parent && byId.has(c.parent)) {
|
|
1249
|
+
const l = kids.get(c.parent);
|
|
1250
|
+
if (l)
|
|
1251
|
+
l.push(c);
|
|
1252
|
+
else
|
|
1253
|
+
kids.set(c.parent, [c]);
|
|
1254
|
+
}
|
|
1255
|
+
const visit = (c) => { ordered.push(c); for (const k of kids.get(c.id) ?? [])
|
|
1256
|
+
visit(k); };
|
|
1257
|
+
top.map((c, i) => ({ c, i })).sort((x, y) => weight(x.c) - weight(y.c) || x.i - y.i).forEach(({ c }) => visit(c));
|
|
1258
|
+
plan.changes = ordered;
|
|
1259
|
+
plan.issues = introducedErrors(pairs.flatMap((p) => (p.b ? [{ label: quoteSheet(p.b.name), after: p.b, beforeOf: beforeCellOf(p.a, p.a ? mapsA.get(p.a.name) : undefined) }] : [])));
|
|
1260
|
+
return plan;
|
|
1261
|
+
}
|
|
1262
|
+
/** The before cell an after cell was compared with: through the pair's row and column alignment; an after row or column the alignment does not reach keeps its own address. */
|
|
1263
|
+
function beforeCellOf(a, al) {
|
|
1264
|
+
if (a === undefined)
|
|
1265
|
+
return () => undefined;
|
|
1266
|
+
return (r, c) => {
|
|
1267
|
+
if (al === undefined)
|
|
1268
|
+
return a.rows.get(r)?.get(c);
|
|
1269
|
+
const k = al.bRowK.get(r), m = al.bColM.get(c);
|
|
1270
|
+
const ra = k === undefined ? r : al.kA[k], ca = m === undefined ? c : al.mA[m];
|
|
1271
|
+
return ra === null || ra === undefined || ca === null || ca === undefined ? undefined : a.rows.get(ra)?.get(ca);
|
|
1272
|
+
};
|
|
1273
|
+
}
|
|
1274
|
+
// What Excel's print measures on this Mac (measured): a column of stored width w prints round(w*8)*0.75 pt (width 13: 156 px at
|
|
1275
|
+
// zoom 100), a hidden column nothing; a row prints at 14/15 of its stored height. The file's own widths (widthPt) are 15% too generous for the zoom sum.
|
|
1276
|
+
export const PRINT_ROW_K = 14 / 15;
|
|
1277
|
+
export const printedWidthPt = (s, c) => (s.hiddenCols.has(c) ? 0 : Math.round((s.widths.get(c) ?? s.defaultWidth) * 8) * 0.75);
|
|
1278
|
+
function mergeAreas(areas) {
|
|
1279
|
+
const out = [];
|
|
1280
|
+
for (const ar of areas) {
|
|
1281
|
+
const hit = out.find((o) => o.rect.r1 <= ar.rect.r2 + 1 && o.rect.r2 >= ar.rect.r1 - 1 && o.rect.c1 <= ar.rect.c2 + 1 && o.rect.c2 >= ar.rect.c1 - 1);
|
|
1282
|
+
if (hit) {
|
|
1283
|
+
hit.rect = { r1: Math.min(hit.rect.r1, ar.rect.r1), r2: Math.max(hit.rect.r2, ar.rect.r2), c1: Math.min(hit.rect.c1, ar.rect.c1), c2: Math.max(hit.rect.c2, ar.rect.c2) };
|
|
1284
|
+
hit.also = [...(hit.also ?? []), { group: ar.group, order: ar.order }];
|
|
1285
|
+
}
|
|
1286
|
+
else
|
|
1287
|
+
out.push({ ...ar, rect: { ...ar.rect } });
|
|
1288
|
+
}
|
|
1289
|
+
// a merge can make two earlier areas touch: repeat until stable
|
|
1290
|
+
return out.length < areas.length ? mergeAreas(out) : out;
|
|
1291
|
+
}
|
|
1292
|
+
function countCells(s) { let k = 0; for (const r of s.rows.values())
|
|
1293
|
+
k += r.size; return k; }
|
|
1294
|
+
function countFormulas(s) { let k = 0; for (const r of s.rows.values())
|
|
1295
|
+
for (const c of r.values())
|
|
1296
|
+
if (c.f !== null)
|
|
1297
|
+
k++; return k; }
|
|
1298
|
+
/** The top of a one-sided sheet: rows 1.. up to 30, columns up to 12. */
|
|
1299
|
+
/**
|
|
1300
|
+
* The top of a sheet shown whole (added or deleted): its first 30 rows by 12 columns of cells, widened to hold every drawing that starts inside that corner (up to 60 rows by 40
|
|
1301
|
+
* columns), so a chart beside the data, or a sheet that holds only charts, is in the picture (a created sheet of four charts and no cells read "0 cells, not pictured").
|
|
1302
|
+
*/
|
|
1303
|
+
function topBlock(s, ch, score) {
|
|
1304
|
+
const near = s.drawings.flatMap((d) => (d.anchor && d.anchor.r1 <= 30 && d.anchor.c1 <= 12 ? [d.anchor] : []));
|
|
1305
|
+
const dr = Math.min(60, near.reduce((m, a) => Math.max(m, a.r2), 0)), dc = Math.min(40, near.reduce((m, a) => Math.max(m, a.c2), 0));
|
|
1306
|
+
const k1 = Math.max(0, Math.max(Math.min(s.maxRow, 30), dr) - 1), m1 = Math.max(0, Math.max(Math.min(s.maxCol, 12), dc) - 1);
|
|
1307
|
+
// widened for drawings: shown as one piece, never folded (a fold through the middle would cut a chart in two)
|
|
1308
|
+
const whole = dr > Math.min(s.maxRow, 30) || dc > Math.min(s.maxCol, 12);
|
|
1309
|
+
return { items: [{ change: ch, k0: 0, k1, m0: 0, m1, score, uniform: false }], k0: 0, k1, m0: 0, m1, score, ...(whole ? { whole: true } : {}) };
|
|
1310
|
+
}
|