Compare commits

..
Author SHA1 Message Date
Dylan M. Taylor 1d94ed40ac Reject unsupported CARGO_BUILD_TARGET in FreeBSD packaging
Only x86_64-unknown-freebsd and aarch64-unknown-freebsd are valid; any other
value now fails before the build or packaging.
2026-10-28 14:50:50 -04:00
Dylan M. Taylor ecbddc49e3 Honor CARGO_BUILD_TARGET in FreeBSD packaging for aarch64 release builds
Cross-built FreeBSD aarch64 archives were not possible: package.sh took the
arch from uname -m (the x86_64 VM host), built for the host, read binaries
from target/release, and ran the aarch64 gridcraft-cli to print its version.

When CARGO_BUILD_TARGET is set, build with --target, read from
target/<triple>/release, name the archive by the target's arch (aarch64 for
aarch64-unknown-freebsd, x86_64 otherwise), and skip running the binary.
Unset behavior is unchanged.
2026-10-28 14:40:51 -04:00
195 changed files with 792 additions and 21972 deletions
-1
View File
@@ -12,7 +12,6 @@ on:
permissions:
contents: write
packages: write
concurrency:
group: release-${{ github.ref }}
+2 -2
View File
@@ -19,8 +19,8 @@ permissions:
jobs:
windows-arm64:
# Intentionally tracked at @main (not SHA-pinned) so org-wide CI improvements, security fixes, and build optimizations propagate without PR churn across all craftapp repos.
uses: storytold/craft-common-actions/.github/workflows/windows-arm64.yml@main
# Immutable supporting workflow revision until the new install contract lands upstream.
uses: storytold/craft-common-actions/.github/workflows/windows-arm64.yml@8bb4e5ab95443171104ad88113fe1ef9b0e5ce3f
with:
app-name: gridcraft
app-dir: GridCraft
+10 -10
View File
@@ -2,6 +2,8 @@
GridCraft is a clean-room, open-source, Rust-native spreadsheet targeting Microsoft Excel parity — and superiority (speed, openness, agent control). It runs natively on macOS, Windows, Linux and BSD, and on the web via WASM. Siblings with the same conventions: `../designcraft` (InDesign-class, the closest reference), `../photocraft` (Photoshop), `../vectorcraft` (Illustrator), `../pdfcraft` (Acrobat), `../filmcraft` (Premiere), `../lightcraft` (Lightroom), `../effectcraft` (After Effects).
Standards and learnings shared across the crafting apps live in `../../craftrules` (checked out next to the craft apps, or `storytold/craftrules`). Read its `AGENTS.md` at the start of a session, follow its standards, and contribute reusable learnings back there. Never code: repos don't share code.
## Start every session here
1. Read `plan/STATUS.md` (current milestone, next task), then the task in `plan/execution-plan.md` and the relevant `plan/architecture.md` section. Behaviour reference: `plan/excel/*.md` (`observed-ui.md` holds measured observations of the running app).
2. Follow the autonomous operation protocol (`plan/execution-plan.md` §7). Don't stop to ask unless §7 lists the decision as the user's.
@@ -18,7 +20,7 @@ GridCraft is a clean-room, open-source, Rust-native spreadsheet targeting Micros
- The ArtCraft brand files in `docs/brand/` are ArtCraft trademarks under `docs/brand/LICENSE-brand.txt`, the one exception.
## Never crash
People trust GridCraft with their numbers; a crash loses their work. **This outranks feature work.**
People trust GridCraft with their numbers; a crash loses their work. **This outranks feature work.** Standard: [`craftrules/standards/never-crash.md`](https://github.com/storytold/craftrules/blob/main/standards/never-crash.md).
- **No panics in non-test code:** no `unwrap()`, `expect()`, `panic!`, `unreachable!`, `todo!`, `unimplemented!`; no `unsafe` (`unsafe_code = "forbid"`).
- **Errors are `Result<T, E>`** through the crate's error type and `?`.
- **Input-derived numbers are hostile** (files, formulas, commands, MCP/control params): validate indexes and ranges before access, use checked access when validity is not established, use checked/saturating arithmetic, and cap input-sized allocations (huge ranges, `REPT`, `SEQUENCE`, whole-column references).
@@ -29,8 +31,8 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr
## Non-negotiables
- **Clean-room.** Microsoft Excel is installed on the dev machine and may be *observed* black-box: run it, use its UI with synthetic workbooks, take screenshots (by window id) stored only under `plan/excel/screenshots/`. Never read, disassemble or copy anything inside the Office bundle, never commit files produced by Excel, never copy GPL/AGPL/LGPL code (LibreOffice, Gnumeric…). Function behaviour comes from public documentation and observation.
- **Fonts live in [`storytold/craft-fonts`](https://github.com/storytold/craft-fonts)**, never in this repo. It is an optional build input (`CRAFT_FONTS_DIR`); without it the app uses system fonts and egui's defaults.
- **Test corpora** (real Excel-authored files) live in a separate corpus repo, fetched pinned and sha256-verified. Never commit large binary fixtures; build test workbooks in code.
- **Fonts live in [`storytold/craft-fonts`](https://github.com/storytold/craft-fonts)**, never in this repo. It is an optional build input (`CRAFT_FONTS_DIR`); without it the app uses system fonts and egui's defaults. Standard: craftrules `standards/fonts.md`.
- **Test corpora** (real Excel-authored files) live in a separate corpus repo, fetched pinned and sha256-verified (craftrules `standards/test-corpora.md`). Never commit large binary fixtures; build test workbooks in code.
- **Everything is a command.** User-visible behaviour = a command in `crates/engine/src/cmd/*` (id, label, ribbon path, shortcut, params doc, `enabled`, `run`) + tests. Ids follow Excel's ribbon (`home.bold`, `insert.chart`, `data.sortAscending`) plus primitives (`cell.set`, `selection.set`, `range.setValues`). UI-only commands live in `crates/ui-egui`. The control channel, CLI and MCP reach all of them.
- **Programmatic calls never open dialogs**: a command with empty params runs with defaults or returns an error; only menu-style invocation opens a dialog (`UiRequest::Dialog`).
- **Layering** is enforced by `cargo xtask layers`: `core`/`numfmt` (L0) → `formula`/`functions` (L1) → `model`/`calc`/`xlsx` (L2) → `chart` (L3) → `engine` (L5) → `ui-egui`/`mcp` (L6) → `apps/*` (L7). Nothing below L6 depends on egui/eframe/winit/rfd. **The UI crate is swappable.**
@@ -49,7 +51,7 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; delete your target dir when done.
## Roadmap
`ROADMAP.md` (committed) is the one-page summary: stage, headline numbers, dimensions, languages, progress log. It points at the detail in `docs/`: `target-app-parity.md` (authoritative assessment), `gaps.md` (ranked work list: pick from it), `roadmap.md` (current focus, beta gates, milestones), `architecture.md`, and the area checklists `function-parity.md`, `chart-parity.md`, `file-format-parity.md`, `ui-parity.md`, `hardware-parity.md`, `localization-parity.md`. Update the affected docs (and their timestamp lines and revision history) whenever work lands. `cargo xtask parity` regenerates `docs/parity-checklist.md` from `crates/engine/src/catalog.rs`.
`ROADMAP.md` (committed) is the one-page summary: stage, headline numbers, dimensions, languages, progress log. It follows craftrules `standards/progress-docs.md` and points at the detail in `docs/`: `target-app-parity.md` (authoritative assessment), `gaps.md` (ranked work list: pick from it), `roadmap.md` (current focus, beta gates, milestones), `architecture.md`, and the area checklists `function-parity.md`, `chart-parity.md`, `file-format-parity.md`, `ui-parity.md`, `hardware-parity.md`, `localization-parity.md`. Update the affected docs (and their timestamp lines and revision history) whenever work lands. `cargo xtask parity` regenerates `docs/parity-checklist.md` from `crates/engine/src/catalog.rs`.
## Contributor credits (About window)
@@ -60,13 +62,11 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr
username has a `[people.<username>]` entry in `contributors/people.toml`. If not, ask them once
whether they want to be credited by more than their username: a real name, a display name, and/or
their public GitHub profile name (`sync_github_name = true`). If yes, add **only their own** entry
(copy the template at the top of the file)
(copy the template at the top of the file, or run
`python3 ../../craftrules/scripts/contributors.py --add-me . --real-name "…" --sync-github-name`)
and include it in their PR, committed as them. If no, change nothing: they are credited as
`@username` anyway.
- Never add, edit, guess or copy anyone else's entry or name (not from git config, commit authors or
GitHub profiles). Never hand-edit `contributors.json`.
## CI workflows
- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here.
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
- Maintainers refresh the stats with `python3 ../../craftrules/scripts/contributors.py .` (it also
re-verifies who wrote each `people.toml` entry; `--check` only verifies).
+9 -9
View File
@@ -12,15 +12,15 @@ Generated-in-code art is original and has no file to list: the UI icon set (`cra
| `epaint_default_fonts::HACK_REGULAR` (dependency font input) | Source Foundry Authors (Hack; DejaVu and Bitstream Vera foundations) | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [Hack](https://github.com/source-foundry/Hack) | MIT + Bitstream Vera License (full upstream texts in `NOTICE`) | Hack-Regular.ttf is egui's bundled monospace face and GridCraft's last-resort symbol fallback. Dependency asset unmodified here, no font file copied into this repository. |
| `epaint_default_fonts::NOTO_EMOJI_REGULAR` (dependency font input) | The Noto Project Authors | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [Noto Emoji](https://fonts.google.com/noto/specimen/Noto+Emoji) | SIL Open Font License 1.1 (full upstream text in `NOTICE`) | NotoEmoji-Regular.ttf is enabled by egui's `monochrome_emoji_fonts` feature and used as the monochrome emoji fallback. Dependency asset unmodified here, no font file copied into this repository. |
| `epaint_default_fonts::UBUNTU_LIGHT` (dependency font input) | Canonical Ltd. and the Ubuntu Font Family authors | [egui 6d518fc](https://github.com/storytold/egui/tree/6d518fc799681571963842f95b55a6b68431b15a/crates/epaint_default_fonts/fonts), [Ubuntu font](https://design.ubuntu.com/font) | Ubuntu Font Licence 1.0 (full upstream text in `NOTICE`) | Ubuntu-Light.ttf is egui's default proportional face. Dependency asset unmodified here, no font file copied into this repository. |
| `docs/brand/artcraft-logo.svg` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms (`docs/brand/LICENSE-brand.txt`) | Trademark, not open source |
| `docs/brand/artcraft-logo.png` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-logo-white.svg` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-logo-white.png` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark.svg` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark.png` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark-black.svg` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark-black.png` | ArtCraft Team | ArtCraft brand kit | ArtCraft brand terms | Trademark |
| `docs/brand/LICENSE-brand.txt` | (licence text) | project | — | |
| `docs/brand/artcraft-logo.svg` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms (`docs/brand/LICENSE-brand.txt`) | Trademark, not open source |
| `docs/brand/artcraft-logo.png` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-logo-white.svg` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-logo-white.png` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark.svg` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark.png` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark-black.svg` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/artcraft-mark-black.png` | ArtCraft Team | craftrules `assets/brand/` | ArtCraft brand terms | Trademark |
| `docs/brand/LICENSE-brand.txt` | (licence text) | craftrules | — | |
| `assets/app-icon/gridcraft.svg` | GridCraft contributors | original (hand-written SVG; `packaging/icons.sh`) | MIT OR Apache-2.0 (`assets/app-icon/LICENSE.txt`) | PLACEHOLDER app icon (ledger page); replaced by the owner's creature art later |
| `packaging/macos/dmg/background.svg` | @XusBadia | original (hand-written SVG, derived from the GridCraft app icon `assets/app-icon/gridcraft.svg`, referenced via `<image>`, not copied; text is outlined glyph paths; `packaging/macos/dmg/generate.py` renders it to `background.tiff`) | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | GridCraft macOS DMG window background for the Finder window; added 2026-10-08 |
| `packaging/macos/dmg/background.tiff` | @XusBadia | Rendered from `packaging/macos/dmg/background.svg` by `packaging/macos/dmg/generate.py` | MIT OR Apache-2.0 (`LICENSE-MIT`, `LICENSE-APACHE`) | 1x + 2x HiDPI TIFF for the macOS DMG window; includes the app icon (see its row) and LittleCMS's built-in sRGB profile ("No copyright, use freely") |
+6 -9
View File
@@ -2,6 +2,8 @@
GridCraft is a clean-room, open-source, Rust-native spreadsheet targeting Microsoft Excel parity — and superiority (speed, openness, agent control). It runs natively on macOS, Windows, Linux and BSD, and on the web via WASM. Siblings with the same conventions: `../designcraft` (InDesign-class, the closest reference), `../photocraft` (Photoshop), `../vectorcraft` (Illustrator), `../pdfcraft` (Acrobat), `../filmcraft` (Premiere), `../lightcraft` (Lightroom), `../effectcraft` (After Effects).
Standards and learnings shared across the crafting apps live in `../../craftrules` (checked out next to the craft apps, or `storytold/craftrules`). Read its `AGENTS.md` at the start of a session, follow its standards, and contribute reusable learnings back there. Never code: repos don't share code.
## Start every session here
1. Read `plan/STATUS.md` (current milestone, next task), then the task in `plan/execution-plan.md` and the relevant `plan/architecture.md` section. Behaviour reference: `plan/excel/*.md` (`observed-ui.md` holds measured observations of the running app).
2. Follow the autonomous operation protocol (`plan/execution-plan.md` §7). Don't stop to ask unless §7 lists the decision as the user's.
@@ -18,7 +20,7 @@ GridCraft is a clean-room, open-source, Rust-native spreadsheet targeting Micros
- The ArtCraft brand files in `docs/brand/` are ArtCraft trademarks under `docs/brand/LICENSE-brand.txt`, the one exception.
## Never crash
People trust GridCraft with their numbers; a crash loses their work. **This outranks feature work.**
People trust GridCraft with their numbers; a crash loses their work. **This outranks feature work.** Standard: [`craftrules/standards/never-crash.md`](https://github.com/storytold/craftrules/blob/main/standards/never-crash.md).
- **No panics in non-test code:** no `unwrap()`, `expect()`, `panic!`, `unreachable!`, `todo!`, `unimplemented!`; no `unsafe` (`unsafe_code = "forbid"`).
- **Errors are `Result<T, E>`** through the crate's error type and `?`.
- **Input-derived numbers are hostile** (files, formulas, commands, MCP/control params): `get()` instead of indexing, checked/saturating arithmetic, cap input-sized allocations (huge ranges, `REPT`, `SEQUENCE`, whole-column references).
@@ -29,8 +31,8 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr
## Non-negotiables
- **Clean-room.** Microsoft Excel is installed on the dev machine and may be *observed* black-box: run it, use its UI with synthetic workbooks, take screenshots (by window id) stored only under `plan/excel/screenshots/`. Never read, disassemble or copy anything inside the Office bundle, never commit files produced by Excel, never copy GPL/AGPL/LGPL code (LibreOffice, Gnumeric…). Function behaviour comes from public documentation and observation.
- **Fonts live in [`storytold/craft-fonts`](https://github.com/storytold/craft-fonts)**, never in this repo. It is an optional build input (`CRAFT_FONTS_DIR`); without it the app uses system fonts and egui's defaults.
- **Test corpora** (real Excel-authored files) live in a separate corpus repo, fetched pinned and sha256-verified. Never commit large binary fixtures; build test workbooks in code.
- **Fonts live in [`storytold/craft-fonts`](https://github.com/storytold/craft-fonts)**, never in this repo. It is an optional build input (`CRAFT_FONTS_DIR`); without it the app uses system fonts and egui's defaults. Standard: craftrules `standards/fonts.md`.
- **Test corpora** (real Excel-authored files) live in a separate corpus repo, fetched pinned and sha256-verified (craftrules `standards/test-corpora.md`). Never commit large binary fixtures; build test workbooks in code.
- **Everything is a command.** User-visible behaviour = a command in `crates/engine/src/cmd/*` (id, label, ribbon path, shortcut, params doc, `enabled`, `run`) + tests. Ids follow Excel's ribbon (`home.bold`, `insert.chart`, `data.sortAscending`) plus primitives (`cell.set`, `selection.set`, `range.setValues`). UI-only commands live in `crates/ui-egui`. The control channel, CLI and MCP reach all of them.
- **Programmatic calls never open dialogs**: a command with empty params runs with defaults or returns an error; only menu-style invocation opens a dialog (`UiRequest::Dialog`).
- **Layering** is enforced by `cargo xtask layers`: `core`/`numfmt` (L0) → `formula`/`functions` (L1) → `model`/`calc`/`xlsx` (L2) → `chart` (L3) → `engine` (L5) → `ui-egui`/`mcp` (L6) → `apps/*` (L7). Nothing below L6 depends on egui/eframe/winit/rfd. **The UI crate is swappable.**
@@ -48,9 +50,4 @@ People trust GridCraft with their numbers; a crash loses their work. **This outr
- Parallel agents: separate `CARGO_TARGET_DIR` per agent; edit only the crates you own; delete your target dir when done.
## Roadmap
`ROADMAP.md` (committed) is the one-page summary: stage, headline numbers, dimensions, languages, progress log. It points at the detail in `docs/`: `target-app-parity.md` (authoritative assessment), `gaps.md` (ranked work list: pick from it), `roadmap.md` (current focus, beta gates, milestones), `architecture.md`, and the area checklists `function-parity.md`, `chart-parity.md`, `file-format-parity.md`, `ui-parity.md`, `hardware-parity.md`, `localization-parity.md`. Update the affected docs (and their timestamp lines and revision history) whenever work lands. `cargo xtask parity` regenerates `docs/parity-checklist.md` from `crates/engine/src/catalog.rs`.
## CI workflows
- Workflows in `.github/workflows/` call the shared workflows in [`storytold/craft-common-actions`](https://github.com/storytold/craft-common-actions) at `@main`, with their inputs and secrets. No other jobs: if no shared workflow does what's needed, it doesn't go here.
- Don't add or change CI in a feature or fix PR. If the existing workflows can't build or test a change, link an existing craft-common-actions issue or PR, or open a new issue, or a PR only for a small fix that is safe for every app and does not increase CI time.
- Some workflow files here still run their own jobs. Don't add to them. Moving a job into craft-common-actions is its own task, never part of another PR, and the job stays here until a shared workflow runs it.
`ROADMAP.md` (committed) is the one-page summary: stage, headline numbers, dimensions, languages, progress log. It follows craftrules `standards/progress-docs.md` and points at the detail in `docs/`: `target-app-parity.md` (authoritative assessment), `gaps.md` (ranked work list: pick from it), `roadmap.md` (current focus, beta gates, milestones), `architecture.md`, and the area checklists `function-parity.md`, `chart-parity.md`, `file-format-parity.md`, `ui-parity.md`, `hardware-parity.md`, `localization-parity.md`. Update the affected docs (and their timestamp lines and revision history) whenever work lands. `cargo xtask parity` regenerates `docs/parity-checklist.md` from `crates/engine/src/catalog.rs`.
Generated
+30 -32
View File
@@ -220,7 +220,7 @@ dependencies = [
"objc2-foundation 0.3.2",
"parking_lot",
"percent-encoding",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
"wl-clipboard-rs",
"x11rb",
]
@@ -889,12 +889,12 @@ dependencies = [
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
[[package]]
name = "craft-i18n"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
dependencies = [
"fixed_decimal",
"icu_decimal",
@@ -917,10 +917,9 @@ dependencies = [
[[package]]
name = "craft-print"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
dependencies = [
"craft-winprint-gdi",
"log",
"miniz_oxide 0.9.1",
"roxmltree",
"tempfile",
@@ -931,7 +930,7 @@ dependencies = [
[[package]]
name = "craft-ui"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
dependencies = [
"craft-fonts-build",
"craft-i18n",
@@ -951,14 +950,13 @@ dependencies = [
[[package]]
name = "craft-update"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
dependencies = [
"chrono",
"dunce",
"egui",
"flate2",
"getrandom 0.3.4",
"image",
"log",
"plist",
"semver",
@@ -977,7 +975,7 @@ dependencies = [
[[package]]
name = "craft-window"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c5a1cbfb5f9049f9270955caffb5bee5997aebf7#c5a1cbfb5f9049f9270955caffb5bee5997aebf7"
source = "git+https://github.com/storytold/craft-libs?rev=92d392cff132eaf53e13afff77f38b5c11b73aa0#92d392cff132eaf53e13afff77f38b5c11b73aa0"
dependencies = [
"egui",
"objc2 0.6.5",
@@ -1457,7 +1455,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -1937,7 +1935,7 @@ dependencies = [
[[package]]
name = "gridcraft"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"arboard",
"chrono",
@@ -1964,7 +1962,7 @@ dependencies = [
[[package]]
name = "gridcraft-calc"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-core",
"gridcraft-formula",
@@ -1976,7 +1974,7 @@ dependencies = [
[[package]]
name = "gridcraft-chart"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-calc",
"gridcraft-core",
@@ -1987,7 +1985,7 @@ dependencies = [
[[package]]
name = "gridcraft-cli"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-engine",
"gridcraft-mcp",
@@ -1999,7 +1997,7 @@ dependencies = [
[[package]]
name = "gridcraft-core"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"chrono",
"js-sys",
@@ -2010,7 +2008,7 @@ dependencies = [
[[package]]
name = "gridcraft-engine"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-calc",
"gridcraft-chart",
@@ -2032,7 +2030,7 @@ dependencies = [
[[package]]
name = "gridcraft-formula"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-core",
"serde",
@@ -2041,7 +2039,7 @@ dependencies = [
[[package]]
name = "gridcraft-functions"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-core",
"regex",
@@ -2049,7 +2047,7 @@ dependencies = [
[[package]]
name = "gridcraft-mcp"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-engine",
"log",
@@ -2058,7 +2056,7 @@ dependencies = [
[[package]]
name = "gridcraft-model"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-core",
"gridcraft-formula",
@@ -2070,18 +2068,18 @@ dependencies = [
[[package]]
name = "gridcraft-numfmt"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-core",
]
[[package]]
name = "gridcraft-pdf"
version = "0.15.0"
version = "0.13.0"
[[package]]
name = "gridcraft-ui-egui"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"craft-ui",
"craft-update",
@@ -2106,7 +2104,7 @@ dependencies = [
[[package]]
name = "gridcraft-web"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"eframe",
"egui",
@@ -2123,7 +2121,7 @@ dependencies = [
[[package]]
name = "gridcraft-xlsx"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"aes",
"gridcraft-core",
@@ -3462,7 +3460,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967"
dependencies = [
"libc",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -3876,7 +3874,7 @@ dependencies = [
"once_cell",
"socket2",
"tracing",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4186,7 +4184,7 @@ dependencies = [
"errno",
"libc",
"linux-raw-sys 0.12.1",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4245,7 +4243,7 @@ dependencies = [
"security-framework",
"security-framework-sys",
"webpki-root-certs",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -4979,7 +4977,7 @@ dependencies = [
"getrandom 0.4.3",
"once_cell",
"rustix 1.1.5",
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -5938,7 +5936,7 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys 0.59.0",
"windows-sys 0.61.2",
]
[[package]]
@@ -6404,7 +6402,7 @@ checksum = "e450f9b2ed1dff33c94c12589a87338689467b9c4f5d8a5710bd09a847d2c8a7"
[[package]]
name = "xtask"
version = "0.15.0"
version = "0.13.0"
dependencies = [
"gridcraft-engine",
"serde_json",
+3 -4
View File
@@ -4,7 +4,7 @@ members = ["crates/*", "apps/*", "xtask"]
default-members = ["crates/*", "apps/gridcraft", "apps/gridcraft-cli", "xtask"]
[workspace.package]
version = "0.15.0"
version = "0.13.0"
edition = "2024"
license = "MIT OR Apache-2.0"
rust-version = "1.95"
@@ -26,8 +26,7 @@ gridcraft-mcp = { path = "crates/mcp" }
gridcraft-pdf = { path = "crates/pdf" }
# Every craft-libs crate (craft-update and craft-window here, craft-ui in ui-egui, craft-print in
# the app) comes from one storytold/craft-libs revision.
# craft-update: app-specific pretend versions apply to startup and manual checks.
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", version = "=0.1.0", features = ["egui"] }
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", version = "=0.1.0", features = ["egui"] }
# foundation
thiserror = "2"
@@ -63,7 +62,7 @@ skrifa = { version = "0.44", default-features = false, features = ["std"] }
epaint_default_fonts = "0.37"
# One native window per workbook: the window list, Window menu titles and the immediate-viewport
# driver. Same craft-libs revision as craft-update above.
craft-window = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7", default-features = false, features = ["egui"] }
craft-window = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0", default-features = false, features = ["egui"] }
[workspace.lints.rust]
unsafe_code = "forbid"
+1 -9
View File
@@ -298,15 +298,7 @@ stand on its own.
| <img src="https://raw.githubusercontent.com/storytold/pdfcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.pdfcraft.png" alt="" width="32" height="32"> | **PdfCraft** | Reading, organizing and protecting PDFs | [GitHub](https://github.com/storytold/pdfcraft) | [Website](https://getartcraft.com/apps/pdfcraft) |
| <img src="https://raw.githubusercontent.com/storytold/effectcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.effectcraft.png" alt="" width="32" height="32"> | **EffectCraft** | Motion graphics and visual effects | [GitHub](https://github.com/storytold/effectcraft) | [Website](https://getartcraft.com/apps/effectcraft) |
| <img src="https://raw.githubusercontent.com/storytold/designcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.designcraft.png" alt="" width="32" height="32"> | **DesignCraft** | Page layout and publishing | [GitHub](https://github.com/storytold/designcraft) | [Website](https://getartcraft.com/apps/designcraft) |
| <img src="https://raw.githubusercontent.com/storytold/figcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.figcraft.png" alt="" width="32" height="32"> | **FigCraft** | Interface design and prototyping | [GitHub](https://github.com/storytold/figcraft) | [Website](https://getartcraft.com/apps/figcraft) |
| <img src="https://raw.githubusercontent.com/storytold/wordcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.wordcraft.png" alt="" width="32" height="32"> | **WordCraft** | Word processing and documents | [GitHub](https://github.com/storytold/wordcraft) | [Website](https://getartcraft.com/apps/wordcraft) |
| <img src="https://raw.githubusercontent.com/storytold/soundcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.soundcraft.png" alt="" width="32" height="32"> | **SoundCraft** | Audio recording and editing | [GitHub](https://github.com/storytold/soundcraft) | [Website](https://getartcraft.com/apps/soundcraft) |
| <img src="https://raw.githubusercontent.com/storytold/deckcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.deckcraft.png" alt="" width="32" height="32"> | **DeckCraft** | Presentations and slide decks | [GitHub](https://github.com/storytold/deckcraft) | [Website](https://getartcraft.com/apps/deckcraft) |
| <img src="https://raw.githubusercontent.com/storytold/cadcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.cadcraft.png" alt="" width="32" height="32"> | **CADCraft** | Computer-aided design | [GitHub](https://github.com/storytold/cadcraft) | [Website](https://getartcraft.com/apps/cadcraft) |
| <img src="https://raw.githubusercontent.com/storytold/gridcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.gridcraft.png" alt="" width="32" height="32"> | **GridCraft** | Spreadsheets and data | [GitHub](https://github.com/storytold/gridcraft) | [Website](https://getartcraft.com/apps/gridcraft) |
| <img src="https://raw.githubusercontent.com/storytold/texturecraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.texturecraft.png" alt="" width="32" height="32"> | **TextureCraft** | 3D texture painting | [GitHub](https://github.com/storytold/texturecraft) | [Website](https://getartcraft.com/apps/texturecraft) |
| <img src="https://raw.githubusercontent.com/storytold/craft-anything/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.craft-anything.png" alt="" width="32" height="32"> | **Craft Anything** | See how any software works, then build your own | [GitHub](https://github.com/storytold/craft-anything) | [Website](https://getartcraft.com/apps/craft-anything) |
| <img src="https://raw.githubusercontent.com/tangency-labs/solidcraft/main/assets/icons/app/linux/exports/hicolor/64x64/apps/io.tangencylabs.solidcraft.png" alt="" width="32" height="32"> | **SolidCraft** | Parametric 3D CAD by Tangency Labs (community app) | [GitHub](https://github.com/tangency-labs/solidcraft) | |
| <img src="https://raw.githubusercontent.com/storytold/gridcraft/main/assets/app-icon/hicolor/64x64/apps/ai.storyteller.gridcraft.png" alt="" width="32" height="32"> | **GridCraft** | **Spreadsheets and data · you are here** | [GitHub](https://github.com/storytold/gridcraft) | [Website](https://getartcraft.com/apps/gridcraft) |
And [**ArtCraft**](https://getartcraft.com/) itself, our AI image and video studio for artists who want real control.
+5 -20
View File
@@ -2,7 +2,7 @@
**Stage: alpha** · next: beta, ~25 points of "ready for real work" and ~160–240 h away
> **Last reviewed:** 2026-11-01 · **Last updated:** 2026-11-01 · **Change:** minor (Formulas › Function Library as labelled buttons with category menus; Recently Used and Insert Function categories; XLSX workbook-view selection, bounded tab ratios and sheet-edit regressions; readiness estimates unchanged; earlier: CSV and text open detects each column's date order, so day-first dates are not silently swapped) · **Target:** Microsoft Excel (Microsoft 365)
> **Last reviewed:** 2026-10-11 · **Last updated:** 2026-10-25 · **Change:** minor (AutoSum over a selected row puts its total to the right, and fills a selection's empty last row and column) · **Target:** Microsoft Excel (Microsoft 365)
GridCraft aims at full Microsoft Excel parity, and at being better in places: faster, open (XLSX
is the native format), fully scriptable by agents (MCP, control channel, CLI), and on the web.
@@ -22,7 +22,7 @@ This page is the summary. The detail is in:
| Number | Value | Kind |
|---|---|---|
| 2026-10-14 | Editing a cell with text that would read back as a number, boolean, date or formula (entered with a leading apostrophe) shows the apostrophe in the editor and formula bar, so Enter unchanged keeps it text (#1362) |
| **Feature breadth** (weighted) | **~75%** | estimated, anchored on two measurements: ribbon catalog 276 / 292 (94.5%), worksheet functions 513 / 523 (98.1%; 504 working, 96.4%) |
| **Feature breadth** (weighted) | **~75%** | estimated, anchored on two measurements: ribbon catalog 274 / 290 (94.5%), worksheet functions 513 / 523 (98.1%; 504 working, 96.4%) |
| **Ready for real work** | **~50%** | estimated from written weights (51.6); [target-app-parity.md](docs/target-app-parity.md) |
| **Mainstream practitioner** | **~50%** | estimated: typical analyst/finance user, weekly features, × 0.90 interaction × 0.92 stability × 0.88 file exchange; [method](docs/target-app-parity.md#mainstream-practitioner) |
| **Essentials user** | **~63%** | estimated: core features only, × 0.92 launch/stability × 0.95 clarity × 0.90 opening sent files; [method](docs/target-app-parity.md#essentials-user) |
@@ -86,7 +86,7 @@ collaboration, mobile, AI).
| Get & Transform, external data, data model | ~15% | ~10% | 60–100 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) |
| Insert objects (pictures, shapes, ink, SmartArt, equations) | ~55% | ~45% | 25–40 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) |
| Review, comments, protection, collaboration | ~55% | ~40% | 35–55 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) |
| Automation (recorded scripts; typed macros with a recorder, a Macros dialog and a step editor; no VBA) | ~30% | ~20% | 12–20 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) |
| Automation (recorded scripts; no VBA) | ~30% | ~20% | 12–20 | [target-app-parity.md](docs/target-app-parity.md#feature-areas) |
## Languages
@@ -135,13 +135,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| Date | What landed |
|---|---|
| 2026-11-01 | XLSX keeps the first visible workbook window's active sheet and display settings together, falling back to the first view when all are hidden. Tab ratios are normalized to 0–1000 on import and export; invalid numeric text uses 600. Synthetic round trips cover isolated attributes, missing views, and active tabs after sheet deletion/reordering; firstSheet remains separate #2264 work (#3057). |
| 2026-10-31 | Esc in File › Info, Print or Options first leaves the text field; the next Esc returns to the sheet (#3035) |
| 2026-10-31 | CSV and text open detects each column's date order: a value that reads only day first (`31/05/2026`) makes the column day first, a value that reads only month first (`12/31/2026`) makes it month first, an all-ambiguous column keeps month first with a warning, and a column with both kinds stays text with a warning (#2765) |
| 2026-10-31 | Recently Used lists the functions most recently entered in a formula from any source (ribbon, control channel, MCP, scripts), kept with the preferences and returned by `formulas.recentlyUsed`; the Insert Function dialog takes Excel's title and a working category list (Most Recently Used, All, each category) |
| 2026-10-31 | Formulas › Function Library as a big Insert Function button, the AutoSum split button and three columns of labelled small buttons, each category book in its own colour; category menus end with Insert Function… on that category; More Functions cascades into the remaining categories; Home › Editing shares the AutoSum menu; the MR key tip opens Recently Used |
| 2026-10-25 | AutoSum over a selection does what Excel does: a selected row gets one total to its right (in its last cell if that is empty), not a one-cell total under each cell; a block whose last column is empty gets each row's total there, one whose last row is empty each column's, and with both the corner gets the total of the block; otherwise totals go under each column as before |
| 2026-10-24 | Macro interface ([macros.md](docs/macros.md#in-the-window)): an Automate › Macros group (Record Macro, which turns into Stop Recording, Use Relative References, Run Macro with recent macros); "Recording · N steps" with Stop in the status bar; a Record Macro dialog (checked name, free Ctrl+Shift shortcut, description); the Macros dialog lists macros beside Automate scripts and a VBA project, with Run, Step Into, Edit, Create, Delete and Options; Ctrl+Shift+letter macro shortcuts; questions that offer Run, Overwrite, Shift Down or Shift Right; a step editor with a breakpoint gutter, drag to reorder, a command picker, reference boxes that switch between relative tokens and absolute references, Inputs and Output tabs, a debug bar (Check, Run, Step, Continue, Run to Here, Stop with keep or undo) and grid overlays for inputs, writes, conflicts and the current step |
| 2026-10-24 | Go To lists the named ranges of every sheet (`Sheet2!Name` for a name scoped to another sheet; hidden names, constants and formulas left out) and the references entered before, a double-click goes to an entry, and the Reference box takes several names or references separated by commas (`Price, Type`, `B14:C22,F19:G30`) |
| 2026-10-24 | Status bar: a right-click opens a menu that sets which selection statistics show (Average, Count and Sum by default; Numerical Count, Minimum and Maximum are new), kept with the preferences (`view.statusBar`) |
| 2026-10-24 | Ctrl+Shift+U (Control+Shift+U on a Mac) expands and collapses the formula bar, on the grid and while editing (`view.expandFormulaBar`); on Windows and Linux it used to underline |
@@ -172,7 +166,6 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| 2026-10-14 | Lookups (MATCH, XMATCH, VLOOKUP, HLOOKUP, XLOOKUP, LOOKUP) compare numbers exactly like Excel, so a computed key such as `=0.1+0.2` no longer finds 0.3; `=` and COUNTIF/SUMIF keep their 15-digit comparison |
| 2026-10-14 | Functions: EOMONTH, EDATE, WORKDAY, NETWORKDAYS, YEARFRAC, WEEKNUM, the bond and coupon functions, DISC, BIN2DEC, IM* and the other Analysis ToolPak functions give `#VALUE!` for a reference to more than one cell in a value argument, as Excel 365 does, instead of spilling; array constants and computed arrays still lift |
| 2026-10-14 | Protect Sheet on a protected sheet: Home › Format, its keytips (HOP, RPS) and the tab menu now read Unprotect Sheet… and unprotect (straight away without a password); a protected sheet that forbids selecting locked (or unlocked) cells is enforced for clicks, arrows and Go To, and the Protect Sheet dialog has the select options (#1346) |
| 2026-10-14 | `.xls` (Excel 97-2003) files open with their formatting: fonts, solid and pattern fills, borders (with diagonals), alignment (rotation, indent, wrap, shrink), locked and hidden cells, the workbook's custom colour palette, its Normal font and its user-defined cell styles |
| 2026-10-14 | XLSX: formulas opened with dynamic-array metadata (`cm`) keep it on save, with `xl/metadata.xml`. Before, a formula returning one value lost it (11 of 40 corpus files that have the part lost it entirely), and Excel recalculated some of them with implicit intersection (`SUMPRODUCT(IFERROR(...))` changed value) |
| 2026-10-14 | Recalculation: a dynamic array's anchor given a new formula that reads its old spill area (`A1` was `=SEQUENCE(3)`, becomes `=SUM(A2:A3)+1` or `=SEQUENCE(1,A2+1)`) sees that area empty instead of the old values |
| 2026-10-14 | Recalculation: when two arrays of one recalculation want the same empty cells, the formulas that read the one laid out second (at its anchor or over the cells it would have spilled into) read `#SPILL!` and empty cells instead of its values |
@@ -186,7 +179,6 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| 2026-10-13 | XLSX: `[Content_Types].xml`, `_rels/.rels`, the workbook rels and `xl/workbook.xml` are the first zip entries (document properties last), so libmagic and PHP `finfo` detect the file type even with large properties (#1090) |
| 2026-10-13 | Conditional formatting: a rule gathers range statistics only when it reads them (top/bottom, above average, duplicates, scales, bars, icons) and borrows them instead of copying them per cell, so the per-frame lookup of an expression or cell-value rule no longer visits its whole range (#1098, part 1) |
| 2026-10-12 | Next/previous sheet navigation stops at the first/last visible worksheet instead of activating a hidden boundary sheet; the current selection is retained |
| 2026-10-21 | Macro engine ([macros.md](docs/macros.md)): typed, declarative macros saved in the workbook (a JSON field; hidden names in xlsx); declared inputs checked with messages that name the cells; tokens (`Input.col(2)`, `Row`, `Output`, `Name:Total`, `{{…}}` in formulas); one undo step with full rollback; a footprint dry run with ask, fail, overwrite, shift down or shift right; a policy that keeps macros away from files and the app, with approval for macros in opened files; Repeat, If and Stop steps; a debugger; a relative recorder; `macro.*` commands beside the unchanged Automate scripts |
| 2026-10-12 | Security: the `--control` server requires an auth token (random, kept in a 0600 file or chosen with `GRIDCRAFT_CONTROL_TOKEN`) on each connection's first line and closes bad connections (#777) |
| 2026-10-12 | Rows auto-fit oversized fonts: opening a file and whole-row/column formatting grow non-custom rows to their tallest font (#833) |
| 2026-10-12 | Number formats: parsing a format code is linear in its length (long runs of `[`, `,` or `/` no longer take quadratic time), and an unclosed `[` is a literal when splitting sections as well as when tokenizing (#810) |
@@ -239,7 +231,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| 2026-10-11 | PivotCharts (`insert.pivotChart`): charts bound to a PivotTable's report, following refreshes and layout changes (#311) |
| 2026-10-11 | FORECAST.ETS, FORECAST.ETS.CONFINT, FORECAST.ETS.SEASONALITY, FORECAST.ETS.STAT: AAA exponential smoothing forecasts (#314) |
| 2026-10-11 | File tab Backstage view (Home, New, Open, Info, Save As, Print preview, Export, Options) and `file.options` (#174) |
| 2026-10-10 | Ribbon translated into Japanese, Chinese, Korean, Brazilian Portuguese and Russian with system-language default (#31, #104, #82); CJK/Thai/Hebrew font fallback (#38); clear messages for encrypted and `.xls` files (#17); smooth line charts (#62). Progress docs restructured; full re-measure (this page). Initial ribbon key tips (#43), in-place text box editing (#84), copy as HTML table (#36). ODS and XLSB data import (#100, #101); recalc linear in the number of range formulas (#66); RISC-V Linux build; Flatpak repository on Pages; web cell-commit trap fixed |
| 2026-10-10 | Ribbon translated into Japanese, Chinese, Korean, Brazilian Portuguese and Russian with system-language default (#31, #104, #82); CJK/Thai/Hebrew font fallback (#38); clear messages for encrypted and `.xls` files (#17); smooth line charts (#62). Progress docs restructured to the craftrules standard; full re-measure (this page). Initial ribbon key tips (#43), in-place text box editing (#84), copy as HTML table (#36). ODS and XLSB data import (#100, #101); recalc linear in the number of range formulas (#66); RISC-V Linux build; Flatpak repository on Pages; web cell-commit trap fixed |
| 2026-10-09 | Community fixes: spill range operator `A1#`, sheet-scoped names, protection refuses locked-cell edits, Insert/Delete Cells adjust references, `DATE(1900,2,29)`, SUMIF sizing, `ROUND` at 15 digits, `_xlpm.` LAMBDA parameters in XLSX, vertical scrollbar, system appearance, border previews; v0.4.0 |
| 2026-10-08 | v0.2.0 and v0.3.0 releases; Flatpak, AppImage zsync, branded DMG; desktop log file; DirectX 12 default on Windows |
| 2026-10-07 | About window with contributor and model credits; macOS release runner |
@@ -250,13 +242,7 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| Date | Change | Summary |
|---|---|---|
| 2026-11-01 | trivial | Workbook views: selected window settings stay together, tab ratios are bounded, and regressions cover isolated settings and sheet edits; readiness estimates unchanged (#3057) |
| 2026-10-31 | minor | File-page text fields keep the view open when Esc cancels an edit (#3035) |
| 2026-10-31 | minor | Progress log: Recently Used and Insert Function categories |
| 2026-10-31 | minor | Progress log: Function Library |
| 2026-10-25 | minor | Progress log: AutoSum over a selected row, and a selection's empty last row and column |
| 2026-10-24 | minor | Macro interface on the Automate tab; progress log row |
| 2026-10-24 | minor | Macro engine and `macro.*` commands; catalog 275 / 291 → 276 / 292 (View › Macros › Record Macro); progress log row |
| 2026-10-23 | minor | Progress log: accounting negatives in number text |
| 2026-10-23 | minor | Progress log: INDEX with one index on a 2-D reference is #REF! |
| 2026-10-22 | trivial | Progress log: formula hardening (#2163, #2162) |
@@ -272,7 +258,6 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| 2026-10-16 | minor | Progress log: floating pictures' textures bounded and kept per workbook |
| 2026-10-16 | trivial | Progress log: web preferences kept between visits (#1562) |
| 2026-10-16 | minor | Progress log: interface fonts held once instead of twice |
| 2026-10-14 | minor | Progress log: `.xls` import reads cell formatting |
| 2026-10-14 | minor | Progress log: lookups compare numbers exactly |
| 2026-10-14 | minor | Progress log: Analysis ToolPak value arguments reject multi-cell references |
| 2026-10-14 | minor | XLSX dynamic-array `cm` metadata kept on save; progress log row |
@@ -307,5 +292,5 @@ Ranked; estimates in Opus 5.5 agent-hours. Detail and the full beta-gate list in
| 2026-10-10 | minor | Readiness-by-audience table with hours to ~95% per audience; full number unchanged (additive weighted sum) |
| 2026-10-10 | minor | Added Mainstream practitioner (~50%) and Essentials user (~63%); languages updated for the 10-10 merges (ribbon in ja, zh, ko, pt-BR, ru); localization 5%→10%; full number re-checked (51.6, unchanged) |
| 2026-10-10 | minor | Checked against the alpha gate (six core workflows pass): stays alpha |
| 2026-10-10 | major | Re-measured against Excel for Mac 16.113.4: stage alpha, breadth ~75%, ready ~50%, ~160–240 h to beta, ~700–1,100 h to full parity; progress docs restructured; detail moved to `docs/` |
| 2026-10-10 | major | Re-measured against Excel for Mac 16.113.4: stage alpha, breadth ~75%, ready ~50%, ~160–240 h to beta, ~700–1,100 h to full parity; restructured to the progress-docs standard; detail moved to `docs/` |
| 2026-10-07 | major | First estimates: catalog 89%, functions ~93%, depth ~65%, ~35 h to alpha, ~215 h to full parity |
+2 -3
View File
@@ -230,9 +230,8 @@ fn services(inbox: Inbox, ctx: egui::Context) -> Services {
&& let gridcraft_engine::links::LinkKind::External(url) = gridcraft_engine::links::classify(url)
{
// With `noopener` the new browsing context can't hold a reference back to the
// editor window and navigate it while the user reads the linked page. It must be a
// window *feature*: inside the target string it would only name the new window.
let _ = w.open_with_url_and_target_and_features(&url, "_blank", "noopener,noreferrer");
// editor window and navigate it while the user reads the linked page.
let _ = w.open_with_url_and_target(&url, "_blank,noopener");
}
})),
inbox: Some(inbox),
+1 -5
View File
@@ -30,7 +30,7 @@ winit = { version = "0.30", default-features = false, features = ["x11"] }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Same craft-libs revision as the workspace's craft-update and craft-window (see the root
# Cargo.toml).
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" }
craft-print = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
craft-update = { workspace = true }
rfd = { workspace = true }
arboard = { workspace = true }
@@ -52,10 +52,6 @@ muda = { version = "0.21", default-features = false }
# by PdfCraft and PhotoCraft) registers the handler (Objective-C interop needs unsafe, which our
# workspace forbids) behind a safe API. Pinned exactly: review the diff before bumping.
fmv-macos-events = "=0.1.0"
# apple_events.rs brings the app forward for a Dock quit's question. Already in Cargo.lock (muda,
# winit); pinned exactly to the same versions.
objc2 = "=0.6.5"
objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "NSApplication", "NSResponder", "NSWindow"] }
# tests/mac_menu_appkit.rs: reads AppKit's menu bar back. Already in Cargo.lock (muda, winit);
# pinned exactly to the same versions.
+1 -28
View File
@@ -11,8 +11,6 @@
use fmv_macos_events::{Event, Inbox, Registration};
use gridcraft_ui_egui::SheetApp;
use objc2::MainThreadMarker;
use objc2_app_kit::{NSApplication, NSRequestUserAttentionType};
/// Keeps the Apple-event handlers registered; hold it until the event loop returns.
pub struct AppleEvents {
@@ -46,32 +44,7 @@ pub fn poll(inbox: &Inbox, app: &mut SheetApp, ctx: &egui::Context) {
}
}
// Quit from the Dock or the system: each unsaved workbook is asked about in its window.
Event::Quit => {
app.request_quit(ctx);
if app.dialog.is_some() {
bring_app_forward();
}
}
Event::Quit => app.request_quit(ctx),
}
}
}
/// A Dock quit reaches GridCraft behind other apps, hidden or minimized, and its question has to
/// be seen. egui's Focus goes through winit, which skips a minimized window or a hidden app, and
/// macOS may refuse an activation it didn't expect, so ask AppKit directly and bounce the Dock
/// icon until the user switches over.
fn bring_app_forward() {
let Some(mtm) = MainThreadMarker::new() else { return };
let ns_app = NSApplication::sharedApplication(mtm);
ns_app.unhide(None);
for window in ns_app.windows().to_vec() {
if window.isMiniaturized() {
window.deminiaturize(None);
}
}
#[allow(deprecated)] // `activate` needs macOS 14; this works on every supported version.
ns_app.activateIgnoringOtherApps(true);
if !ns_app.isActive() {
ns_app.requestUserAttention(NSRequestUserAttentionType::CriticalRequest);
}
}
+15 -164
View File
@@ -12,47 +12,16 @@
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{Receiver, Sender, channel};
use std::time::{Duration, Instant};
use std::time::Duration;
use gridcraft_mcp::control_auth;
use gridcraft_ui_egui::ControlRequest;
use serde_json::{Value as Json, json};
const MAX_LINE: usize = 64 * 1024 * 1024;
/// How long a new connection may take to send its token, in total.
/// How long a new connection may take to send its token.
const AUTH_TIMEOUT: Duration = Duration::from_secs(10);
/// How many connections may be waiting to authenticate at once. Anyone on the machine can
/// connect without the token, so past this a new connection is closed before anything is read:
/// each waiting one holds a thread and a socket.
const MAX_PENDING_AUTH: usize = 16;
/// How the server treats connections that haven't authenticated yet.
#[derive(Clone, Copy)]
struct AuthLimits {
timeout: Duration,
max_pending: usize,
}
const AUTH_LIMITS: AuthLimits = AuthLimits { timeout: AUTH_TIMEOUT, max_pending: MAX_PENDING_AUTH };
/// A connection that is still waiting to authenticate; dropping it frees its place.
struct Pending(Arc<AtomicUsize>);
impl Pending {
/// A place among the `max` waiting connections, or `None` when they are all taken.
fn claim(waiting: &Arc<AtomicUsize>, max: usize) -> Option<Pending> {
waiting.try_update(Ordering::AcqRel, Ordering::Acquire, |n| (n < max).then_some(n + 1)).ok()?;
Some(Pending(Arc::clone(waiting)))
}
}
impl Drop for Pending {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::AcqRel);
}
}
/// The secret of this run: the one the user chose (`--control-token`, `GRIDCRAFT_CONTROL_TOKEN`),
/// else 256 random bits from the operating system, written to a file only the user can read.
@@ -82,75 +51,35 @@ pub fn start(port: u16, chosen_token: Option<String>, ctx: egui::Context) -> std
/// Accept connections on `listener`; requests arrive on the returned receiver.
fn serve_listener(listener: TcpListener, token: Arc<str>, ctx: egui::Context) -> std::io::Result<Receiver<ControlRequest>> {
serve_listener_with(listener, token, ctx, AUTH_LIMITS)
}
fn serve_listener_with(listener: TcpListener, token: Arc<str>, ctx: egui::Context, limits: AuthLimits) -> std::io::Result<Receiver<ControlRequest>> {
let (tx, rx) = channel();
let waiting = Arc::new(AtomicUsize::new(0));
std::thread::Builder::new().name("control".into()).spawn(move || {
for stream in listener.incoming() {
let Ok(stream) = stream else {
// Out of sockets or handles: wait for some to free instead of spinning on the error.
std::thread::sleep(Duration::from_millis(50));
continue;
};
// Dropping the stream closes it before anything is read.
let Some(place) = Pending::claim(&waiting, limits.max_pending) else { continue };
for stream in listener.incoming().flatten() {
let tx = tx.clone();
let ctx = ctx.clone();
let token = Arc::clone(&token);
let _ =
std::thread::Builder::new().name("control-conn".into()).spawn(move || serve(stream, &token, tx, ctx, limits.timeout, Some(place)));
let _ = std::thread::Builder::new().name("control-conn".into()).spawn(move || serve(stream, &token, tx, ctx));
}
})?;
Ok(rx)
}
/// Read the connection's first line (at most [`control_auth::MAX_AUTH_LINE`] bytes, within
/// `timeout` in total) and check the token. `true` when the peer may go on.
#[allow(clippy::disallowed_methods)] // the desktop app (native only): wasm never runs it
fn authenticate(reader: &mut BufReader<TcpStream>, out: &mut TcpStream, token: &str, timeout: Duration) -> bool {
let deadline = Instant::now().checked_add(timeout).unwrap_or_else(Instant::now);
let mut first = Vec::new();
let read = read_auth_line(reader, &mut first, deadline);
let verdict = match (read, std::str::from_utf8(&first)) {
(Ok(()), Ok(line)) if line.ends_with('\n') => control_auth::check_first_line(line, token),
/// [`AUTH_TIMEOUT`]) and check the token. `true` when the peer may go on.
fn authenticate(reader: &mut BufReader<TcpStream>, out: &mut TcpStream, token: &str) -> bool {
let _ = out.set_read_timeout(Some(AUTH_TIMEOUT));
let mut first = String::new();
let read = reader.by_ref().take(control_auth::MAX_AUTH_LINE as u64).read_line(&mut first);
let verdict = match read {
Ok(n) if n > 0 && first.ends_with('\n') => control_auth::check_first_line(&first, token),
_ => Err(json!({"ok": false, "error": "authentication required"})),
};
let allowed = verdict.is_ok();
let reply = verdict.unwrap_or_else(|e| e);
let _ = writeln!(out, "{reply}");
let _ = reader.get_ref().set_read_timeout(None);
let _ = out.set_read_timeout(None);
allowed
}
/// Read the first line, at most [`control_auth::MAX_AUTH_LINE`] bytes, by `deadline`.
///
/// The timeout goes on the handle the reader reads: on Windows a `try_clone` of the stream keeps
/// its own, so one set on the writing half never fired (a silent peer held its thread forever).
/// It is shortened before every read, so a peer sending one byte at a time can't stretch it.
#[allow(clippy::disallowed_methods)] // the desktop app (native only): wasm never runs it
fn read_auth_line(reader: &mut BufReader<TcpStream>, line: &mut Vec<u8>, deadline: Instant) -> std::io::Result<()> {
let max = control_auth::MAX_AUTH_LINE;
while line.len() < max && line.last() != Some(&b'\n') {
let left = deadline.saturating_duration_since(Instant::now());
if left.is_zero() {
return Err(std::io::ErrorKind::TimedOut.into());
}
reader.get_ref().set_read_timeout(Some(left))?;
let buf = reader.fill_buf()?;
if buf.is_empty() {
break;
}
let room = max.saturating_sub(line.len());
let take = buf.iter().take(room).position(|b| *b == b'\n').map_or(buf.len().min(room), |i| i + 1);
line.extend(buf.iter().take(take));
reader.consume(take);
}
Ok(())
}
/// Read one `\n`-terminated line of at most `max` bytes into `buf` (newline stripped).
/// `Ok(false)` at end of stream; `Err` when the line is too long or the read fails.
fn read_bounded_line(reader: &mut impl BufRead, buf: &mut Vec<u8>, max: usize) -> Result<bool, String> {
@@ -219,14 +148,11 @@ fn is_http_request_line(text: &str) -> bool {
|| text.rsplit_once(' ').is_some_and(|(_, version)| version.starts_with("HTTP/"))
}
fn serve(stream: TcpStream, token: &str, tx: Sender<ControlRequest>, ctx: egui::Context, auth_timeout: Duration, place: Option<Pending>) {
fn serve(stream: TcpStream, token: &str, tx: Sender<ControlRequest>, ctx: egui::Context) {
let Ok(read) = stream.try_clone() else { return };
let mut out = stream;
let mut reader = BufReader::new(read);
let allowed = authenticate(&mut reader, &mut out, token, auth_timeout);
// Authenticated or refused, the connection no longer counts as waiting.
drop(place);
if !allowed {
if !authenticate(&mut reader, &mut out, token) {
return;
}
let mut buf = Vec::new();
@@ -368,7 +294,7 @@ mod tests {
std::thread::spawn(move || {
for stream in listener.incoming().flatten() {
let tx = tx.clone();
std::thread::spawn(move || serve(stream, TOKEN, tx, egui::Context::default(), AUTH_TIMEOUT, None));
std::thread::spawn(move || serve(stream, TOKEN, tx, egui::Context::default()));
}
});
addr
@@ -420,81 +346,6 @@ mod tests {
assert_eq!(got[1]["ok"], json!(false));
}
/// Whether the server closed `c` (end of stream, or a refusal and then end of stream) within
/// `within`, and how long it took.
#[allow(clippy::disallowed_methods)] // a native-only test of the desktop app
fn closed_within(c: TcpStream, within: Duration) -> (bool, Duration) {
let start = Instant::now();
c.set_read_timeout(Some(within)).unwrap();
let mut r = BufReader::new(c);
let mut line = String::new();
loop {
line.clear();
match r.read_line(&mut line) {
Ok(0) => return (true, start.elapsed()),
Ok(_) => continue,
Err(e) if matches!(e.kind(), std::io::ErrorKind::WouldBlock | std::io::ErrorKind::TimedOut) => return (false, start.elapsed()),
Err(_) => return (true, start.elapsed()),
}
}
}
/// The token deadline is for the whole first line: neither a silent peer nor one sending a
/// byte at a time keeps the connection (and its thread) past it.
#[test]
fn a_silent_or_slow_peer_is_closed_at_the_auth_deadline() {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let addr = listener.local_addr().unwrap();
let limits = AuthLimits { timeout: Duration::from_millis(300), max_pending: MAX_PENDING_AUTH };
let _rx = serve_listener_with(listener, TOKEN.into(), egui::Context::default(), limits).unwrap();
let silent = TcpStream::connect(addr).unwrap();
let mut slow = TcpStream::connect(addr).unwrap();
let mut drip = slow.try_clone().unwrap();
std::thread::spawn(move || {
for b in control_auth::auth_line(1, TOKEN).bytes() {
if drip.write_all(&[b]).is_err() {
break;
}
std::thread::sleep(Duration::from_millis(50));
}
});
let (closed, took) = closed_within(silent, Duration::from_secs(5));
assert!(closed && took < Duration::from_secs(3), "silent peer kept for {took:?}");
let (closed, took) = closed_within(slow.try_clone().unwrap(), Duration::from_secs(5));
assert!(closed && took < Duration::from_secs(3), "byte-at-a-time peer kept for {took:?}");
let _ = slow.flush();
}
/// Peers without the token can't pile up threads: past the cap a new connection is closed at
/// once, and a waiting one that authenticates frees its place.
#[test]
fn connections_waiting_to_authenticate_are_capped() {
let listener = TcpListener::bind(("127.0.0.1", 0)).unwrap();
let addr = listener.local_addr().unwrap();
let limits = AuthLimits { timeout: Duration::from_secs(20), max_pending: 2 };
let _rx = serve_listener_with(listener, TOKEN.into(), egui::Context::default(), limits).unwrap();
let mut first = TcpStream::connect(addr).unwrap();
let _second = TcpStream::connect(addr).unwrap();
std::thread::sleep(Duration::from_millis(200));
let (closed, took) = closed_within(TcpStream::connect(addr).unwrap(), Duration::from_secs(5));
assert!(closed && took < Duration::from_secs(3), "a third waiting connection was kept");
first.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
first.write_all(format!("{}\n", control_auth::auth_line(1, TOKEN)).as_bytes()).unwrap();
let mut reply = String::new();
BufReader::new(first.try_clone().unwrap()).read_line(&mut reply).unwrap();
assert_eq!(serde_json::from_str::<Json>(&reply).unwrap()["ok"], json!(true), "{reply}");
std::thread::sleep(Duration::from_millis(200));
let mut next = TcpStream::connect(addr).unwrap();
next.set_read_timeout(Some(Duration::from_secs(5))).unwrap();
next.write_all(format!("{}\n", control_auth::auth_line(1, TOKEN)).as_bytes()).unwrap();
let mut reply = String::new();
BufReader::new(next).read_line(&mut reply).unwrap();
assert_eq!(serde_json::from_str::<Json>(&reply).unwrap()["ok"], json!(true), "a freed place was not reused: {reply}");
}
#[test]
fn a_chosen_token_must_be_long_enough() {
assert!(make_token(1, Some("short".into())).is_err());
+2 -78
View File
@@ -1,5 +1,4 @@
//! Graphics instance configuration, applied before wgpu can initialize any driver: Windows
//! backends and the DirectX 12 shader compiler, and the Intel Mac indirect-validation skip.
//! Windows graphics instance configuration, applied before wgpu can initialize any driver.
//!
//! Vulkan is never initialized unless `WGPU_BACKEND` asks for it: enumerating Vulkan adapters
//! crashes inside Intel's `igvk64.dll` on some UHD Graphics drivers (#120), and that access
@@ -9,38 +8,12 @@
//! whichever `dxcompiler.dll` the DLL search path happens to find (#339, #354): see
//! [`dx12_compiler`].
// On macOS only the indirect-validation items are used outside tests; on Windows the other way
// round, so those two carry their own `allow`.
#![cfg_attr(target_os = "macos", allow(dead_code))]
use eframe::{
NativeOptions,
egui_wgpu::WgpuSetup,
wgpu::{Backends, DeviceType, Dx12Compiler, InstanceFlags},
wgpu::{Backends, DeviceType, Dx12Compiler},
};
/// wgpu compiles its indirect-call validation compute shaders inside `request_device`. On Intel
/// Macs (Iris 5100 in a Mac mini 2014) Metal's compiler aborts on them ("Compiler encountered an
/// internal error"), the device is lost and eframe exits before the window opens. GridCraft issues
/// no indirect draws or dispatches, so Intel Macs skip them; every other GPU keeps wgpu's default.
/// Same as WordCraft (storytold/wordcraft#2652) and PhotoCraft (storytold/PhotoCraft#3171).
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub fn skips_indirect_validation(macos: bool, intel: bool) -> bool {
macos && intel
}
/// Leaves wgpu's indirect-call validation out of the instance eframe creates when `skip`.
/// `WGPU_VALIDATION_INDIRECT_CALL=1` still turns it back on.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
pub fn configure_indirect_validation(options: &mut NativeOptions, skip: bool) {
if !skip {
return;
}
if let WgpuSetup::CreateNew(create) = &mut options.wgpu_options.wgpu_setup {
create.instance_descriptor.flags = create.instance_descriptor.flags.difference(InstanceFlags::VALIDATION_INDIRECT_CALL).with_env();
}
}
/// What the start-up probe learned about one DirectX 12 adapter.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Dx12Adapter {
@@ -168,55 +141,6 @@ fn probe_dx12(options: &NativeOptions) -> Vec<Dx12Adapter> {
mod tests {
use super::*;
fn instance_flags(options: &NativeOptions) -> InstanceFlags {
let WgpuSetup::CreateNew(create) = &options.wgpu_options.wgpu_setup else {
panic!("default native options must create a wgpu instance");
};
create.instance_descriptor.flags
}
#[test]
fn intel_macs_skip_the_indirect_validation_shaders() {
// Metal's Intel compiler aborted on them, losing the device before the window opened.
assert!(skips_indirect_validation(true, true));
// Apple silicon, Windows and Linux keep wgpu's validation.
for (macos, intel) in [(true, false), (false, true), (false, false)] {
assert!(!skips_indirect_validation(macos, intel), "macos={macos} intel={intel}");
}
let mut options = NativeOptions::default();
let before = instance_flags(&options);
configure_indirect_validation(&mut options, false);
assert_eq!(instance_flags(&options), before, "flags untouched when not skipping");
if std::env::var_os("WGPU_VALIDATION_INDIRECT_CALL").is_none() {
configure_indirect_validation(&mut options, true);
assert!(!instance_flags(&options).contains(InstanceFlags::VALIDATION_INDIRECT_CALL));
}
}
/// `WGPU_VALIDATION_INDIRECT_CALL=1` turns the validation back on even when it is skipped.
/// Tests can't set environment variables (`set_var` is unsafe, and unsafe code is forbidden),
/// so this runs itself again as a child process with the variable set.
#[test]
fn the_environment_turns_indirect_validation_back_on() {
const NAME: &str = "graphics::tests::the_environment_turns_indirect_validation_back_on";
if std::env::var_os("GRIDCRAFT_INDIRECT_VALIDATION_CHILD").is_some() {
let mut options = NativeOptions::default();
configure_indirect_validation(&mut options, true);
assert!(instance_flags(&options).contains(InstanceFlags::VALIDATION_INDIRECT_CALL));
return;
}
let exe = std::env::current_exe().unwrap();
let out = std::process::Command::new(exe)
.args(["--exact", NAME, "--test-threads=1"])
.env("GRIDCRAFT_INDIRECT_VALIDATION_CHILD", "1")
.env("WGPU_VALIDATION_INDIRECT_CALL", "1")
.output()
.unwrap();
let stdout = String::from_utf8_lossy(&out.stdout);
// `--exact` matching nothing would pass too, so the child must report this one test.
assert!(out.status.success() && stdout.contains("1 passed"), "child test failed:\n{stdout}{}", String::from_utf8_lossy(&out.stderr));
}
fn configured_backends(backends: Backends) -> Backends {
let mut options = NativeOptions::default();
configure_backends(&mut options, backends);
+7 -11
View File
@@ -12,7 +12,7 @@
#[cfg(target_os = "macos")]
mod apple_events;
mod control_server;
#[cfg(any(target_os = "windows", target_os = "macos", test))]
#[cfg(any(target_os = "windows", test))]
mod graphics;
mod logging;
#[cfg(target_os = "macos")]
@@ -256,7 +256,7 @@ fn main() -> eframe::Result<()> {
}));
let args: Vec<String> = std::env::args().skip(1).collect();
// The restart helper a self-update spawns: it finishes the swap and relaunches, then exits.
if let Some(done) = craft_update::self_update::finish(&gridcraft_ui_egui::UPDATE_APP, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
if let Some(done) = craft_update::self_update::finish(&craft_update::suite::GRIDCRAFT, &std::env::args_os().skip(1).collect::<Vec<_>>()) {
if let Err(e) = done {
eprintln!("GridCraft update helper failed: {e}");
std::process::exit(1);
@@ -311,16 +311,15 @@ fn main() -> eframe::Result<()> {
{
log::warn!("sample {s}: {e}");
}
if session.documents().is_empty() {
session.new_workbook();
}
let mut app = SheetApp::new(session, services());
app.watch_desktop_theme();
app.start_update_check();
app.after_engine(); // Show warnings from the sample.
let control_port = control_port;
load_prefs(&mut app);
// Stamp the startup workbook's author from the persisted user name.
if app.session.documents().is_empty() {
app.session.new_workbook();
}
app.after_engine(); // Show warnings from the sample.
// Files named on the command line (a double-click on Windows and Linux) open as File › Open
// opens them: a password-protected one asks for its password, and errors are shown, not
// only logged. Each replaces the untouched blank Book1, as a file opened from Finder does.
@@ -341,11 +340,8 @@ fn main() -> eframe::Result<()> {
}
let window_builder = viewport.clone();
let options = eframe::NativeOptions { viewport, ..Default::default() };
#[cfg(unix)]
#[cfg(all(unix, not(target_os = "macos")))]
let mut options = options;
// Intel Macs: Metal's compiler aborts on wgpu's indirect-validation shaders (see graphics.rs).
#[cfg(target_os = "macos")]
graphics::configure_indirect_validation(&mut options, graphics::skips_indirect_validation(true, cfg!(target_arch = "x86_64")));
// winit has no file drag-and-drop on Wayland (only on X11), so dropping files from the file
// manager showed a "no" cursor. Run through XWayland when it's there; GRIDCRAFT_WAYLAND=1
// keeps the native Wayland backend.
+7 -8
View File
@@ -390,12 +390,11 @@ impl NativeMenu {
#[cfg(test)]
mod tests {
// No `use super::*`: tests/mac_menu_appkit.rs builds this file with cfg(test) but no test
// harness, so the #[test] functions are left out and a module-level import would be unused.
use super::*;
#[test]
fn file_menu_has_export_as_pdf_dispatching_the_command() {
let tree = super::tree();
let tree = tree();
let (_, file) = tree.iter().find(|(title, _)| *title == "File").expect("File menu");
let item = file.iter().find(|e| e.0 == "Export as PDF…").expect("Export as PDF… in the File menu");
assert_eq!(item.1, "file.exportPdf");
@@ -404,23 +403,23 @@ mod tests {
#[test]
fn help_menu_checks_for_updates_through_the_registered_command() {
let tree = super::tree();
let tree = tree();
let (_, help) = tree.iter().find(|(title, _)| *title == "Help").expect("Help menu");
let item = help.iter().find(|e| e.0 == "Check for Updates…").expect("Check for Updates… in the Help menu");
assert_eq!(item.1, "app.checkForUpdates");
let mut app = super::SheetApp::new(gridcraft_engine::Session::new(), Default::default());
let mut app = SheetApp::new(gridcraft_engine::Session::new(), Default::default());
assert!(app.run(item.1, serde_json::json!({})).is_ok());
}
#[test]
fn theme_choices_use_the_shared_preference_command() {
let mut app = super::SheetApp::new(gridcraft_engine::Session::new(), Default::default());
let mut app = SheetApp::new(gridcraft_engine::Session::new(), Default::default());
for mode in ["dark", "system", "light"] {
assert!(super::select_theme(&mut app, &format!("theme:{mode}")));
assert!(select_theme(&mut app, &format!("theme:{mode}")));
assert_eq!(app.ui.theme_mode(), mode);
assert!(app.message.is_none());
}
assert!(!super::select_theme(&mut app, "view.formulaBar"));
assert!(!select_theme(&mut app, "view.formulaBar"));
assert_eq!(app.ui.theme_mode(), "light");
}
}
+2 -13
View File
@@ -19,23 +19,12 @@ fn main() {
nsapp.finishLaunching();
let ctx = egui::Context::default();
let mut menu = native_menu::NativeMenu::install(&ctx);
let _menu = native_menu::NativeMenu::install(&ctx);
let bar = nsapp.mainMenu().expect("a main menu");
let tops: Vec<String> = (0..bar.numberOfItems()).filter_map(|i| bar.itemAtIndex(i)?.submenu()).map(|m| m.title().to_string()).collect();
assert_eq!(tops, ["GridCraft", "File", "Edit", "View", "Insert", "Format", "Tools", "Data", "Window", "Help"]);
let windows_menu = nsapp.windowsMenu().expect("AppKit knows the Window menu");
assert_eq!(windows_menu.title().to_string(), "Window");
// A poll lists the open workbook windows at the end of the Window menu (#716).
let mut session = gridcraft_engine::Session::new();
session.new_workbook();
let mut app = gridcraft_ui_egui::SheetApp::new(session, Default::default());
menu.poll(&mut app, &ctx, egui::ViewportId::ROOT);
let open = app.open_windows();
assert_eq!(open.len(), 1, "one workbook window");
let last = windows_menu.itemAtIndex(windows_menu.numberOfItems() - 1).expect("a Window menu item");
assert_eq!(last.title().to_string(), open[0].label(), "the Window menu lists the open workbook");
assert_eq!(nsapp.windowsMenu().map(|m| m.title().to_string()).as_deref(), Some("Window"), "AppKit knows the Window menu");
println!("mac_menu_appkit: ok");
}
+1 -1
View File
@@ -1,7 +1,7 @@
# GridCraft app icon (PLACEHOLDER)
**This icon is temporary.** Every Crafting App's icon is an engraved creature portrait that the
product owner makes; GridCraft's hasn't been made yet.
product owner makes (craftrules `standards/icon-design.md`); GridCraft's hasn't been made yet.
Until it is, this placeholder stands in: a simple ledger page with a header row, a row-number
column, a selected cell with its fill handle and a double rule under a total, on a full-bleed
GridCraft-green tile. It is original geometry written by hand for this repository, deliberately
+1 -1
View File
@@ -1,7 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
<!-- GridCraft PLACEHOLDER icon: a ledger page with a selected cell, on GridCraft green.
Original work; temporary until the owner's engraved creature portrait replaces it
Palette: ink #0b0b0c, paper #efe9dc,
(craftrules standards/icon-design.md). Palette: ink #0b0b0c, paper #efe9dc,
app colour #1f9d55 (proposed). -->
<defs>
<clipPath id="tile"><rect width="512" height="512" rx="112"/></clipPath>

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

@@ -1,7 +1,7 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" width="512" height="512">
<!-- GridCraft PLACEHOLDER icon: a ledger page with a selected cell, on GridCraft green.
Original work; temporary until the owner's engraved creature portrait replaces it
Palette: ink #0b0b0c, paper #efe9dc,
(craftrules standards/icon-design.md). Palette: ink #0b0b0c, paper #efe9dc,
app colour #1f9d55 (proposed). -->
<defs>
<clipPath id="tile"><rect width="512" height="512" rx="112"/></clipPath>

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

+5 -11
View File
@@ -1,10 +1,13 @@
# Contributor names for the About window.
# Contributor names for the About window (craftrules standards/contributors.md).
#
# Everyone who contributes is credited by GitHub username automatically. Anything more is up to
# you: add YOUR OWN entry below, in your own PR (commit it yourself). Never add someone else; a
# maintainer may add a person only when that person asked, recording where in consent_source.
#
# Copy this block, replacing your-github-username (every field is optional):
# Easiest: python3 ../../craftrules/scripts/contributors.py --add-me . \
# [--real-name "Ada Lovelace"] [--display-name "Ada"] [--sync-github-name]
#
# Or copy this block, replacing your-github-username (every field is optional):
#
# [people.your-github-username]
# real_name = "Your Name" # shown when the viewer picks "Real name"
@@ -100,12 +103,3 @@ real_name = "Helen Kwok"
[people.dotCooCoo]
real_name = "Robert Lee"
sync_github_name = true
[people.dylanmtaylor]
sync_github_name = true
[people.Plutonium-fream]
sync_github_name = true
[people.maxm11]
sync_github_name = true
+2 -29
View File
@@ -89,14 +89,8 @@ pub fn zip2(a: &Value, b: &Value, f: impl Fn(&Value, &Value) -> Value) -> Value
/// Cell count of a `rows`×`cols` broadcast result, or `None` when it would exceed [`MAX_CELLS`].
/// Each operand passes its own size cap, but their broadcast product can be far larger
/// (`SEQUENCE(1048576)*SEQUENCE(1,16384)` is 17 billion cells), so it is checked before allocating.
/// The product is formed in `u64`: on wasm32 `usize` is 32 bits, where a `usize` product above
/// `u32::MAX` would wrap (2^32 cells → 0) and slip past the cap.
pub(super) fn broadcast_len(rows: usize, cols: usize) -> Option<usize> {
let cells = u64::try_from(rows).ok()?.checked_mul(u64::try_from(cols).ok()?)?;
if cells > MAX_CELLS {
return None;
}
usize::try_from(cells).ok()
rows.checked_mul(cols).filter(|&n| n as u64 <= MAX_CELLS)
}
fn broadcast_dim(a: usize, b: usize) -> usize {
@@ -130,7 +124,7 @@ pub(super) fn zip3(c: &Array, t: &Value, f: &Value, g: impl Fn(&Value, &Value, &
/// can't nest, so the whole helper gives `#CALC!`, as in Excel.
pub(super) fn single(v: Value) -> Option<Value> {
match v {
Value::Array(a) if a.data.len() > 1 => None,
Value::Array(a) if a.rows * a.cols > 1 => None,
v => Some(v.scalar()),
}
}
@@ -184,24 +178,3 @@ fn binary_scalar(op: BinOp, a: &Value, b: &Value, sys: gridcraft_core::DateSyste
BinOp::Range | BinOp::Union | BinOp::Intersect => Value::Error(CellError::Value),
}
}
#[cfg(test)]
mod tests {
use super::{MAX_CELLS, broadcast_len};
#[test]
fn broadcast_len_caps_at_max_cells() {
assert_eq!(broadcast_len(4_000, 4_000), Some(MAX_CELLS as usize));
assert_eq!(broadcast_len(4_000, 4_001), None);
assert_eq!(broadcast_len(1, MAX_CELLS as usize), Some(MAX_CELLS as usize));
}
/// `65_536 * 65_536` is 2^32. Multiplied as `usize` on wasm32 it wraps to 0, which a
/// `> MAX_CELLS` test would let through; the guard must reject the true product.
#[test]
fn broadcast_len_rejects_a_product_that_wraps_to_zero_on_32_bits() {
assert_eq!(65_536u32.wrapping_mul(65_536), 0);
assert_eq!(broadcast_len(65_536, 65_536), None);
assert_eq!(broadcast_len(1_048_576, 16_384), None);
}
}
-38
View File
@@ -614,25 +614,6 @@ fn lambda_calls_spend_one_work_budget_per_cell() {
assert_eq!(t.num("A2"), 4000.0 * 4001.0 / 2.0);
}
#[test]
fn lifted_lookups_stop_past_their_work_budget() {
// Security: each needle searched the whole lookup vector, so this one cell ran for hours
// (about 1.1e12 compares) and hung opening the file that held it. It is #NUM! at once now.
let mut t = T::new();
t.set("A1", "=ROWS(XMATCH(SEQUENCE(1048576),-SEQUENCE(1048576)))");
assert_eq!(t.get("A1"), Value::Error(CellError::Num));
// Ordinary lifted lookups are unaffected.
for (i, k) in [10, 20, 30, 40, 50].into_iter().enumerate() {
t.set(&format!("B{}", i + 1), &k.to_string());
t.set(&format!("C{}", i + 1), &(i + 1).to_string());
}
t.set("D1", "=SUM(XMATCH({30;50;10},B1:B5))");
t.set("D2", "=SUM(XLOOKUP({20;40},B1:B5,C1:C5))");
t.set("D3", "=SUM(VLOOKUP({30,10},B1:C5,2,FALSE))");
t.set("D4", "=SUM(MATCH({25,50},B1:B5))");
assert_eq!([t.num("D1"), t.num("D2"), t.num("D3"), t.num("D4")], [9.0, 6.0, 4.0, 7.0]);
}
#[test]
fn accounting_negative_text_coerces() {
let mut t = T::new();
@@ -1251,25 +1232,6 @@ fn array_broadcast_result_is_capped() {
assert_eq!(t.num("C3"), 5.0);
}
/// MAP refuses a result past `MAX_CELLS` by its checked product, before allocating it (the
/// 2^32-cell wrap that a 32-bit `usize` product would turn into 0 is pinned in
/// `broadcast_len_rejects_a_product_that_wraps_to_zero_on_32_bits`; `SEQUENCE` tops out at
/// 16384 columns, so the widest product it can build is 16384 squared).
#[test]
fn map_caps_its_result_size() {
let mut t = T::new();
for (at, formula) in
[("A1", "=MAP(SEQUENCE(4001),SEQUENCE(1,4000),LAMBDA(a,b,a))"), ("A2", "=MAP(SEQUENCE(16384),SEQUENCE(1,16384),LAMBDA(a,b,a))")]
{
t.set(at, formula);
assert_eq!(t.get(at), Value::Error(CellError::Num), "{formula}");
}
// Within the cap the broadcast shape is unchanged.
t.set("A3", "=ROWS(MAP(SEQUENCE(3),SEQUENCE(1,2),LAMBDA(a,b,a+b)))");
t.set("A4", "=COLUMNS(MAP(SEQUENCE(3),SEQUENCE(1,2),LAMBDA(a,b,a+b)))");
assert_eq!((t.num("A3"), t.num("A4")), (3.0, 2.0));
}
#[test]
fn text_function() {
let mut t = T::new();
-1
View File
@@ -269,7 +269,6 @@ pub const CATALOG: &[(&str, &str, &str, &str)] = &[
("view.unhideWindow", "View", "Window", "Unhide"),
("view.switchWindows", "View", "Window", "Switch Windows"),
("view.macros", "View", "Macros", "Macros"),
("macro.record", "View", "Macros", "Record Macro"),
// Automate
("automate.newScript", "Automate", "Scripting Tools", "New Script"),
("automate.allScripts", "Automate", "Scripting Tools", "All Scripts"),
+2 -29
View File
@@ -12,7 +12,6 @@ pub mod format;
pub mod formulas;
pub mod insert;
pub mod inspect;
pub mod macros;
pub mod pivot;
pub mod print;
pub mod review;
@@ -144,7 +143,6 @@ pub fn command_specs() -> &'static [CommandSpec] {
v.extend(view::specs());
v.extend(inspect::specs());
v.extend(extra::specs());
v.extend(macros::specs());
v.extend(draw::specs());
v.extend(spelling::specs());
v.extend(solver::specs());
@@ -339,9 +337,6 @@ pub struct Ctx<'a> {
/// The command applied sheet protection itself (row/column inserts and deletes the
/// protection allows), so cells it shifts aren't refused as edits of locked cells.
pub protection_checked: bool,
/// The structural edits the command made, in order (the sheet's name, the edit), as
/// references were rewritten for them.
pub edits: Vec<(String, gridcraft_formula::adjust::Edit)>,
}
pub(crate) fn edit<R>(s: &mut Session, f: impl FnOnce(&mut Ctx) -> Result<R>) -> Result<R> {
@@ -363,33 +358,15 @@ pub(crate) fn commit<R>(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result<R>)
let t0 = if cfg!(target_arch = "wasm32") { None } else { std::env::var_os("GRIDCRAFT_PROFILE").map(|_| wall_clock()) };
let since = |t0: std::time::Duration| wall_clock().saturating_sub(t0);
let mut sel = d.selection.clone();
let mut ctx = Ctx {
wb: (*d.wb).clone(),
changed: Vec::new(),
structural: false,
sel: &mut sel,
fit_rows: Vec::new(),
protection_checked: false,
edits: Vec::new(),
};
let mut ctx = Ctx { wb: (*d.wb).clone(), changed: Vec::new(), structural: false, sel: &mut sel, fit_rows: Vec::new(), protection_checked: false };
let r = f(&mut ctx)?;
let Ctx { mut wb, changed, structural, mut fit_rows, protection_checked, edits, .. } = ctx;
let Ctx { mut wb, changed, structural, mut fit_rows, protection_checked, .. } = ctx;
if !protection_checked {
check_protection(&d.wb, &wb, &d.unlocked_ranges)?;
}
if !structural {
check_arrays(&d.wb, &wb)?;
}
if let Some(log) = d.touched.as_mut() {
for (si, c) in changed.iter().take(TOUCH_LOG_LIMIT.saturating_sub(log.len())) {
if let Some(sh) = wb.sheet(*si) {
log.push((sh.name.clone(), *c));
}
}
}
if let Some(log) = d.edits.as_mut() {
log.extend(edits);
}
if changed.len() <= 200_000 {
fit_rows.extend(changed.iter().map(|(s, c)| (*s, c.row)));
}
@@ -417,10 +394,6 @@ pub(crate) fn commit<R>(d: &mut DocState, f: impl FnOnce(&mut Ctx) -> Result<R>)
Ok(r)
}
/// The most cells a dry run's touch log keeps; a footprint whose log is full may be missing writes
/// that kept a cell's value, so a shift is refused then.
pub(crate) const TOUCH_LOG_LIMIT: usize = 1_000_000;
/// Excel's message for an edit of a locked cell on a protected sheet.
pub(crate) const PROTECTED: &str = "The cell or chart you're trying to change is on a protected sheet.";
+8 -26
View File
@@ -20,7 +20,7 @@ mod paste_special;
mod selection;
use cell::{cell_set, range_fill, range_set_values};
pub(crate) use cell::{check_arg_counts, input_to_cell, is_formula_input, json_to_input, parse_typed_formula};
pub(crate) use cell::{check_arg_counts, input_to_cell, json_to_input, parse_typed_formula};
#[cfg(test)]
pub(crate) use clipboard::parse_tsv;
use clipboard::{clear_clipboard, copy, cut, paste};
@@ -46,7 +46,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Set Values",
[],
None,
"{range?: \"A1\", values: [[...]] (rows of numbers/strings/bools/null; strings starting with = are formulas), parse?: bool (default true: a string goes through typed input, so \"05.04.2026\" becomes a date and \"2.500\" a number, by en-US rules; false stores every other string as the text it is, so a dd.mm.yyyy date or a thousands separator survives, a leading ' is kept and an empty string clears the cell)} → {cells, convertedCount, converted: [{cell, input, stored}] (up to 20 of the strings typed input read as a number, date, time, boolean or error, so a script can notice)}",
"{range?: \"A1\", values: [[...]] (rows of numbers/strings/bools/null; strings starting with = are formulas), parse?: bool (default true: a string goes through typed input, so \"05.04.2026\" becomes a date and \"2.500\" a number, by en-US rules; false stores every other string as the text it is, so a dd.mm.yyyy date or a thousands separator survives)}",
has_doc,
range_set_values
),
@@ -183,8 +183,6 @@ fn undo(s: &mut Session, p: &Json) -> Result<Json> {
label = e.label.clone();
d.wb = e.wb;
d.selection = e.selection;
// Step by step: a rename and a new sheet undone together match up only one step apart.
super::extra::keep_view_modes(d, &cur_wb);
crate::push_history(&mut d.redo, label.clone(), cur_wb, &d.wb, cur_selection);
}
super::extra::keep_watch_list(&now, &mut d.wb);
@@ -205,7 +203,6 @@ fn redo(s: &mut Session, p: &Json) -> Result<Json> {
label = e.label.clone();
d.wb = e.wb;
d.selection = e.selection;
super::extra::keep_view_modes(d, &cur_wb);
crate::push_history(&mut d.undo, label.clone(), cur_wb, &d.wb, cur_selection);
}
super::extra::keep_watch_list(&now, &mut d.wb);
@@ -216,13 +213,6 @@ fn redo(s: &mut Session, p: &Json) -> Result<Json> {
// ---------------------------------------------------------------- clear
/// The rows of `range` an applied filter hides (rows hidden by hand are not among them).
fn filtered_rows(sh: &gridcraft_model::Sheet, range: RangeRef) -> std::collections::BTreeSet<u32> {
sh.rows.range(range.start.row..=range.end.row).filter(|(r, info)| info.hidden && sh.is_row_filtered(**r)).map(|(r, _)| *r).collect()
}
/// The Clear commands (Delete is Clear Contents). As in Excel, the rows an applied filter hides
/// are left as they are; rows hidden by hand are cleared.
fn clear(s: &mut Session, p: &Json, what: &str) -> Result<Json> {
let sheet = target_sheet(s, p)?;
let ranges = target_ranges(s, p)?;
@@ -233,10 +223,7 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result<Json> {
d.wb.sheet(sheet).is_some_and(|sh| {
ranges.iter().any(|r| {
r.intersection(&sh.used_range().unwrap_or(RangeRef::cell(r.start))).is_some_and(|r| {
let skip = filtered_rows(sh, r);
sh.cells
.iter_range(r)
.any(|(c, x)| !skip.contains(&c.row) && x.value == Value::Bool(true) && super::extra::is_checkbox_cell(&d.wb, sh, c))
sh.cells.iter_range(r).any(|(c, x)| x.value == Value::Bool(true) && super::extra::is_checkbox_cell(&d.wb, sh, c))
})
})
})
@@ -250,12 +237,8 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result<Json> {
},
None => continue,
};
let skip = cx.wb.sheet(sheet).map(|sh| filtered_rows(sh, r)).unwrap_or_default();
let cells: Vec<(CellRef, Cell)> = cx
.wb
.sheet(sheet)
.map(|sh| sh.cells.iter_range(r).filter(|(c, _)| !skip.contains(&c.row)).map(|(c, x)| (c, x.clone())).collect())
.unwrap_or_default();
let cells: Vec<(CellRef, Cell)> =
cx.wb.sheet(sheet).map(|sh| sh.cells.iter_range(r).map(|(c, x)| (c, x.clone())).collect()).unwrap_or_default();
// Report every stored cell we touch, even beyond a million coordinates in a sparse
// selection. Removed formula anchors must also reach calc so their spills disappear.
cx.changed.extend(cells.iter().map(|(c, _)| (sheet, *c)));
@@ -290,17 +273,16 @@ fn clear(s: &mut Session, p: &Json, what: &str) -> Result<Json> {
_ => {}
}
}
let cleared = |c: CellRef| r.contains(c) && !skip.contains(&c.row);
if matches!(what, "all" | "comments") {
sh.comments.retain(|c, _| !cleared(*c));
sh.comments.retain(|c, _| !r.contains(*c));
}
if matches!(what, "all" | "hyperlinks") {
sh.hyperlinks.retain(|c, _| !cleared(*c));
sh.hyperlinks.retain(|c, _| !r.contains(*c));
}
if matches!(what, "all" | "formats") {
let mut unmerged = false;
sh.merges.retain(|m| {
let keep = !r.contains_range(m) || skip.range(m.start.row..=m.end.row).next().is_some();
let keep = !r.contains_range(m);
unmerged |= !keep;
keep
});
+12 -67
View File
@@ -5,16 +5,6 @@ use super::{
// ---------------------------------------------------------------- entry
/// Whether typing `input` into a cell enters a formula: `=…`, or `+…`/`-…` followed by a name or
/// a parenthesis (not a number), as Excel takes `+SUM(1)`.
pub(crate) fn is_formula_input(input: &str) -> bool {
input.starts_with('=')
|| (input.len() > 1
&& (input.starts_with('+') || input.starts_with('-'))
&& gridcraft_core::parse::parse_number_text(input).is_none()
&& input.chars().nth(1).is_some_and(|c| c.is_ascii_alphabetic() || c == '('))
}
/// What typing `input` into a cell produces (constant or formula, plus an automatic number
/// format). Inherits the destination's cell, row or column style, including its number
/// format, before parsing. Errors when the formula can't be parsed.
@@ -28,7 +18,12 @@ pub(crate) fn input_to_cell(input: &str, sheet: usize, at: CellRef, wb: &mut gri
if fmt_is_text {
return Ok(Some(Cell { value: Value::text(input), formula: None, style }));
}
if is_formula_input(input) {
let is_formula = input.starts_with('=')
|| (input.len() > 1
&& (input.starts_with('+') || input.starts_with('-'))
&& gridcraft_core::parse::parse_number_text(input).is_none()
&& input.chars().nth(1).is_some_and(|c| c.is_ascii_alphabetic() || c == '('));
if is_formula {
let body = input.strip_prefix('=').unwrap_or(input);
let expr = wb.name_call_case(parse_typed_formula(body)?);
check_arg_counts(&expr)?;
@@ -188,22 +183,6 @@ pub(crate) fn json_to_input(v: &Json) -> String {
}
}
/// How many converted strings `range.setValues` reports back, and how much of each input and
/// stored text: the report is a diagnostic, so a big write must not put a second copy of its data
/// in the result.
const MAX_REPORTED_CONVERSIONS: usize = 20;
const MAX_REPORTED_CHARS: usize = 32;
/// `text` cut to [`MAX_REPORTED_CHARS`] characters for the conversion report, with `…` when it was
/// cut. Counting characters keeps a multi-byte input whole.
fn clipped(text: &str) -> String {
let mut out: String = text.chars().take(MAX_REPORTED_CHARS).collect();
if out.len() < text.len() {
out.push('…');
}
out
}
pub(super) fn range_set_values(s: &mut Session, p: &Json) -> Result<Json> {
let sheet = target_sheet(s, p)?;
let start = target_range(s, p)?.start;
@@ -221,10 +200,6 @@ pub(super) fn range_set_values(s: &mut Session, p: &Json) -> Result<Json> {
return Err(bad("range.setValues", "too many rows"));
}
let mut count = 0;
// The strings typed input read as something other than the text given, for the result: a
// caller can notice a "05.04.2026" that became a date without reading the file back (#2255).
let mut converted: Vec<(CellRef, String)> = Vec::new();
let mut converted_count = 0;
edit(s, |cx| {
for (ri, row) in rows.iter().enumerate() {
let cols: Vec<&Json> = match row.as_array() {
@@ -247,35 +222,15 @@ pub(super) fn range_set_values(s: &mut Session, p: &Json) -> Result<Json> {
// text rather than the marker that makes it text. A formula is still a
// formula, which is what the ticket asks for and what `=` means in a cell.
Json::String(text) if !parse && !text.starts_with('=') => {
if text.is_empty() {
// An empty string clears the cell, as `null` and the default path do:
// an empty text cell is not blank, and a script clearing cells with
// `""` would leave cells `COUNTA` counts. The cell keeps its formatting.
input_to_cell("", sheet, at, &mut cx.wb)?
} else {
let mut style = cx.wb.sheet(sheet).map(|sh| sh.style_id(at)).unwrap_or_default();
// Line breaks still turn on Wrap Text, as typed input does.
if text.contains('\n') && !cx.wb.styles.get(style).align.wrap {
style = cx.wb.styles.derive(style, |s| s.align.wrap = true);
}
Some(Cell { value: Value::Text(text.as_str().into()), formula: None, style })
let mut style = cx.wb.sheet(sheet).map(|sh| sh.style_id(at)).unwrap_or_default();
// Line breaks still turn on Wrap Text, as typed input does.
if text.contains('\n') && !cx.wb.styles.get(style).align.wrap {
style = cx.wb.styles.derive(style, |s| s.align.wrap = true);
}
Some(Cell { value: Value::Text(text.as_str().into()), formula: None, style })
}
_ => input_to_cell(&input, sheet, at, &mut cx.wb)?,
};
// A string that became a formula was written as one (`=`), which is deliberate;
// only a number (dates and times included), boolean or error is a reading of the
// string and worth reporting.
if let Some(c) = &cell
&& matches!(v, Json::String(_))
&& c.formula.is_none()
&& matches!(c.value, Value::Number(_) | Value::Bool(_) | Value::Error(_))
{
converted_count += 1;
if converted.len() < MAX_REPORTED_CONVERSIONS {
converted.push((at, input.clone()));
}
}
let sh = cx.sheet_mut(sheet)?;
match cell {
Some(c) => sh.set_cell(at, c),
@@ -290,17 +245,7 @@ pub(super) fn range_set_values(s: &mut Session, p: &Json) -> Result<Json> {
}
Ok(())
})?;
let d = s.doc()?;
let reported: Vec<Json> =
d.wb.sheet(sheet)
.map(|sh| {
converted
.iter()
.map(|(at, input)| json!({"cell": at.a1(), "input": clipped(input), "stored": clipped(&crate::display::cell_text(&d.wb, sh, *at))}))
.collect()
})
.unwrap_or_default();
Ok(json!({"cells": count, "convertedCount": converted_count, "converted": reported}))
Ok(json!({"cells": count}))
}
pub(super) fn range_fill(s: &mut Session, p: &Json) -> Result<Json> {
-68
View File
@@ -192,74 +192,6 @@ fn set_values_can_store_strings_as_the_text_they_are() {
assert!(d.wb.styles.get(sh.style_id(CellRef::parse("A9").unwrap())).align.wrap);
}
/// The result names the strings typed input read as something else, so a script notices without
/// reading the file back (#2255). A formula and a leading apostrophe are not conversions.
#[test]
fn set_values_reports_the_strings_typed_input_read() {
let mut s = Session::new();
s.new_workbook();
let row = json!(["05.04.2026", "02/11/2026", "2.500", "4.25 %", "=1+1", "'keep"]);
let r = s.execute("range.setValues", json!({"range": "A1", "values": [row.clone()]})).unwrap();
assert_eq!(r["cells"], 6, "{r}");
assert_eq!(r["convertedCount"], 4, "{r}");
assert_eq!(r["converted"][0], json!({"cell": "A1", "input": "05.04.2026", "stored": "5/4/2026"}), "{r}");
assert_eq!(r["converted"][1], json!({"cell": "B1", "input": "02/11/2026", "stored": "2/11/2026"}), "{r}");
assert_eq!(r["converted"][2], json!({"cell": "C1", "input": "2.500", "stored": "2.5"}), "{r}");
assert_eq!(r["converted"][3], json!({"cell": "D1", "input": "4.25 %", "stored": "4.25%"}), "{r}");
// The report is a diagnostic, not a second copy of the data: a long input is cut.
let long = s.execute("range.setValues", json!({"range": "J1", "values": [["9".repeat(100)]]})).unwrap();
assert_eq!(long["convertedCount"], 1, "{long}");
let input = long["converted"][0]["input"].as_str().unwrap_or_default();
assert_eq!(input.chars().count(), 33, "32 characters and the ellipsis: {input}");
assert!(input.ends_with('…'), "{input}");
// Nothing is read with `parse: false`, so nothing is reported.
let verbatim = s.execute("range.setValues", json!({"range": "A3", "parse": false, "values": [row]})).unwrap();
assert_eq!(verbatim["convertedCount"], 0, "{verbatim}");
assert_eq!(verbatim["converted"], json!([]), "{verbatim}");
}
/// An empty string clears the cell with `parse: false` too, as `null` and the default path do: a
/// script clearing cells with "" must not leave cells `COUNTA` counts.
#[test]
fn set_values_parse_false_clears_an_empty_string() {
let mut s = Session::new();
s.new_workbook();
s.execute("range.setValues", json!({"range": "A1:B1", "values": [["keep", "keep"]]})).unwrap();
s.execute("home.bold", json!({"range": "A1"})).unwrap();
s.execute("range.setValues", json!({"range": "A1", "parse": false, "values": [["", ""]]})).unwrap();
let get = |s: &mut Session, a: &str| s.execute("cell.get", json!({"cell": a})).unwrap();
assert_eq!(get(&mut s, "A1")["type"], "empty");
assert_eq!(get(&mut s, "B1")["type"], "empty");
let d = s.doc().unwrap();
let sh = d.wb.sheet(0).unwrap();
assert!(d.wb.styles.get(sh.style_id(CellRef::parse("A1").unwrap())).font.bold, "clearing with \"\" keeps the formatting");
assert_eq!(s.execute("formulas.evaluate", json!({"formula": "=COUNTA(A1:B1)"})).unwrap(), json!(0.0));
}
/// The text `parse: false` stores is still text after the workbook is saved and reopened, and the
/// default's reading of a date survives as a date (#2255).
#[test]
fn set_values_text_survives_an_xlsx_roundtrip() {
let mut s = Session::new();
s.new_workbook();
let values = json!([["05.04.2026", "2.500", "4.25 %", "=1+1", 3, true]]);
s.execute("range.setValues", json!({"range": "A1", "parse": false, "values": values})).unwrap();
s.execute("range.setValues", json!({"range": "A3", "values": [["05.04.2026"]]})).unwrap();
let bytes = crate::io::save_bytes(&s.doc().unwrap().wb, "book.xlsx").unwrap();
let (wb, _) = crate::io::open_bytes("book.xlsx", &bytes).unwrap();
let sh = wb.sheet(0).unwrap();
for (col, want) in [(0, "05.04.2026"), (1, "2.500"), (2, "4.25 %")] {
assert_eq!(sh.value(CellRef::new(0, col)), Value::text(want), "column {col} is the text it was given");
}
assert_eq!(sh.value(CellRef::new(0, 3)), Value::number(2.0), "=1+1 is still a formula");
assert_eq!(sh.value(CellRef::new(0, 4)), Value::number(3.0));
assert_eq!(sh.value(CellRef::new(0, 5)), Value::Bool(true));
// The default read the date, and the file keeps it as one.
let at = CellRef::new(2, 0);
assert_eq!(crate::display::cell_text(&wb, sh, at), "5/4/2026");
assert_eq!(wb.styles.get(sh.style_id(at)).num_fmt.as_str(), "m/d/yyyy");
}
#[test]
fn legacy_array_entry() {
let mut s = Session::new();
+2 -2
View File
@@ -28,7 +28,7 @@ use objects::{align, arrange, rotate, selection_pane, shape_fill, shape_outline}
pub(crate) use scripts::has_scripts;
use scripts::{all_scripts, delete_script, new_script, record, run_script, stop_action, trust_scripts};
use window::{arrange_all, custom_views, hide_window, new_window, ruler, set_view, split, switch_windows, unhide_window, watch_window};
pub(crate) use window::{keep_view_modes, keep_watch_list, window_param};
pub(crate) use window::{keep_watch_list, window_param};
pub fn specs() -> Vec<CommandSpec> {
vec![
@@ -310,7 +310,7 @@ pub fn specs() -> Vec<CommandSpec> {
new_cell_style
),
cmd!(query "home.addIns", "Add-ins", ["Home", "Add-ins"], None, "{} → GridCraft extends through MCP and scripts instead of add-ins", always, |_, _| Ok(json!({"addIns": [], "hint": "Use gridcraft-cli mcp or Automate › scripts"}))),
cmd!(query "formulas.recentlyUsed", "Recently Used", ["Formulas", "Function Library"], None, "{} → the functions most recently entered in formulas, newest first", has_doc, recently_used),
cmd!(query "formulas.recentlyUsed", "Recently Used", ["Formulas", "Function Library"], None, "{} → recently used functions in this workbook", has_doc, recently_used),
cmd!(query "formulas.financial", "Financial", ["Formulas", "Function Library"], None, "{}", always, |s, _| s.execute("formulas.functions", json!({"category": "Financial"}))),
cmd!(query "formulas.logical", "Logical", ["Formulas", "Function Library"], None, "{}", always, |s, _| s.execute("formulas.functions", json!({"category": "Logical"}))),
cmd!(query "formulas.text", "Text", ["Formulas", "Function Library"], None, "{}", always, |s, _| s.execute("formulas.functions", json!({"category": "Text"}))),
+18 -3
View File
@@ -122,10 +122,25 @@ pub(super) fn new_cell_style(s: &mut Session, p: &Json) -> Result<Json> {
})
}
/// Formulas › Recently Used: the functions most recently entered in a formula, newest first
/// ([`crate::Prefs::recent_functions`]).
pub(super) fn recently_used(s: &mut Session, _: &Json) -> Result<Json> {
Ok(json!(s.prefs.recent_functions))
let mut seen: Vec<String> = Vec::new();
for (id, p) in s.journal.iter().rev() {
if id != "cell.set" {
continue;
}
let input = p.get("input").and_then(Json::as_str).unwrap_or("");
if let Ok(e) = gridcraft_formula::parse(input.trim_start_matches('=')) {
for f in e.functions() {
if !seen.iter().any(|x| x == f) {
seen.push(f.to_string());
}
}
}
if seen.len() >= 10 {
break;
}
}
Ok(json!(seen))
}
pub(super) fn use_in_formula(s: &mut Session, p: &Json) -> Result<Json> {
+7 -42
View File
@@ -1,15 +1,10 @@
use super::{EngineError, Json, Result, SCRIPTS_NAME, Session, Workbook, bad, bool_param, edit, json, stash_get, stash_map, stash_set, str_param};
use super::{Json, Result, SCRIPTS_NAME, Session, Workbook, bad, bool_param, edit, json, stash_get, stash_map, stash_set, str_param};
pub(super) fn record(s: &mut Session, p: &Json) -> Result<Json> {
if bool_param(p, "stop").unwrap_or(false) || s.recording.is_some() {
let Some(start) = s.recording.take() else { return Ok(json!({"recording": false})) };
let cmds: Vec<Json> = s
.journal
.iter()
.skip(start)
.filter(|(id, _)| !id.starts_with("automate.") && !crate::record::NOT_RECORDED.contains(&id.as_str()))
.map(|(id, p)| json!({"command": id, "params": p}))
.collect();
let cmds: Vec<Json> =
s.journal.iter().skip(start).filter(|(id, _)| !id.starts_with("automate.")).map(|(id, p)| json!({"command": id, "params": p})).collect();
let name = str_param(p, "name").map(str::to_string).unwrap_or_else(|| format!("Script {}", start));
s.execute("automate.newScript", json!({"name": name, "commands": cmds}))?;
return Ok(json!({"recording": false, "saved": name, "steps": cmds.len()}));
@@ -109,16 +104,12 @@ pub(super) fn run_script(s: &mut Session, p: &Json) -> Result<Json> {
}
let _sandbox = crate::io::ScriptSandbox::enter();
let mut n = 0;
// What ran, for a recorder in progress: the recording keeps these steps with the run, so
// editing or deleting the script before the recording stops does not change what it saved.
let mut ran_steps: Vec<(String, Json)> = Vec::new();
for st in &steps {
let id = st["command"].as_str().unwrap_or("");
if id.starts_with("automate.") {
continue;
}
let before = s.ui_requests.len();
let wb_before = s.doc().ok().map(|d| (d.uid, d.wb.clone()));
let ran = s.execute(id, st["params"].clone());
// A step that asks the host to open a URL leaves the app: drop the request and stop.
if s.ui_requests.get(before..).is_some_and(|new| new.iter().any(|r| matches!(r, crate::UiRequest::OpenUrl(_)))) {
@@ -129,43 +120,17 @@ pub(super) fn run_script(s: &mut Session, p: &Json) -> Result<Json> {
});
return Err(bad("automate.runScript", format!("scripts can't open links (`{id}`)")));
}
let result = ran?;
ran?;
n += 1;
if s.recorder.is_some() {
// The recorder's rule for each step, as for a command run on its own: a menu-style
// step that only opened a dialog is left out (playing it back would stop at the
// dialog), and a toggle keeps the state it set.
let opened_dialog = s.ui_requests.get(before..).is_some_and(|new| new.iter().any(|r| matches!(r, crate::UiRequest::Dialog(..))));
let changed = wb_before.as_ref().is_some_and(|(uid, wb)| s.docs.iter().any(|d| d.uid == *uid && !std::sync::Arc::ptr_eq(&d.wb, wb)));
if !opened_dialog || changed {
ran_steps.push((id.to_string(), crate::record::with_toggle_state(st["params"].clone(), &result)));
}
}
}
if s.recorder.is_some() {
s.script_ran = Some(ran_steps);
}
Ok(json!({"ran": n}))
}
/// Redo work there again. A macro being debugged on that workbook is stopped with them.
/// Automate › Stop Action: rolls back the undo groups open on the active workbook, so Undo and
/// Redo work there again.
pub(super) fn stop_action(s: &mut Session, _: &Json) -> Result<Json> {
// Inside another command the open groups are that command's own (an action in progress):
// a step of it that stopped them would pull the workbook from under the rest of its run.
if s.depth() > 1 {
return Err(EngineError::Disabled("automate.stopAction".into(), "Stop Action runs on its own, not as a step of a script or macro.".into()));
}
let uid = s.doc()?.uid;
let mut out = json!({"stopped": s.abort_groups(uid)});
// A macro being debugged here had its group among them: its run is over.
if let Some((name, wrote_files)) = crate::macros::debug::end_stopped(s, uid) {
out["macro"] = json!(name);
out["message"] = json!(format!(
"Macro '{name}', which was being debugged, was stopped and rolled back. {}",
crate::macros::run::rolled_back(&wrote_files)
));
}
Ok(out)
Ok(json!({"stopped": s.abort_groups(uid)}))
}
pub(super) fn delete_script(s: &mut Session, p: &Json) -> Result<Json> {
-29
View File
@@ -38,35 +38,6 @@ pub(crate) fn keep_watch_list(from: &Workbook, to: &mut std::sync::Arc<Workbook>
wb.names.extend(keep);
}
/// Normal, Page Layout and Page Break Preview belong to the window, not to the edits: Excel doesn't
/// undo a switch between them, and `set_view` is no undo step. So undo and redo keep each sheet's
/// current view when they swap in another snapshot of the workbook, and a view kept this way
/// doesn't make a saved workbook dirty.
pub(crate) fn keep_view_modes(d: &mut crate::DocState, now: &Workbook) {
// Sheets have no stable id. A name missing from `now` is a rename being undone or redone, which
// keeps the sheet count and position: the sheet is the one at the same index, under a name the
// restored snapshot doesn't have.
let renamed = |i: usize| {
(now.sheets.len() == d.wb.sheets.len()).then(|| now.sheets.get(i)).flatten().filter(|sh| !d.wb.sheets.iter().any(|s| s.name == sh.name))
};
let current = |i: usize, name: &str| now.sheets.iter().find(|sh| sh.name == name).or_else(|| renamed(i)).map(|sh| sh.view);
let changed: Vec<(usize, SheetViewMode)> =
d.wb.sheets.iter().enumerate().filter_map(|(i, sh)| current(i, &sh.name).filter(|v| *v != sh.view).map(|v| (i, v))).collect();
if changed.is_empty() {
return;
}
let dirty = d.is_dirty();
let wb = std::sync::Arc::make_mut(&mut d.wb);
for (i, v) in changed {
if let Some(sh) = wb.sheet_mut(i) {
sh.view = v;
}
}
if !dirty {
d.saved = d.wb.clone();
}
}
pub(super) fn custom_views(s: &mut Session, p: &Json) -> Result<Json> {
let d = s.doc()?;
let mut views = stash_map(&d.wb, VIEWS_NAME);
+2 -6
View File
@@ -13,7 +13,7 @@ pub fn specs() -> Vec<CommandSpec> {
cmd!(noundo "file.saveAs", "Save As…", ["File"], Some("Cmd+Shift+S"), "{path} (.xlsx, .csv, .tsv, .json, .html, or .pdf: a PDF of the active sheet)", has_doc, save_as),
cmd!(query "file.saveBytes", "Encode Workbook", [], None, "{format?: xlsx|csv|tsv|json|html} → {base64}", has_doc, save_bytes),
cmd!(noundo "file.close", "Close", ["File"], Some("Cmd+W"), "{force?: bool, document?: bool} (closes the active window, or with `document` the workbook and all its windows; the workbook closes with its last window)", has_doc, close),
cmd!(noundo "file.exportCsv", "Export as CSV", ["File", "Export"], None, "{path, sheet?, values?: bool} → {path, bytes}: writes the cells as displayed, like Excel's Save As CSV; `values: true` writes raw values instead (full-precision numbers, ISO 8601 dates and times, TRUE/FALSE)", has_doc, |s, p| export(s, p, "csv")),
cmd!(noundo "file.exportCsv", "Export as CSV", ["File", "Export"], None, "{path, sheet?}", has_doc, |s, p| export(s, p, "csv")),
cmd!(noundo "file.exportHtml", "Save as Web Page", ["File", "Export"], None, "{path}", has_doc, |s, p| export(s, p, "html")),
cmd!("file.properties", "Properties", ["File"], None, "{title?, subject?, author?, company?, keywords?, description?}", has_doc, properties),
cmd!(
@@ -191,11 +191,7 @@ fn export(s: &mut Session, p: &Json, ext: &str) -> Result<Json> {
wb.set_active_sheet(target_sheet(s, p)?);
}
let target = if path.ends_with(&format!(".{ext}")) { path.clone() } else { format!("{path}.{ext}") };
let bytes = if ext == "csv" && bool_param(p, "values").unwrap_or(false) {
crate::io::csv_values_bytes(&wb)?
} else {
crate::io::save_bytes(&wb, &target)?
};
let bytes = crate::io::save_bytes(&wb, &target)?;
crate::io::write_file(&target, &bytes)?;
Ok(json!({"path": target, "bytes": bytes.len()}))
}
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -60,7 +60,7 @@ pub(super) fn insert_table(s: &mut Session, p: &Json) -> Result<Json> {
let e = Edit::InsertCells { axis: Axis::Rows, range: RangeRef::new(r.start, CellRef::new(r.start.row, r.end.col)) };
shift_cell_features(sh, &e);
let name = sh.name.clone();
rewrite(cx, &name, &e);
rewrite(&mut cx.wb, &name, &e);
let sh = cx.sheet_mut(sheet)?;
for (i, c) in (r.start.col..=r.end.col).enumerate() {
sh.set_value(CellRef::new(r.start.row, c), gridcraft_core::Value::text(format!("Column{}", i + 1)));
-372
View File
@@ -1,372 +0,0 @@
//! Macros: typed command lists that run as one undo step. See [`crate::macros`] for the model,
//! tokens, runner, debugger, recorder and policy. Automate scripts (`automate.*`) are a separate,
//! older feature with their own storage; these commands neither read nor change it. A recording
//! keeps the steps a script run ran while it recorded, as the run reported them.
use serde_json::{Value as Json, json};
use super::{CommandSpec, EngineError, Result, Session, bad, bool_param, cmd, edit, has_doc, str_param};
use crate::macros::recorder::RecordingStart;
pub fn specs() -> Vec<CommandSpec> {
vec![
cmd!(query "macro.list", "List Macros", [], None, "{} → [{name, description, shortcut?, steps, inputs: [names], onConflict, permissions}]", has_doc, list),
cmd!(query "macro.get", "Get Macro", [], None, "{name} → the macro: {v, name, description, shortcut?, inputs, steps, output, permissions}", has_doc, get),
cmd!(
"macro.save",
"Save Macro",
[],
None,
"{macro: {name, description?, shortcut?, inputs?: [{name, kind: cell|range, rows: [min, max], cols: [min, max], header, colTypes: [number|text|date|bool|any], nonEmpty, defaultRef?}], steps: [{type: command, id, params, enabled?, breakpoint?, note?} | {type: repeat, over, of: rows|columns, max, body} | {type: if, cell, op: eq|ne|gt|ge|lt|le|empty|notEmpty|contains, value, then, else} | {type: stop, message, rollback?}], output?: {anchor?, onConflict: ask|fail|overwrite|shiftDown|shiftRight}, permissions?: [command ids]} (or those fields at the top level), replace?: bool (default false: saving over a macro with the name is refused)} → {name, steps}",
has_doc,
save
),
cmd!("macro.delete", "Delete Macro", [], None, "{name}", has_doc, delete),
cmd!("macro.rename", "Rename Macro", [], None, "{name, to}", has_doc, rename),
cmd!(
"macro.run",
"Run Macro",
[],
None,
"{name, inputs?: {Input: \"B2:D9\"} | \"B2:D9\", onConflict?: ask|fail|overwrite|shiftDown|shiftRight, overwrite?: bool, approve?: bool, timeLimit?: seconds} runs as one undo step; any error rolls it all back → {macro, ran, inputs, stopped?, shifted?}",
has_doc,
run
),
cmd!(
query "macro.check",
"Check Macro",
[],
None,
"{name, inputs?, onConflict?, overwrite?, timeLimit?: seconds} validates the inputs and dry-runs the macro on the workbook (then puts everything back) → {ok (macro.run with these parameters would go ahead without a question), errors?, needsApproval?, onConflict, inputs, output?, conflicts, footprint}",
has_doc,
check
),
cmd!(
noundo "macro.record",
"Record Macro",
["View", "Macros"],
None,
"{stop?: bool, cancel?: bool (ends a recording without saving it), relative?: bool (default true; when starting), name?, shortcut?: \"Ctrl+Shift+M\" | \"M\", description?} toggles recording; when starting, the name (new and valid) and the shortcut (free) are checked and kept for the save, and without a name the macro gets the next MacroN; when stopping, a name or description given then wins (a taken name gets a suffix); with relative references the selection when it starts is the example input → {recording, name?, relative?, input?} or {recording: false, saved, steps, dropped, skipped, inputs, shortcutDropped? (the shortcut another macro took while recording)}",
has_doc,
record
),
cmd!(
"macro.debug",
"Step Into",
[],
None,
"{action: start|step|continue|runTo|stop|state, name, inputs?, onConflict?, overwrite?, approve?, timeLimit?: seconds per action (start, as macro.run), step: path (runTo), keep?: bool (stop), breakpoints?: [\"3\", \"2.1\"] (steps of the macro), breakpointsOnly?: bool (the list replaces the steps' own breakpoints)} debugs a macro one step at a time; stop keeps the changes as one undo step or rolls them back. Runs on its own, never as a step of a script or macro. Until it finishes or stops, its workbook runs only macro.debug, queries, selection and sheet changes, Go To, Find, Copy, zoom, Switch Window, Close and Stop Action (other workbooks stay editable) → {state: paused|finished|stopped|failed, current: {step, kind, command?, params?, iteration?}, lastParams, executed, changed (at most 500), changedCells, atBreakpoint?, shifted?, wroteFiles?, message?, error?}",
has_doc,
|s, p| crate::macros::debug::debug(s, p)
),
]
}
fn name_param<'a>(cmd: &str, p: &'a Json) -> Result<&'a str> {
str_param(p, "name").filter(|n| !n.trim().is_empty()).ok_or_else(|| bad(cmd, "missing `name`"))
}
/// The index of macro `name` in the active workbook, or an error that says there is none.
fn index_of(s: &Session, name: &str) -> Result<usize> {
crate::macros::find(&s.doc()?.wb, name).ok_or_else(|| EngineError::Other(format!("There is no macro called '{name}'.")))
}
fn list(s: &mut Session, _: &Json) -> Result<Json> {
let wb = &s.doc()?.wb;
Ok(Json::Array(
wb.macros
.iter()
.map(|m| {
let mut o = json!({
"name": m.name,
"description": m.description,
"steps": m.step_count(),
"inputs": m.inputs.iter().map(|i| i.name.clone()).collect::<Vec<_>>(),
"onConflict": m.output.on_conflict.name(),
"permissions": m.permissions,
});
if let Some(k) = &m.shortcut {
o["shortcut"] = json!(k);
}
o
})
.collect(),
))
}
fn get(s: &mut Session, p: &Json) -> Result<Json> {
let i = index_of(s, name_param("macro.get", p)?)?;
let wb = &s.doc()?.wb;
Ok(wb.macros.get(i).map(crate::macros::macro_json).unwrap_or(Json::Null))
}
fn save(s: &mut Session, p: &Json) -> Result<Json> {
let v = p.get("macro").filter(|m| m.is_object()).unwrap_or(p);
let m = crate::macros::parse_macro(v).map_err(|e| bad("macro.save", e))?;
// Replacing is asked for, never assumed: a save with a name in use is refused.
let replace = match p.get("replace") {
None | Some(Json::Null) => false,
Some(Json::Bool(b)) => *b,
Some(_) => return Err(bad("macro.save", "`replace` must be true or false")),
};
let (name, steps) = (m.name.clone(), m.step_count());
edit(s, |cx| {
if let Some(kept) = crate::macros::kept_name(&cx.wb, &m.name) {
return Err(EngineError::Other(taken_by_kept(&kept)));
}
match crate::macros::find(&cx.wb, &m.name) {
Some(i) if !replace => {
let existing = cx.wb.macros.get(i).map_or(m.name.as_str(), |o| o.name.as_str());
return Err(EngineError::Other(format!("A macro called '{existing}' already exists. Pass `replace: true` to replace it.")));
}
Some(i) => {
if let Some(old) = cx.wb.macros.get_mut(i) {
*old = m;
}
}
None => {
crate::macros::check_room(&cx.wb).map_err(EngineError::Other)?;
cx.wb.macros.push(m);
}
}
Ok(())
})?;
Ok(json!({"name": name, "steps": steps}))
}
/// The refusal of a name that a macro kept in a newer format has.
fn taken_by_kept(name: &str) -> String {
format!(
"A macro called '{name}' made by a newer version of GridCraft is kept in this workbook; this version can't change it. Choose another name."
)
}
fn delete(s: &mut Session, p: &Json) -> Result<Json> {
let i = index_of(s, name_param("macro.delete", p)?)?;
edit(s, |cx| {
if i < cx.wb.macros.len() {
cx.wb.macros.remove(i);
}
Ok(Json::Null)
})
}
fn rename(s: &mut Session, p: &Json) -> Result<Json> {
let i = index_of(s, name_param("macro.rename", p)?)?;
let to = str_param(p, "to").map(str::trim).filter(|t| !t.is_empty()).ok_or_else(|| bad("macro.rename", "missing `to`"))?.to_string();
if to.chars().any(char::is_control) {
return Err(bad("macro.rename", "a macro name can't hold line breaks or other control characters"));
}
let wb = &s.doc()?.wb;
if crate::macros::find(wb, &to).is_some_and(|j| j != i) {
return Err(EngineError::Other(format!("A macro called '{to}' already exists.")));
}
if let Some(kept) = crate::macros::kept_name(wb, &to) {
return Err(EngineError::Other(taken_by_kept(&kept)));
}
edit(s, |cx| {
let Some(m) = cx.wb.macros.get_mut(i) else { return Ok(()) };
let mut renamed = m.clone();
renamed.name = to.clone();
// The whole check, size included: a longer name can take a macro past what a file holds.
crate::macros::check(&renamed).map_err(EngineError::Other)?;
*m = renamed;
Ok(())
})?;
Ok(json!({"name": to}))
}
fn run(s: &mut Session, p: &Json) -> Result<Json> {
let name = name_param("macro.run", p)?;
// While recording: the steps a recording keeps for this run, taken from the macro as it runs,
// so saving or deleting the macro before the recording stops doesn't change what it saved.
let inline = if s.is_recording() {
let wb = &s.doc()?.wb;
crate::macros::find(wb, name).and_then(|i| wb.macros.get(i)).and_then(crate::macros::recorder::inline_steps)
} else {
None
};
let out = crate::macros::run::run(s, name, p)?;
if s.is_recording() {
// `None` when the macro's steps don't stand on their own: the recording leaves the run out.
s.script_ran = inline;
}
Ok(out)
}
fn check(s: &mut Session, p: &Json) -> Result<Json> {
let name = name_param("macro.check", p)?;
crate::macros::run::check(s, name, p)
}
fn record(s: &mut Session, p: &Json) -> Result<Json> {
check_record_params(s.is_recording(), p)?;
// `cancel` ends a recording without saving it: the way out of a recording whose macro
// cannot be saved (a name the check refuses, a full workbook).
if bool_param(p, "cancel").unwrap_or(false) {
let was = s.stop_recording().is_some();
s.macro_recording = None;
return Ok(json!({"recording": false, "cancelled": was}));
}
if bool_param(p, "stop").unwrap_or(false) || s.is_recording() {
return stop_recording(s, p);
}
start_recording(s, p, bool_param(p, "relative").unwrap_or(true))
}
/// Refuses parameters `macro.record` would ignore (`null` counts as not given): `relative` and
/// `shortcut` when stopping (they are chosen when recording starts), `stop: false` while
/// recording (the call stops it), anything beside `cancel: true`, and parameters of the wrong
/// kind. `name` and `description` count when starting and when stopping (stopping wins).
fn check_record_params(recording: bool, p: &Json) -> Result<()> {
let given = |k: &str| p.get(k).filter(|v| !v.is_null());
for k in ["stop", "cancel", "relative"] {
if given(k).is_some_and(|v| !v.is_boolean()) {
return Err(bad("macro.record", format!("`{k}` must be true or false")));
}
}
for k in ["name", "description", "shortcut"] {
if given(k).is_some_and(|v| !v.is_string()) {
return Err(bad("macro.record", format!("`{k}` must be text")));
}
}
if given("cancel").and_then(Json::as_bool) == Some(true) {
if let Some(k) = ["stop", "relative", "name", "description", "shortcut"].into_iter().find(|k| given(k).is_some()) {
return Err(bad("macro.record", format!("`cancel: true` ends the recording without saving it, so `{k}` would be ignored")));
}
return Ok(());
}
let stop = given("stop").and_then(Json::as_bool);
if recording && stop == Some(false) {
return Err(bad(
"macro.record",
"a recording is in progress, and this call stops it: pass `stop: true`, or `cancel: true` to end it unsaved",
));
}
if (recording || stop == Some(true))
&& let Some(k) = ["relative", "shortcut"].into_iter().find(|k| given(k).is_some())
{
return Err(bad("macro.record", format!("`{k}` is chosen when recording starts")));
}
Ok(())
}
/// Starts recording. `name`, `shortcut` and `description` (from the Record Macro dialog) are
/// checked now, so a bad name or a taken shortcut is refused before anything is recorded.
fn start_recording(s: &mut Session, p: &Json, relative: bool) -> Result<Json> {
let d = s.doc()?;
let name = match str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()) {
Some(n) => Some(crate::macros::check_new_name(&d.wb, n).map_err(|e| bad("macro.record", e))?),
None => None,
};
let shortcut = match str_param(p, "shortcut").map(str::trim).filter(|k| !k.is_empty()) {
Some(k) => {
let letter = crate::macros::parse_shortcut(k)
.ok_or_else(|| bad("macro.record", "`shortcut` must be one letter from A to Z (used as Ctrl+Shift+letter)"))?;
if let Some(why) = crate::macros::shortcut_conflict(&d.wb, name.as_deref().unwrap_or(""), letter) {
return Err(bad("macro.record", why));
}
Some(crate::macros::shortcut_text(letter))
}
None => None,
};
let description = match str_param(p, "description") {
Some(t) if t.chars().count() > gridcraft_model::MAX_MACRO_TEXT => {
return Err(bad("macro.record", format!("`description` can be at most {} characters long", gridcraft_model::MAX_MACRO_TEXT)));
}
Some(t) => Some(t.to_string()),
None => None,
};
let mut start = RecordingStart::new(d, relative);
(start.name, start.shortcut, start.description) = (name.clone(), shortcut, description);
let input = start.selection.current().a1();
s.start_recording();
s.macro_recording = Some(start);
let mut out = if relative { json!({"recording": true, "relative": true, "input": input}) } else { json!({"recording": true, "relative": false}) };
if let Some(n) = name {
out["name"] = json!(n);
}
Ok(out)
}
/// Stops recording and saves what was recorded as a macro, in the workbook where recording
/// started. When saving fails (a bad name, a full workbook, a macro being debugged there) the
/// recording goes on, so stopping again keeps what was recorded; when that workbook was closed,
/// the recording ends unsaved.
fn stop_recording(s: &mut Session, p: &Json) -> Result<Json> {
let Some(rec) = s.stop_recording() else { return Ok(json!({"recording": false})) };
let start = s.macro_recording.take();
let at = match &start {
Some(st) => s
.docs
.iter()
.position(|d| d.uid == st.doc)
.ok_or_else(|| EngineError::Other("The workbook where recording started was closed, so the recording was not saved.".into()))?,
None => s.active_index(),
};
let saved = save_recording(s, p, &rec, start.clone(), at);
if saved.is_err() {
s.recorder = Some(rec);
s.macro_recording = start;
}
saved
}
/// Saves the recording into the workbook at index `at`.
fn save_recording(s: &mut Session, p: &Json, rec: &crate::record::Recorder, start: Option<RecordingStart>, at: usize) -> Result<Json> {
let d = s.docs.get(at).ok_or(EngineError::NoDocument)?;
// A macro being debugged there could still roll the saved macro back with its run.
if s.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid)) {
return Err(EngineError::Disabled("macro.record".into(), crate::macros::debug::BUSY.into()));
}
// A recording started without `macro.record` (`Session::start_recording`) takes its example
// input from the workbook as it is now.
let start = start.unwrap_or_else(|| RecordingStart::new(d, true));
let (doc, wb) = (d.uid, d.wb.clone());
// A name given when stopping wins over the one given when starting; either one, if another
// macro has it, gets a suffix, so the save never replaces a macro.
let name = match str_param(p, "name").map(str::trim).filter(|n| !n.is_empty()).or(start.name.as_deref()) {
Some(n) => crate::macros::unique_name(&wb, n),
None => crate::macros::next_name(&wb),
};
let mut converted = crate::macros::recorder::convert(&name, &start, &rec.steps, &rec.ran);
// Steps past the macro limit or its size, and ones recorded in another workbook, are left
// out and counted with the ones the recorder dropped.
let mut cut = converted.cut.saturating_add(converted.mac.steps.len().saturating_sub(gridcraft_model::MAX_MACRO_STEPS));
converted.mac.steps.truncate(gridcraft_model::MAX_MACRO_STEPS);
if let Some(desc) = str_param(p, "description").or(start.description.as_deref()) {
converted.mac.description = desc.chars().take(gridcraft_model::MAX_MACRO_TEXT).collect();
}
cut = cut.saturating_add(crate::macros::fit_size(&mut converted.mac));
// The shortcut, unless another macro took it while recording; then it is left off and the
// result says so.
let mut shortcut_dropped = None;
if let Some(k) = &start.shortcut {
if crate::macros::parse_shortcut(k).is_some_and(|c| crate::macros::shortcut_conflict(&wb, &name, c).is_none()) {
converted.mac.shortcut = Some(k.clone());
} else {
shortcut_dropped = Some(k.clone());
}
}
let steps = converted.mac.steps.len();
let inputs: Vec<String> = converted.mac.inputs.iter().map(|i| i.name.clone()).collect();
// Saved into that workbook even when another one is active now.
let active = s.active_index();
s.set_active(at);
let saved = s.execute("macro.save", json!({"macro": crate::macros::macro_json(&converted.mac), "replace": false}));
s.set_active(active);
saved?;
// The person recorded it, so it runs without the approval a macro that came with a file
// needs, until it changes.
if let Some(d) = s.docs.iter().find(|d| d.uid == doc)
&& let Some(m) = crate::macros::find(&d.wb, &name).and_then(|i| d.wb.macros.get(i))
{
let key = (doc, crate::macros::fingerprint(m));
s.macro_approvals.insert(key);
}
let mut out = json!({"recording": false, "saved": name, "steps": steps, "dropped": rec.dropped.saturating_add(cut), "skipped": converted.skipped, "inputs": inputs});
if let Some(k) = shortcut_dropped {
out["shortcutDropped"] = json!(k);
}
Ok(out)
}
+2 -36
View File
@@ -2,8 +2,8 @@ use super::layout::{DENSE_BLOCK, SheetLayout};
use super::objects::draw_cell_picture;
use super::text::{TextPlacement, draw_text};
use super::{
BorderLine, BorderStyle, Borders, CellRef, Color, Font, Page, PatternType, Point, RangeRef, Rgb, Sheet, Style, StyleId, Underline, Value,
Workbook, split_sheet,
BorderLine, BorderStyle, Borders, CellRef, Color, Font, Page, PatternType, Point, RangeRef, Rgb, Sheet, Style, StyleId, Underline, Workbook,
split_sheet,
};
// ---------------------------------------------------------------- drawing
@@ -280,7 +280,6 @@ pub(super) fn draw_block(pg: &mut Page, b: &Block, gridlines: bool, cf: &mut Opt
|| merges.iter().any(|m| m.start == *c)
|| !sh.value(*c).is_empty()
|| l.style.borders != Borders::default()
|| l.style.num_fmt.as_str() == "checkbox"
})
.collect();
let rect_of = |c: CellRef| -> (f32, f32, f32, f32, bool) {
@@ -344,8 +343,6 @@ pub(super) fn draw_block(pg: &mut Page, b: &Block, gridlines: bool, cf: &mut Opt
}
if let Some(picture) = sh.cell_pictures.get(c) {
draw_cell_picture(pg, &picture.data, (x, y, w, h), b.k);
} else if let Some(on) = checkbox_state(l, &sh.value(*c)) {
draw_checkbox(pg, (x, y, w, h), b.k, on);
} else {
draw_text(pg, b, TextPlacement { cell: *c, look: l, rect: (x, y, w, h), merged, merges: &merges, limits: (bx, bx + bw) });
}
@@ -353,37 +350,6 @@ pub(super) fn draw_block(pg: &mut Page, b: &Block, gridlines: bool, cf: &mut Opt
pg.restore();
}
/// A checkbox cell prints TRUE/FALSE (and empty) as a box, as on screen (#2972); other values
/// print as text.
fn checkbox_state(l: &Look, v: &Value) -> Option<bool> {
if l.style.num_fmt.as_str() != "checkbox" || l.cf.as_ref().is_some_and(|cf| cf.hide_value) {
return None;
}
match v {
Value::Bool(on) => Some(*on),
Value::Empty => Some(false),
_ => None,
}
}
/// A checkbox centred in the cell: white with a grey outline, or green with a white tick.
fn draw_checkbox(pg: &mut Page, (x, y, w, h): (f32, f32, f32, f32), k: f32, on: bool) {
let s = (13.2 * k).min(w - 2.0 * k).min(h - 2.0 * k);
if s <= 0.0 {
return;
}
let (bx, by) = (x + (w - s) / 2.0, y + (h - s) / 2.0);
let corners = [[bx, by], [bx + s, by], [bx + s, by + s], [bx, by + s]];
if on {
let green = [0x10, 0x7C, 0x41];
pg.polygon(&corners, Some(green), Some((green, 0.75 * k)));
let tick = [[bx + 0.22 * s, by + 0.52 * s], [bx + 0.42 * s, by + 0.72 * s], [bx + 0.78 * s, by + 0.30 * s]];
pg.polyline(&tick, 0.14 * s, [0xFF, 0xFF, 0xFF], &[]);
} else {
pg.polygon(&corners, Some([0xFF, 0xFF, 0xFF]), Some(([110, 110, 110], 0.75 * k)));
}
}
pub(super) struct PendingPdfLink {
pub(super) page: usize,
pub(super) sheet: usize,
-16
View File
@@ -565,19 +565,3 @@ fn rotated_wrapped_text_prints_rotated() {
assert_eq!(up + down, blocks.len(), "a header line printed flat: {blocks:?}");
assert!((1..=3).contains(&up) && (1..=3).contains(&down), "up {up}, down {down}: {blocks:?}");
}
#[test]
fn checkbox_cells_print_as_boxes_not_true_false() {
// #2972: a checkbox cell printed its value as TRUE or FALSE.
let mut s = s();
s.execute("insert.checkbox", json!({"range": "A1:A2"})).unwrap();
s.execute("cell.toggleCheckbox", json!({"cell": "A2"})).unwrap();
s.execute("cell.set", json!({"cell": "C1", "input": "TRUE"})).unwrap();
let (pdf, _) = pdf_of(&mut s, json!({}));
assert!(!contains(&pdf, "(FALSE) Tj"), "the unchecked box printed as FALSE");
// Only the plain TRUE in C1 prints as text; the checked box is a filled box.
let text = String::from_utf8_lossy(&pdf);
assert_eq!(text.matches("(TRUE) Tj").count(), 1, "the checked box printed as TRUE");
assert!(contains(&pdf, "0.063 0.486 0.255 rg"), "the checked box is not filled");
assert!(contains(&pdf, "0.431 0.431 0.431 RG"), "the unchecked box is not outlined");
}
+10 -12
View File
@@ -232,9 +232,7 @@ fn shift_sheet_features(sh: &mut Sheet, axis: Axis, at: u32, count: u32, insert:
}
/// Rewrites every formula reference (and chart series) for a structural edit on `target`.
pub(super) fn rewrite(cx: &mut Ctx, target: &str, e: &Edit) {
cx.edits.push((target.to_string(), e.clone()));
let wb = &mut cx.wb;
pub(super) fn rewrite(wb: &mut gridcraft_model::Workbook, target: &str, e: &Edit) {
super::edit::rewrite_all_formulas(wb, target, e);
for si in 0..wb.sheets.len() {
let host = wb.sheets.get(si).map(|s| s.name.clone()).unwrap_or_default();
@@ -298,7 +296,7 @@ fn check_push_off_sheet(s: &Session, sheet: usize, axis: Axis, at: u32, count: u
if let Some(u) = s.doc()?.wb.sheet(sheet).and_then(|sh| sh.used_range()) {
let last = if axis == Axis::Rows { u.end.row } else { u.end.col };
if last >= at && last as u64 + count as u64 >= limit as u64 {
return Err(EngineError::Other("To prevent possible loss of data, GridCraft cannot shift nonblank cells off of the worksheet.".into()));
return Err(EngineError::Other("To prevent possible loss of data, Excel cannot shift nonblank cells off of the worksheet.".into()));
}
}
Ok(())
@@ -327,7 +325,7 @@ fn insert_lines(s: &mut Session, p: &Json, axis: Axis) -> Result<Json> {
// New lines take the format of the line before (Excel's "Format Same As Above").
inherit_line_format(sh, axis, at, count);
}
rewrite(cx, &name, &Edit::Insert { axis, at, count });
rewrite(&mut cx.wb, &name, &Edit::Insert { axis, at, count });
cx.structural = true;
Ok(Json::Null)
})?;
@@ -392,7 +390,7 @@ fn delete_lines(s: &mut Session, p: &Json, axis: Axis) -> Result<Json> {
}
shift_sheet_features(sh, axis, at, count, false);
}
rewrite(cx, &name, &Edit::Delete { axis, at, count });
rewrite(&mut cx.wb, &name, &Edit::Delete { axis, at, count });
}
cx.structural = true;
Ok(Json::Null)
@@ -444,7 +442,7 @@ fn move_columns(s: &mut Session, p: &Json) -> Result<Json> {
sh.cells.shift_cols(before, count as i64);
shift_sheet_features(sh, Axis::Cols, before, count, true);
}
rewrite(cx, &name, &Edit::Insert { axis: Axis::Cols, at: before, count });
rewrite(&mut cx.wb, &name, &Edit::Insert { axis: Axis::Cols, at: before, count });
let mut touched = Vec::new();
{
@@ -482,14 +480,14 @@ fn move_columns(s: &mut Session, p: &Json) -> Result<Json> {
for cell in touched {
cx.touch(sheet, cell);
}
rewrite(cx, &name, &Edit::Move { from: source, to_row: 0, to_col: destination });
rewrite(&mut cx.wb, &name, &Edit::Move { from: source, to_row: 0, to_col: destination });
{
let sh = cx.sheet_mut(sheet)?;
sh.cells.shift_cols(delete_at + count, -(count as i64));
shift_sheet_features(sh, Axis::Cols, delete_at, count, false);
}
rewrite(cx, &name, &Edit::Delete { axis: Axis::Cols, at: delete_at, count });
rewrite(&mut cx.wb, &name, &Edit::Delete { axis: Axis::Cols, at: delete_at, count });
*cx.sel = crate::Selection {
active: CellRef::new(active_row, final_start),
anchor: CellRef::new(active_row, final_start),
@@ -521,7 +519,7 @@ fn insert_cells(s: &mut Session, p: &Json) -> Result<Json> {
RangeRef::new(CellRef::new(r.start.row.max(MAX_ROWS.saturating_sub(r.height())), r.start.col), CellRef::new(MAX_ROWS - 1, r.end.col))
};
if sh.cells.iter_range(doomed).any(|(_, x)| x.formula.is_some() || !x.value.is_empty()) {
return Err(EngineError::Other("To prevent possible loss of data, GridCraft cannot shift nonblank cells off of the worksheet.".into()));
return Err(EngineError::Other("To prevent possible loss of data, Excel cannot shift nonblank cells off of the worksheet.".into()));
}
}
let right = shift == "right";
@@ -534,7 +532,7 @@ fn insert_cells(s: &mut Session, p: &Json) -> Result<Json> {
}
shift_cell_features(sh, &e);
let name = cx.wb.sheet(sheet).map(|s| s.name.clone()).unwrap_or_default();
rewrite(cx, &name, &e);
rewrite(&mut cx.wb, &name, &e);
cx.structural = true;
Ok(Json::Null)
})?;
@@ -569,7 +567,7 @@ fn delete_cells(s: &mut Session, p: &Json) -> Result<Json> {
shift_cell_features(sh, &e);
}
// References into the deleted cells become #REF!; the rest move.
rewrite(cx, &name, &e);
rewrite(&mut cx.wb, &name, &e);
cx.structural = true;
Ok(Json::Null)
})?;
+1 -1
View File
@@ -199,7 +199,7 @@ fn phase_one(tableau: &mut Tableau, normalized: &[(Vec<f64>, Sense, f64)], budge
return Err(Outcome::Infeasible);
}
for i in 0..tableau.rows.len() {
if tableau.basis.get(i).is_none_or(|basic| *basic < tableau.decision_cols + tableau.slack_cols) {
if !tableau.basis.get(i).is_some_and(|basic| *basic >= tableau.decision_cols + tableau.slack_cols) {
continue;
}
let pivot_col = tableau
+37 -105
View File
@@ -15,7 +15,7 @@
//! noncharacter, which XML carries but real text does not use, so the escape costs nothing on
//! ordinary text. Legacy `_sc_` items are read as they are.
use std::collections::{BTreeMap, HashSet};
use std::collections::BTreeMap;
use std::fmt::Write as _;
use gridcraft_model::{DefinedName, Workbook};
@@ -102,29 +102,15 @@ pub fn get(wb: &Workbook, key: &str) -> Option<String> {
assemble(&parts)
}
/// What a listing found: the first [`MAX_ITEMS`] items or keys, and how many more there were.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Listing<T> {
pub found: T,
/// Items past the cap, not listed. A caller says so rather than letting them vanish.
pub more: usize,
}
/// The items whose key starts with `prefix` (`"macro_"`), by lower-cased key, read in one pass
/// over the names: the first [`MAX_ITEMS`] items that can be read, and the count of the readable
/// ones past them. An item over the size limit is neither listed nor counted, so it takes no
/// place another item could have. ([`keys`] lists keys without reading the items.)
pub fn items(wb: &Workbook, prefix: &str) -> Listing<BTreeMap<String, String>> {
let mut listing: Listing<BTreeMap<String, String>> = Listing::default();
for (k, parts) in group(wb, prefix) {
let Some(text) = parts.is_item().then(|| assemble(&parts)).flatten() else { continue };
if listing.found.len() < MAX_ITEMS {
listing.found.insert(k, text);
} else {
listing.more = listing.more.saturating_add(1);
}
}
listing
/// over the names: the first [`MAX_ITEMS`] of [`keys`], without those over the size limit.
pub fn items(wb: &Workbook, prefix: &str) -> BTreeMap<String, String> {
group(wb, prefix)
.into_iter()
.filter(|(_, parts)| parts.is_item())
.take(MAX_ITEMS)
.filter_map(|(k, parts)| assemble(&parts).map(|text| (k, text)))
.collect()
}
/// The text of the item held by `parts`, as [`get`] reads it.
@@ -183,44 +169,28 @@ fn unescape(s: &str) -> String {
/// Stores `text` under `key`, replacing what was there (in either format). Empty text removes it.
pub fn set(wb: &mut Workbook, key: &str, text: &str) -> Result<()> {
set_many(wb, &[(key.to_string(), text.to_string())])
}
/// Stores every `(key, text)` of `items` as [`set`] would, with the checks done before anything
/// changes and one pass over the names for all of them, so writing a thousand items into a
/// workbook with many thousands of names stays near-linear.
pub fn set_many(wb: &mut Workbook, items: &[(String, String)]) -> Result<()> {
let mut planned = Vec::new();
let mut keys: HashSet<String> = HashSet::new();
for (key, text) in items {
check_key(key)?;
if text.chars().count() > MAX_ITEM_CHARS {
return Err(EngineError::Other(format!(
"The data is too large to keep in the workbook ({} MB at most).",
MAX_ITEM_CHARS / (1024 * 1024)
)));
}
// Two items under one key (names compare without case) would leave two hidden names of
// one name, of which an xlsx file keeps one: a caller's mistake, refused.
if !keys.insert(key.to_ascii_lowercase()) {
return Err(EngineError::Other(format!("`{key}` is given twice.")));
}
for (i, formula) in chunk_formulas(text).into_iter().enumerate() {
planned.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true });
}
check_key(key)?;
if text.chars().count() > MAX_ITEM_CHARS {
return Err(EngineError::Other(format!("The data is too large to keep in the workbook ({} MB at most).", MAX_ITEM_CHARS / (1024 * 1024))));
}
// A visible name of an item's shape is someone's own (see `item_of`); a hidden name beside
// it with the same name would be the one an xlsx file drops. Refuse before changing anything.
let names: HashSet<String> = planned.iter().map(|n| n.name.to_ascii_lowercase()).collect();
if let Some(n) = wb.names.iter().find(|n| n.scope.is_none() && !n.hidden && names.contains(&n.name.to_ascii_lowercase())) {
let formulas = chunk_formulas(text);
// A visible name of the item's shape is someone's own (see `item_of`); a hidden name beside
// it with the same name would be the one an xlsx file drops. Refuse before changing anything,
// in one pass over the names (a file can carry many thousands).
let chunks = formulas.len() as u64;
let taken = |n: &&DefinedName| {
n.scope.is_none() && !n.hidden && parse_name(&n.name).is_some_and(|(k, i)| k.eq_ignore_ascii_case(key) && i.is_some_and(|i| i < chunks))
};
if let Some(n) = wb.names.iter().find(taken) {
return Err(EngineError::Other(format!(
"The workbook has a defined name '{}', which GridCraft needs for its own data. Rename it first.",
n.name
)));
}
// What was there under these keys goes, in either format, then the new chunks come.
wb.names.retain(|n| !item_of(n).is_some_and(|(k, _)| keys.contains(&k.to_ascii_lowercase())));
wb.names.extend(planned);
remove(wb, key);
for (i, formula) in formulas.into_iter().enumerate() {
wb.names.push(DefinedName { name: format!("{PREFIX}{key}_{i}"), scope: None, formula, comment: "GridCraft data".into(), hidden: true });
}
Ok(())
}
@@ -241,11 +211,8 @@ pub fn set_json(wb: &mut Workbook, key: &str, v: &Json) -> Result<()> {
/// The keys of the items stored in the workbook that start with `prefix` (`"macro_"`), sorted
/// and lower-cased: the first [`MAX_ITEMS`] of them. To read them all, [`items`] takes one pass
/// where a [`get`] per key would take one each.
pub fn keys(wb: &Workbook, prefix: &str) -> Listing<Vec<String>> {
let mut found: Vec<String> = group(wb, prefix).into_iter().filter(|(_, parts)| parts.is_item()).map(|(k, _)| k).collect();
let more = found.len().saturating_sub(MAX_ITEMS);
found.truncate(MAX_ITEMS);
Listing { found, more }
pub fn keys(wb: &Workbook, prefix: &str) -> Vec<String> {
group(wb, prefix).into_iter().filter(|(_, parts)| parts.is_item()).map(|(k, _)| k).take(MAX_ITEMS).collect()
}
/// Whether the workbook holds an item under `name` (compared without case): a hidden workbook-scope
@@ -450,10 +417,10 @@ mod tests {
// Only a later chunk: no item, not listed, and the legacy name is still read.
wb.names.push(name("_gc_orphan_2", "\"c\""));
assert_eq!(get(&wb, "orphan"), None);
assert_eq!(keys(&wb, "orphan").found, Vec::<String>::new());
assert_eq!(keys(&wb, "orphan"), Vec::<String>::new());
wb.names.push(name("_sc_orphan", "\"old\""));
assert_eq!(get(&wb, "orphan").as_deref(), Some("old"));
assert_eq!(keys(&wb, "orphan").found, ["orphan"]);
assert_eq!(keys(&wb, "orphan"), ["orphan"]);
// Chunks out of order, a duplicate, junk around the literals, an unterminated literal.
wb.names.push(name("_gc_mix_1", "\"b\"&A1&"));
wb.names.push(name("_gc_mix_0", "SUM(\"a\")"));
@@ -501,7 +468,7 @@ mod tests {
let own = DefinedName { hidden: false, ..name("_gc_watch_0", "\"mine\"") };
wb.names.push(own.clone());
assert_eq!(get(&wb, "watch"), None);
assert!(keys(&wb, "").found.is_empty());
assert!(keys(&wb, "").is_empty());
// Writing the item would put a hidden name of the same name beside it, and an xlsx file
// keeps only one of them: refused, with the names as they were.
let err = set(&mut wb, "watch", "theirs").unwrap_err().to_string();
@@ -515,18 +482,6 @@ mod tests {
assert_eq!(wb.names, vec![own]);
}
#[test]
fn set_many_refuses_one_key_twice() {
// Two items under one key, in any case, would leave two hidden names of one name.
let mut wb = Workbook::new();
wb.names.push(name("_gc_other_0", "\"stays\""));
let err = set_many(&mut wb, &[("k".into(), "a".into()), ("K".into(), "b".into())]).unwrap_err().to_string();
assert!(err.contains("given twice"), "{err}");
assert_eq!(wb.names.len(), 1, "nothing changed");
set_many(&mut wb, &[("k".into(), "a".into()), ("k2".into(), "b".into())]).unwrap();
assert_eq!((get(&wb, "k").as_deref(), get(&wb, "K2").as_deref(), get(&wb, "other").as_deref()), (Some("a"), Some("b"), Some("stays")));
}
#[test]
fn a_chunk_number_with_leading_zeros_is_another_name() {
// `_gc_watch_00` is not chunk 0 under another spelling: a hidden one is not read, and a
@@ -534,7 +489,7 @@ mod tests {
let mut wb = Workbook::new();
wb.names.push(name("_gc_watch_00", "\"theirs\""));
assert_eq!(get(&wb, "watch"), None);
assert!(keys(&wb, "").found.is_empty());
assert!(keys(&wb, "").is_empty());
let own = DefinedName { hidden: false, ..name("_gc_watch_00", "\"mine\"") };
wb.names.push(own.clone());
set(&mut wb, "watch", "ours").unwrap();
@@ -550,7 +505,7 @@ mod tests {
set(&mut wb, "macro_2", "b").unwrap();
set(&mut wb, "macro_1", &"a".repeat(9_000)).unwrap();
set(&mut wb, "scripts", "{}").unwrap();
assert_eq!(keys(&wb, "macro_").found, ["macro_1", "macro_2"]);
assert_eq!(keys(&wb, "macro_"), ["macro_1", "macro_2"]);
assert_eq!(item_key("_gc_macro_1_3").as_deref(), Some("macro_1"));
assert_eq!(item_key("_SC_Scripts").as_deref(), Some("scripts"));
assert_eq!(item_key("Total"), None);
@@ -570,8 +525,7 @@ mod tests {
wb.names.push(name("_gc_macro_orphan_1", "\"no chunk 0\""));
wb.names.push(name("_gc_other_0", "\"not a macro\""));
NAME_VISITS.with(|v| v.set(0));
let listing = items(&wb, "MACRO_");
let all = &listing.found;
let all = items(&wb, "MACRO_");
assert_eq!(NAME_VISITS.with(|v| v.get()), wb.names.len(), "one pass over the names");
assert_eq!(all.len(), MAX_ITEMS);
let first: Vec<&String> = all.keys().take(2).collect();
@@ -581,41 +535,19 @@ mod tests {
NAME_VISITS.with(|v| v.set(0));
let listed = keys(&wb, "macro_");
assert_eq!(NAME_VISITS.with(|v| v.get()), wb.names.len());
assert_eq!(listed.found, all.keys().cloned().collect::<Vec<_>>(), "keys and items agree");
assert_eq!((listing.more, listed.more), (10_002, 10_002), "the rest are counted, not lost");
assert_eq!(listed, all.keys().cloned().collect::<Vec<_>>(), "keys and items agree");
// Under the cap every item is there, read as `get` reads it.
wb.names.retain(|n| !n.name.starts_with("_gc_macro_0") && !n.name.starts_with("_gc_macro_1"));
let few = items(&wb, "macro_").found;
let few = items(&wb, "macro_");
let want: BTreeMap<String, String> =
[("macro_big", "b".repeat(9_000)), ("macro_legacy", "old".into())].into_iter().map(|(k, t)| (k.to_string(), t)).collect();
assert_eq!(few, want);
for (k, text) in &few {
assert_eq!(get(&wb, k).as_ref(), Some(text));
}
assert_eq!(keys(&wb, "macro_").found, ["macro_big", "macro_legacy"]);
assert_eq!((items(&wb, "").found.len(), items(&wb, "").more), (3, 0), "the other item too, none past the cap");
}
#[test]
fn an_item_over_the_size_limit_takes_no_place_in_a_listing() {
// One item too large to read sorts first and one sorts last, around MAX_ITEMS readable
// ones: every readable item is listed, and neither oversized one is counted as more.
let mut wb = Workbook::new();
let oversized = |wb: &mut Workbook, key: &str| {
for (i, formula) in chunk_formulas(&"x".repeat(MAX_ITEM_CHARS + 1)).into_iter().enumerate() {
wb.names.push(name(&format!("_gc_{key}_{i}"), &formula));
}
};
oversized(&mut wb, "macro_0");
for i in 0..MAX_ITEMS {
wb.names.push(name(&format!("_gc_macro_{i:05}_0"), &format!("\"m{i}\"")));
}
oversized(&mut wb, "macro_z");
assert_eq!(get(&wb, "macro_0"), None, "too large to read");
let listing = items(&wb, "macro_");
assert_eq!((listing.found.len(), listing.more), (MAX_ITEMS, 0));
assert!(listing.found.contains_key("macro_09999"));
assert_eq!(keys(&wb, "macro_"), ["macro_big", "macro_legacy"]);
assert_eq!(items(&wb, "").len(), 3, "the other item too");
}
#[test]
+1 -16
View File
@@ -593,7 +593,7 @@ pub fn series(cx: &mut Ctx, sheet: usize, options: SeriesOptions<'_>) -> Result<
};
// The stop value bounds the series in the direction it runs (a growth by 0.5 falls).
if let Some(s) = stop
&& ((v >= prev && round15(v) > s) || (v < prev && round15(v) < s))
&& ((v >= prev && v > s) || (v < prev && v < s))
{
break;
}
@@ -806,21 +806,6 @@ mod tests {
assert_eq!(sh.value(CellRef::new(1, 1)), Value::from("03/12/2023"));
}
#[test]
fn series_keeps_value_equal_to_stop_after_float_drift() {
let mut s = crate::Session::new();
s.new_workbook();
s.execute("cell.set", serde_json::json!({"cell": "A1", "input": "1"})).unwrap();
s.execute("edit.fillSeries", serde_json::json!({"range": "A1:A6", "type": "linear", "step": 0.1, "stop": 1.3})).unwrap();
s.execute("cell.set", serde_json::json!({"cell": "B1", "input": "0.1"})).unwrap();
s.execute("edit.fillSeries", serde_json::json!({"range": "B1:B4", "type": "growth", "step": 3, "stop": 0.3})).unwrap();
let sh = s.doc().unwrap().wb.active().unwrap();
let col = |c: u32, n: u32| (0..n).map(|r| sh.value(CellRef::new(r, c))).collect::<Vec<_>>();
let n = Value::number;
assert_eq!(col(0, 6), vec![n(1.0), n(1.1), n(1.2), n(1.3), Value::Empty, Value::Empty]);
assert_eq!(col(1, 4), vec![n(0.1), n(0.3), Value::Empty, Value::Empty]);
}
#[test]
fn flash_programs() {
let p = learn(&[("John Smith".into(), "Smith, John".into())]).unwrap();
+9 -55
View File
@@ -118,14 +118,8 @@ fn decrypt_message(name: &str, e: gridcraft_xlsx::DecryptError) -> String {
}
}
/// Parses bytes that aren't encrypted, then takes the workbook's macros out of their hidden names.
/// Parses bytes that aren't encrypted.
fn open_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook, Vec<String>)> {
let (mut wb, mut warnings) = read_plain(name, bytes, csv_as_text)?;
warnings.extend(crate::macros::load(&mut wb));
Ok((wb, warnings))
}
fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook, Vec<String>)> {
let kind = FileKind::from_path(name);
let sniffed = gridcraft_xlsx::sniff(bytes);
if sniffed == gridcraft_xlsx::Format::Xls {
@@ -153,8 +147,7 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook,
}
// Text named `.xlsx` is read as text.
if sniffed == gridcraft_xlsx::Format::Xlsx || (kind == Some(FileKind::Xlsx) && sniffed != gridcraft_xlsx::Format::Csv) {
// Google Sheets' export wrapper is read as the formula inside when GridCraft calculates it (#2871).
let (wb, report) = gridcraft_xlsx::read_xlsx_with(bytes, &gridcraft_calc::is_known_function).map_err(|e| {
let (wb, report) = gridcraft_xlsx::read_xlsx(bytes).map_err(|e| {
EngineError::Other(match (&e, gridcraft_xlsx::oversized_part(bytes)) {
(gridcraft_xlsx::IoError::TooLarge(_), Some((part, size))) => part_too_large_message(name, &part, size),
_ => format!("We can't open '{name}': {e}"),
@@ -167,10 +160,7 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook,
let mut wb: Workbook = serde_json::from_slice(bytes).map_err(|e| EngineError::Other(format!("not a GridCraft JSON workbook: {e}")))?;
wb.styles.rebuild_index();
if wb.sheets.is_empty() {
// The file's macros are read after this (`macros::load`), so they come along.
let macros = std::mem::take(&mut wb.macros_read);
wb = Workbook::new();
wb.macros_read = macros;
}
// The file's band placement and cell count are derived state: rebuild them from the
// cells themselves, dropping what is outside the sheet grid (#1008).
@@ -183,17 +173,17 @@ fn read_plain(name: &str, bytes: &[u8], csv_as_text: bool) -> Result<(Workbook,
}
Some(FileKind::Tsv) => {
let opts = gridcraft_xlsx::CsvOptions { delimiter: b'\t', parse_values: !csv_as_text, ..Default::default() };
let (mut wb, report) = gridcraft_xlsx::read_csv_with_report(bytes, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
let mut wb = gridcraft_xlsx::read_csv(bytes, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
// Tab is what makes it a .tsv/.txt; saved as .csv it gets the CSV default.
wb.csv_delimiter = None;
rename_first_sheet(&mut wb, name);
Ok((wb, report.warnings))
Ok((wb, vec![]))
}
_ => {
let opts = gridcraft_xlsx::CsvOptions { delimiter: 0, parse_values: !csv_as_text, ..Default::default() };
let (mut wb, report) = gridcraft_xlsx::read_csv_with_report(bytes, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
let mut wb = gridcraft_xlsx::read_csv(bytes, &opts).map_err(|e| EngineError::Other(e.to_string()))?;
rename_first_sheet(&mut wb, name);
Ok((wb, report.warnings))
Ok((wb, vec![]))
}
}
}
@@ -248,12 +238,9 @@ pub fn save_bytes_as(wb: &Workbook, path: &str, kind: FileKind) -> Result<Vec<u8
match kind {
// The extension picks the package kind; only the macro-enabled kinds (.xlsm, .xltm) carry
// the VBA project, so any other name saves without it, as Excel does.
// Macros travel in hidden names.
FileKind::Xlsx => {
let named = crate::macros::with_names(wb)?;
let book = std::path::Path::new(path).file_name().and_then(std::ffi::OsStr::to_str).unwrap_or_default();
gridcraft_xlsx::write_xlsx_named(named.as_ref().unwrap_or(wb), gridcraft_xlsx::XlsxKind::from_path(path), book)
.map_err(|e| EngineError::Other(e.to_string()))
gridcraft_xlsx::write_xlsx_named(wb, gridcraft_xlsx::XlsxKind::from_path(path), book).map_err(|e| EngineError::Other(e.to_string()))
}
FileKind::Xls => {
Err(EngineError::Other("Excel 97-2003 (.xls) is supported for import only. Save as .xlsx or another supported export format.".into()))
@@ -263,7 +250,7 @@ pub fn save_bytes_as(wb: &Workbook, path: &str, kind: FileKind) -> Result<Vec<u8
// A workbook read from CSV keeps the separator it was read with (a `;` file stays `;`).
FileKind::Csv => wb.sheet(sheet).map(|sh| csv(wb, sh, wb.csv_delimiter.unwrap_or(b','))).transpose().map(Option::unwrap_or_default),
FileKind::Tsv => wb.sheet(sheet).map(|sh| csv(wb, sh, b'\t')).transpose().map(Option::unwrap_or_default),
FileKind::Json => crate::macros::json_bytes(wb),
FileKind::Json => serde_json::to_vec_pretty(wb).map_err(|e| EngineError::Other(e.to_string())),
FileKind::Html => to_html(wb, sheet).map(String::into_bytes),
}
}
@@ -307,39 +294,6 @@ fn csv(wb: &Workbook, sh: &Sheet, delimiter: u8) -> Result<Vec<u8>> {
.map_err(|e| EngineError::Other(e.to_string()))
}
/// The active sheet as CSV of raw cell values, for automation: numbers at full precision, dates and
/// times as ISO 8601, booleans `TRUE`/`FALSE`, errors as their text, text as stored. Save As CSV
/// writes displayed text instead ([`save_bytes`]) and stays the default.
pub fn csv_values_bytes(wb: &Workbook) -> Result<Vec<u8>> {
let Some(sh) = wb.sheet(wb.active_sheet()) else { return Ok(Vec::new()) };
gridcraft_xlsx::write_csv(sh, wb.csv_delimiter.unwrap_or(b','), |c| match sh.value(c) {
gridcraft_core::Value::Number(n) if n.is_finite() => {
let shape = crate::display::number_format(wb.styles.get(sh.style_id(c)).num_fmt.as_str()).date_shape(n);
match shape.zip(gridcraft_core::date::datetime_from_serial(wb.date_system, n)) {
Some(((date, time), d)) => iso(&d, date, time),
None => n.to_string(),
}
}
gridcraft_core::Value::Text(t) => t.to_string(),
v => crate::display::value_text_as(wb, &v, "General"),
})
.map_err(|e| EngineError::Other(e.to_string()))
}
/// `date` and `time` say which parts the cell's format shows; a format with neither is not a date.
fn iso(d: &gridcraft_core::date::DateTime, date: bool, time: bool) -> String {
let day = format!("{:04}-{:02}-{:02}", d.year, d.month, d.day);
let mut clock = format!("{:02}:{:02}:{:02}", d.hour, d.minute, d.second);
if d.milli != 0 {
clock.push_str(&format!(".{:03}", d.milli));
}
match (date, time) {
(true, true) => format!("{day}T{clock}"),
(false, true) => clock,
_ => day,
}
}
pub(crate) fn esc(s: &str) -> String {
s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;").replace('"', "&quot;")
}
@@ -645,7 +599,7 @@ impl Drop for ScriptSandbox {
/// thread is being torn down).
fn script_sandbox_check(path: &str) -> Result<()> {
if SCRIPT_SANDBOX.try_with(|c| c.get() > 0).unwrap_or(true) {
return Err(EngineError::Other(format!("{path}: scripts, and macro steps without the permission, can't read or write files")));
return Err(EngineError::Other(format!("{path}: scripts can't read or write files")));
}
Ok(())
}
-44
View File
@@ -19,7 +19,6 @@ fn copied(wb: &Workbook, range: &str) -> String {
}
#[cfg(not(target_arch = "wasm32"))]
#[cfg(unix)]
#[test]
fn save_fails_upfront_when_the_temp_name_is_taken() {
// Another local user who can write the directory must not be able to point the
@@ -291,20 +290,6 @@ fn csv_named_history_does_not_get_the_reserved_sheet_name() {
assert_eq!(wb.sheets[0].name, "Sales");
}
#[test]
fn csv_and_text_open_read_day_first_dates_and_report_a_guessed_order() {
let may = gridcraft_core::date::serial_from_ymd(gridcraft_core::DateSystem::D1900, 2026, 5, 31).unwrap();
let april = gridcraft_core::date::serial_from_ymd(gridcraft_core::DateSystem::D1900, 2026, 4, 3).unwrap();
for file in ["dates.csv", "dates.txt"] {
let (wb, warnings) = open_bytes(file, b"When\n31/05/2026\n03/04/2026\n").unwrap();
assert_eq!(wb.sheet(0).unwrap().value(CellRef::new(1, 0)), gridcraft_core::Value::Number(may), "{file}");
assert_eq!(wb.sheet(0).unwrap().value(CellRef::new(2, 0)), gridcraft_core::Value::Number(april), "{file}");
assert!(warnings.is_empty(), "{file}: {warnings:?}");
}
let (_, warnings) = open_bytes("ambiguous.csv", b"When\n03/04/2026\n12/01/2026\n").unwrap();
assert!(warnings.iter().any(|w| w.contains("column A") && w.contains("ambiguous")), "{warnings:?}");
}
/// A save that can't be put in place (here a folder already has the workbook's name, as
/// with a file another app holds locked) fails without leaving `<name>.sctmp` behind.
#[cfg(not(target_arch = "wasm32"))]
@@ -575,32 +560,3 @@ fn content_wins_over_a_mismatched_extension() {
assert_eq!(opened.sheet(0).unwrap().value(CellRef::new(0, 1)), gridcraft_core::Value::text("b"), "{name}");
}
}
/// #2871: opening an XLSX reads Google Sheets' `IFERROR(__xludf.DUMMYFUNCTION("…"),cached)`
/// wrapper as the formula inside when GridCraft calculates every function it calls (including
/// the evaluator's own, such as ROW), and keeps the wrapper, and so the cached value, for a
/// Google-only function.
#[test]
fn opening_google_sheets_export_unwraps_only_formulas_gridcraft_calculates() {
use gridcraft_core::{CellRef, Value};
let mut wb = Workbook::new();
let sh = std::sync::Arc::make_mut(&mut wb.sheets[0]);
let wrapped = [
("A1", "IFERROR(__xludf.DUMMYFUNCTION(\"SUM(ROW(B1:B3))\"),6)", Value::Number(6.0)),
("A2", "IFERROR(__xludf.DUMMYFUNCTION(\"GOOGLETRANSLATE(\"\"hi\"\",\"\"en\"\",\"\"fr\"\")\"),\"salut\")", Value::text("salut")),
];
for (at, text, value) in &wrapped {
let cell = gridcraft_model::Cell {
value: value.clone(),
formula: Some(std::sync::Arc::new(gridcraft_model::Formula::new(text))),
..Default::default()
};
sh.cells.set(CellRef::parse(at).unwrap(), cell);
}
let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap();
let (opened, _) = open_bytes("google.xlsx", &bytes).unwrap();
let text = |at: &str| opened.sheets[0].cells.get(CellRef::parse(at).unwrap()).and_then(|c| c.formula.as_ref()).map(|f| f.text.clone());
assert_eq!(text("A1").as_deref(), Some("SUM(ROW(B1:B3))"));
assert_eq!(text("A2").as_deref(), Some("IFERROR(__XLUDF.DUMMYFUNCTION(\"GOOGLETRANSLATE(\"\"hi\"\",\"\"en\"\",\"\"fr\"\")\"),\"salut\")"));
assert_eq!(opened.sheets[0].cells.get(CellRef::parse("A2").unwrap()).map(|c| c.value.clone()), Some(Value::text("salut")));
}
+4 -140
View File
@@ -15,12 +15,10 @@ pub mod display;
pub mod fill;
pub mod io;
pub mod links;
pub mod macros;
pub mod outline;
pub mod paper;
pub mod pivot;
pub mod pivot_calc;
pub mod recent;
pub mod record;
pub mod sample;
pub mod selection;
@@ -56,12 +54,6 @@ pub enum EngineError {
NoDocument,
#[error("{0}")]
Other(String),
/// The command would replace something; run it again with `overwrite: true` to go ahead.
#[error("confirm: {0} (run again with `overwrite: true`)")]
Confirm(String),
/// Active content (a macro) asks to run; run it again with `approve: true` to allow it.
#[error("approve: {0} (run again with `approve: true`)")]
Approve(String),
#[error("internal error in `{0}` (the workbook was kept as it was): {1}")]
Internal(String, String),
}
@@ -105,9 +97,6 @@ struct OpenGroup {
selection: Selection,
/// The redo stack when the group began (a rollback puts it back).
redo: Vec<HistoryEntry>,
/// Where the recording was when the group began (`None` when nothing was recording): a
/// rollback takes back the steps recorded since, as it takes back their edits.
recorded: Option<(u64, u64)>,
}
/// Where the last [`Session::group_end`] or [`Session::group_rollback`] left its document: the
@@ -214,12 +203,6 @@ pub struct DocState {
/// workbook is opened from a file, carried by content pasted from such a workbook, and never
/// changed by a trust decision.
pub trust: Trust,
/// While a macro's footprint is measured: every cell a command wrote, by sheet name, even
/// where it wrote what was already there.
pub touched: Option<Vec<(String, CellRef)>>,
/// While a macro step runs: the structural edits its command made (the sheet's name, the
/// edit), so the macro's inputs, output and Repeat ranges follow their cells as formulas do.
pub edits: Option<Vec<(String, gridcraft_formula::adjust::Edit)>>,
/// The references `edit.goTo` with `record: true` went to in this document, newest first,
/// without names, each area written with its sheet (`Sheet1!B14:C22,Sheet1!F19:G30`).
pub go_to_recent: Vec<String>,
@@ -248,10 +231,6 @@ fn history_retained_bytes(older: &Workbook, newer: &Workbook) -> usize {
let next = newer.sheets.get(index).map_or(&empty, |next| &next.cells);
bytes = bytes.saturating_add(sheet.cells.retained_bytes_against(next));
}
// Snapshots share the macro list until a change to it; a snapshot with its own list keeps it.
if !older.macros.shares(&newer.macros) {
bytes = bytes.saturating_add(older.macros.estimated_bytes());
}
bytes
}
@@ -324,8 +303,6 @@ impl DocState {
content_kind: None,
scripts_trusted,
trust,
touched: None,
edits: None,
go_to_recent: vec![],
}
}
@@ -391,10 +368,6 @@ pub struct Prefs {
pub formula_bar: bool,
/// The statistics the status bar shows for a selection (right-click the status bar).
pub status_bar: StatusBarStats,
/// Formulas › Recently Used: the functions most recently entered in a formula, newest first
/// (at most [`recent::MAX`]; see [`recent`]). An application list, as in Excel.
#[serde(deserialize_with = "recent::lenient")]
pub recent_functions: Vec<String>,
}
/// The selection statistics shown on the status bar, set with `view.statusBar`. Excel turns on
@@ -429,7 +402,6 @@ impl Default for Prefs {
user_dictionary: vec![],
formula_bar: true,
status_bar: StatusBarStats::default(),
recent_functions: recent::defaults(),
}
}
}
@@ -474,9 +446,6 @@ pub struct Session {
/// Serialized size of each journal entry, measured once when it is pushed, and their sum.
journal_sizes: Vec<usize>,
journal_bytes: usize,
/// While a dry run runs (the footprint, `macro.check`), its commands are not journaled: the
/// dry run is put back, and journaling it would evict the person's own oldest entries.
pub(crate) journal_paused: u32,
pub ui_requests: Vec<UiRequest>,
/// Format painter source style (sticky when double-clicked).
pub format_painter: Option<(Arc<Workbook>, usize, RangeRef, bool)>,
@@ -491,17 +460,6 @@ pub struct Session {
pub recorder: Option<record::Recorder>,
/// How many `execute` calls are running (0 between top-level commands).
depth: u32,
/// The steps the last successful `automate.runScript` or `macro.run` ran while a recorder was
/// on, for the recorder to keep with that step (see [`record::Recorder::ran`]); taken by the
/// recording hook.
pub(crate) script_ran: Option<Vec<(String, Json)>>,
/// Macros the person allowed to run in this session: (document uid, the macro's content as
/// canonical JSON, from [`macros::fingerprint`]).
pub macro_approvals: std::collections::HashSet<(u64, String)>,
/// The macro being debugged, between debugger actions.
pub macro_debug: Option<macros::debug::Debugger>,
/// Where the macro recorder started (beside `recorder`, its buffer).
pub macro_recording: Option<macros::recorder::RecordingStart>,
/// Draw tab tool: "" (select), "pen" or "eraser"; pen colour and width.
pub draw_tool: String,
pub draw_color: String,
@@ -564,17 +522,6 @@ impl Session {
pub fn doc_mut(&mut self) -> Result<&mut DocState> {
self.active_mut().ok_or(EngineError::NoDocument)
}
/// The warning an edit of `at` on `sheet` gets from sheet protection, or `None` when the edit
/// is allowed. Front ends ask before opening an editor, so typing into a locked cell never
/// leaves a draft that can't be entered (and so blocks saving).
pub fn locked_cell_message(&self, sheet: usize, at: CellRef) -> Option<String> {
let d = self.active()?;
let sh = d.wb.sheet(sheet)?;
if !sh.is_protected() || !d.wb.styles.get(sh.style_id(at)).protection.locked {
return None;
}
cmd::locked_cell_error(sh, at, &d.unlocked_ranges)
}
/// Opens a workbook in a new window, which becomes the active one.
pub fn add_document(&mut self, d: DocState) -> usize {
let uid = d.uid;
@@ -588,10 +535,6 @@ impl Session {
if i < self.docs.len() {
let d = self.docs.remove(i);
self.groups.retain(|g| g.doc != d.uid);
// A macro being debugged in this workbook ends with it; its group just went.
if self.macro_debug.as_ref().is_some_and(|m| m.doc == d.uid) {
self.macro_debug = None;
}
self.windows.retain(|w| w.doc_uid != d.uid);
if self.active_window.is_some_and(|w| self.window(w).is_none()) {
self.activate_recent();
@@ -604,7 +547,6 @@ impl Session {
pub fn new_workbook(&mut self) -> usize {
let n = (1..).find(|n| !self.docs.iter().any(|d| d.title == format!("Book{n}") && d.path.is_none())).unwrap_or(1);
let mut wb = Workbook::new();
wb.props.author = self.prefs.user_name.clone();
for _ in 1..self.prefs.sheets_in_new_workbook.clamp(1, 255) {
let name = wb.next_sheet_name();
wb.sheets.push(Arc::new(gridcraft_model::Sheet::new(name)));
@@ -644,16 +586,6 @@ impl Session {
}
}
/// Cuts the journal back to its first `keep` entries, with its size bookkeeping.
pub(crate) fn truncate_journal(&mut self, keep: usize) {
if self.journal_sizes.len() == self.journal.len() {
self.journal_sizes.truncate(keep);
self.journal_bytes = self.journal_sizes.iter().sum();
}
self.journal.truncate(keep);
self.recording = self.recording.map(|r| r.min(self.journal.len()));
}
pub fn commands(&self) -> Vec<CommandInfo> {
command_specs().iter().map(|c| c.info(self)).collect()
}
@@ -666,25 +598,7 @@ impl Session {
if let Err(why) = (spec.enabled)(self) {
return Err(EngineError::Disabled(id.to_string(), why));
}
// A debugger whose undo group something else ended locks nothing any more (`macro.debug`
// itself says that the run had ended).
if id != "macro.debug" {
macros::debug::drop_if_ended(self);
}
let query = !spec.journal && !spec.undoable;
// While a macro is being debugged its undo group is open, and its workbook is locked:
// between steps only the debugger, queries and commands that look around run there
// (`macros::debug::LOOKS_AROUND`). Anything else waits, undoable or not (saving, view
// modes, stopping Record Actions, which saves a script), also when another command runs
// it. Only the run's own steps go ahead. Other workbooks stay editable.
let locked = self.active().is_some_and(|d| self.macro_debug.as_ref().is_some_and(|m| m.locks(d.uid) && !m.stepping()));
if locked && !macros::debug::runs_while_locked(id, query) {
return Err(EngineError::Disabled(id.to_string(), macros::debug::BUSY.into()));
}
let before = self.active().map(|d| (d.uid, d.wb.clone(), d.selection.clone()));
// A dry run inside this command that a panic cuts short leaves journaling paused and its
// touch log open; the panic branch puts both back.
let paused_before = self.journal_paused;
let params = if params.is_null() { Json::Object(Default::default()) } else { params };
let (first_group, group_epoch) = (self.next_group, self.group_epoch);
// Whether a group was open on the document before the command, and how long its history was.
@@ -694,6 +608,7 @@ impl Session {
// because a nested `edit.undo` removes entries. A query runs nothing that changes the
// history (`every_query_leaves_the_history_alone` runs each one), so it needs no
// copy; front ends run queries all the time.
let query = !spec.journal && !spec.undoable;
let history_before = before.as_ref().filter(|_| !query).and_then(|(uid, ..)| {
#[cfg(test)]
tests::HISTORY_COPIES.with(|n| n.set(n.get().saturating_add(1)));
@@ -754,16 +669,7 @@ impl Session {
}
d.calc.rebuild(&d.wb);
}
if self.journal_paused != paused_before {
self.journal_paused = paused_before;
for d in self.docs.iter_mut() {
d.touched = None;
}
}
if self.depth == 0 {
// Only the `macro.debug` call itself can lose the run's place: a command nested
// in a step that panics is an error of that step, which the run handles.
macros::debug::recover(self);
self.trim_history();
self.group_step = None;
}
@@ -781,34 +687,13 @@ impl Session {
// finds no recorder. A menu-style call that only opened a dialog (`file.open` with no
// file, `home.formatCells` with no style) is not recorded either: the dialog's own
// call is, so playing the recording back runs the command once, with its parameters.
// A call that opened a dialog and changed the workbook too (a script with an edit before
// a menu-style step) is recorded: the edit happened, and the dialog's confirming call
// is a step of its own.
let opened_dialog = self.ui_requests.get(requests_before..).is_some_and(|new| new.iter().any(|q| matches!(q, UiRequest::Dialog(..))));
let changed = before.as_ref().is_some_and(|(uid, wb, _)| self.docs.iter().any(|d| d.uid == *uid && !Arc::ptr_eq(&d.wb, wb)));
if let Some((doc, sheet, sheet_name, selection)) = place.filter(|_| (!opened_dialog || changed) && !record::NOT_RECORDED.contains(&id))
if let Some((doc, sheet, sheet_name, selection)) = place.filter(|_| !opened_dialog)
&& let Some(rec) = self.recorder.as_mut()
{
// A toggle (Bold) is recorded with the state it set, as Excel records
// `Bold = True`: playing it back sets that state rather than flipping it again.
let params = match &result {
Ok(r) if params.is_object() => record::with_toggle_state(cmd::journal_params(&params), r),
_ => cmd::journal_params(&params),
};
let kept = rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params });
// A script or macro run is kept with the steps it ran, so editing or deleting the
// script or macro before the recording stops does not change what was recorded.
if let Some(ran) = self.script_ran.take()
&& kept
&& matches!(id, "automate.runScript" | "macro.run")
{
rec.attach_ran(ran);
}
}
if self.journal_paused == 0 {
self.push_journal(id, cmd::journal_params(&params));
recent::note(&mut self.prefs.recent_functions, id, &params);
rec.push(record::RecordedStep { doc, sheet, sheet_name, selection, id: id.to_string(), params: cmd::journal_params(&params) });
}
self.push_journal(id, cmd::journal_params(&params));
}
// Inside an undo group the group owns the step. A command that ended a group open before
// it leaves the group its step and makes one of its own for what it changed after the end.
@@ -923,7 +808,6 @@ impl Session {
wb: d.wb.clone(),
selection: d.selection.clone(),
redo: d.redo.clone(),
recorded: self.recorder.as_ref().map(record::Recorder::mark),
};
let token = GroupToken { id: g.id, doc: g.doc };
self.next_group = self.next_group.saturating_add(1);
@@ -1020,11 +904,6 @@ impl Session {
d.undo.truncate(g.start);
d.redo = g.redo;
d.revision += 1;
// The steps recorded while the group was open go with their edits (all of them when the
// recording started inside the group).
if let Some(rec) = self.recorder.as_mut() {
rec.cut_back(g.recorded.unwrap_or((0, 0)), g.doc);
}
// The record of the last group step stays when the document is back exactly where that
// step left it (this group began there); otherwise the rollback is where the group ended.
let kept = self.group_step.as_ref().is_some_and(|st| st.doc == g.doc && st.len == g.start && Arc::ptr_eq(&st.after, &d.wb));
@@ -1056,21 +935,6 @@ impl Session {
self.depth
}
/// Runs a command as a command in progress would (one level deeper), for tests of what a
/// nested call may do.
#[cfg(test)]
pub(crate) fn execute_nested(&mut self, id: &str, params: Json) -> Result<Json> {
self.depth = self.depth.saturating_add(1);
let r = self.execute(id, params);
self.depth = self.depth.saturating_sub(1);
r
}
/// Whether the token's undo group is still open (nothing has ended or rolled it back).
pub fn group_is_open(&self, token: &GroupToken) -> bool {
self.groups.iter().any(|g| g.id == token.id)
}
/// Whether an undo group is open on the document with this uid.
pub fn in_group(&self, doc: u64) -> bool {
self.groups.iter().any(|g| g.doc == doc)
-694
View File
@@ -1,694 +0,0 @@
//! The macro debugger: a run that stops between steps.
//!
//! `macro.debug` starts a run that pauses before its first step, then steps, continues to the next
//! breakpoint, runs to a chosen step, or stops. The run lives on the session between calls (one
//! action per call) inside an undo group that spans them; Stop keeps the changes as one undo step
//! or rolls them back. While a macro is being debugged its workbook is locked: only the
//! debugger, queries and the commands in [`LOOKS_AROUND`] run there, and everything else, saving
//! included, is refused ("Finish or stop the macro first"). Other workbooks stay editable. The
//! debugger runs on its own, never as a step of a script or macro, and is never recorded.
use std::collections::BTreeSet;
use std::sync::Arc;
use gridcraft_model::Workbook;
use serde_json::{Value as Json, json};
use super::run::{Machine, StepError, rolled_back};
use crate::{EngineError, GroupToken, Result, Selection, Session};
/// The most breakpoints one call sets.
pub const MAX_BREAKPOINTS: usize = 10_000;
/// The most entries `changed` lists (ranges and sheet notes), as the footprint lists ranges.
const MAX_LISTED: usize = 500;
/// Commands that run on the debugged workbook between steps, besides `macro.debug` and queries.
/// They only look around (the selection, the active sheet, Go To, Find, zoom, copying), switch to
/// another window, close the workbook (which ends the run) or stop the run (Stop Action). The
/// debugger puts the run's active sheet and selection back before its next step runs.
pub const LOOKS_AROUND: &[&str] = &[
"selection.set",
"selection.move",
"selection.next",
"selection.row",
"selection.column",
"selection.currentRegion",
"edit.selectAll",
"sheet.activate",
"sheet.next",
"sheet.previous",
"edit.goTo",
"edit.goToSpecial",
"edit.find",
"edit.copy",
"view.zoom",
"view.zoom100",
"view.zoomToSelection",
"window.activate",
"file.close",
"automate.stopAction",
];
/// Whether command `id` (a query when `query`) runs on a workbook the debugger locks.
pub fn runs_while_locked(id: &str, query: bool) -> bool {
// `macro.check` is a query, but its dry run runs the macro's steps, which the lock refuses: it
// waits too, rather than report the lock as the failure of a step.
(query && id != "macro.check") || id == "macro.debug" || LOOKS_AROUND.contains(&id)
}
/// A macro being debugged.
pub struct Debugger {
pub name: String,
pub doc: u64,
/// Out of the session only while a step runs.
machine: Option<Machine>,
token: Option<GroupToken>,
/// Breakpoints set for this run (step paths), on top of the steps' own.
pub breakpoints: BTreeSet<String>,
/// Only `breakpoints` stop the run; the steps' own flags are ignored (an editor that shows
/// its own breakpoints sends them all, so clearing one takes effect at once).
pub only_listed: bool,
/// The workbook when the run started, for "changed so far".
start: Arc<Workbook>,
/// Every step path in the macro, for checking `runTo` targets and breakpoints.
paths: BTreeSet<String>,
/// The workbooks the run has touched (by uid). Steps run on the macro's own workbook and the
/// policy refuses window and file commands, so this is the macro's workbook unless a command
/// reaches another one some other way; edits of these wait until the run ends.
touched: BTreeSet<u64>,
/// Steps the footprint's dry runs executed before the run (they count towards the run's
/// budget, not towards `executed`).
base: u64,
/// The conflict rule that shifted the data at the start (`shiftDown`, `shiftRight`).
shifted: Option<&'static str>,
/// Where the run left its workbook after the last action.
view: Option<View>,
}
/// The active sheet and the selections of the debugged workbook where the run left them.
#[derive(Clone)]
struct View {
sheet: usize,
selection: Selection,
sheet_selections: Vec<Selection>,
}
impl Debugger {
/// Whether edits of the workbook with this uid wait for the run to end.
pub fn locks(&self, doc: u64) -> bool {
self.touched.contains(&doc)
}
/// Whether one of the run's steps is running (its machine is out of the session).
pub fn stepping(&self) -> bool {
self.machine.is_none()
}
/// The run's own steps so far.
fn executed(&self) -> u64 {
self.machine.as_ref().map_or(0, |m| m.executed.saturating_sub(self.base))
}
}
/// Each open workbook's uid and its current version, to see which ones a step changes.
fn versions(s: &Session) -> Vec<(u64, Arc<Workbook>)> {
s.docs.iter().map(|d| (d.uid, d.wb.clone())).collect()
}
/// Adds to the run's touched log the workbooks that changed since `before`.
fn note_touched(s: &mut Session, before: &[(u64, Arc<Workbook>)]) {
let changed: Vec<u64> =
s.docs.iter().filter(|d| before.iter().any(|(uid, wb)| *uid == d.uid && !Arc::ptr_eq(wb, &d.wb))).map(|d| d.uid).collect();
if let Some(dbg) = s.macro_debug.as_mut() {
dbg.touched.extend(changed);
}
}
/// Remembers where the run left its workbook: the active sheet and the selections.
fn remember_view(s: &mut Session) {
let Some(uid) = s.macro_debug.as_ref().map(|d| d.doc) else { return };
let view = s.docs.iter().find(|d| d.uid == uid).map(|d| View {
sheet: d.wb.active_sheet(),
selection: d.selection.clone(),
sheet_selections: d.sheet_selections.clone(),
});
if let Some(dbg) = s.macro_debug.as_mut() {
dbg.view = view;
}
}
/// Puts back the active sheet and the selections where the run left them, so the next step runs
/// where `macro.run` would run it, however the person looked around meanwhile.
fn put_back_view(s: &mut Session) {
let Some((uid, Some(view))) = s.macro_debug.as_ref().map(|d| (d.doc, d.view.clone())) else { return };
let Some(d) = s.docs.iter_mut().find(|d| d.uid == uid) else { return };
if view.sheet < d.wb.sheets.len() {
// The active sheet is view state: setting it doesn't make the workbook dirty.
d.wb.set_active_sheet(view.sheet);
}
d.selection = view.selection;
d.sheet_selections = view.sheet_selections;
}
/// The message for edits refused while a macro is being debugged.
pub const BUSY: &str = "Finish or stop the macro first";
/// Ends a debugger whose undo group is no longer open (something other than the debugger ended
/// or rolled it back): its workbook is not locked any more. Returns the macro's name when it did.
pub(crate) fn drop_if_ended(s: &mut Session) -> Option<String> {
let d = s.macro_debug.as_ref()?;
if d.stepping() || d.token.as_ref().is_some_and(|t| s.group_is_open(t)) {
return None;
}
s.macro_debug.take().map(|d| d.name)
}
/// After Stop Action rolled back the undo groups of the workbook with uid `doc`: ends the
/// debugger if its run was there. Returns the macro's name and the files the run wrote, which stay.
pub(crate) fn end_stopped(s: &mut Session, doc: u64) -> Option<(String, Vec<String>)> {
if !s.macro_debug.as_ref().is_some_and(|d| d.doc == doc && !d.stepping()) {
return None;
}
let d = s.macro_debug.take()?;
Some((d.name, d.machine.map(|m| m.wrote_files).unwrap_or_default()))
}
/// Why an action finds the run already ended: its undo group was closed outside the debugger.
fn already_ended(name: &str) -> String {
format!("Macro '{name}' was no longer being debugged: its undo group had been closed outside the debugger, so nothing was kept or rolled back.")
}
enum Mode {
Step,
Continue,
RunTo(String),
}
const ACTIONS: [&str; 6] = ["start", "step", "continue", "runTo", "stop", "state"];
/// `macro.debug {name?, inputs?, action: start|step|continue|runTo|stop|state, step?, keep?,
/// breakpoints?, breakpointsOnly?}`.
pub fn debug(s: &mut Session, p: &Json) -> Result<Json> {
// A step of a script or macro that started the debugger would leave it behind when the
// script fails and rolls back its groups, the debugger's among them.
if s.depth() > 1 {
return Err(EngineError::Disabled("macro.debug".into(), "The macro debugger runs on its own, not as a step of a script or macro.".into()));
}
let ended = drop_if_ended(s);
let action = match p.get("action") {
None | Some(Json::Null) if s.macro_debug.is_some() || ended.is_some() => "state".to_string(),
None | Some(Json::Null) => "start".to_string(),
Some(Json::String(a)) => a.clone(),
// `{"action": {"runTo": {"step": "3"}}}`
Some(Json::Object(o)) if o.len() == 1 => o.keys().next().cloned().unwrap_or_default(),
Some(_) => return Err(bad("`action` must be start, step, continue, runTo, stop or state")),
};
if !ACTIONS.contains(&action.as_str()) {
return Err(bad(format!("action must be start, step, continue, runTo, stop or state, not '{action}'")));
}
if let Some(name) = ended
&& action != "start"
{
return Err(EngineError::Other(already_ended(&name)));
}
let only = bool_param(p, "breakpointsOnly")?;
if action == "start" {
return start(s, p, only.unwrap_or(false));
}
let keep = bool_param(p, "keep")?;
let d = s.macro_debug.as_ref().ok_or_else(not_debugging)?;
let target = if action == "runTo" {
let target = p
.get("step")
.or_else(|| p.pointer("/action/runTo/step"))
.and_then(path_of)
.ok_or_else(|| bad("`runTo` needs `step`: the path of the step to stop at"))?;
if !d.paths.contains(&target) {
return Err(bad(format!("macro '{}' has no step {target}", d.name)));
}
Some(target)
} else {
None
};
let breakpoints = p.get("breakpoints").filter(|v| !v.is_null()).map(|v| paths_param(v, &d.paths, &d.name)).transpose()?;
if let Some(d) = s.macro_debug.as_mut() {
if let Some(bps) = breakpoints {
d.breakpoints = bps;
}
if let Some(only) = only {
d.only_listed = only;
}
}
match (action.as_str(), target) {
("step", _) => advance(s, Mode::Step),
("continue", _) => advance(s, Mode::Continue),
("runTo", Some(target)) => advance(s, Mode::RunTo(target)),
("stop", _) => stop(s, keep.unwrap_or(false)),
_ => state(s),
}
}
fn bad(msg: impl Into<String>) -> EngineError {
crate::cmd::bad("macro.debug", msg)
}
fn not_debugging() -> EngineError {
EngineError::Other("No macro is being debugged. Start one with action: start.".into())
}
/// `true`, `false`, or not given (`null` counts as not given); anything else is refused.
fn bool_param(p: &Json, key: &str) -> Result<Option<bool>> {
match p.get(key) {
None | Some(Json::Null) => Ok(None),
Some(Json::Bool(b)) => Ok(Some(*b)),
Some(_) => Err(bad(format!("`{key}` must be true or false"))),
}
}
/// A step path from JSON: `"2.1"` or `3`.
fn path_of(v: &Json) -> Option<String> {
match v {
Json::String(t) if !t.trim().is_empty() => Some(t.trim().to_string()),
Json::Number(n) => n.as_u64().map(|n| n.to_string()),
_ => None,
}
}
/// The `breakpoints` list: step paths of macro `name` (one of `paths`), at most
/// [`MAX_BREAKPOINTS`].
fn paths_param(v: &Json, paths: &BTreeSet<String>, name: &str) -> Result<BTreeSet<String>> {
let list = v.as_array().ok_or_else(|| bad("`breakpoints` must be a list of step paths"))?;
if list.len() > MAX_BREAKPOINTS {
return Err(bad(format!("`breakpoints` lists {} steps; at most {MAX_BREAKPOINTS} can be set", list.len())));
}
let mut out = BTreeSet::new();
for x in list {
let path = path_of(x).ok_or_else(|| bad("a breakpoint is a step path such as \"3\" or \"2.1\""))?;
if !paths.contains(&path) {
return Err(bad(format!("macro '{name}' has no step {path} for a breakpoint")));
}
out.insert(path);
}
Ok(out)
}
fn start(s: &mut Session, p: &Json, only_listed: bool) -> Result<Json> {
if let Some(d) = &s.macro_debug {
return Err(EngineError::Other(format!("Macro '{}' is being debugged. Stop it first.", d.name)));
}
let name = p.get("name").and_then(Json::as_str).filter(|n| !n.trim().is_empty()).ok_or_else(|| bad("missing `name`"))?;
let b = super::run::begin(s, name, p)?;
let paths = all_paths(&b.mac.steps, "");
let breakpoints = match p.get("breakpoints").filter(|v| !v.is_null()) {
Some(v) => paths_param(v, &paths, &b.mac.name)?,
None => BTreeSet::new(),
};
let start_wb = s.doc()?.wb.clone();
let token = s.group_begin(&format!("Run Macro: {}", b.mac.name))?;
// The run's machine: unless the rule is overwrite, it checks each step for writes over data.
let (mut machine, shifted) = match super::run::make_way(s, &b) {
Ok(made) => made,
Err(e) => {
let _ = s.group_rollback(token);
return Err(e);
}
};
// Each action has the time limit to itself; the dry runs just had theirs.
machine.restart_clock();
let base = machine.executed;
s.macro_debug = Some(Debugger {
name: b.mac.name.clone(),
doc: b.prepared.doc,
machine: Some(machine),
token: Some(token),
breakpoints,
only_listed,
start: start_wb,
paths,
touched: BTreeSet::from([b.prepared.doc]),
base,
shifted: shifted.then(|| b.rule.name()),
view: None,
});
if s.macro_debug.as_ref().and_then(|d| d.machine.as_ref()).is_some_and(Machine::is_done) {
return finish(s, None);
}
remember_view(s);
state(s)
}
fn all_paths(steps: &[gridcraft_model::Step], prefix: &str) -> BTreeSet<String> {
let mut out = BTreeSet::new();
for (i, st) in steps.iter().enumerate() {
let path = if prefix.is_empty() { (i + 1).to_string() } else { format!("{prefix}.{}", i + 1) };
match &st.kind {
gridcraft_model::StepKind::Repeat { body, .. } => out.extend(all_paths(body, &path)),
gridcraft_model::StepKind::If { then, otherwise, .. } => {
out.extend(all_paths(then, &format!("{path}.then")));
out.extend(all_paths(otherwise, &format!("{path}.else")));
}
_ => {}
}
out.insert(path);
}
out
}
/// Runs one step (`Step`), or steps until a breakpoint, the target step or the end.
fn advance(s: &mut Session, mode: Mode) -> Result<Json> {
put_back_view(s);
let d = s.macro_debug.as_mut().ok_or_else(not_debugging)?;
let (name, base) = (d.name.clone(), d.base);
let breakpoints = d.breakpoints.clone();
let only_listed = d.only_listed;
let mut machine = d.machine.take().ok_or_else(not_debugging)?;
machine.restart_clock();
let before = versions(s);
let mut first = true;
let mut hit = false;
let mut outcome = Ok(());
while !machine.is_done() {
if !first {
let Some((path, st)) = machine.current() else { break };
if matches!(mode, Mode::Step) {
break;
}
if (st.breakpoint && !only_listed) || breakpoints.contains(&path) {
hit = true;
break;
}
if let Mode::RunTo(target) = &mode
&& *target == path
{
break;
}
}
first = false;
outcome = machine.step(s);
if outcome.is_err() {
break;
}
}
let done = machine.is_done();
match s.macro_debug.as_mut() {
Some(d) => d.machine = Some(machine),
// The macro.debug call panicked and `recover` has already rolled the run back.
None => {
return match outcome {
Err(e) => Ok(failed(&name, machine.executed.saturating_sub(base), &e)),
Ok(()) => Err(not_debugging()),
};
}
}
note_touched(s, &before);
if let Err(e) = outcome {
return finish(s, Some(e));
}
if done {
return finish(s, None);
}
remember_view(s);
let mut out = state(s)?;
out["atBreakpoint"] = json!(hit);
Ok(out)
}
/// What every answer about the run's end or its state adds: the data shift at the start and the
/// files the run wrote (they stay, whatever happens to the workbook).
fn add_notes(out: &mut Json, d: &Debugger, wrote_files: &[String]) {
if let Some(rule) = d.shifted {
out["shifted"] = json!(rule);
}
if !wrote_files.is_empty() {
out["wroteFiles"] = json!(wrote_files);
}
}
/// Ends the run after its last step (kept as one undo step, unless a Stop step asked for a
/// rollback) or a failed step (rolled back).
fn finish(s: &mut Session, error: Option<StepError>) -> Result<Json> {
let Some(mut d) = s.macro_debug.take() else { return Err(not_debugging()) };
let (changed, cells) = changed(s, &d);
let executed = d.executed();
let machine = d.machine.take();
let stopped = machine.as_ref().and_then(|m| m.stopped.clone());
let wrote = machine.map(|m| m.wrote_files).unwrap_or_default();
let rollback = error.is_some() || matches!(stopped, Some((_, true)));
let ended = match d.token.take() {
Some(token) if rollback => s.group_rollback(token).is_ok(),
Some(token) => s.group_end(token).is_ok(),
None => false,
};
if !ended {
let step = error.map(|e| format!("{} ", e.describe())).unwrap_or_default();
return Err(EngineError::Other(format!("{step}{}", already_ended(&d.name))));
}
if let Some(e) = &error {
return Ok(failed(&d.name, executed, e));
}
let mut out = json!({"macro": d.name, "executed": executed});
match &stopped {
Some((msg, rolled)) => {
out["state"] = json!("stopped");
out["stopped"] = json!(msg);
out["kept"] = json!(!rolled);
if *rolled {
out["message"] = json!(rolled_back(&wrote));
}
}
None => out["state"] = json!("finished"),
}
if !rollback {
out["changed"] = json!(changed);
out["changedCells"] = json!(cells);
}
add_notes(&mut out, &d, &wrote);
Ok(out)
}
/// The result of a run that a failed step rolled back.
fn failed(name: &str, executed: u64, e: &StepError) -> Json {
let mut out = json!({
"macro": name,
"executed": executed,
"state": "failed",
"error": serde_json::to_value(e).unwrap_or(Json::Null),
"message": format!("{} {}", e.describe(), rolled_back(&e.wrote_files)),
});
if !e.wrote_files.is_empty() {
out["wroteFiles"] = json!(e.wrote_files);
}
out
}
/// Stops the run: keeps what it did as one undo step, or rolls it back.
fn stop(s: &mut Session, keep: bool) -> Result<Json> {
let Some(mut d) = s.macro_debug.take() else { return Err(not_debugging()) };
let (changed, cells) = changed(s, &d);
let ended = match d.token.take() {
Some(token) if keep => s.group_end(token).is_ok(),
Some(token) => s.group_rollback(token).is_ok(),
None => false,
};
if !ended {
return Err(EngineError::Other(already_ended(&d.name)));
}
let wrote = d.machine.as_ref().map(|m| m.wrote_files.clone()).unwrap_or_default();
let mut out = json!({"state": "stopped", "macro": d.name, "kept": keep, "executed": d.executed()});
if keep {
out["changed"] = json!(changed);
out["changedCells"] = json!(cells);
} else {
out["message"] = json!(rolled_back(&wrote));
}
add_notes(&mut out, &d, &wrote);
Ok(out)
}
/// Where the run is: the next step with its parameters as they would run, the step that just
/// ran, and what the run has changed in its workbook so far.
fn state(s: &mut Session) -> Result<Json> {
let d = s.macro_debug.as_ref().ok_or_else(not_debugging)?;
let machine = d.machine.as_ref().ok_or_else(not_debugging)?;
let wb = s.docs.iter().find(|x| x.uid == d.doc).map(|x| x.wb.clone()).ok_or(EngineError::NoDocument)?;
let (changed, cells) = changed(s, d);
let mut out = json!({
"state": "paused",
"macro": d.name,
"current": machine.current_json(&wb),
"lastParams": machine.last_params,
"executed": d.executed(),
"breakpoints": d.breakpoints,
"changed": changed,
"changedCells": cells,
});
add_notes(&mut out, d, &machine.wrote_files);
Ok(out)
}
/// What the run changed in its workbook since it started: cells as `Sheet!A1:B2` ranges and the
/// sheets it added, deleted or renamed (at most [`MAX_LISTED`] entries), and how many cells
/// changed on the sheets the workbook had at the start.
fn changed(s: &Session, d: &Debugger) -> (Vec<String>, usize) {
let Some(now) = s.docs.iter().find(|x| x.uid == d.doc).map(|x| x.wb.clone()) else { return (vec![], 0) };
let (pairs, added) = super::run::pair_sheets(&d.start, &now);
let mut out = Vec::new();
let mut count = 0usize;
for (bi, ai) in pairs {
let Some(before) = d.start.sheets.get(bi) else { continue };
let Some(after) = ai.and_then(|a| now.sheets.get(a)) else {
out.push(format!("{} (deleted sheet)", before.name));
continue;
};
if before.name != after.name {
out.push(format!("{} (renamed from {})", after.name, before.name));
}
if Arc::ptr_eq(before, after) {
continue;
}
let mut cells = before.cells.diff(&after.cells);
count = count.saturating_add(cells.len());
if out.len() < MAX_LISTED {
let q = super::resolve::quote_sheet(&after.name);
out.extend(super::run::ranges(&mut cells).into_iter().map(|r| format!("{q}!{r}")));
}
}
out.extend(added.into_iter().filter_map(|a| now.sheets.get(a)).map(|sh| format!("{} (new sheet)", sh.name)));
out.truncate(MAX_LISTED);
(out, count)
}
/// After the `macro.debug` call itself panicked: if the run lost its place, roll it back so its
/// undo group doesn't stay open.
pub(crate) fn recover(s: &mut Session) {
if s.macro_debug.as_ref().is_some_and(Debugger::stepping)
&& let Some(mut d) = s.macro_debug.take()
&& let Some(token) = d.token.take()
{
let _ = s.group_rollback(token);
}
}
#[cfg(test)]
mod tests {
use serde_json::json;
use crate::Session;
#[test]
fn a_run_that_lost_its_place_is_rolled_back() {
let mut s = Session::new();
s.new_workbook();
let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [
{"command": "cell.set", "params": {"cell": "A1", "input": "x"}},
{"command": "cell.set", "params": {"cell": "A2", "input": "y"}}
]}))
.unwrap();
let d = s.doc_mut().unwrap();
let mut wb = (*d.wb).clone();
wb.macros.push(m);
d.wb = std::sync::Arc::new(wb);
let start = s.doc().unwrap().wb.clone();
s.execute("macro.debug", json!({"name": "M"})).unwrap();
s.execute("macro.debug", json!({"action": "step"})).unwrap();
// As if a step had panicked with the machine out of the session.
s.macro_debug.as_mut().unwrap().machine = None;
super::recover(&mut s);
assert!(s.macro_debug.is_none());
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().wb, &start));
assert!(!s.in_group(s.doc().unwrap().uid));
s.execute("cell.set", json!({"cell": "B1", "input": "free"})).unwrap();
}
#[test]
fn a_step_that_panics_reports_the_step_failure() {
crate::register_test_command("test.debugStepPanics", |_, _| panic!("boom"));
let mut s = Session::new();
s.new_workbook();
let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [
{"command": "cell.set", "params": {"cell": "A1", "input": "x"}},
{"command": "test.debugStepPanics", "params": {}}
]}))
.unwrap();
let d = s.doc_mut().unwrap();
let mut wb = (*d.wb).clone();
wb.macros.push(m);
d.wb = std::sync::Arc::new(wb);
let start = s.doc().unwrap().wb.clone();
s.execute("macro.debug", json!({"name": "M"})).unwrap();
s.execute("macro.debug", json!({"action": "step"})).unwrap();
let r = s.execute("macro.debug", json!({"action": "step"})).unwrap();
assert_eq!(r["state"], "failed", "{r}");
assert_eq!(r["macro"], "M");
assert_eq!(r["error"]["step"], "2");
assert!(r["message"].as_str().unwrap().contains("boom"), "{r}");
assert!(s.macro_debug.is_none());
assert!(std::sync::Arc::ptr_eq(&s.doc().unwrap().wb, &start));
assert!(!s.in_group(s.doc().unwrap().uid));
}
#[test]
fn a_workbook_in_the_touched_log_waits_until_the_run_ends() {
let mut s = Session::new();
s.new_workbook();
let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [
{"command": "cell.set", "params": {"cell": "A1", "input": "x"}},
{"command": "cell.set", "params": {"cell": "A2", "input": "y"}}
]}))
.unwrap();
let d = s.doc_mut().unwrap();
let mut wb = (*d.wb).clone();
wb.macros.push(m);
d.wb = std::sync::Arc::new(wb);
s.new_workbook();
let other = s.doc().unwrap().uid;
s.set_active(0);
s.execute("macro.debug", json!({"name": "M"})).unwrap();
assert!(!s.macro_debug.as_ref().unwrap().locks(other));
// As if a step had changed the other workbook too.
let before = super::versions(&s);
let d = s.docs.iter_mut().find(|d| d.uid == other).unwrap();
let mut wb = (*d.wb).clone();
wb.props.title = "changed by the macro".into();
d.wb = std::sync::Arc::new(wb);
super::note_touched(&mut s, &before);
assert!(s.macro_debug.as_ref().unwrap().locks(other));
s.set_active(1);
assert!(s.execute("cell.set", json!({"cell": "B1", "input": "no"})).is_err());
s.execute("macro.debug", json!({"action": "stop", "keep": true})).unwrap();
s.execute("cell.set", json!({"cell": "B1", "input": "free"})).unwrap();
}
#[test]
fn edits_run_by_another_command_wait_too() {
let mut s = Session::new();
s.new_workbook();
let m = crate::macros::parse_macro(&json!({"name": "M", "output": {"onConflict": "overwrite"}, "steps": [
{"command": "cell.set", "params": {"cell": "A1", "input": "x"}},
{"command": "cell.set", "params": {"cell": "A2", "input": "y"}}
]}))
.unwrap();
let d = s.doc_mut().unwrap();
let mut wb = (*d.wb).clone();
wb.macros.push(m);
d.wb = std::sync::Arc::new(wb);
s.execute("automate.recordActions", json!({})).unwrap();
s.execute("cell.set", json!({"cell": "C1", "input": "1"})).unwrap();
s.execute("macro.debug", json!({"name": "M"})).unwrap();
s.execute("macro.debug", json!({"action": "step"})).unwrap();
// Record Actions changes nothing itself; stopping it saves the script with a nested
// command, which would land in the run's undo group.
let e = s.execute("automate.recordActions", json!({"stop": true, "name": "S"})).unwrap_err().to_string();
assert!(e.contains(super::BUSY), "{e}");
assert!(s.execute("automate.allScripts", json!({})).unwrap().as_array().unwrap().is_empty());
// The run's own steps still go ahead.
let r = s.execute("macro.debug", json!({"action": "step"})).unwrap();
assert_eq!(r["executed"], 2, "{r}");
assert!(s.macro_debug.is_none());
// The recording was kept: stopping now saves it.
let saved = s.execute("automate.recordActions", json!({"stop": true, "name": "S"})).unwrap();
assert_eq!(saved["saved"], "S", "{saved}");
let scripts = s.execute("automate.allScripts", json!({})).unwrap();
assert_eq!(scripts[0]["name"], "S", "{scripts}");
}
}
-515
View File
@@ -1,515 +0,0 @@
//! GridCraft macros: typed, declarative command lists, not VBA. The model is plain data in
//! `gridcraft_model::Macro`; this module reads and writes it and keeps it within its limits.
//!
//! Storage: the JSON format keeps `Workbook::macros` as a field. In xlsx each macro is one item of
//! GridCraft's hidden workbook data ([`crate::cmd::stash`]), `_gc_macro_<n>_<k>`, written when the
//! file is saved and taken back out of the names when it is opened, so the names never linger in
//! the open document. Scripts made with Automate › New Script or Record Actions are a separate,
//! older feature with their own storage; macros neither read nor change it. A recording keeps the
//! steps a script run ran while it recorded, as the run reported them.
use std::collections::{BTreeMap, HashMap, HashSet};
use gridcraft_model::{DefinedName, Macro, Step, StepKind, Workbook};
use serde_json::{Value as Json, json};
use crate::cmd::stash;
pub mod debug;
pub mod policy;
pub mod recorder;
pub mod resolve;
pub mod run;
/// The hidden-data key prefix of a stored macro (`macro_<n>`).
const MACRO_KEY: &str = "macro_";
/// Reads a macro from JSON, as `macro.save` and files give it. A command step may also use the
/// short form `{"command": "cell.set", "params": {…}}`.
pub fn parse_macro(v: &Json) -> Result<Macro, String> {
// A newer format is refused before its steps are read, so the message says why.
if let Some(found) = v.get("v").and_then(Json::as_u64) {
gridcraft_model::check_version(found)?;
}
let mut v = v.clone();
if let Some(steps) = v.get_mut("steps") {
normalize_steps(steps, 1)?;
}
if v.get("v").is_none()
&& let Some(o) = v.as_object_mut()
{
o.insert("v".into(), json!(gridcraft_model::MACRO_VERSION));
}
let mut m: Macro = serde_json::from_value(v).map_err(|e| format!("This isn't a valid macro: {e}"))?;
m.name = m.name.trim().to_string();
check(&m)?;
Ok(m)
}
/// Every check a macro must pass before it is kept in a workbook: the model's limits
/// ([`Macro::check_limits`]) and the size an xlsx file can hold. Every path that adds a macro,
/// from JSON, from a file or built in code, goes through this one check.
pub fn check(m: &Macro) -> Result<(), String> {
m.check_limits()?;
check_size(m)
}
/// Refuses a macro too large to keep in an xlsx file ([`stash::MAX_ITEM_CHARS`] characters of
/// JSON). Every command that adds or changes a macro calls [`check`], so the size of a macro kept
/// in the workbook doesn't make a save fail. (A clashing visible name doesn't either:
/// [`with_names`] stores the macro under another index.)
fn check_size(m: &Macro) -> Result<(), String> {
if macro_json(m).to_string().chars().count() > stash::MAX_ITEM_CHARS {
return Err(format!("This macro is too large to keep in the workbook ({} characters at most).", stash::MAX_ITEM_CHARS));
}
Ok(())
}
/// Leaves out the last top-level steps of `m` until it fits the size [`check_size`] allows, and
/// returns how many steps it left out (a recording cut to fit, as at the step limit). Each step's
/// JSON is measured once.
pub fn fit_size(m: &mut Macro) -> usize {
let chars = |v: &Json| v.to_string().chars().count();
let steps = std::mem::take(&mut m.steps);
// The macro without steps holds `"steps":[]`; each step adds its JSON, and a comma after the
// first.
let mut size = chars(&macro_json(m));
let mut kept = 0usize;
for (i, st) in steps.iter().enumerate() {
let add = chars(&serde_json::to_value(st).unwrap_or(Json::Null)).saturating_add(usize::from(i > 0));
if size.saturating_add(add) > stash::MAX_ITEM_CHARS {
break;
}
size = size.saturating_add(add);
kept = i.saturating_add(1);
}
let cut = steps.len().saturating_sub(kept);
m.steps = steps;
m.steps.truncate(kept);
cut
}
/// How many macros the workbook holds: the readable ones and the ones kept in a newer format
/// (`Workbook::macros_unread`), which a save writes back and the next open counts too.
pub fn count(wb: &Workbook) -> usize {
wb.macros.len().saturating_add(wb.macros_unread.len())
}
/// Refuses one more macro in a workbook that holds [`gridcraft_model::MAX_MACROS`] already,
/// counting the kept ones, so a save never holds more than the next open keeps.
pub fn check_room(wb: &Workbook) -> Result<(), String> {
let max = gridcraft_model::MAX_MACROS;
if count(wb) < max {
return Ok(());
}
Err(match wb.macros_unread.len() {
0 => format!("A workbook can hold at most {max} macros."),
kept => format!(
"A workbook can hold at most {max} macros, and {kept} of the {} in this one {} made by a newer version of GridCraft and kept unchanged.",
count(wb),
if kept == 1 { "was" } else { "were" }
),
})
}
/// The name of the macro kept in a newer format called `name` (compared as [`find`] compares),
/// as the file held it. Its name is taken: a new or renamed macro with it would clash when the
/// newer version reads the file.
pub fn kept_name(wb: &Workbook, name: &str) -> Option<String> {
let name = name.trim();
wb.macros_unread
.iter()
.filter_map(|v| v.get("name").and_then(Json::as_str))
.map(str::trim)
.find(|k| k.eq_ignore_ascii_case(name))
.map(str::to_string)
}
/// Rewrites the short step form into the tagged one, in place, refusing nesting past the limit
/// before the steps are deserialized.
fn normalize_steps(steps: &mut Json, level: usize) -> Result<(), String> {
let Some(list) = steps.as_array_mut() else { return Err("`steps` must be a list.".into()) };
for st in list {
let Some(o) = st.as_object_mut() else { return Err("Every step must be an object.".into()) };
if !o.contains_key("type")
&& let Some(id) = o.remove("command")
{
o.insert("type".into(), json!("command"));
o.insert("id".into(), id);
}
// A step with a body is a Repeat or an If: one at the top is nested 1 deep.
let nested = ["body", "then", "else"].iter().any(|key| o.contains_key(*key));
if nested && level > gridcraft_model::MAX_MACRO_DEPTH {
return Err(format!("Repeat and If steps can nest at most {} deep.", gridcraft_model::MAX_MACRO_DEPTH));
}
for key in ["body", "then", "else"] {
if let Some(body) = o.get_mut(key) {
normalize_steps(body, level + 1)?;
}
}
}
Ok(())
}
/// A macro as JSON (the shape `macro.get` returns and `macro.save` takes).
pub fn macro_json(m: &Macro) -> Json {
serde_json::to_value(m).unwrap_or(Json::Null)
}
/// The index of the macro called `name` (case-insensitive).
pub fn find(wb: &Workbook, name: &str) -> Option<usize> {
let name = name.trim();
wb.macros.iter().position(|m| m.name.eq_ignore_ascii_case(name))
}
/// A macro's content as canonical JSON text (object keys sorted): approval to run a macro holds
/// until the macro changes. It is the text itself rather than a hash, so no two different macros
/// share it.
pub fn fingerprint(m: &Macro) -> String {
macro_json(m).to_string()
}
/// A copy of `wb` for xlsx with each macro in hidden names (`_gc_macro_<n>_<k>`) instead of the
/// field, or `None` when there is nothing to change. The macros keep their order; the index `n`
/// is this build's choice, so it steps around any a person's own visible name already uses.
pub fn with_names(wb: &Workbook) -> crate::Result<Option<Workbook>> {
// Only the numbered items are this build's: a hidden `_gc_macro_<x>_<k>` name with some
// other key stays, as `load` leaves it.
let stored = |n: &DefinedName| macro_item(n).is_some_and(|(_, i)| i.is_some());
if wb.macros.is_empty() && wb.macros_unread.is_empty() && !wb.names.iter().any(stored) {
return Ok(None);
}
// A macro that skipped `check` (built in code) fails the save, as the JSON save does, rather
// than being written and dropped the next time the file opens.
for m in wb.macros.iter() {
check(m).map_err(crate::EngineError::Other)?;
}
let mut out = wb.clone();
// One pass drops the items of the last save, and one writes the macros (a file can hold a
// thousand macros and many thousands of names).
out.names.retain(|n| !stored(n));
// A visible workbook-scope name of the storage shape is someone's own, and `set_many` won't
// write a hidden one of the same name beside it. Such a name (`_GC_MACRO_1_0`, or one a
// person defined after the file opened) blocks its item's key, in any chunk, so the macros
// take the free indices from 0 up. The keys compare as the stash reads them, so this skips
// exactly what `set_many` would refuse, and no arithmetic depends on a number from the file.
let blocked: HashSet<String> = out.names.iter().filter(|n| !n.hidden && n.scope.is_none()).filter_map(|n| stash::item_key(&n.name)).collect();
let free = (0..=u64::MAX).map(|i| format!("{MACRO_KEY}{i}")).filter(|key| !blocked.contains(key));
// Macros in a newer format follow the readable ones, written back as the file held them.
let texts = wb.macros.iter().map(|m| macro_json(m).to_string()).chain(wb.macros_unread.iter().map(Json::to_string));
let items: Vec<(String, String)> = texts.zip(free).map(|(text, key)| (key, text)).collect();
stash::set_many(&mut out, &items)?;
// An empty list of its own, not a copy of the shared one cleared.
out.macros = gridcraft_model::Macros::default();
out.macros_unread = std::sync::Arc::default();
Ok(Some(out))
}
/// The JSON format's bytes for `wb`: the workbook, with the macros in a newer format put back into
/// the `macros` list as the file held them.
pub fn json_bytes(wb: &Workbook) -> crate::Result<Vec<u8>> {
// Every path that adds a macro checks it, so this refuses only one that skipped the check, as
// the xlsx save does, instead of writing a macro the next open would drop.
for m in &wb.macros {
check(m).map_err(crate::EngineError::Other)?;
}
let bytes = if wb.macros_unread.is_empty() {
serde_json::to_vec_pretty(wb)
} else {
serde_json::to_value(wb).and_then(|mut v| {
if let Some(list) = v.as_object_mut().map(|o| o.entry("macros").or_insert_with(|| Json::Array(vec![]))).and_then(Json::as_array_mut) {
list.extend(wb.macros_unread.iter().cloned());
}
serde_json::to_vec_pretty(&v)
})
};
bytes.map_err(|e| crate::EngineError::Other(e.to_string()))
}
/// Whether `v` is a macro in a newer format than this build reads.
fn is_newer(v: &Json) -> bool {
v.get("v").and_then(Json::as_u64).is_some_and(|found| found > u64::from(gridcraft_model::MACRO_VERSION))
}
/// Keeps a macro in a newer format unread, if it is small enough to save back
/// ([`stash::MAX_ITEM_CHARS`] characters of JSON, the size every xlsx save can write).
fn keep_unread(unread: &mut Vec<Json>, v: Json, bad: &mut usize) {
if v.to_string().chars().count() <= stash::MAX_ITEM_CHARS {
unread.push(v);
} else {
*bad = bad.saturating_add(1);
}
}
/// After a file is opened: moves macros out of hidden names into `Workbook::macros`, keeps
/// macros in a newer format unread (`Workbook::macros_unread`), and drops macros that are damaged
/// or over the limits. Returns a warning for what was kept unread and one for what was dropped.
pub fn load(wb: &mut Workbook) -> Vec<String> {
let mut warnings = Vec::new();
let mut bad = 0usize;
let mut unread = Vec::new();
// Entries of the JSON format's field, each read on its own so a damaged one drops alone.
let read = std::mem::take(&mut wb.macros_read);
bad = bad.saturating_add(read.len().saturating_sub(gridcraft_model::MAX_MACROS));
for v in read.into_iter().take(gridcraft_model::MAX_MACROS) {
if is_newer(&v) {
keep_unread(&mut unread, v, &mut bad);
continue;
}
match parse_macro(&v) {
Ok(m) => wb.macros.push(m),
Err(_) => bad += 1,
}
}
// One pass takes the hidden macro names out of the workbook, grouped by macro in saved order.
let mut stored: BTreeMap<(u64, String), Vec<DefinedName>> = BTreeMap::new();
let mut kept = Vec::new();
for n in std::mem::take(&mut wb.names) {
match macro_item(&n) {
None => kept.push(n),
Some((key, Some(i))) => stored.entry((i, key)).or_default().push(n),
// A `macro_` item that isn't numbered isn't one this build wrote: it stays as it is,
// unread, and a save keeps it.
Some((_, None)) => kept.push(n),
}
}
wb.names = kept;
// Past the macro limit, the rest are counted without being read.
bad = bad.saturating_add(stored.len().saturating_sub(gridcraft_model::MAX_MACROS));
let mut found = Vec::new();
let mut item = Workbook::new();
for ((_, key), names) in stored.into_iter().take(gridcraft_model::MAX_MACROS) {
item.names = names;
let v = stash::get_json(&item, &key);
if let Some(v) = v.as_ref().filter(|v| is_newer(v)) {
keep_unread(&mut unread, v.clone(), &mut bad);
continue;
}
match v.ok_or_else(|| "unreadable".to_string()).and_then(|v| parse_macro(&v)) {
Ok(m) => found.push(m),
Err(_) => bad += 1,
}
}
// All of them go through the same limits, and a clashing name gets a suffix.
let mut all = std::mem::take(&mut wb.macros);
all.extend(found);
let mut taken = Taken::default();
for m in all {
if wb.macros.len() >= gridcraft_model::MAX_MACROS {
bad += 1;
continue;
}
let mut m = m;
let checked = m.check_limits().and_then(|()| {
m.name = taken.unique(m.name.trim());
// The macro as it is kept, renamed, must still pass, or the next save would lose it.
check(&m)
});
match checked {
Ok(()) => {
taken.insert(&m.name);
wb.macros.push(m);
}
Err(_) => bad += 1,
}
}
// Readable and unread macros together stay within the limit, the readable ones first.
let room = gridcraft_model::MAX_MACROS.saturating_sub(wb.macros.len());
bad = bad.saturating_add(unread.len().saturating_sub(room));
unread.truncate(room);
if !unread.is_empty() {
let one = unread.len() == 1;
warnings.push(format!(
"{} macro{} in the workbook {} made by a newer version of GridCraft: this version can't show or run {}, and saving keeps {} unchanged.",
unread.len(),
if one { "" } else { "s" },
if one { "was" } else { "were" },
if one { "it" } else { "them" },
if one { "it" } else { "them" },
));
}
wb.macros_unread = std::sync::Arc::new(unread);
if bad > 0 {
warnings.push(format!(
"{bad} macro{} in the workbook couldn't be opened: {} damaged or over the limits ({} macros, {} steps each, Repeat and If nested {} deep).",
if bad == 1 { "" } else { "s" },
if bad == 1 { "it is" } else { "they are" },
gridcraft_model::MAX_MACROS,
gridcraft_model::MAX_MACRO_STEPS,
gridcraft_model::MAX_MACRO_DEPTH
));
}
warnings
}
/// The item key (`macro_<n>`, lower-cased) of a hidden macro name, and `n` when it is a number.
fn macro_item(n: &DefinedName) -> Option<(String, Option<u64>)> {
// Only a hidden workbook-scope name is macro storage: a person's own visible name that
// happens to look like one stays a name.
let key = stash::item_key(&n.name).filter(|k| n.hidden && n.scope.is_none() && k.starts_with(MACRO_KEY))?;
let i = key.strip_prefix(MACRO_KEY).and_then(|i| i.parse().ok());
Some((key, i))
}
/// `name`, or `name (2)`, `name (3)`… if a macro already has it, a kept one too. A free name comes back as it is;
/// a renamed one is shortened to make room for the suffix, so it stays within
/// [`gridcraft_model::MAX_MACRO_NAME`] characters. The result still goes through [`check`].
pub fn unique_name(wb: &Workbook, name: &str) -> String {
let mut taken = Taken::default();
for m in &wb.macros {
taken.insert(&m.name);
}
for kept in wb.macros_unread.iter().filter_map(|v| v.get("name").and_then(Json::as_str)) {
taken.insert(kept);
}
taken.unique(name)
}
/// Macro names in use, compared as [`find`] compares them, and the next suffix to try for each
/// shortened name and suffix width, so naming many clashing macros takes near-linear work.
#[derive(Default)]
struct Taken {
names: HashSet<String>,
next: HashMap<(String, usize), u64>,
/// Name lookups made, for tests.
probes: usize,
}
impl Taken {
fn key(name: &str) -> String {
name.trim().to_ascii_lowercase()
}
fn insert(&mut self, name: &str) {
self.names.insert(Taken::key(name));
}
fn contains(&mut self, name: &str) -> bool {
self.probes = self.probes.saturating_add(1);
self.names.contains(&Taken::key(name))
}
/// The first of `name`, `name (2)`, `name (3)`… not in use.
fn unique(&mut self, name: &str) -> String {
let name = if name.is_empty() { "Macro" } else { name };
if !self.contains(name) {
return name.to_string();
}
let mut n: u64 = 2;
loop {
let suffix = format!(" ({n})");
let keep = gridcraft_model::MAX_MACRO_NAME.saturating_sub(suffix.chars().count());
let base: String = name.chars().take(keep).collect();
let base = base.trim_end();
// Every candidate for this base and suffix width below `next` is in use already
// (names are only added), so the search goes on from there.
let key = (Taken::key(base), suffix.len());
let next = self.next.get(&key).copied().unwrap_or(n);
if next > n {
n = next;
continue;
}
let Some(after) = n.checked_add(1) else { return name.to_string() };
self.next.insert(key, after);
let candidate = format!("{base}{suffix}");
if !self.contains(&candidate) {
return candidate;
}
n = after;
}
}
}
/// Ctrl+Shift (Cmd+Shift on a Mac) letters that GridCraft already uses, on any platform, which a
/// macro cannot take.
pub const RESERVED_SHORTCUTS: &[(char, &str)] = &[
('F', "Format Cells (Font) or Filter"),
('G', "Workbook Statistics"),
('H', "Replace"),
('J', "Ungroup"),
('K', "Group"),
('L', "Filter"),
('R', "Sort"),
('S', "Save As"),
('T', "AutoSum"),
('Z', "Redo"),
];
/// The letter of a macro shortcut: `"Ctrl+Shift+M"`, `"Cmd+Shift+M"`, `"⌘⇧M"` and `"m"` all give
/// `'M'`. `None` when it is not one letter from A to Z, alone or after Ctrl+Shift (Cmd+Shift):
/// other modifiers (`"Ctrl+Alt+M"`) are refused rather than read as Ctrl+Shift.
pub fn parse_shortcut(s: &str) -> Option<char> {
let letter = |t: &str| {
let mut chars = t.trim().chars();
match (chars.next(), chars.next()) {
(Some(c), None) if c.is_ascii_alphabetic() => Some(c.to_ascii_uppercase()),
_ => None,
}
};
let t = s.trim().to_ascii_lowercase();
["ctrl+shift+", "cmd+shift+", "⌘⇧"].iter().find_map(|p| t.strip_prefix(p)).map_or_else(|| letter(&t), letter)
}
/// How a shortcut is stored and shown: `Ctrl+Shift+M`.
pub fn shortcut_text(letter: char) -> String {
format!("Ctrl+Shift+{}", letter.to_ascii_uppercase())
}
/// Why macro `name` cannot use Ctrl+Shift+`letter`: GridCraft uses it, or another macro has it.
pub fn shortcut_conflict(wb: &Workbook, name: &str, letter: char) -> Option<String> {
let letter = letter.to_ascii_uppercase();
if let Some((_, what)) = RESERVED_SHORTCUTS.iter().find(|(c, _)| *c == letter) {
return Some(format!("{} is {what}. Choose another letter.", shortcut_text(letter)));
}
wb.macros
.iter()
.find(|m| !m.name.eq_ignore_ascii_case(name.trim()) && m.shortcut.as_deref().and_then(parse_shortcut) == Some(letter))
.map(|m| format!("{} already runs macro “{}”. Choose another letter.", shortcut_text(letter), m.name))
}
/// `name` trimmed, if it can name a new macro in `wb`; otherwise why not: it is empty, too long,
/// holds line breaks or another macro has it.
pub fn check_new_name(wb: &Workbook, name: &str) -> Result<String, String> {
let name = name.trim();
let probe = Macro { name: name.to_string(), ..Macro::default() };
probe.check_limits()?;
if name.chars().any(char::is_control) {
return Err("A macro name cannot hold line breaks or other control characters.".into());
}
if find(wb, name).is_some() {
return Err(format!("A macro called “{name}” already exists. Choose another name."));
}
if let Some(kept) = kept_name(wb, name) {
return Err(format!("A macro called “{kept}” is kept from a newer version of GridCraft. Choose another name."));
}
Ok(name.to_string())
}
/// The first free `Macro1`, `Macro2`… name, also free among the kept macros ([`kept_name`]).
pub fn next_name(wb: &Workbook) -> String {
(1..).map(|n| format!("Macro{n}")).find(|n| find(wb, n).is_none() && kept_name(wb, n).is_none()).unwrap_or_else(|| "Macro".into())
}
/// Every command step of `steps` (inside Repeat and If too), with its path.
pub fn command_steps(steps: &[Step]) -> Vec<(String, &str, &Json)> {
fn walk<'a>(steps: &'a [Step], prefix: &str, out: &mut Vec<(String, &'a str, &'a Json)>) {
for (i, st) in steps.iter().enumerate() {
let path = if prefix.is_empty() { (i + 1).to_string() } else { format!("{prefix}.{}", i + 1) };
match &st.kind {
StepKind::Command { id, params } => out.push((path, id.as_str(), params)),
StepKind::Repeat { body, .. } => walk(body, &path, out),
StepKind::If { then, otherwise, .. } => {
walk(then, &format!("{path}.then"), out);
walk(otherwise, &format!("{path}.else"), out);
}
StepKind::Stop { .. } => {}
}
}
}
let mut out = Vec::new();
walk(steps, "", &mut out);
out
}
#[cfg(test)]
mod tests;
-148
View File
@@ -1,148 +0,0 @@
//! What a macro step may run. Macros run commands, so this keeps a macro from saving, opening or
//! closing files, driving the app, running other macros, or reading and writing files without the
//! person knowing. It goes by command id and the `path` parameter; the runner also runs every
//! step except an approved file step inside the scripts' file sandbox, so a command that touches a
//! file under another parameter can't either.
use serde_json::Value as Json;
use crate::Trust;
/// Whether a command may run as a macro step.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Policy {
Allow,
/// Allowed once the person approves the macro; the text says why it asks ("came with a
/// workbook you opened").
Ask(String),
/// Reads or writes files: the macro must list the command in its permissions, and the person
/// approves the macro before it runs. The text says what it does ("reads a file").
NeedsPermission(String),
/// Never runs in a macro; the text says why.
Deny(String),
}
/// Prefixes of commands that never run in a macro, with the reason.
const DENIED_PREFIXES: &[(&str, &str)] = &[
("file.", "macros don't open, save, close or create workbooks"),
("app.", "macros don't change the app"),
("window.", "macros don't switch windows"),
("automate.", "macros don't record or run scripts or other macros"),
("macro.", "macros don't record or run other macros"),
];
/// Single commands that never run in a macro.
const DENIED: &[(&str, &str)] = &[
("edit.undo", "a macro is undone as one step after it runs"),
("edit.redo", "a macro is undone as one step after it runs"),
("view.newWindow", "macros don't open, hide or show windows"),
("view.hideWindow", "macros don't open, hide or show windows"),
("view.unhideWindow", "macros don't open, hide or show windows"),
];
/// Commands under a denied prefix that only change the workbook.
const ALLOWED: &[&str] = &["file.properties"];
/// Commands that read a file when given a `path`.
const READS_FILES: &[&str] = &["data.getData", "data.fromTextCsv", "insert.picture", "pageLayout.background"];
/// File commands that only produce bytes unless given a `path` (then they write a file).
const EXPORTS: &[&str] = &["file.exportPdf", "file.exportCsv", "file.exportHtml", "file.print", "file.printPreview", "file.saveBytes"];
/// Whether command `id` with `params` writes a file (an export or print given a `path`).
pub fn writes_file(id: &str, params: &Json) -> bool {
EXPORTS.contains(&id) && params.get("path").is_some_and(|p| !p.is_null())
}
/// The policy for running command `id` with `params` as a macro step in a document with `trust`.
///
/// Any `path` other than `null` counts as touching a file: a token such as `{"$ref": "Input"}`
/// becomes a path when the step runs, and exports write to `""` and `" "` as file names. The
/// runner checks the parameters both as written and as resolved.
pub fn macro_policy(id: &str, params: &Json, trust: Trust) -> Policy {
let has_path = params.get("path").is_some_and(|p| !p.is_null());
let base = if EXPORTS.contains(&id) {
if has_path { Policy::NeedsPermission("writes a file".into()) } else { Policy::Allow }
} else if ALLOWED.contains(&id) {
Policy::Allow
} else if let Some((_, why)) = DENIED.iter().find(|(d, _)| *d == id) {
Policy::Deny((*why).into())
} else if let Some((_, why)) = DENIED_PREFIXES.iter().find(|(p, _)| id.starts_with(p)) {
Policy::Deny((*why).into())
} else if READS_FILES.contains(&id) && has_path {
Policy::NeedsPermission("reads a file".into())
} else {
Policy::Allow
};
match base {
Policy::Allow if !trust.allows_code() => Policy::Ask("came with a workbook you opened".into()),
other => other,
}
}
/// Whether the footprint dry run can preview a command. Solver can take long, so a dry run stops
/// before it and reports the footprint as partial.
pub fn previewable(id: &str) -> bool {
id != "data.solver"
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn the_deny_and_permission_lists() {
let none = json!({});
let path = json!({"path": "/tmp/x"});
for id in [
"file.saveAs",
"file.save",
"file.open",
"file.close",
"file.new",
"app.commands",
"window.activate",
"automate.runScript",
"automate.recordActions",
"macro.run",
"macro.record",
"edit.undo",
"edit.redo",
"view.newWindow",
"view.hideWindow",
"view.unhideWindow",
] {
assert!(matches!(macro_policy(id, &none, Trust::Local), Policy::Deny(_)), "{id}");
assert!(matches!(macro_policy(id, &path, Trust::FromFile), Policy::Deny(_)), "{id}");
}
for id in ["data.getData", "data.fromTextCsv", "insert.picture", "pageLayout.background", "file.exportPdf", "file.print", "file.exportCsv"] {
assert!(matches!(macro_policy(id, &path, Trust::Local), Policy::NeedsPermission(_)), "{id}");
assert!(matches!(macro_policy(id, &path, Trust::FromFile), Policy::NeedsPermission(_)), "{id}");
}
// Without a path they touch no file.
for id in ["data.getData", "insert.picture", "file.exportPdf", "file.saveBytes", "file.printPreview"] {
assert_eq!(macro_policy(id, &none, Trust::Local), Policy::Allow, "{id}");
assert_eq!(macro_policy(id, &json!({"path": null}), Trust::Local), Policy::Allow, "{id}");
}
// A path that is blank or a token still touches a file once the step runs.
for path in [json!(""), json!(" "), json!({"$ref": "Input"}), json!("{{Input}}.csv"), json!(1)] {
for id in ["file.exportCsv", "data.fromTextCsv"] {
let p = json!({ "path": path });
assert!(matches!(macro_policy(id, &p, Trust::Local), Policy::NeedsPermission(_)), "{id} {p}");
}
}
for id in ["cell.set", "home.bold", "view.macros", "edit.copy", "file.properties"] {
assert_eq!(macro_policy(id, &none, Trust::Local), Policy::Allow, "{id}");
assert!(matches!(macro_policy(id, &none, Trust::FromFile), Policy::Ask(_)), "{id}");
}
assert!(!previewable("data.solver") && previewable("cell.set"));
}
#[test]
fn every_listed_command_exists() {
for id in ALLOWED.iter().chain(READS_FILES).chain(EXPORTS).chain(DENIED.iter().map(|(id, _)| id)).chain(&["data.solver"]) {
assert!(crate::find_command(id).is_some(), "{id}");
}
}
}
-528
View File
@@ -1,528 +0,0 @@
//! Turning a recording into a macro.
//!
//! The recording buffer (`crate::record`) holds the top-level commands run while recording, each
//! with the sheet and selection it ran in. When recording stops:
//!
//! - commands a macro may not run (saving, opening files…) and other macros' commands are left
//! out; a macro run while recording is replaced by its steps when it has no inputs, no output
//! and only plain commands (otherwise it is left out), and an Automate script run while
//! recording by the steps it ran, leaving out the steps a macro may not run; both are the steps
//! as they ran, kept with the recording ([`crate::record::Recorder::ran`]), and a run without
//! them is left out;
//! - a `sheet.activate` or `selection.set` step is added where the sheet or selection changed
//! between commands without one, and runs of `selection.set` collapse to the last;
//! - with relative references (the default), the selection when recording started is the example
//! input: references on its sheet in, next to or near it become tokens (`Input.col(2)`,
//! `Input.below`, `Input.cell(1,4)`), also inside formulas (`{{Input.col(2)}}`), and the input's
//! shape, header and column types are inferred from it as it was when recording started;
//! - recorded text that playback would read as a token stays text: a reference parameter that
//! reads as one (a defined name called `Output`) becomes `{"$text": …}`, and `{{` in a string
//! becomes `{{{{`.
use gridcraft_core::{CellRef, RangeRef, Value};
use gridcraft_model::{ColType, InputKind, MAX_MACRO_STEPS, Macro, NamedInput, Step, StepKind, Workbook};
use serde_json::{Map, Value as Json, json};
use std::collections::BTreeMap;
use super::policy::{Policy, macro_policy};
use crate::Selection;
use crate::record::RecordedStep;
/// How far outside the example input a reference may be and still be made relative to it.
const NEAR_ROWS: u32 = 50;
const NEAR_COLS: u32 = 26;
/// Where recording started, kept beside the buffer, with the name, shortcut and description the
/// macro is to be saved with (from the Record Macro dialog), applied when recording stops.
#[derive(Clone, Debug)]
pub struct RecordingStart {
pub doc: u64,
pub sheet: usize,
pub sheet_name: String,
pub selection: Selection,
pub relative: bool,
/// With relative references, the example input (the selection) as it was when recording
/// started: the recorded steps may change its values (text made into numbers), and the
/// macro is for data like the data it was recorded on.
pub input: Option<NamedInput>,
/// The name, shortcut and description given when recording started (Record Macro).
pub name: Option<String>,
pub shortcut: Option<String>,
pub description: Option<String>,
}
impl RecordingStart {
/// Recording starting now in `d`.
pub fn new(d: &crate::DocState, relative: bool) -> RecordingStart {
let sheet = d.wb.active_sheet();
RecordingStart {
doc: d.uid,
sheet,
sheet_name: d.wb.active().map(|sh| sh.name.clone()).unwrap_or_default(),
selection: d.selection.clone(),
relative,
input: relative.then(|| infer_input(&d.wb, sheet, d.selection.current())),
name: None,
shortcut: None,
description: None,
}
}
}
/// The steps a recording keeps for a run of `m`: its enabled command steps as written, when they
/// stand on their own (no inputs, no output and only plain commands; a Repeat, If or Stop means
/// nothing outside its macro). `None` when they don't, and the recording leaves the run out.
pub fn inline_steps(m: &Macro) -> Option<Vec<(String, Json)>> {
if !m.inputs.is_empty() || m.output.anchor.is_some() || !m.steps.iter().all(|x| matches!(x.kind, StepKind::Command { .. })) {
return None;
}
Some(
m.steps
.iter()
.filter(|x| x.enabled)
.filter_map(|x| match &x.kind {
StepKind::Command { id, params } => Some((id.clone(), params.clone())),
_ => None,
})
.collect(),
)
}
/// Where a gathered command came from, which says how its parameters are written.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Origin {
/// Run while recording: plain parameters, which may become relative.
Recorded,
/// A step of a recorded script run: plain parameters, kept as they are.
Script,
/// A step of a recorded macro run: macro parameters (tokens and all), kept as written.
Macro,
}
/// A recording turned into a macro, with what was left out.
pub struct Converted {
pub mac: Macro,
/// Commands left out (ids), in order.
pub skipped: Vec<String>,
/// Steps not kept: past the macro's step limit (a recorded run of a long macro adds all of
/// its steps) or recorded in another workbook.
pub cut: usize,
}
/// Builds a macro called `name` from the recorded steps.
/// `ran` is what a macro or script run ran, by step index ([`crate::record::Recorder::ran`]); a
/// run without an entry is left out and listed in `skipped`.
pub fn convert(name: &str, start: &RecordingStart, steps: &[RecordedStep], ran: &BTreeMap<usize, Vec<(String, Json)>>) -> Converted {
let mut skipped = Vec::new();
// Commands as they'll be played back: (id, params, sheet before, selection before, where
// they came from). At most the macro's step limit are gathered: a recording of a thousand
// runs of a long macro would otherwise expand to millions of steps before the limit cut them
// back.
let mut cmds: Vec<(String, Json, usize, String, Selection, Origin)> = Vec::new();
let mut cut = steps.iter().filter(|st| st.doc != start.doc).count();
let refused = |id: &str, params: &Json| matches!(macro_policy(id, params, crate::Trust::Local), Policy::Deny(_) | Policy::NeedsPermission(_));
let mut push = |cmds: &mut Vec<(String, Json, usize, String, Selection, Origin)>, cmd: (String, Json, usize, String, Selection, Origin)| {
if cmds.len() < MAX_MACRO_STEPS {
cmds.push(cmd);
} else {
cut = cut.saturating_add(1);
}
};
for (index, st) in steps.iter().enumerate().filter(|(_, st)| st.doc == start.doc) {
let id = st.id.as_str();
// Another macro's steps (when they stand on their own, [`inline_steps`]) or an Automate
// script's, as they ran: what the run kept with the recording. Without them the run is
// left out; the macro or script as it stands now may not be what ran.
let origin = match id {
"macro.run" => Some(Origin::Macro),
"automate.runScript" => Some(Origin::Script),
_ => None,
};
if let Some(origin) = origin {
match ran.get(&index) {
Some(steps) => {
for (cid, params) in steps {
if refused(cid, params) {
skipped.push(cid.clone());
continue;
}
push(&mut cmds, (cid.clone(), params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), origin));
}
}
None => skipped.push(id.to_string()),
}
continue;
}
if refused(id, &st.params) {
skipped.push(id.to_string());
continue;
}
push(&mut cmds, (id.to_string(), st.params.clone(), st.sheet, st.sheet_name.clone(), st.selection.clone(), Origin::Recorded));
}
// Sheet and selection changes made without a command: where a command ran on another sheet,
// or with another selection, than the one before it, say so (a command that moved the
// selection itself does it again when played back, so the extra step is harmless).
// Each step keeps the name of the sheet it runs on.
let mut out: Vec<(String, Json, String, Origin)> = Vec::new();
let (mut sheet, mut sel) = (start.sheet, start.selection.clone());
for (id, params, at_sheet, sheet_name, at_sel, origin) in cmds {
if at_sheet != sheet {
out.push(("sheet.activate".into(), json!({"sheet": sheet_name}), sheet_name.clone(), Origin::Recorded));
}
// A recorded selection.set just before already put the selection there.
let selected = matches!(out.last(), Some((last, ..)) if last == "selection.set");
if (at_sel != sel || at_sheet != sheet) && !selected {
out.push(("selection.set".into(), selection_params(&at_sel), sheet_name.clone(), Origin::Recorded));
}
(sheet, sel) = (at_sheet, at_sel);
out.push((id, params, sheet_name, origin));
}
// Runs of selection.set (or sheet.activate): the last one wins.
let mut collapsed: Vec<(String, Json, String, Origin)> = Vec::new();
for (id, params, on, origin) in out {
if matches!(id.as_str(), "selection.set" | "sheet.activate")
&& let Some((last, ..)) = collapsed.last()
&& *last == id
{
collapsed.pop();
}
collapsed.push((id, params, on, origin));
}
// Relative references. Only references on the input's sheet become tokens: on another sheet
// `C4` is that sheet's C4.
let example = start.selection.current();
let mut used_input = false;
let steps: Vec<Step> = collapsed
.into_iter()
.map(|(id, params, on, origin)| {
// A step that names another sheet with its `sheet` parameter runs there, whatever
// sheet was active: its unqualified references are that sheet's cells.
let names_other_sheet = params.get("sheet").is_some_and(|v| match v {
Json::String(n) => !n.eq_ignore_ascii_case(&start.sheet_name),
Json::Number(i) => i.as_u64() != Some(start.sheet as u64),
_ => false,
});
let here = on.eq_ignore_ascii_case(&start.sheet_name) && !names_other_sheet;
// Plain text first, so the tokens added next are the only ones.
let params = if origin == Origin::Macro { params } else { literal(&params, None) };
let params = if start.relative && origin == Origin::Recorded {
relative(&params, example, &start.sheet_name, here, &mut used_input, None)
} else {
params
};
Step::command(id, params)
})
.collect();
let mut mac = Macro { name: name.to_string(), steps, ..Macro::default() };
if start.relative && used_input {
mac.inputs = vec![start.input.clone().unwrap_or_default()];
}
Converted { mac, skipped, cut }
}
fn selection_params(sel: &Selection) -> Json {
let ranges: Vec<String> = sel.ranges.iter().map(RangeRef::a1).collect();
json!({"range": ranges.join(","), "active": sel.active.a1()})
}
/// Plain recorded `params` written so playback reads them as they are: a string in a reference
/// parameter that playback would read as a token, or that holds `{{`, becomes `{"$text": …}`,
/// and `{{` in any other string becomes `{{{{`.
fn literal(v: &Json, key: Option<&str>) -> Json {
match v {
Json::Object(o) => Json::Object(o.iter().map(|(k, x)| (k.clone(), literal(x, Some(k)))).collect()),
Json::Array(a) => Json::Array(a.iter().map(|x| literal(x, key)).collect()),
Json::String(t) if key.is_some_and(super::resolve::is_ref_key) && (t.contains("{{") || super::resolve::reads_as_token(t, &["Input"])) => {
json!({"$text": t})
}
Json::String(t) if t.contains("{{") => json!(t.replace("{{", "{{{{")),
other => other.clone(),
}
}
/// `params` with references near `example` (on `sheet`) made into tokens; `here` says whether the
/// step runs on that sheet, so its unqualified references are on it.
fn relative(v: &Json, example: RangeRef, sheet: &str, here: bool, used: &mut bool, key: Option<&str>) -> Json {
match v {
// Text kept as text ([`literal`]).
Json::Object(o) if o.len() == 1 && o.contains_key("$text") => v.clone(),
Json::Object(o) => {
// A selection of several areas stays as it is, its active cell with it: a token for
// the active cell alone would put it outside the areas on playback.
let several_areas = o.get("range").and_then(Json::as_str).is_some_and(|r| r.contains(','));
let mut out = Map::new();
for (k, x) in o {
let x = if several_areas && k == "active" { x.clone() } else { relative(x, example, sheet, here, used, Some(k)) };
out.insert(k.clone(), x);
}
Json::Object(out)
}
Json::Array(a) => Json::Array(a.iter().map(|x| relative(x, example, sheet, here, used, key)).collect()),
Json::String(t) if key.is_some_and(super::resolve::is_ref_key) => {
// One area becomes a token; a list of areas ("A1:B2,D4") stays as it is.
if t.contains(',') {
return v.clone();
}
match ref_token(t, example, sheet, here) {
Some(token) => {
*used = true;
json!(token)
}
None => v.clone(),
}
}
Json::String(t) if here && t.starts_with('=') && example.count() > 1 => {
let (f, changed) = formula_tokens(t, example);
*used |= changed;
json!(f)
}
other => other.clone(),
}
}
/// The token for an A1 reference on the example's sheet near the example input. `here` says whether
/// an unqualified reference is on that sheet.
fn ref_token(text: &str, example: RangeRef, sheet: &str, here: bool) -> Option<String> {
let (on, body) = crate::cmd::split_sheet(text.trim());
match on {
Some(on) if !on.eq_ignore_ascii_case(sheet) => return None,
None if !here => return None,
_ => {}
}
let t = RangeRef::parse(body)?;
if t.is_full_cols() || t.is_full_rows() {
return None;
}
token(t, example)
}
/// `t` as a token relative to `r`.
fn token(t: RangeRef, r: RangeRef) -> Option<String> {
if t == r {
return Some("Input".into());
}
let rel_row = |row: u32| i64::from(row) - i64::from(r.start.row) + 1;
let rel_col = |col: u32| i64::from(col) - i64::from(r.start.col) + 1;
let cols = |a: u32, b: u32| if a == b { format!("col({})", rel_col(a)) } else { format!("cols({}..{})", rel_col(a), rel_col(b)) };
if r.contains_range(&t) {
let full_rows = t.start.row == r.start.row && t.end.row == r.end.row;
let full_cols = t.start.col == r.start.col && t.end.col == r.end.col;
return Some(if full_rows {
format!("Input.{}", cols(t.start.col, t.end.col))
} else if full_cols && t.start.row == t.end.row {
format!("Input.row({})", rel_row(t.start.row))
} else if full_cols && t.end.row == r.end.row {
format!("Input.rows({}..)", rel_row(t.start.row))
} else if full_cols {
format!("Input.rows({}..{})", rel_row(t.start.row), rel_row(t.end.row))
} else if t.is_single() {
format!("Input.cell({},{})", rel_row(t.start.row), rel_col(t.start.col))
} else {
format!("Input.cell({},{}).resize({},{})", rel_row(t.start.row), rel_col(t.start.col), t.height(), t.width())
});
}
let near = t.start.row.saturating_add(NEAR_ROWS) >= r.start.row
&& t.end.row <= r.end.row.saturating_add(NEAR_ROWS)
&& t.start.col.saturating_add(NEAR_COLS) >= r.start.col
&& t.end.col <= r.end.col.saturating_add(NEAR_COLS);
if !near {
return None;
}
// Just below the input, in its columns: follows the input's length.
if t.start.row == r.end.row + 1 && t.start.col >= r.start.col && t.end.col <= r.end.col {
let base = if t.start.col == r.start.col && t.end.col == r.end.col {
"Input".to_string()
} else {
format!("Input.{}", cols(t.start.col, t.end.col))
};
return Some(if t.height() == 1 { format!("{base}.below") } else { format!("{base}.below({})", t.height()) });
}
// Just right of the input, in its rows: follows its width.
if t.start.col == r.end.col + 1 && t.start.row >= r.start.row && t.end.row <= r.end.row {
let base = if t.start.row == r.start.row && t.end.row == r.end.row {
"Input".to_string()
} else if t.start.row == t.end.row {
format!("Input.row({})", rel_row(t.start.row))
} else {
format!("Input.rows({}..{})", rel_row(t.start.row), rel_row(t.end.row))
};
return Some(if t.width() == 1 { format!("{base}.right") } else { format!("{base}.right({})", t.width()) });
}
let cell = format!("Input.cell({},{})", rel_row(t.start.row), rel_col(t.start.col));
Some(if t.is_single() { cell } else { format!("{cell}.resize({},{})", t.height(), t.width()) })
}
/// Replaces references in a formula that are exactly the example input, one of its columns or
/// rows, or a column without its first row, with `{{token}}`.
fn formula_tokens(f: &str, r: RangeRef) -> (String, bool) {
// Each reference in the formula text is read and matched on its own, so the work is the
// formula's length whatever the example's size (an example of a whole sheet has a million
// rows, which no list of candidates may grow with).
let mut out = String::with_capacity(f.len());
let mut changed = false;
let (mut i, mut in_text) = (0, false);
let bytes = f.as_bytes();
while i < f.len() {
let Some(rest) = f.get(i..) else { break };
let ch = rest.chars().next().unwrap_or(' ');
if ch == '"' {
in_text = !in_text;
}
// A reference right after `{` stays as it is: `{{` before the token would read as the
// escape of a literal `{{`.
let prev_ok = i == 0 || !bytes.get(i - 1).is_some_and(|b| b.is_ascii_alphanumeric() || matches!(b, b'$' | b'!' | b'_' | b'.' | b':' | b'{'));
if !in_text && prev_ok && ch.is_ascii_alphabetic() {
// The longest run of reference characters here: letters, digits and one `:`.
let n = rest.bytes().take_while(|b| b.is_ascii_alphanumeric() || *b == b':').count();
let text = rest.get(..n).unwrap_or("");
let followed = rest.get(n..).and_then(|x| x.chars().next()).is_some_and(|c| matches!(c, '!' | '(' | '_' | '$'));
if !followed
&& !text.contains('$')
&& let Some(t) = RangeRef::parse(text)
&& let Some(tok) = formula_token(t, r)
{
out.push_str("{{");
out.push_str(&tok);
out.push_str("}}");
i += n;
changed = true;
continue;
}
}
out.push(ch);
i += ch.len_utf8();
}
(out, changed)
}
/// The token for a reference inside a formula: exactly the example input, one of its columns or
/// rows, or a column without its first row. Other references stay as they are.
fn formula_token(t: RangeRef, r: RangeRef) -> Option<String> {
if t == r {
return Some("Input".into());
}
if !r.contains_range(&t) {
return None;
}
let col = usize::try_from(t.start.col.checked_sub(r.start.col)?).ok()?.checked_add(1)?;
let row = usize::try_from(t.start.row.checked_sub(r.start.row)?).ok()?.checked_add(1)?;
if t.start.col == t.end.col && t.start.row == r.start.row && t.end.row == r.end.row {
return Some(format!("Input.col({col})"));
}
if t.start.col == t.end.col && r.height() > 2 && t.start.row == r.start.row.checked_add(1)? && t.end.row == r.end.row {
return Some(format!("Input.rows(2..).col({col})"));
}
if r.width() > 1 && t.start.row == t.end.row && t.start.col == r.start.col && t.end.col == r.end.col {
return Some(format!("Input.row({row})"));
}
None
}
/// The example input's shape: a header when the first row is all text over other kinds of
/// values, and each column's type when all its values agree.
pub fn infer_input(wb: &Workbook, sheet: usize, r: RangeRef) -> NamedInput {
let mut input = NamedInput::default();
if r.is_single() {
input.kind = InputKind::Cell;
input.rows = [1, 1];
input.cols = [1, 1];
return input;
}
input.cols = [r.width(), r.width()];
let Some(sh) = wb.sheet(sheet) else { return input };
if r.count() > 100_000 {
return input;
}
let kind = |c: CellRef| -> Option<ColType> {
let v = sh.value(c);
Some(match v {
Value::Empty => return None,
Value::Text(t) if t.is_empty() => return None,
Value::Number(_) if crate::display::number_format(wb.styles.get(sh.style_id(c)).num_fmt.as_str()).is_date() => ColType::Date,
Value::Number(_) => ColType::Number,
Value::Text(_) => ColType::Text,
Value::Bool(_) => ColType::Bool,
_ => ColType::Any,
})
};
let column = |col: u32, from: u32| -> ColType {
let mut seen: Option<ColType> = None;
for row in from..=r.end.row {
match (seen, kind(CellRef::new(row, col))) {
(_, None) => {}
(None, k) => seen = k,
(Some(a), Some(b)) if a != b => return ColType::Any,
_ => {}
}
}
seen.unwrap_or(ColType::Any)
};
let first_text = (r.start.col..=r.end.col).all(|c| kind(CellRef::new(r.start.row, c)) == Some(ColType::Text));
let below = (r.start.col..=r.end.col).map(|c| column(c, r.start.row + 1)).collect::<Vec<_>>();
input.header = r.height() > 1 && first_text && below.iter().any(|k| !matches!(k, ColType::Text | ColType::Any));
input.col_types = if input.header { below } else { (r.start.col..=r.end.col).map(|c| column(c, r.start.row)).collect() };
if input.col_types.iter().all(|k| *k == ColType::Any) {
input.col_types.clear();
}
input.rows = [1, 0];
input
}
#[cfg(test)]
mod tests {
use super::*;
fn r(a1: &str) -> RangeRef {
RangeRef::parse(a1).unwrap()
}
#[test]
fn references_near_the_example_become_tokens() {
let ex = r("B2:D11");
for (t, want) in [
("B2:D11", Some("Input")),
("C2:C11", Some("Input.col(2)")),
("C2:D11", Some("Input.cols(2..3)")),
("B2:D2", Some("Input.row(1)")),
("B3:D11", Some("Input.rows(2..)")),
("B3:D5", Some("Input.rows(2..4)")),
("C4", Some("Input.cell(3,2)")),
("C4:D5", Some("Input.cell(3,2).resize(2,2)")),
("B12:D12", Some("Input.below")),
("C12", Some("Input.col(2).below")),
("C12:C13", Some("Input.col(2).below(2)")),
("E2:E11", Some("Input.right")),
("E5", Some("Input.row(4).right")),
("E3:F4", Some("Input.rows(2..3).right(2)")),
("G2", Some("Input.cell(1,6)")),
("A1", Some("Input.cell(0,0)")),
("Z500", None),
("A:A", None),
] {
assert_eq!(ref_token(t, ex, "Sheet1", true).as_deref(), want, "{t}");
}
assert_eq!(ref_token("Sheet1!C4", ex, "Sheet1", true).as_deref(), Some("Input.cell(3,2)"));
assert_eq!(ref_token("Other!C4", ex, "Sheet1", true), None);
// On another sheet only a reference qualified with the input's sheet is on it.
assert_eq!(ref_token("C4", ex, "Sheet1", false), None);
assert_eq!(ref_token("Sheet1!C4", ex, "Sheet1", false).as_deref(), Some("Input.cell(3,2)"));
}
#[test]
fn formulas_get_tokens_for_exact_references() {
let ex = r("B2:C5");
for (f, want) in [
("=SUM(C3:C5)", "=SUM({{Input.rows(2..).col(2)}})"),
("=SUM(C2:C5)+COUNT(B2:C5)", "=SUM({{Input.col(2)}})+COUNT({{Input}})"),
("=SUM($C$3:$C$5)", "=SUM($C$3:$C$5)"),
("=SUM(Other!C3:C5)", "=SUM(Other!C3:C5)"),
("=\"C3:C5\"&C3:C5", "=\"C3:C5\"&{{Input.rows(2..).col(2)}}"),
("=SUM(AC3:C5)", "=SUM(AC3:C5)"),
("=B2", "=B2"),
] {
assert_eq!(formula_tokens(f, ex).0, want, "{f}");
}
}
}
-967
View File
@@ -1,967 +0,0 @@
//! Macro tokens: references in step parameters that are worked out when the step runs, so a
//! macro made on one block of data works on another.
//!
//! A token is a base followed by methods:
//!
//! - bases: a declared input (`Input`, `Rates`), `Output` (the output rule's anchor), `Row` or
//! `Column` (the current item of the Repeat step around it), and `Name:Total` (a defined name or
//! a table's data; a name can hold periods, and the longest one that exists is used, so
//! `Name:Sales.Q1.cell(1,1)` is a cell of `Sales.Q1`);
//! - methods, 1-based and chainable: `.col(n)` or `.col("Amount")` (by an input's header row,
//! also after `.data`), `.row(n)`, `.cols(a..b)`, `.rows(a..)`, `.cell(r,c)` (relative to the
//! top-left cell; any whole numbers, so `cell(0,1)` is the cell above), `.offset(r,c)`,
//! `.resize(h,w)`, `.data` (without the header row), `.below(n)` and `.right(n)` (the n rows
//! below or columns right of the range).
//!
//! In parameters a token is written three ways: `{"$ref": "Input.col(2)"}` anywhere; the whole
//! string `"Input.col(2)"` in a reference parameter (`cell`, `range`, `target`, `to`, keys ending
//! in `Cell` or `Range`…); or `{{Input.col(2)}}` inside any string (`"=SUM({{Input.col(2)}})"`).
//! Each becomes A1 text before the command runs, so commands never see a token. Text that would
//! read as a token is written `{"$text": "Output"}`: it is passed as it is, never resolved or
//! interpolated (a defined name called `Output`, or `Dear {{name}}`). Plain A1
//! references in steps are absolute: they don't follow row inserts (as in VBA); a `Name:` anchor
//! does.
use gridcraft_core::{CellRef, MAX_COLS, MAX_ROWS, RangeRef};
use gridcraft_model::{RepeatOf, Workbook};
use serde_json::{Map, Value as Json};
/// A resolved reference: a block of cells on a sheet.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Place {
pub sheet: usize,
pub range: RangeRef,
}
/// Why a reference couldn't be worked out.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ResolveError {
/// It runs past the edge of the sheet.
Edge(String),
/// Anything else (an unknown input, a malformed token).
Bad(String),
}
impl ResolveError {
pub fn message(&self) -> &str {
match self {
ResolveError::Edge(m) | ResolveError::Bad(m) => m,
}
}
}
impl std::fmt::Display for ResolveError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.message())
}
}
type R<T> = Result<T, ResolveError>;
fn bad<T>(msg: impl Into<String>) -> R<T> {
Err(ResolveError::Bad(msg.into()))
}
/// What tokens refer to while a step runs.
#[derive(Clone, Copy, Debug)]
pub struct Env<'a> {
pub wb: &'a Workbook,
/// The sheet a reference without a sheet name is on.
pub active_sheet: usize,
/// Declared inputs, with whether each has a header row.
pub inputs: &'a [(String, Place, bool)],
pub output: Option<Place>,
/// The innermost Repeat step's current row or column.
pub item: Option<(RepeatOf, Place)>,
}
/// Parameter keys that hold references, where a whole-string token is resolved. Elsewhere a
/// string is text (a cell may hold "Row", a chart's type is "column"), and a token needs
/// `{"$ref": …}` or `{{…}}`.
const REF_KEYS: &[&str] = &[
"active",
"anchor",
"area",
"areas",
"at",
"cell",
"cells",
"changing",
"copyTo",
"destination",
"from",
"fromCell",
"location",
"objective",
"over",
"range",
"ranges",
"reference",
"resultCells",
"source",
"sources",
"target",
"to",
"variables",
];
/// Whether a parameter called `key` holds a reference.
pub fn is_ref_key(key: &str) -> bool {
let k = key.to_ascii_lowercase();
REF_KEYS.iter().any(|r| r.eq_ignore_ascii_case(key)) || ["cell", "cells", "range", "ranges", "ref"].iter().any(|end| k.ends_with(end))
}
/// Reference keys that other commands use for plain words: `to` is a replacement word in
/// Review › Change Spelling, `area` is `row` or `column` in a pivot field move. Under these a
/// bare `Row` or `Column` stays text; `Row.col(2)`, `{{Row}}` or `{"$ref": "Row"}` is the token.
fn is_general_key(key: &str) -> bool {
["area", "areas", "at", "destination", "from", "location", "source", "sources", "target", "to"].iter().any(|k| k.eq_ignore_ascii_case(key))
}
/// `Row` or `Column` on its own, with no method or sheet after it.
fn is_bare_item_word(text: &str) -> bool {
let t = text.trim();
t.eq_ignore_ascii_case("row") || t.eq_ignore_ascii_case("column")
}
/// The most nesting [`resolve_params`] walks into.
const MAX_JSON_DEPTH: usize = 64;
/// Whether `text` starts like a token (its base is one this environment knows). A reference on a
/// sheet with such a name (`Output!A1`) is not a token.
pub fn is_token(text: &str, env: &Env) -> bool {
starts_like_token(text, |base| env.inputs.iter().any(|(n, ..)| n.eq_ignore_ascii_case(base)))
}
/// Whether `text` would read as a token in a macro whose inputs are `inputs` (see [`is_token`]).
pub fn reads_as_token(text: &str, inputs: &[&str]) -> bool {
starts_like_token(text, |base| inputs.iter().any(|n| n.eq_ignore_ascii_case(base)))
}
fn starts_like_token(text: &str, is_input: impl Fn(&str) -> bool) -> bool {
let t = text.trim();
if t.get(..5).is_some_and(|h| h.eq_ignore_ascii_case("name:")) {
return true;
}
let n: usize = t.chars().take_while(|c| c.is_alphanumeric() || *c == '_').map(char::len_utf8).sum();
let base = t.get(..n).unwrap_or("");
if t.get(n..).unwrap_or("").trim_start().starts_with('!') {
return false;
}
is_builtin(base) || is_input(base)
}
fn is_builtin(base: &str) -> bool {
["Output", "Row", "Column"].iter().any(|b| b.eq_ignore_ascii_case(base))
}
/// Resolves a token, or a plain reference (`B2`, `Sheet2!A1:C3`, a defined name).
pub fn resolve_ref(text: &str, env: &Env) -> R<Place> {
let t = text.trim();
if is_token(t, env) {
return resolve_token(t, env);
}
plain_ref(t, env)
}
/// `A1`, `'My Sheet'!A1:B2` or a defined name or table.
fn plain_ref(t: &str, env: &Env) -> R<Place> {
let (sheet, body) = crate::cmd::split_sheet(t);
let si = match &sheet {
Some(name) => env.wb.sheet_index(name).ok_or_else(|| ResolveError::Bad(format!("There is no sheet named '{name}'.")))?,
None => env.active_sheet,
};
if let Some(range) = RangeRef::parse(body) {
return Ok(Place { sheet: si, range });
}
if sheet.is_none() {
return named(t, env);
}
bad(format!("'{t}' isn't a reference."))
}
/// A defined name (by its formula) or a table (its data rows).
fn named(name: &str, env: &Env) -> R<Place> {
if let Some(n) = env.wb.name(name, env.active_sheet) {
// The sheet part keeps its characters (a sheet may be called `US$`); only the reference
// loses its `$` anchors.
let f = n.formula.trim().trim_start_matches('=');
let (sheet, body) = crate::cmd::split_sheet(f);
let si = match sheet {
Some(s) => env.wb.sheet_index(&s),
None => n.scope.or(Some(env.active_sheet)),
};
if let (Some(sheet), Some(range)) = (si, RangeRef::parse(&body.replace('$', ""))) {
return Ok(Place { sheet, range });
}
return bad(format!("The name '{name}' doesn't refer to a range."));
}
if let Some((si, ti)) = env.wb.table(name)
&& let Some(t) = env.wb.sheet(si).and_then(|sh| sh.tables.get(ti))
{
// A table stands for its data rows; one with none (a header, perhaps a totals row) has
// nothing to give.
let Some(range) = t.data_range() else {
return bad(format!("The table '{name}' has no data rows."));
};
return Ok(Place { sheet: si, range });
}
bad(format!("There is no name or table called '{name}'."))
}
/// Token method names.
const METHODS: &[&str] = &["col", "row", "cols", "rows", "cell", "offset", "resize", "data", "below", "right"];
/// Where the name ends in `run`, the text after `Name:` up to the first character a name can't
/// hold: at the end or at a period, the longest choice that is a defined name or table. When
/// none is, the name ends before the first method (`Nope.cell`), so the error names it.
fn name_end(run: &str, env: &Env) -> usize {
// One pass over the names and tables, as a single lookup makes.
let fits = |name: &str| {
!name.is_empty()
&& run.get(..name.len()).is_some_and(|head| head.eq_ignore_ascii_case(name))
&& run.get(name.len()..).is_some_and(|rest| rest.is_empty() || rest.starts_with('.'))
};
let names = env.wb.names.iter().filter(|n| n.scope.is_none() || n.scope == Some(env.active_sheet)).map(|n| n.name.as_str());
let tables = env.wb.sheets.iter().flat_map(|sh| sh.tables.iter().map(|t| t.name.as_str()));
if let Some(end) = names.chain(tables).filter(|n| fits(n)).map(str::len).max() {
return end;
}
run.match_indices('.')
.map(|(i, _)| i)
.find(|&i| {
let next = run.get(i + 1..).unwrap_or("").split('.').next().unwrap_or("");
METHODS.iter().any(|m| m.eq_ignore_ascii_case(next))
})
.unwrap_or(run.len())
}
/// Resolves a token (see the module docs).
pub fn resolve_token(text: &str, env: &Env) -> R<Place> {
let t = text.trim();
let mut p = Parser { s: t, at: 0 };
let (mut place, mut header) = p.base(env)?;
// The row of column names `col("…")` reads: the input's header row, kept through `data`.
let mut names_row = header.then_some(place.range.start.row);
while p.eat('.') {
let name = p.ident();
if name.is_empty() {
return bad(format!("'{t}': expected a method name after '.'."));
}
let args = if p.eat('(') { p.args(t)? } else { vec![] };
let r = place.range;
let method = name.to_ascii_lowercase();
let range = match (method.as_str(), args.as_slice()) {
("col", [Arg::Int(n)]) => sub(r, None, Some((*n, *n)), t)?,
("col", [Arg::Text(h)]) => {
let Some(row) = names_row else {
return bad(format!("'{t}': col(\"{h}\") needs an input with a header row; use col(n)."));
};
let c = header_col(env, place, row, h).ok_or_else(|| ResolveError::Bad(format!("'{t}': no column is headed '{h}'.")))?;
sub(r, None, Some((c, c)), t)?
}
("row", [Arg::Int(n)]) => sub(r, Some((*n, *n)), None, t)?,
("cols", [Arg::Span(a, b)]) => sub(r, None, Some(span(*a, *b, i64::from(r.width()))), t)?,
("rows", [Arg::Span(a, b)]) => sub(r, Some(span(*a, *b, i64::from(r.height()))), None, t)?,
("cell", [Arg::Int(row), Arg::Int(col)]) => {
let c = shift(r.start, row - 1, col - 1, t)?;
RangeRef::cell(c)
}
("offset", [Arg::Int(dr), Arg::Int(dc)]) => RangeRef::new(shift(r.start, *dr, *dc, t)?, shift(r.end, *dr, *dc, t)?),
("resize", [Arg::Int(h), Arg::Int(w)]) => {
if *h < 1 || *w < 1 {
return bad(format!("'{t}': resize needs at least 1 row and 1 column."));
}
RangeRef::new(r.start, shift(r.start, h - 1, w - 1, t)?)
}
("data", []) => {
if header {
// A header row with nothing under it has no data rows to give.
if r.height() < 2 {
return bad(format!("'{t}': the range is only a header row, with no data under it."));
}
RangeRef::new(CellRef::new(r.start.row + 1, r.start.col), r.end)
} else {
r
}
}
("below", []) | ("below", [Arg::Int(_)]) => {
let n = count_arg(&args, t)?;
let top = shift(CellRef::new(r.end.row, r.start.col), 1, 0, t)?;
RangeRef::new(top, shift(CellRef::new(r.end.row, r.end.col), n, 0, t)?)
}
("right", []) | ("right", [Arg::Int(_)]) => {
let n = count_arg(&args, t)?;
let left = shift(CellRef::new(r.start.row, r.end.col), 0, 1, t)?;
RangeRef::new(left, shift(r.end, 0, n, t)?)
}
_ => {
return bad(format!("'{t}': '{name}' with these arguments isn't a token method ({}).", METHODS.join(", ")));
}
};
// After a method the result is plain cells, except that `col` and `cols` keep the input's top
// row, so a later `data` still takes the header off; only `data` keeps the header row for
// `col("…")`.
if !matches!(method.as_str(), "col" | "cols") {
header = false;
}
if method != "data" {
names_row = None;
}
place = Place { sheet: place.sheet, range };
}
p.skip_ws();
if p.at < p.s.len() {
return bad(format!("'{t}' isn't a token: unexpected '{}'.", p.s.get(p.at..).unwrap_or("")));
}
Ok(place)
}
/// The count `below(n)` and `right(n)` take: 1 when left out, and never less than 1.
fn count_arg(args: &[Arg], t: &str) -> R<i64> {
match args.first() {
Some(Arg::Int(n)) if *n < 1 => bad(format!("'{t}': the count must be at least 1.")),
Some(Arg::Int(n)) => Ok(*n),
_ => Ok(1),
}
}
/// `a..b` with open ends filled from 1 and `len`.
fn span(a: Option<i64>, b: Option<i64>, len: i64) -> (i64, i64) {
(a.unwrap_or(1), b.unwrap_or(len))
}
/// Rows `rows` and columns `cols` (1-based, inclusive) of `r`; `None` keeps them all.
fn sub(r: RangeRef, rows: Option<(i64, i64)>, cols: Option<(i64, i64)>, t: &str) -> R<RangeRef> {
let (h, w) = (i64::from(r.height()), i64::from(r.width()));
let (r0, r1) = rows.unwrap_or((1, h));
let (c0, c1) = cols.unwrap_or((1, w));
if r0 < 1 || c0 < 1 || r1 < r0 || c1 < c0 || r1 > h || c1 > w {
return bad(format!("'{t}' is outside the range it starts from ({} rows by {} columns).", h, w));
}
Ok(RangeRef::new(shift(r.start, r0 - 1, c0 - 1, t)?, shift(r.start, r1 - 1, c1 - 1, t)?))
}
fn shift(c: CellRef, dr: i64, dc: i64, t: &str) -> R<CellRef> {
let row = i64::from(c.row).checked_add(dr);
let col = i64::from(c.col).checked_add(dc);
match (row, col) {
(Some(row), Some(col)) if (0..i64::from(MAX_ROWS)).contains(&row) && (0..i64::from(MAX_COLS)).contains(&col) => {
Ok(CellRef::new(row as u32, col as u32))
}
(Some(row), Some(_)) if row < 0 => Err(ResolveError::Edge(format!("'{t}' runs past the top of the sheet."))),
(Some(row), Some(_)) if row >= i64::from(MAX_ROWS) => Err(ResolveError::Edge(format!("'{t}' runs past the last row of the sheet."))),
_ => Err(ResolveError::Edge(format!("'{t}' runs past the edge of the sheet."))),
}
}
/// The 1-based column of `place` whose header (in sheet row `row`) reads `h`.
fn header_col(env: &Env, place: Place, row: u32, h: &str) -> Option<i64> {
let sh = env.wb.sheet(place.sheet)?;
let r = place.range;
(r.start.col..=r.end.col)
// The header's value, however the sheet shows it: with Show Formulas on, the cell's
// display text is its formula.
.position(|c| crate::display::value_text(env.wb, sh, CellRef::new(row, c)).trim().eq_ignore_ascii_case(h.trim()))
.map(|i| i as i64 + 1)
}
#[derive(Debug)]
enum Arg {
Int(i64),
Span(Option<i64>, Option<i64>),
Text(String),
}
struct Parser<'a> {
s: &'a str,
at: usize,
}
impl Parser<'_> {
fn rest(&self) -> &str {
self.s.get(self.at..).unwrap_or("")
}
fn skip_ws(&mut self) {
let n = self.rest().len() - self.rest().trim_start().len();
self.at += n;
}
fn eat(&mut self, c: char) -> bool {
self.skip_ws();
if self.rest().starts_with(c) {
self.at += c.len_utf8();
true
} else {
false
}
}
fn ident(&mut self) -> String {
self.skip_ws();
let n: usize = self.rest().chars().take_while(|c| c.is_alphanumeric() || *c == '_').map(char::len_utf8).sum();
let out = self.rest().get(..n).unwrap_or("").to_string();
self.at += n;
out
}
fn int(&mut self) -> Option<i64> {
self.skip_ws();
let neg = self.rest().starts_with('-');
let start = self.at + usize::from(neg);
let digits: usize = self.s.get(start..).unwrap_or("").chars().take_while(char::is_ascii_digit).count();
if digits == 0 || digits > 9 {
return None;
}
let n: i64 = self.s.get(start..start + digits)?.parse().ok()?;
self.at = start + digits;
Some(if neg { -n } else { n })
}
/// The base of a token and whether it has a header row.
fn base(&mut self, env: &Env) -> R<(Place, bool)> {
self.skip_ws();
if self.rest().get(..5).is_some_and(|h| h.eq_ignore_ascii_case("name:")) {
self.at += 5;
// A name can hold periods (`Sales.Q1`), so it is the longest part of the run, up to
// a period or the end, that names something; methods follow it.
let n: usize = self.rest().chars().take_while(|c| c.is_alphanumeric() || matches!(c, '_' | '\\' | '.')).map(char::len_utf8).sum();
let run = self.rest().get(..n).unwrap_or("").to_string();
let end = name_end(&run, env);
let name = run.get(..end).unwrap_or("");
if name.is_empty() {
return bad("'Name:' needs a defined name after it.");
}
self.at += end;
return Ok((named(name, env)?, false));
}
let base = self.ident();
if base.eq_ignore_ascii_case("Output") {
return env.output.map(|p| (p, false)).ok_or_else(|| ResolveError::Bad("'Output' is used, but the macro has no output anchor.".into()));
}
if base.eq_ignore_ascii_case("Row") || base.eq_ignore_ascii_case("Column") {
return match env.item {
Some((_, p)) => Ok((p, false)),
None => bad(format!("'{base}' only means something inside a Repeat step.")),
};
}
match env.inputs.iter().find(|(n, ..)| n.eq_ignore_ascii_case(&base)) {
Some((_, p, header)) => Ok((*p, *header)),
None => bad(format!("'{base}' isn't an input of this macro.")),
}
}
fn args(&mut self, t: &str) -> R<Vec<Arg>> {
let mut out = Vec::new();
if self.eat(')') {
return Ok(out);
}
loop {
self.skip_ws();
if self.rest().starts_with('"') {
self.at += 1;
let n = self.rest().find('"').ok_or_else(|| ResolveError::Bad(format!("'{t}': a quote isn't closed.")))?;
out.push(Arg::Text(self.rest().get(..n).unwrap_or("").to_string()));
self.at += n + 1;
} else {
let a = self.int();
self.skip_ws();
if self.rest().starts_with("..") {
self.at += 2;
let b = self.int();
out.push(Arg::Span(a, b));
} else {
out.push(Arg::Int(a.ok_or_else(|| ResolveError::Bad(format!("'{t}': expected a whole number.")))?));
}
}
if out.len() > 4 {
return bad(format!("'{t}': too many arguments."));
}
if self.eat(')') {
return Ok(out);
}
if !self.eat(',') {
return bad(format!("'{t}': expected ',' or ')'."));
}
}
}
}
/// `place` as A1 text, with the sheet name when it isn't the active sheet.
pub fn a1(place: Place, env: &Env) -> String {
if place.sheet == env.active_sheet {
return place.range.a1();
}
let name = env.wb.sheet(place.sheet).map(|s| s.name.as_str()).unwrap_or("");
format!("{}!{}", quote_sheet(name), place.range.a1())
}
/// `Sheet1` or `'My Sheet'` (quotes doubled).
pub fn quote_sheet(name: &str) -> String {
// The engine's one rule: a name that reads as a cell reference, an R1C1 token or a boolean
// is quoted too, or `Q1!A1` would be read as a reference to cell Q1.
gridcraft_core::quote_sheet(name)
}
/// Step parameters with every token replaced by A1 text.
pub fn resolve_params(params: &Json, env: &Env) -> R<Json> {
walk(params, None, env, 0)
}
fn walk(v: &Json, key: Option<&str>, env: &Env, depth: usize) -> R<Json> {
if depth > MAX_JSON_DEPTH {
return bad("The step's parameters nest too deeply.");
}
Ok(match v {
Json::Object(o) => {
if o.len() == 1
&& let Some(Json::String(t)) = o.get("$ref")
{
return Ok(Json::String(a1(resolve_ref(t, env)?, env)));
}
// Text passed as it is: never a token, and no `{{…}}` in it is read.
if o.len() == 1
&& let Some(Json::String(t)) = o.get("$text")
{
return Ok(Json::String(t.clone()));
}
let mut out = Map::new();
for (k, x) in o {
out.insert(k.clone(), walk(x, Some(k), env, depth + 1)?);
}
Json::Object(out)
}
// Items of a list keep their key's kind (`ranges: ["Input", …]`).
Json::Array(a) => Json::Array(a.iter().map(|x| walk(x, key, env, depth + 1)).collect::<R<Vec<_>>>()?),
Json::String(s) => {
if key.is_some_and(|k| is_ref_key(k) && !(is_general_key(k) && is_bare_item_word(s))) && is_token(s, env) {
Json::String(a1(resolve_token(s, env)?, env))
} else {
Json::String(interpolate(s, env)?)
}
}
other => other.clone(),
})
}
/// Replaces each `{{token}}` in `s`. `{{{{` is a literal `{{`, for text that needs the braces
/// themselves (`"Dear {{{{name}}"` writes `Dear {{name}}`).
pub fn interpolate(s: &str, env: &Env) -> R<String> {
if !s.contains("{{") {
return Ok(s.to_string());
}
let mut out = String::with_capacity(s.len());
let mut rest = s;
while let Some(i) = rest.find("{{") {
out.push_str(rest.get(..i).unwrap_or(""));
if rest.get(i..i + 4) == Some("{{{{") {
out.push_str("{{");
rest = rest.get(i + 4..).unwrap_or("");
continue;
}
let after = rest.get(i + 2..).unwrap_or("");
let Some(j) = after.find("}}") else {
out.push_str(rest.get(i..).unwrap_or(""));
return Ok(out);
};
let token = after.get(..j).unwrap_or("");
out.push_str(&a1(resolve_ref(token, env)?, env));
rest = after.get(j + 2..).unwrap_or("");
}
out.push_str(rest);
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn wb() -> Workbook {
let mut wb = Workbook::new();
wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("My Data")));
let sh = wb.sheet_mut(0).unwrap();
for (i, h) in ["Name", "Amount", "Date"].iter().enumerate() {
sh.set_value(CellRef::new(1, 1 + i as u32), gridcraft_core::Value::text(*h));
}
wb.names.push(gridcraft_model::DefinedName {
name: "Total".into(),
scope: None,
formula: "Sheet1!$F$20".into(),
comment: String::new(),
hidden: false,
});
wb.names.push(gridcraft_model::DefinedName {
name: "Rates".into(),
scope: None,
formula: "'My Data'!$A$1:$A$5".into(),
comment: String::new(),
hidden: false,
});
wb
}
fn place(a1: &str) -> Place {
Place { sheet: 0, range: RangeRef::parse(a1).unwrap() }
}
fn check(env: &Env, token: &str, want: &str) {
match resolve_ref(token, env) {
Ok(p) => assert_eq!(a1(p, env), want, "{token}"),
Err(e) => panic!("{token}: {e}"),
}
}
#[test]
fn a_formula_header_matches_while_formulas_are_shown() {
// The header was read through the cell's display text, which is the formula while Show
// Formulas is on, so `col("Amount")` found no column headed by a formula.
let mut wb = wb();
let sh = wb.sheet_mut(0).unwrap();
let formula = Some(std::sync::Arc::new(gridcraft_model::Formula::new("\"Amo\"&\"unt\"")));
sh.set_cell(CellRef::new(1, 2), gridcraft_model::Cell { value: gridcraft_core::Value::text("Amount"), formula, style: Default::default() });
sh.show_formulas = true;
let inputs = vec![("Input".to_string(), place("B2:D11"), true)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None };
check(&env, "Input.col(\"Amount\")", "C2:C11");
}
#[test]
fn every_token_resolves() {
let wb = wb();
// Input B2:D11 with a header row; Other on another sheet.
let inputs = vec![
("Input".to_string(), place("B2:D11"), true),
("Other".to_string(), Place { sheet: 1, range: RangeRef::parse("C3:C4").unwrap() }, false),
];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: Some(place("G2")), item: Some((RepeatOf::Rows, place("B5:D5"))) };
for (token, want) in [
("Input", "B2:D11"),
("input", "B2:D11"),
("Input.col(2)", "C2:C11"),
("Input.col(\"Amount\")", "C2:C11"),
("Input.col(\"date\").data", "D3:D11"),
("Input.cols(2..3).data", "C3:D11"),
("Input.col(2).data.col(1)", "C3:C11"),
("Input.data", "B3:D11"),
("Input.data.col(2)", "C3:C11"),
("Input.data.col(\"Amount\")", "C3:C11"),
("Input.row(1)", "B2:D2"),
("Input.rows(2..)", "B3:D11"),
("Input.rows(2..4)", "B3:D5"),
("Input.rows(..2)", "B2:D3"),
("Input.cols(2..3)", "C2:D11"),
("Input.cell(1,1)", "B2"),
("Input.cell(3,2)", "C4"),
("Input.cell(0,1)", "B1"),
("Input.cell(12,3)", "D13"),
("Input.offset(0,4)", "F2:H11"),
("Input.offset(-1,-1)", "A1:C10"),
("Input.cell(1,1).resize(2,3)", "B2:D3"),
("Input.below()", "B12:D12"),
("Input.below(2)", "B12:D13"),
("Input.col(2).below", "C12"),
("Input.right", "E2:E11"),
(" Input . col( 2 ) ", "C2:C11"),
("Output", "G2"),
("Output.offset(1,0)", "G3"),
("Row", "B5:D5"),
("Row.cell(1,2)", "C5"),
("Column", "B5:D5"),
("Name:Total", "F20"),
("Name:Rates", "'My Data'!A1:A5"),
("Name:Rates.cell(2,1)", "'My Data'!A2"),
("Other", "'My Data'!C3:C4"),
("B7", "B7"),
("'My Data'!A1:B2", "'My Data'!A1:B2"),
("Total", "F20"),
] {
check(&env, token, want);
}
}
#[test]
fn bad_tokens_say_why() {
let wb = wb();
// The same cells, read with and without a header row.
let inputs = vec![("Input".to_string(), place("B2:D11"), false), ("Headed".to_string(), place("B2:D11"), true)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None };
for (token, needle, edge) in [
("Input.col(4)", "outside the range", false),
("Input.col(0)", "outside the range", false),
("Input.rows(5..2)", "outside the range", false),
("Headed.col(\"Nope\")", "no column is headed", false),
("Input.col(\"Name\")", "needs an input with a header row", false),
("Headed.col(2).col(\"Amount\")", "needs an input with a header row", false),
("Name:Rates.col(\"Name\")", "needs an input with a header row", false),
("Input.colour(1)", "isn't a token method", false),
("Input.col(1", "expected", false),
("Input.col(x)", "whole number", false),
("Input.col(99999999999)", "whole number", false),
("Input.", "method name", false),
("Input junk", "unexpected", false),
("Input.resize(0,1)", "at least 1 row", false),
("Output", "no output anchor", false),
("Row", "inside a Repeat", false),
("Name:", "needs a defined name", false),
("Name:Nothing", "no name or table", false),
("Nowhere!A1", "no sheet named", false),
("Input.offset(-2,0)", "past the top", true),
("Input.offset(0,-2)", "edge of the sheet", true),
("Input.offset(1048570,0)", "past the last row", true),
("Input.cell(1,16384)", "edge of the sheet", true),
] {
let e = resolve_ref(token, &env).unwrap_err();
assert!(e.message().contains(needle), "{token}: {e}");
assert_eq!(matches!(e, ResolveError::Edge(_)), edge, "{token}");
}
}
#[test]
fn params_resolve_tokens_in_reference_keys_and_braces_anywhere() {
let wb = wb();
let inputs = vec![("Input".to_string(), place("B2:D11"), true)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: Some(place("F2")), item: None };
let p = json!({
"cell": "Output",
"range": "Input.col(2)",
"ranges": ["Input.row(1)", "A1"],
"input": "Row",
"value": "Input",
"formula": "=SUM({{Input.data.col(2)}})/{{Name:Total}}",
"nested": {"target": {"$ref": "Input.cell(1,1)"}, "n": 3, "flag": true},
"text": {"$ref": "Output"},
"type": "column",
"sheet": "Input",
"rowInputCell": "Input.cell(1,1)",
"unclosed": "a {{b"
});
let r = resolve_params(&p, &env).unwrap();
assert_eq!(
r,
json!({
"cell": "F2",
"range": "C2:C11",
"ranges": ["B2:D2", "A1"],
"input": "Row",
"value": "Input",
"formula": "=SUM(C3:C11)/F20",
"nested": {"target": "B2", "n": 3, "flag": true},
"text": "F2",
"type": "column",
"sheet": "Input",
"rowInputCell": "B2",
"unclosed": "a {{b"
})
);
// Deeply nested parameters are refused, not followed.
let mut deep = json!(1);
for _ in 0..100 {
deep = json!([deep]);
}
assert!(resolve_params(&deep, &env).is_err());
assert!(resolve_params(&json!({"cell": "Input.col(9)"}), &env).is_err());
}
#[test]
fn references_on_sheets_named_like_tokens_stay_references() {
// Sheets named after the built-in bases and after an input: `Output!A1` is a cell on the
// sheet Output, in a reference key, in `$ref` and in `{{…}}`.
let mut wb = Workbook::new();
for name in ["Output", "Row", "Column", "Input", "Rates"] {
wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new(name)));
}
let inputs = vec![("Input".to_string(), place("B2:D11"), true), ("Rates".to_string(), place("F2:F5"), false)];
for output in [None, Some(place("H2"))] {
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output, item: Some((RepeatOf::Rows, place("B5:D5"))) };
for (sheet, i) in [("Output", 1), ("Row", 2), ("Column", 3), ("Input", 4), ("Rates", 5)] {
for text in [format!("{sheet}!A1:B2"), format!("'{sheet}'!A1:B2")] {
let want = Place { sheet: i, range: RangeRef::parse("A1:B2").unwrap() };
assert_eq!(resolve_ref(&text, &env), Ok(want), "{text}");
let a = a1(want, &env);
let p = json!({"range": text, "target": {"$ref": text}, "formula": format!("=SUM({{{{{text}}}}})")});
let r = resolve_params(&p, &env).unwrap();
// A plain reference in a reference key is passed on as written.
assert_eq!(r, json!({"range": text, "target": a, "formula": format!("=SUM({a})")}), "{text}");
}
}
assert!(!is_token("Output !A1", &env));
// The bare names are still tokens.
check(&env, "Input.col(2)", "C2:C11");
check(&env, "Rates", "F2:F5");
check(&env, "Row", "B5:D5");
}
}
#[test]
fn defined_names_can_hold_periods() {
// `Sales.Q1` is one defined name, beside `Sales`; `Data.row` has a segment that reads like
// a method. The longest name that exists is used, and methods follow it.
let mut wb = wb();
for (name, formula) in [("Sales", "Sheet1!$A$1"), ("Sales.Q1", "Sheet1!$B$2:$C$5"), ("Data.row", "'My Data'!$E$1:$E$3")] {
wb.names.push(gridcraft_model::DefinedName {
name: name.into(),
scope: None,
formula: formula.into(),
comment: String::new(),
hidden: false,
});
}
let env = Env { wb: &wb, active_sheet: 0, inputs: &[], output: None, item: None };
for (token, want) in [
("Name:Sales.Q1", "B2:C5"),
("Name:sales.q1", "B2:C5"),
("Name:Sales.Q1.cell(1,1)", "B2"),
("Name:Sales.Q1.col(2).row(4)", "C5"),
("Name:Sales", "A1"),
("Name:Sales.offset(1,0)", "A2"),
("Name:Data.row", "'My Data'!E1:E3"),
("Name:Data.row.row(2)", "'My Data'!E2"),
("Name:Data.row.cell(1,1)", "'My Data'!E1"),
("Sales.Q1", "B2:C5"),
] {
check(&env, token, want);
}
// In a reference parameter, `$ref` and `{{…}}`, with and without a method.
let p = json!({
"range": "Name:Sales.Q1",
"cell": "Name:Sales.Q1.cell(1,1)",
"a": {"$ref": "Name:Sales.Q1"},
"b": {"$ref": "Name:Sales.Q1.cell(2,2)"},
"formula": "=SUM({{Name:Sales.Q1}})+{{Name:Sales.Q1.cell(1,2)}}"
});
let r = resolve_params(&p, &env).unwrap();
assert_eq!(r, json!({"range": "B2:C5", "cell": "B2", "a": "B2:C5", "b": "C3", "formula": "=SUM(B2:C5)+C2"}));
// Unknown names and bad methods still fail, naming what is wrong.
for (token, needle) in [
("Name:Nope.Q1", "no name or table called 'Nope.Q1'"),
("Name:Nope.cell(1,1)", "no name or table called 'Nope'"),
("Name:Sales.Q1.bogus(1)", "'bogus' with these arguments isn't a token method"),
("Name:Sales.Q1.cell(1", "expected"),
("Name:Sales.Q1.", "method name"),
("Name:.cell(1,1)", "needs a defined name"),
] {
let e = resolve_ref(token, &env).unwrap_err();
assert!(e.message().contains(needle), "{token}: {e}");
let e = resolve_params(&json!({"formula": format!("={{{{{token}}}}}")}), &env).unwrap_err();
assert!(e.message().contains(needle), "{token} in braces: {e}");
}
}
#[test]
fn sheet_names_are_quoted_when_needed() {
// The engine's rule: a name that reads as a cell reference, an R1C1 token or a boolean
// is quoted too, and a period on its own is not a reason to quote.
for (name, want) in [
("Sheet1", "Sheet1"),
("My Data", "'My Data'"),
("O'Hara", "'O''Hara'"),
("2024", "'2024'"),
("", "''"),
("Q1", "'Q1'"),
("R1C1", "'R1C1'"),
("TRUE", "'TRUE'"),
("Sales.Q1", "Sales.Q1"),
] {
assert_eq!(quote_sheet(name), want, "{name}");
}
}
#[test]
fn a_bare_row_or_column_under_a_general_key_is_text() {
// `to` is a replacement word in Change Spelling and `area` is `row` or `column` in a
// pivot field move, so a bare `Row` or `Column` there stays text, inside a Repeat or not;
// the token forms still resolve there, and the bare word resolves under `range`.
let wb = wb();
let inputs = vec![("Input".to_string(), place("B2:D11"), true)];
for item in [None, Some((RepeatOf::Rows, place("B5:D5")))] {
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: Some(place("F2")), item };
let p = json!({"word": "Colum", "to": "Column", "area": "row", "from": "Input", "at": "Output"});
let r = resolve_params(&p, &env).unwrap();
assert_eq!(r, json!({"word": "Colum", "to": "Column", "area": "row", "from": "B2:D11", "at": "F2"}), "{item:?}");
}
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: Some((RepeatOf::Rows, place("B5:D5"))) };
let p = json!({"to": "Row.col(2)", "range": "Row", "area": {"$ref": "Row"}, "text": "{{Row}}"});
let r = resolve_params(&p, &env).unwrap();
assert_eq!(r, json!({"to": "C5", "range": "B5:D5", "area": "B5:D5", "text": "B5:D5"}));
}
#[test]
fn a_header_row_alone_and_a_table_without_data_rows_are_refused() {
let mut wb = wb();
let sh = wb.sheet_mut(0).unwrap();
sh.tables.push(gridcraft_model::Table {
id: 1,
name: "Empty".into(),
range: RangeRef::parse("H1:J1").unwrap(),
header_row: true,
totals_row: false,
columns: Vec::new(),
style: String::new(),
banded_rows: false,
banded_cols: false,
first_col: false,
last_col: false,
filter_button: false,
filter: Vec::new(),
table_type: None,
connection_id: None,
});
let inputs = vec![("Input".to_string(), place("B2:D2"), true), ("Plain".to_string(), place("B2:D2"), false)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None };
assert!(resolve_ref("Input.data", &env).unwrap_err().to_string().contains("only a header row"));
assert!(resolve_ref("Input.data.col(2)", &env).unwrap_err().to_string().contains("only a header row"));
check(&env, "Plain.data", "B2:D2");
check(&env, "Input.col(2)", "C2");
assert!(resolve_ref("Name:Empty", &env).unwrap_err().to_string().contains("no data rows"));
}
#[test]
fn doubled_braces_are_literal_braces() {
let wb = wb();
let inputs = vec![("Input".to_string(), place("B2:D11"), true)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None };
assert_eq!(interpolate("Dear {{{{name}},", &env).unwrap(), "Dear {{name}},");
assert_eq!(interpolate("{{{{ {{Input.cell(1,1)}} }}", &env).unwrap(), "{{ B2 }}");
assert!(interpolate("Dear {{name}},", &env).is_err(), "an unknown token is still an error");
}
#[test]
fn text_marked_as_text_is_never_a_token() {
// A defined name called Output or Input in a reference parameter, a Name: prefix and
// braces: `$text` passes each as it is, in any key; unmarked, each reads as a token.
let wb = wb();
let inputs = vec![("Input".to_string(), place("B2:D11"), true)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None };
for text in ["Output", "Input", "Name:Nowhere", "Row", "Dear {{name}}"] {
let p = json!({"range": {"$text": text}, "input": {"$text": text}});
assert_eq!(resolve_params(&p, &env).unwrap(), json!({"range": text, "input": text}), "{text}");
assert_ne!(resolve_params(&json!({"range": text}), &env).ok(), Some(json!({"range": text})), "{text}");
assert!(reads_as_token(text, &["Input"]) || text.contains("{{"), "{text}");
}
assert!(!reads_as_token("Output!A1", &["Input"]) && !reads_as_token("B2", &["Input"]));
}
#[test]
fn a_defined_name_on_a_sheet_with_a_dollar_sign_resolves() {
let mut wb = wb();
wb.sheets.push(std::sync::Arc::new(gridcraft_model::Sheet::new("US$")));
wb.names.push(gridcraft_model::DefinedName {
name: "Dollars".into(),
scope: None,
formula: "'US$'!$F$20".into(),
comment: String::new(),
hidden: false,
});
let env = Env { wb: &wb, active_sheet: 0, inputs: &[], output: None, item: None };
check(&env, "Name:Dollars", "'US$'!F20");
}
#[test]
fn below_and_right_refuse_a_count_under_one() {
let wb = wb();
let inputs = vec![("Input".to_string(), place("B2:D11"), true)];
let env = Env { wb: &wb, active_sheet: 0, inputs: &inputs, output: None, item: None };
check(&env, "Input.below", "B12:D12");
check(&env, "Input.below(2)", "B12:D13");
check(&env, "Input.right(1)", "E2:E11");
for bad in ["Input.below(0)", "Input.below(-3)", "Input.right(0)"] {
assert!(resolve_ref(bad, &env).unwrap_err().to_string().contains("at least 1"), "{bad}");
}
}
}
File diff suppressed because it is too large Load Diff
-740
View File
@@ -1,740 +0,0 @@
use gridcraft_model::*;
use serde_json::json;
use super::*;
fn sample_macro(name: &str) -> Macro {
parse_macro(&json!({
"name": name,
"description": "Totals the amounts",
"shortcut": "Ctrl+Shift+T",
"inputs": [{"name": "Input", "rows": [1, 500], "cols": [2, 2], "header": true, "colTypes": ["text", "number"], "nonEmpty": true}],
"steps": [
{"type": "command", "id": "cell.set", "params": {"cell": "Output", "input": "=SUM({{Input.col(2)}})"}, "breakpoint": true, "note": "the total"},
{"type": "repeat", "over": "Input.rows(2..)", "of": "rows", "max": 100, "body": [
{"type": "if", "cell": "Row.cell(1,2)", "op": "gt", "value": 100, "then": [
{"command": "home.bold", "params": {"range": "Row"}}
], "else": [{"type": "stop", "message": "done"}]}
]},
{"type": "command", "id": "home.italic", "enabled": false}
],
"output": {"anchor": "Input.offset(0,3)", "onConflict": "shiftDown"},
"permissions": ["data.getData"]
}))
.unwrap()
}
#[test]
fn macros_read_from_json_in_both_step_forms() {
let m = sample_macro("Totals");
assert_eq!(m.v, MACRO_VERSION);
assert_eq!(m.inputs[0].col_types, [ColType::Text, ColType::Number]);
assert_eq!(m.output.on_conflict, OnConflict::ShiftDown);
assert_eq!(m.step_count(), 6);
assert!(m.steps[0].breakpoint && m.steps[0].enabled && !m.steps[2].enabled);
let StepKind::Repeat { body, max, .. } = &m.steps[1].kind else { panic!("{:?}", m.steps[1]) };
assert_eq!(*max, 100);
let StepKind::If { then, otherwise, op, .. } = &body[0].kind else { panic!() };
assert_eq!(*op, CompareOp::Gt);
// The short form of earlier scripts reads as a command step.
assert_eq!(then[0].kind, StepKind::Command { id: "home.bold".into(), params: json!({"range": "Row"}) });
assert!(matches!(&otherwise[0].kind, StepKind::Stop { message, rollback: false } if message == "done"));
// Writing and reading again gives the same macro; defaults stay out of the JSON.
let v = macro_json(&m);
assert_eq!(v["steps"][0]["type"], "command");
assert!(v["steps"][1].get("enabled").is_none() && v["steps"][1].get("breakpoint").is_none());
assert_eq!(v["steps"][2]["enabled"], false);
assert_eq!(parse_macro(&v).unwrap(), m);
// A minimal macro gets the defaults.
let m = parse_macro(&json!({"name": " Bare "})).unwrap();
assert_eq!((m.name.as_str(), m.output.on_conflict, m.inputs.len()), ("Bare", OnConflict::Ask, 0));
}
#[test]
fn macros_over_the_limits_are_refused() {
let flat = |n: usize| json!({"name": "Big", "steps": vec![json!({"command": "cell.set"}); n]});
assert!(parse_macro(&flat(MAX_MACRO_STEPS)).is_ok());
assert!(parse_macro(&flat(MAX_MACRO_STEPS + 1)).unwrap_err().contains("10000 steps"));
// Nested Repeat steps: 4 Repeats one inside the other are fine (the innermost holds a
// command, or nothing), a 5th is not.
let nest = |repeats: usize| {
let mut step = json!({"command": "cell.set"});
for _ in 0..repeats {
step = json!({"type": "repeat", "over": "Input", "body": [step]});
}
json!({"name": "Deep", "steps": [step]})
};
assert!(parse_macro(&nest(MAX_MACRO_DEPTH)).is_ok());
assert_eq!(parse_macro(&nest(MAX_MACRO_DEPTH)).unwrap().step_count(), MAX_MACRO_DEPTH + 1);
assert!(parse_macro(&nest(MAX_MACRO_DEPTH + 1)).unwrap_err().contains("4 deep"));
let mut empty = json!({"type": "repeat", "over": "Input", "body": []});
for _ in 1..MAX_MACRO_DEPTH {
empty = json!({"type": "if", "cell": "A1", "op": "eq", "value": 1, "then": [empty], "else": []});
}
let deep = parse_macro(&json!({"name": "Deep", "steps": [empty]})).unwrap();
assert_eq!(deep.step_count(), MAX_MACRO_DEPTH);
assert!(deep.check_limits().is_ok(), "the model's check agrees with the parser");
// Far deeper than serde would allow is refused before it is read.
assert!(parse_macro(&nest(200)).is_err());
for bad in [
json!({"name": ""}),
json!({"name": "x".repeat(256)}),
json!({"name": "a\nb"}),
json!({"name": "A", "steps": {}}),
json!({"name": "A", "steps": [1]}),
json!({"name": "A", "steps": [{"type": "nope"}]}),
json!({"name": "A", "steps": [{"type": "if", "cell": "A1", "op": "between"}]}),
json!({"name": "A", "inputs": [{"name": ""}]}),
json!({"name": "A", "inputs": [{"name": "Has space"}]}),
json!({"name": "A", "inputs": [{"name": "row"}]}),
json!({"name": "A", "inputs": [{"name": "In"}, {"name": "IN"}]}),
json!({"name": "A", "output": {"onConflict": "explode"}}),
json!([]),
] {
assert!(parse_macro(&bad).is_err(), "{bad}");
}
}
fn names_of(wb: &Workbook) -> Vec<String> {
wb.names.iter().map(|n| n.name.clone()).collect()
}
#[test]
fn macros_round_trip_through_xlsx_and_json() {
let mut wb = Workbook::new();
wb.macros.push(sample_macro("Totals"));
// Large enough to need several hidden names.
let long: Vec<Step> = (0..300).map(|r| Step::command("cell.set", json!({"cell": format!("A{r}"), "input": "x\"y".repeat(10)}))).collect();
wb.macros.push(Macro { name: "Long".into(), steps: long, ..Macro::default() });
for file in ["book.xlsx", "book.json"] {
let bytes = crate::io::save_bytes(&wb, file).unwrap();
let (back, warnings) = crate::io::open_bytes(file, &bytes).unwrap();
assert!(warnings.is_empty(), "{file}: {warnings:?}");
assert_eq!(back.macros, wb.macros, "{file}");
assert!(back.names.is_empty(), "{file}: macros stay out of the names once opened: {:?}", names_of(&back));
}
// In the xlsx file they are hidden names; the long one spans several.
let bytes = crate::io::save_bytes(&wb, "book.xlsx").unwrap();
let (raw, _) = gridcraft_xlsx::read_xlsx(&bytes).unwrap();
let names = names_of(&raw);
assert!(names.contains(&"_gc_macro_0_0".to_string()), "{names:?}");
assert!(names.iter().filter(|n| n.starts_with("_gc_macro_1_")).count() >= 2, "{names:?}");
assert!(raw.names.iter().all(|n| n.hidden));
// Saving without macros writes no macro names, even if stale ones linger.
let mut empty = raw.clone();
empty.macros.clear();
let (again, _) = gridcraft_xlsx::read_xlsx(&crate::io::save_bytes(&empty, "x.xlsx").unwrap()).unwrap();
assert!(again.names.iter().all(|n| !n.name.starts_with("_gc_macro_")));
// The JSON format keeps the field (and leaves it out when there are no macros).
let json = String::from_utf8(crate::io::save_bytes(&wb, "book.json").unwrap()).unwrap();
assert!(json.contains("\"macros\""));
let plain = String::from_utf8(crate::io::save_bytes(&Workbook::new(), "book.json").unwrap()).unwrap();
assert!(!plain.contains("\"macros\""));
}
fn hidden(name: &str, text: &str) -> DefinedName {
DefinedName { name: name.into(), scope: None, formula: format!("\"{}\"", text.replace('"', "\"\"")), comment: String::new(), hidden: true }
}
#[test]
fn a_visible_name_that_looks_like_macro_storage_is_left_alone() {
// Macro storage is hidden workbook-scope names; a visible `_gc_macro_0_0` someone defined
// is neither read as a macro nor removed when the workbook opens.
let mut wb = Workbook::new();
let name = DefinedName { hidden: false, ..hidden("_gc_macro_0_0", "x") };
wb.names.push(name.clone());
let warnings = load(&mut wb);
assert!(warnings.is_empty(), "{warnings:?}");
assert!(wb.macros.is_empty());
assert_eq!(wb.names, vec![name]);
}
#[test]
fn input_names_that_read_as_cell_references_are_refused() {
// A recorded step holds plain A1 text; an input called `Q1` would take its place.
let with_input = |name: &str| json!({"name": "M", "inputs": [{"name": name}]});
for name in ["Q1", "AB12", "Sum1", "a1"] {
let err = parse_macro(&with_input(name)).unwrap_err();
assert!(err.contains("reads as a cell reference"), "{name}: {err}");
}
for name in ["Amount", "Q", "Sales_Q1", "Total1a"] {
assert!(parse_macro(&with_input(name)).is_ok(), "{name}");
}
assert!(parse_macro(&with_input("Row")).unwrap_err().contains("token of its own"));
}
#[test]
fn a_hidden_macro_name_this_build_did_not_write_is_kept() {
// `_gc_macro_index_0` is not a numbered item: it is neither read nor dropped on open, and a
// save with macros keeps it beside the items written.
let mut wb = Workbook::new();
let theirs = hidden("_gc_macro_index_0", "{}");
wb.names.push(theirs.clone());
wb.macros.push(sample_macro("Totals"));
let (raw, _) = gridcraft_xlsx::read_xlsx(&crate::io::save_bytes(&wb, "book.xlsx").unwrap()).unwrap();
let names = names_of(&raw);
assert!(names.contains(&"_gc_macro_index_0".to_string()) && names.contains(&"_gc_macro_0_0".to_string()), "{names:?}");
let (back, warnings) = crate::io::open_bytes("book.xlsx", &crate::io::save_bytes(&wb, "book.xlsx").unwrap()).unwrap();
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(back.macros, wb.macros);
assert_eq!(names_of(&back), ["_gc_macro_index_0"]);
}
#[test]
fn workbook_snapshots_share_the_macro_list_until_it_changes() {
// An undo snapshot or a copy made for a cell edit holds the same list, not a copy of every
// step; the undo budget charges a list only once it is a snapshot's own.
let mut wb = Workbook::new();
wb.macros.push(sample_macro("Totals"));
let snapshot = wb.clone();
assert!(wb.macros.shares(&snapshot.macros));
assert_eq!(crate::history_retained_bytes(&snapshot, &wb), std::mem::size_of::<Workbook>());
wb.macros.push(sample_macro("More"));
assert!(!wb.macros.shares(&snapshot.macros), "a write copies the list first");
assert_eq!(snapshot.macros.len(), 1);
assert!(crate::history_retained_bytes(&snapshot, &wb) > std::mem::size_of::<Workbook>());
// The copy is per list, not per workbook copy: cloning again shares the new list.
let again = wb.clone();
assert!(wb.macros.shares(&again.macros));
let rebuilt: Macros = wb.macros.iter().cloned().collect();
assert_eq!(rebuilt, wb.macros);
assert_eq!(rebuilt.into_iter().count(), 2);
}
#[test]
fn a_macro_built_in_code_goes_through_the_size_check_too() {
// `check` is the one check for every path, so a macro that passes it never makes a save fail.
let big = Macro {
name: "Big".into(),
steps: vec![Step::command("cell.set", json!({"input": "x".repeat(crate::cmd::stash::MAX_ITEM_CHARS)}))],
..Macro::default()
};
assert!(big.check_limits().is_ok(), "the model's limits say nothing about the size");
assert!(check(&big).unwrap_err().contains("too large"));
// A macro that fails `check` fails the save in either format, rather than being written and
// dropped the next time the file opens: too large, or past the model's limits.
let bad_name = Macro { name: "Two\nlines".into(), ..Macro::default() };
assert!(check(&bad_name).is_err());
for m in [big, bad_name] {
let mut wb = Workbook::new();
wb.macros.push(m);
for file in ["book.xlsx", "book.json"] {
assert!(crate::io::save_bytes(&wb, file).is_err(), "{file}");
}
}
}
fn visible(name: &str) -> DefinedName {
DefinedName { name: name.into(), scope: None, formula: "Sheet1!$A$1".into(), comment: String::new(), hidden: false }
}
/// Saves `wb` to xlsx and returns the names in the file and the workbook opened from it.
fn xlsx_round_trip(wb: &Workbook) -> (Vec<String>, Workbook) {
let bytes = crate::io::save_bytes(wb, "book.xlsx").unwrap();
let raw = names_of(&gridcraft_xlsx::read_xlsx(&bytes).unwrap().0);
let (back, warnings) = crate::io::open_bytes("book.xlsx", &bytes).unwrap();
assert!(warnings.is_empty(), "{warnings:?}");
(raw, back)
}
#[test]
fn a_visible_name_of_the_storage_shape_does_not_block_a_save() {
// The index a macro is stored under is GridCraft's choice, so a person's own visible name
// of that shape (here in capitals: names compare without case) is stepped around, not
// refused. Stale hidden items still go in the same pass.
let mut wb = Workbook::new();
wb.macros.push(sample_macro("A"));
wb.macros.push(sample_macro("B"));
wb.names.push(visible("_GC_MACRO_0_0"));
wb.names.push(hidden("_gc_macro_7_0", "stale"));
let (raw, back) = xlsx_round_trip(&wb);
assert_eq!(raw, ["_GC_MACRO_0_0", "_gc_macro_1_0", "_gc_macro_2_0"]);
assert_eq!(back.macros, wb.macros, "both macros, in their order");
assert_eq!(back.names, vec![visible("_GC_MACRO_0_0")], "the person's name is kept as it was");
assert_eq!(wb.names.len(), 2, "the open workbook is not changed by a save");
}
#[test]
fn a_huge_visible_index_takes_no_part_in_numbering() {
// A file can carry a visible name with any number in it. The macros take the free indices
// from 0 up, so no arithmetic depends on that number (one past the largest would overflow).
let mut wb = Workbook::new();
wb.macros.push(sample_macro("A"));
wb.names.push(visible(&format!("_gc_macro_{}_0", u64::MAX)));
wb.names.push(visible("_gc_macro_0_0"));
let (raw, back) = xlsx_round_trip(&wb);
assert_eq!(raw, [format!("_gc_macro_{}_0", u64::MAX), "_gc_macro_0_0".into(), "_gc_macro_1_0".into()]);
assert_eq!(back.macros, wb.macros);
assert_eq!(names_of(&back), names_of(&wb));
}
#[test]
fn a_visible_legacy_name_of_the_storage_shape_is_kept() {
// The stash reads a legacy `_sc_<key>` name as data whether or not it is hidden, so writing
// key `macro_0` would remove a person's visible `_sc_macro_0`. That key is stepped around too.
let mut wb = Workbook::new();
wb.macros.push(sample_macro("A"));
wb.names.push(visible("_sc_macro_0"));
let (raw, back) = xlsx_round_trip(&wb);
assert_eq!(raw, ["_sc_macro_0", "_gc_macro_1_0"]);
assert_eq!(back.macros, wb.macros);
assert_eq!(back.names, vec![visible("_sc_macro_0")]);
}
#[test]
fn a_name_defined_after_opening_does_not_block_the_next_save() {
// Once a file is open its macros are out of the names, so Define Name accepts a visible
// `_gc_macro_0_0`; the next save steps around it.
let mut s = crate::Session::new();
s.execute("file.new", json!({})).unwrap();
std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().wb).macros.push(sample_macro("A"));
s.execute("formulas.defineName", json!({"name": "_gc_macro_0_0", "refersTo": "=Sheet1!$A$1"})).unwrap();
let (raw, back) = xlsx_round_trip(&s.doc().unwrap().wb);
assert_eq!(raw, ["_gc_macro_0_0", "_gc_macro_1_0"]);
assert_eq!(back.macros.len(), 1);
assert_eq!(names_of(&back), ["_gc_macro_0_0"]);
}
#[test]
fn scripts_and_vba_stay_as_they_are() {
// Automate scripts keep their own storage, and a VBA project is carried through unchanged.
let mut wb = Workbook::new();
let scripts = json!({"Fill": [{"command": "cell.set", "params": {"cell": "A1", "input": "x"}}]});
wb.names.push(hidden("_sc_scripts", &scripts.to_string()));
wb.vba_project = Some(std::sync::Arc::new(b"\xd0\xcf\x11\xe0 synthetic vbaProject".to_vec()));
wb.macros.push(sample_macro("Totals"));
let bytes = crate::io::save_bytes(&wb, "book.xlsm").unwrap();
let (back, warnings) = crate::io::open_bytes("book.xlsm", &bytes).unwrap();
// The only warning is the reader's note that the VBA project is kept and not run.
assert!(warnings.iter().all(|w| w.contains("VBA project")), "{warnings:?}");
assert_eq!(back.macros, wb.macros);
assert_eq!(names_of(&back), ["_sc_scripts"], "the scripts item is untouched");
assert_eq!(back.names[0].formula, wb.names[0].formula);
assert_eq!(back.vba_project, wb.vba_project);
}
#[test]
fn damaged_or_oversized_macros_are_dropped_with_a_warning() {
let mut wb = Workbook::new();
wb.names.push(hidden("_gc_macro_0_0", &json!({"name": "Good"}).to_string()));
wb.names.push(hidden("_gc_macro_1_0", "{not json"));
wb.names.push(hidden("_gc_macro_2_0", &json!({"name": "Huge", "steps": vec![json!({"command": "x"}); MAX_MACRO_STEPS + 1]}).to_string()));
wb.names.push(hidden("_gc_macro_3_0", &json!({"name": "good"}).to_string()));
wb.names.push(hidden("_gc_macro_10_0", &json!({"name": "Tenth"}).to_string()));
let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap();
let (back, warnings) = crate::io::open_bytes("hostile.xlsx", &bytes).unwrap();
let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect();
// Saved order is by number (10 after 3); a clashing name gets a suffix.
assert_eq!(names, ["Good", "good (2)", "Tenth"]);
assert!(back.names.is_empty(), "{:?}", names_of(&back));
assert_eq!(warnings.len(), 1, "{warnings:?}");
assert!(warnings[0].starts_with("2 macros"), "{warnings:?}");
// The JSON format: more than the limit, and one over the step limit.
let mut wb = Workbook::new();
wb.macros = (0..MAX_MACROS + 5).map(|i| Macro { name: format!("M{i}"), ..Macro::default() }).collect();
wb.macros[0].steps = vec![Step::command("x", json!({})); MAX_MACRO_STEPS + 1];
// Written as another program would: a GridCraft save refuses a macro that fails `check`.
let bytes = serde_json::to_vec(&wb).unwrap();
let (back, warnings) = crate::io::open_bytes("many.json", &bytes).unwrap();
// Only the first MAX_MACROS entries are read, as in xlsx.
assert_eq!(back.macros.len(), MAX_MACROS - 1);
assert_eq!(back.macros[0].name, "M1");
assert!(warnings[0].starts_with("6 macros"), "{warnings:?}");
// A macro too large for the hidden names is refused, so a save never fails on it.
let huge = "x".repeat(crate::cmd::stash::MAX_ITEM_CHARS);
let err = parse_macro(&json!({"name": "Huge", "steps": [{"command": "cell.set", "params": {"input": huge}}]})).unwrap_err();
assert!(err.contains("too large"), "{err}");
let mut wb = Workbook::new();
wb.macros.push(Macro { name: "Huge".into(), steps: vec![Step::command("cell.set", json!({"input": huge}))], ..Macro::default() });
// Written as another program would: a GridCraft save refuses a macro that fails `check`.
let bytes = serde_json::to_vec(&wb).unwrap();
let (back, warnings) = crate::io::open_bytes("huge.json", &bytes).unwrap();
assert!(back.macros.is_empty() && warnings[0].starts_with("1 macro "), "{warnings:?}");
}
#[test]
fn names() {
let mut wb = Workbook::new();
wb.macros.push(Macro { name: "Macro1".into(), ..Macro::default() });
assert_eq!(unique_name(&wb, "macro1"), "macro1 (2)");
assert_eq!(unique_name(&wb, "Other"), "Other");
assert_eq!(unique_name(&wb, ""), "Macro");
// A long name is shortened to make room for the suffix.
let long = "y".repeat(MAX_MACRO_NAME);
wb.macros.push(Macro { name: long.clone(), ..Macro::default() });
let renamed = unique_name(&wb, &long);
assert_eq!(renamed, format!("{} (2)", "y".repeat(MAX_MACRO_NAME - 4)));
wb.macros.push(Macro { name: renamed, ..Macro::default() });
assert_eq!(unique_name(&wb, &long).chars().count(), MAX_MACRO_NAME);
assert_eq!(find(&wb, " MACRO1 "), Some(0));
assert_eq!(find(&wb, "Macro2"), None);
let a = sample_macro("A");
let mut b = a.clone();
assert_eq!(fingerprint(&a), fingerprint(&b));
b.steps[0].note = "changed".into();
assert_ne!(fingerprint(&a), fingerprint(&b));
assert_eq!(next_name(&wb), "Macro2");
let paths: Vec<(String, &str)> = command_steps(&a.steps).into_iter().map(|(p, id, _)| (p, id)).collect();
assert_eq!(paths, [("1".into(), "cell.set"), ("2.1.then.1".into(), "home.bold"), ("3".into(), "home.italic")]);
}
#[test]
fn json_field_macros_get_the_name_check_too() {
// A JSON workbook can't bring in a name that macro.save and macro.rename refuse.
let mut wb = Workbook::new();
wb.macros.push(Macro { name: "a\nb".into(), ..Macro::default() });
wb.macros.push(Macro { name: "Fine".into(), ..Macro::default() });
// Written as another program would: a GridCraft save refuses a macro that fails `check`.
let bytes = serde_json::to_vec(&wb).unwrap();
let (back, warnings) = crate::io::open_bytes("ctl.json", &bytes).unwrap();
let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect();
assert_eq!(names, ["Fine"]);
assert!(warnings.len() == 1 && warnings[0].starts_with("1 macro "), "{warnings:?}");
}
#[test]
fn duplicate_names_at_the_length_limit_survive_save_and_reopen() {
// Two macros with the same 255-character name: the second is renamed within the limit, so a
// save and a second open keep both.
let long = "é".repeat(MAX_MACRO_NAME);
for file in ["dup.json", "dup.xlsx"] {
let mut wb = Workbook::new();
wb.macros.push(Macro { name: long.clone(), ..Macro::default() });
wb.macros.push(Macro { name: long.clone(), description: "second".into(), ..Macro::default() });
let (first, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&wb, file).unwrap()).unwrap();
assert!(warnings.is_empty(), "{file}: {warnings:?}");
assert_eq!(first.macros.len(), 2, "{file}");
let renamed = &first.macros[1].name;
assert_eq!(renamed.chars().count(), MAX_MACRO_NAME, "{file}");
assert!(renamed.ends_with(" (2)") && renamed.starts_with("éé"), "{file}: {renamed}");
let (again, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&first, file).unwrap()).unwrap();
assert!(warnings.is_empty(), "{file}: {warnings:?}");
assert_eq!(again.macros, first.macros, "{file}");
}
}
#[test]
#[allow(clippy::disallowed_methods)] // a native-only timing test: the clock is read off wasm
fn many_hidden_macro_names_are_read_in_one_pass() {
// A hostile file with many damaged macro items: only the first MAX_MACROS are read, and the
// names are gathered and removed in one pass instead of one scan of every name per item.
let mut wb = Workbook::new();
let items = 20_000;
for i in 0..items {
wb.names.push(hidden(&format!("_gc_macro_{i}_0"), "{not json"));
}
wb.names.push(hidden("_gc_other_0", "kept"));
let start = std::time::Instant::now();
let warnings = load(&mut wb);
let took = start.elapsed();
assert!(wb.macros.is_empty());
assert_eq!(names_of(&wb), ["_gc_other_0"]);
assert!(warnings.len() == 1 && warnings[0].starts_with(&format!("{items} macros")), "{warnings:?}");
assert!(took < std::time::Duration::from_secs(3), "{took:?}");
}
#[test]
fn a_damaged_json_macro_drops_alone() {
// One macro this build can't read (a newer step type, an unknown value, the wrong shape) is
// dropped with the usual warning; the workbook and its other macros open.
let mut wb = Workbook::new();
wb.sheet_mut(0).unwrap().set_value(gridcraft_core::CellRef::new(0, 0), gridcraft_core::Value::text("kept"));
let mut v: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&wb, "book.json").unwrap()).unwrap();
v["macros"] = json!([
{"name": "Good"},
{"name": "Newer", "steps": [{"type": "fromALaterBuild"}]},
{"name": "Odd", "output": {"onConflict": "explode"}},
{"name": "Short", "steps": [{"command": "cell.set", "params": {"cell": "A1", "input": "x"}}]},
"not a macro"
]);
let (back, warnings) = crate::io::open_bytes("book.json", v.to_string().as_bytes()).unwrap();
let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect();
assert_eq!(names, ["Good", "Short"]);
assert!(matches!(&back.macros[1].steps[0].kind, StepKind::Command { id, .. } if id == "cell.set"));
assert!(back.macros_read.is_empty());
assert!(warnings.len() == 1 && warnings[0].starts_with("3 macros"), "{warnings:?}");
assert_eq!(back.sheet(0).unwrap().value(gridcraft_core::CellRef::new(0, 0)), gridcraft_core::Value::text("kept"));
}
#[test]
fn a_macro_in_a_newer_format_is_kept_unread_and_saved_back_unchanged() {
// A newer build's macro (`v` 2, with a field this build doesn't know) would lose that field
// if it were read here, so it is never read: it is kept as the file held it, saved back
// unchanged, and the newer build gets it back whole. Version 1 and a missing version are
// read as before.
let future = json!({"name": "Future", "v": MACRO_VERSION + 1, "fromALaterBuild": {"x": 1}, "steps": [{"command": "cell.set"}]});
let current = json!({"name": "Current", "v": MACRO_VERSION, "steps": [{"command": "cell.set"}]});
let missing = json!({"name": "Missing"});
let err = parse_macro(&future).unwrap_err();
assert!(err.contains("newer version") && err.contains("format 2"), "{err}");
assert!(parse_macro(&json!({"name": "Huge", "v": u64::from(u32::MAX) + 1})).unwrap_err().contains("newer version"));
// A macro built in code goes through the same check on every path.
assert!(Macro { name: "Built".into(), v: MACRO_VERSION + 1, ..Macro::default() }.check_limits().is_err());
let check = |file: &str, back: &Workbook, warnings: &[String]| {
let names: Vec<&str> = back.macros.iter().map(|m| m.name.as_str()).collect();
assert_eq!(names, ["Current", "Missing"], "{file}");
assert!(back.macros.iter().all(|m| m.v == MACRO_VERSION), "{file}");
assert_eq!(*back.macros_unread, std::slice::from_ref(&future), "{file}");
assert!(
warnings.len() == 1 && warnings[0].starts_with("1 macro ") && warnings[0].contains("newer version") && warnings[0].contains("unchanged"),
"{file}: {warnings:?}"
);
// Saved and opened again, it is still there, whole, and the others are as they were.
let (again, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(back, file).unwrap()).unwrap();
assert_eq!(warnings.len(), 1, "{file}: {warnings:?}");
assert_eq!(again.macros, back.macros, "{file}");
assert_eq!(again.macros_unread, back.macros_unread, "{file}");
};
// The JSON format's field.
let mut v: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&Workbook::new(), "book.json").unwrap()).unwrap();
v["macros"] = json!([future, current, missing]);
let (back, warnings) = crate::io::open_bytes("book.json", v.to_string().as_bytes()).unwrap();
check("book.json", &back, &warnings);
let saved: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&back, "book.json").unwrap()).unwrap();
assert!(saved["macros"].as_array().unwrap().contains(&future), "the file holds it as it was");
// Hidden names in xlsx.
let mut wb = Workbook::new();
for (i, m) in [&future, &current, &missing].into_iter().enumerate() {
wb.names.push(hidden(&format!("_gc_macro_{i}_0"), &m.to_string()));
}
let bytes = gridcraft_xlsx::write_xlsx(&wb).unwrap();
let (back, warnings) = crate::io::open_bytes("book.xlsx", &bytes).unwrap();
assert!(back.names.is_empty(), "{:?}", names_of(&back));
check("book.xlsx", &back, &warnings);
}
#[test]
fn kept_macros_share_the_save_limits_and_the_numbering() {
// A newer macro too large for the file is left out (keeping it would make every xlsx save
// fail), and readable and unread macros together stay within the macro limit, readable ones
// first. Kept ones are numbered after the readable ones, around a person's visible name.
let newer = |name: &str| json!({"name": name, "v": MACRO_VERSION + 1});
let mut big = newer("Big");
big["pad"] = json!(long(crate::cmd::stash::MAX_ITEM_CHARS));
let mut file: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&Workbook::new(), "book.json").unwrap()).unwrap();
let mut list = vec![big, newer("Kept"), json!({"name": "Read"})];
file["macros"] = json!(list);
let (mut wb, warnings) = crate::io::open_bytes("book.json", file.to_string().as_bytes()).unwrap();
assert_eq!(*wb.macros_unread, [newer("Kept")]);
assert_eq!(warnings.len(), 2, "one kept, one left out: {warnings:?}");
wb.names.push(visible("_gc_macro_1_0"));
let (raw, back) = {
let bytes = crate::io::save_bytes(&wb, "book.xlsx").unwrap();
let raw = names_of(&gridcraft_xlsx::read_xlsx(&bytes).unwrap().0);
(raw, crate::io::open_bytes("book.xlsx", &bytes).unwrap().0)
};
assert_eq!(raw, ["_gc_macro_1_0", "_gc_macro_0_0", "_gc_macro_2_0"]);
assert_eq!((back.macros.len(), back.macros_unread.len()), (1, 1));
// Past the limit the rest are left out; and when a JSON file's list is full, an unread item
// it also holds in hidden names is the one left out: readable macros come first.
list = (0..MAX_MACROS).map(|i| json!({"name": format!("M{i}")})).collect();
let mut with_item = Workbook::new();
with_item.names.push(hidden("_gc_macro_0_0", &newer("InNames").to_string()));
let mut file: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&with_item, "book.json").unwrap()).unwrap();
for extra in [None, Some(newer("Over"))] {
file["macros"] = json!(list.iter().cloned().chain(extra).collect::<Vec<_>>());
let (wb, warnings) = crate::io::open_bytes("book.json", file.to_string().as_bytes()).unwrap();
assert_eq!((wb.macros.len(), wb.macros_unread.len()), (MAX_MACROS, 0));
assert!(warnings.len() == 1 && warnings[0].contains("couldn't be opened"), "{warnings:?}");
}
}
#[test]
fn many_macros_with_one_name_all_survive_save_and_reopen() {
// MAX_MACROS macros with the same name, at the length limit, so the suffixes run from " (2)"
// to " (1000)" and the kept part of the name shrinks as they widen.
let long = "é".repeat(MAX_MACRO_NAME);
for file in ["same.json", "same.xlsx"] {
let mut wb = Workbook::new();
wb.macros = (0..MAX_MACROS).map(|i| Macro { name: long.clone(), description: format!("{i}"), ..Macro::default() }).collect();
let (first, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&wb, file).unwrap()).unwrap();
assert!(warnings.is_empty(), "{file}: {warnings:?}");
assert_eq!(first.macros.len(), MAX_MACROS, "{file}");
let names: std::collections::HashSet<String> = first.macros.iter().map(|m| m.name.to_lowercase()).collect();
assert_eq!(names.len(), MAX_MACROS, "{file}: every name is different");
assert!(first.macros.iter().all(|m| m.name.chars().count() <= MAX_MACRO_NAME && m.check_limits().is_ok()), "{file}");
assert_eq!(first.macros[0].name, long);
assert_eq!(first.macros[9].name, format!("{} (10)", "é".repeat(MAX_MACRO_NAME - 5)));
assert_eq!(first.macros[MAX_MACROS - 1].name, format!("{} (1000)", "é".repeat(MAX_MACRO_NAME - 7)));
assert!(first.macros.iter().enumerate().all(|(i, m)| m.description == format!("{i}")), "{file}: order kept");
let (again, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&first, file).unwrap()).unwrap();
assert!(warnings.is_empty(), "{file}: {warnings:?}");
assert_eq!(again.macros, first.macros, "{file}");
}
}
#[test]
fn naming_clashing_macros_takes_linear_work() {
// Each suffix is looked up about once, not once per earlier macro with the same name.
let mut taken = Taken::default();
// Names a file may already use, in the way of the first suffixes.
for n in 2..=500 {
taken.insert(&format!("Same ({n})"));
}
let mut out = Vec::new();
for _ in 0..MAX_MACROS {
let name = taken.unique("SAME");
taken.insert(&name);
out.push(name);
}
assert_eq!(out[..3], ["SAME", "SAME (501)", "SAME (502)"]);
assert!(taken.probes <= 3 * MAX_MACROS, "{} lookups", taken.probes);
// The smallest free suffix is still the one used.
let mut taken = Taken::default();
for name in ["A", "A (3)"] {
taken.insert(name);
}
assert_eq!(taken.unique("a"), "a (2)");
taken.insert("a (2)");
assert_eq!(taken.unique("a"), "a (4)");
}
#[test]
fn a_json_workbook_with_no_sheets_keeps_its_macros() {
// A JSON file with an empty sheet list opens as a new workbook; its macros come along.
let mut wb = Workbook::new();
wb.macros.push(sample_macro("A"));
let mut v: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&wb, "book.json").unwrap()).unwrap();
v["sheets"] = json!([]);
let (back, warnings) = crate::io::open_bytes("book.json", v.to_string().as_bytes()).unwrap();
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(back.sheets.len(), 1);
assert_eq!(back.macros, wb.macros);
}
fn long(n: usize) -> String {
"x".repeat(n)
}
/// `sample_macro` as JSON with `change` made to it.
fn changed(change: fn(&mut serde_json::Value)) -> serde_json::Value {
let mut v = macro_json(&sample_macro("T"));
change(&mut v);
v
}
#[test]
fn every_field_a_file_sets_has_a_limit() {
// A named edit to the sample file's JSON.
type Edit = (&'static str, fn(&mut serde_json::Value));
// The sample's steps: 0 a command, 1 a Repeat whose body holds an If whose else holds a Stop.
let refused: [Edit; 14] = [
("shortcut", |v| v["shortcut"] = json!(long(MAX_MACRO_SHORTCUT + 1))),
("shortcut line break", |v| v["shortcut"] = json!("Ctrl+\nM")),
("permission count", |v| v["permissions"] = json!(vec!["data.getData"; MAX_MACRO_PERMISSIONS + 1])),
("permission text", |v| v["permissions"] = json!(["data getData"])),
("output anchor", |v| v["output"]["anchor"] = json!(long(MAX_MACRO_REF + 1))),
("default range", |v| v["inputs"][0]["defaultRef"] = json!(long(MAX_MACRO_REF + 1))),
("column types", |v| {
v["inputs"][0]["cols"] = json!([1, 0]);
v["inputs"][0]["colTypes"] = json!(vec!["any"; MAX_MACRO_COLUMNS + 1]);
}),
("command id", |v| v["steps"][0]["id"] = json!("cell set")),
("empty command id", |v| v["steps"][0]["id"] = json!("")),
("Repeat range", |v| v["steps"][1]["over"] = json!(long(MAX_MACRO_REF + 1))),
("If cell", |v| v["steps"][1]["body"][0]["cell"] = json!(long(MAX_MACRO_REF + 1))),
// As JSON the string takes its two quotes as well.
("If value", |v| v["steps"][1]["body"][0]["value"] = json!(long(MAX_MACRO_TEXT - 1))),
("Stop message", |v| v["steps"][1]["body"][0]["else"][0]["message"] = json!(long(MAX_MACRO_TEXT + 1))),
("Stop message, nested", |v| v["steps"][1]["body"][0]["else"][0]["message"] = json!(long(MAX_MACRO_TEXT * 2))),
];
for (what, change) in refused {
assert!(parse_macro(&changed(change)).is_err(), "{what} past its limit is refused");
}
let accepted: [Edit; 9] = [
("shortcut", |v| v["shortcut"] = json!(long(MAX_MACRO_SHORTCUT))),
("permission count", |v| v["permissions"] = json!(vec!["data.getData"; MAX_MACRO_PERMISSIONS])),
("output anchor", |v| v["output"]["anchor"] = json!(long(MAX_MACRO_REF))),
("column types", |v| {
v["inputs"][0]["cols"] = json!([1, 0]);
v["inputs"][0]["colTypes"] = json!(vec!["any"; MAX_MACRO_COLUMNS]);
}),
("If value", |v| v["steps"][1]["body"][0]["value"] = json!(long(MAX_MACRO_TEXT - 2))),
// Empty and NotEmpty ignore the value, which is then left out (null).
("If with no value", |v| {
v["steps"][1]["body"][0]["op"] = json!("empty");
v["steps"][1]["body"][0].as_object_mut().unwrap().remove("value");
}),
// How many times a Repeat may run is the runner's to bound, not the format's.
("Repeat max", |v| v["steps"][1]["max"] = json!(u32::MAX)),
("Stop message", |v| v["steps"][1]["body"][0]["else"][0]["message"] = json!(long(MAX_MACRO_TEXT))),
("no shortcut", |v| {
v.as_object_mut().unwrap().remove("shortcut");
}),
];
for (what, change) in accepted {
assert!(parse_macro(&changed(change)).is_ok(), "{what} at its limit is accepted");
}
// Every command there is passes as a step and as a permission.
for spec in crate::command_specs() {
let m = json!({"name": "T", "steps": [{"command": spec.id}], "permissions": [spec.id]});
assert!(parse_macro(&m).is_ok(), "{}", spec.id);
}
}
#[test]
fn an_input_declaration_is_checked_as_a_whole() {
let input = |i: serde_json::Value| json!({"name": "T", "inputs": [i]});
for i in [
json!({"name": "Input", "rows": [5, 2]}),
json!({"name": "Input", "cols": [3, 2]}),
json!({"name": "Input", "kind": "cell", "cols": [2, 5]}),
json!({"name": "Input", "kind": "cell", "rows": [0, 0]}),
json!({"name": "Input", "kind": "cell", "header": true}),
json!({"name": "Input", "kind": "cell", "colTypes": ["number", "text"]}),
json!({"name": "Input", "cols": [2, 2], "colTypes": ["number", "text", "any"]}),
] {
assert!(parse_macro(&input(i.clone())).is_err(), "{i}");
}
for i in [
json!({"name": "Input", "rows": [5, 0]}),
json!({"name": "Input", "rows": [2, 2], "cols": [0, 3]}),
json!({"name": "Input", "kind": "cell"}),
json!({"name": "Input", "kind": "cell", "rows": [1, 1], "cols": [1, 1], "colTypes": ["number"]}),
json!({"name": "Input", "cols": [1, 2], "colTypes": ["number", "text"]}),
] {
assert!(parse_macro(&input(i.clone())).is_ok(), "{i}");
}
// From a file, a macro whose declaration fails is left out with the usual warning, and the
// workbook's other macros open.
let mut file: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&Workbook::new(), "book.json").unwrap()).unwrap();
file["macros"] = json!([input(json!({"name": "Input", "rows": [5, 2]})), {"name": "Fine"}]);
let (wb, warnings) = crate::io::open_bytes("book.json", file.to_string().as_bytes()).unwrap();
assert_eq!(wb.macros.iter().map(|m| m.name.as_str()).collect::<Vec<_>>(), ["Fine"]);
assert_eq!(warnings.len(), 1, "{warnings:?}");
}
#[test]
fn the_storage_rules_hold_together_through_both_formats() {
// A visible name of the storage shape, a macro in a newer format and one whose input breaks
// the declaration rules, in one file, saved and opened again in each format and then the
// other: the visible name stays, the newer macro comes back whole, the broken one is left out
// once, and the readable one survives.
let newer = json!({"name": "Kept", "v": MACRO_VERSION + 1, "fromALaterBuild": true});
let broken = json!({"name": "Broken", "inputs": [{"name": "Input", "rows": [5, 2]}]});
let mut start = Workbook::new();
start.names.push(visible("_gc_macro_0_0"));
let mut file: serde_json::Value = serde_json::from_slice(&crate::io::save_bytes(&start, "book.json").unwrap()).unwrap();
file["macros"] = json!([newer, broken, {"name": "Good"}]);
let (wb, warnings) = crate::io::open_bytes("book.json", file.to_string().as_bytes()).unwrap();
assert_eq!(warnings.len(), 2, "one kept, one left out: {warnings:?}");
let expect = |wb: &Workbook, how: &str| {
assert_eq!(wb.macros.iter().map(|m| m.name.as_str()).collect::<Vec<_>>(), ["Good"], "{how}");
assert_eq!(*wb.macros_unread, std::slice::from_ref(&newer), "{how}");
assert_eq!(names_of(wb), ["_gc_macro_0_0"], "{how}");
};
expect(&wb, "opened");
for formats in [["book.xlsx", "book.json"], ["book.json", "book.xlsx"]] {
let mut now = wb.clone();
for file in formats {
let (back, warnings) = crate::io::open_bytes(file, &crate::io::save_bytes(&now, file).unwrap()).unwrap();
assert_eq!(warnings.len(), 1, "{file}: only the kept macro is reported again: {warnings:?}");
expect(&back, file);
now = back;
}
}
let bytes = crate::io::save_bytes(&wb, "book.xlsx").unwrap();
assert_eq!(names_of(&gridcraft_xlsx::read_xlsx(&bytes).unwrap().0), ["_gc_macro_0_0", "_gc_macro_1_0", "_gc_macro_2_0"]);
}
-205
View File
@@ -1,205 +0,0 @@
//! Formulas › Recently Used: the functions most recently entered in a formula.
//!
//! The list is [`crate::Prefs::recent_functions`], an application list like Excel's, saved with
//! the other preferences. [`crate::Session::execute`] calls [`note`] for each `cell.*` and
//! `range.*` command it journals, from any source (the interface, the control channel, MCP or a
//! script), so the list never depends on the journal, which drops its oldest entries.
use std::collections::HashSet;
use std::sync::OnceLock;
use crate::cmd::edit::is_formula_input;
use gridcraft_formula::lexer::{Tok, tokenize};
use gridcraft_formula::normalize_function_name;
use serde::Deserialize;
use serde_json::Value as Json;
/// How many functions the list keeps.
pub const MAX: usize = 10;
/// The longest formula read (Excel's formula length limit): a longer one was not typed.
const FORMULA_MAX: usize = 8_192;
/// The most formulas read from one command: a large paste needs only its last few.
const SCAN_BUDGET: usize = 2_000;
/// What the list holds before any function has been used.
pub fn defaults() -> Vec<String> {
["SUM", "AVERAGE", "IF", "COUNT", "MAX", "MIN", "SUMIF", "XLOOKUP", "PMT", "TODAY"].map(String::from).to_vec()
}
/// Whether `name` (upper case, without `_xlfn.`) is a function of the catalog.
fn known(name: &str) -> bool {
static NAMES: OnceLock<HashSet<String>> = OnceLock::new();
NAMES
.get_or_init(|| crate::cmd::formulas::function_list().iter().filter_map(|f| f["name"].as_str().map(str::to_ascii_uppercase)).collect())
.contains(name)
}
/// The functions a formula calls, outermost first, known functions only. A formula is what a
/// cell takes as one (`=…`, or `+…`/`-…` as Excel takes `+SUM(1)`).
pub fn functions_in_formula(text: &str) -> Vec<String> {
if !is_formula_input(text) {
return vec![];
}
let body = text.strip_prefix('=').unwrap_or(text);
if body.len() > FORMULA_MAX {
return vec![];
}
let mut out: Vec<String> = Vec::new();
for t in tokenize(body).unwrap_or_default() {
if let Tok::Func(name) = t.tok {
let name = normalize_function_name(&name);
if known(&name) && !out.contains(&name) {
out.push(name);
}
}
}
out
}
/// The formula texts a command's params enter, the last first, at most `budget` of them.
fn formulas_newest_first<'a>(id: &str, params: &'a Json, budget: usize) -> Vec<&'a str> {
fn walk<'a>(v: &'a Json, out: &mut Vec<&'a str>, depth: usize, budget: usize) {
match v {
Json::String(s) if is_formula_input(s) && out.len() < budget => out.push(s),
// A range's values are rows of cells; anything deeper is not a cell value.
Json::Array(a) if depth < 2 => {
for x in a.iter().rev() {
if out.len() >= budget {
return;
}
walk(x, out, depth + 1, budget);
}
}
_ => {}
}
}
let mut out = Vec::new();
if id.starts_with("cell.") || id.starts_with("range.") {
for key in ["formula", "values", "value", "input"] {
if let Some(v) = params.get(key) {
walk(v, &mut out, 0, budget);
}
}
}
out
}
/// Moves `names` (most recent first) to the front of `recent`, keeping at most [`MAX`].
pub fn push(recent: &mut Vec<String>, names: &[String]) {
let mut next: Vec<String> = Vec::with_capacity(MAX);
for n in names.iter().chain(recent.iter()) {
if next.len() == MAX {
break;
}
if !next.contains(n) {
next.push(n.clone());
}
}
*recent = next;
}
/// Moves the functions that the formulas a command entered call to the front of `recent`. The
/// last formulas are read first (they are the most recent), within [`SCAN_BUDGET`].
pub(crate) fn note(recent: &mut Vec<String>, id: &str, params: &Json) {
let mut names: Vec<String> = Vec::new();
for f in formulas_newest_first(id, params, SCAN_BUDGET) {
for n in functions_in_formula(f) {
if !names.contains(&n) {
names.push(n);
}
}
if names.len() >= MAX {
break;
}
}
if !names.is_empty() {
push(recent, &names);
}
}
/// Reads a saved list leniently: known function names only, at most [`MAX`]. A value that is not
/// a list gives the default list, so a hand-edited or hostile `prefs.json` loses this list, never
/// the other preferences.
pub(crate) fn lenient<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<String>, D::Error> {
let raw = Json::deserialize(d)?;
let Some(list) = raw.as_array() else { return Ok(defaults()) };
let mut out = Vec::new();
for name in list.iter().filter_map(Json::as_str) {
if out.len() == MAX {
break;
}
let name = name.to_ascii_uppercase();
if known(&name) && !out.contains(&name) {
out.push(name);
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn formulas_name_their_functions_outermost_first() {
assert_eq!(functions_in_formula("=IF(SUM(A1:A3)>2,_xlfn.XLOOKUP(1,B:B,C:C),sum(1))"), ["IF", "SUM", "XLOOKUP"]);
assert!(functions_in_formula("=MYLAMBDA(1)+A1").is_empty(), "only known functions");
assert!(functions_in_formula("SUM(1)").is_empty(), "not a formula");
assert!(functions_in_formula("=\"SUM(\"").is_empty(), "inside a string");
assert!(functions_in_formula(&format!("=SUM({})", "1+".repeat(9000))).is_empty(), "too long");
assert!(functions_in_formula("=((((").is_empty());
assert!(functions_in_formula("=").is_empty());
// A formula may be typed with + or - instead of =, as input_to_cell takes it.
assert_eq!(functions_in_formula("+SUM(1)"), ["SUM"]);
assert_eq!(functions_in_formula("-ABS(A1)"), ["ABS"]);
assert!(functions_in_formula("-5").is_empty() && functions_in_formula("+1e3").is_empty(), "numbers");
assert!(functions_in_formula("+").is_empty() && functions_in_formula("- SUM(1)").is_empty(), "not formulas");
}
#[test]
fn commands_give_their_formulas_newest_first() {
let read = |id: &str, params: Json| formulas_newest_first(id, &params, SCAN_BUDGET).into_iter().map(String::from).collect::<Vec<_>>();
assert_eq!(read("range.setValues", json!({"values": [["=SUM(1)", 2], ["x", "=PMT(1,2,3)"]]})), ["=PMT(1,2,3)", "=SUM(1)"]);
assert_eq!(read("cell.set", json!({"input": "=NPV(1,2)"})), ["=NPV(1,2)"]);
assert_eq!(read("range.setValues", json!({"values": [["+SUM(1)", "-5", "-ABS(2)"]]})), ["-ABS(2)", "+SUM(1)"]);
assert!(read("home.bold", json!({"input": "=IRR(1)"})).is_empty(), "only cell.* and range.* enter formulas");
assert!(read("range.setValues", json!({"values": [[["=FV(1,2,3)"]]]})).is_empty(), "deeper than rows of cells");
// A large paste is read from its end, within the budget.
let rows: Vec<Json> = (0..5_000).map(|i| json!([format!("=ABS({i})")])).collect();
let paste = json!({ "values": rows });
let last = formulas_newest_first("range.setValues", &paste, 3);
assert_eq!(last, ["=ABS(4999)", "=ABS(4998)", "=ABS(4997)"]);
}
#[test]
fn recent_keeps_ten_distinct_newest_first() {
let mut r = defaults();
assert!(r.iter().all(|n| known(n)), "the default list names known functions");
push(&mut r, &["PMT".into(), "NPV".into()]);
assert_eq!(r.len(), MAX);
assert_eq!(&r[..3], ["PMT", "NPV", "SUM"]);
assert_eq!(r.iter().filter(|n| *n == "PMT").count(), 1);
let many: Vec<String> =
["ABS", "ACOS", "AND", "CHOOSE", "DATE", "EXP", "FIND", "INDEX", "LEFT", "LN", "MOD", "NOW"].map(String::from).to_vec();
push(&mut r, &many);
assert_eq!(r, many[..MAX]);
}
#[test]
fn a_saved_list_is_read_leniently() {
let prefs = |v: Json| serde_json::from_value::<crate::Prefs>(v).unwrap();
assert_eq!(prefs(json!({"r1c1": true})).recent_functions, defaults(), "a prefs.json from before the list gets the default");
// A hostile value loses only itself, never the other preferences.
let bad = prefs(json!({"r1c1": true, "recentFunctions": 5}));
assert!(bad.r1c1);
assert_eq!(bad.recent_functions, defaults());
let mut odd: Vec<Json> = ["npv", "NOT_A_FUNCTION", "SUM", "NPV"].iter().map(|n| json!(n)).chain([json!(3), json!(null)]).collect();
odd.extend(crate::cmd::formulas::function_list().into_iter().take(40).map(|f| f["name"].clone()));
let odd = prefs(json!({ "recentFunctions": odd }));
assert_eq!(&odd.recent_functions[..2], ["NPV", "SUM"]);
assert_eq!(odd.recent_functions.len(), MAX);
let back = prefs(serde_json::to_value(&odd).unwrap());
assert_eq!(back.recent_functions, odd.recent_functions, "the list survives a save and reload");
}
}
+5 -98
View File
@@ -8,7 +8,6 @@
use serde::Serialize;
use serde_json::Value as Json;
use std::collections::BTreeMap;
use crate::Selection;
@@ -19,11 +18,6 @@ pub const RECORD_LIMIT: usize = 100_000;
/// without a size cap a recording of a few opens of a large workbook keeps tens of MB alive.
pub const RECORD_MAX_BYTES: usize = 64 * 1024 * 1024;
/// Commands never recorded: recovery actions and the debugger, not steps of the person's work. A
/// recording that replayed Stop Action would stop the run it is a step of, and the debugger runs
/// on its own, never as a step of a script or macro.
pub const NOT_RECORDED: [&str; 2] = ["automate.stopAction", "macro.debug"];
/// One recorded command and where it ran.
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
@@ -40,120 +34,33 @@ pub struct RecordedStep {
pub params: Json,
}
/// Numbers each recording, so an undo group's mark from an earlier one isn't read as this one's.
static NEXT_RECORDING: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
/// The steps recorded since recording started.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Default)]
pub struct Recorder {
pub steps: Vec<RecordedStep>,
/// Steps not kept because the recording reached [`RECORD_LIMIT`] or [`RECORD_MAX_BYTES`].
/// Once a step is dropped, every later one is too, so `steps` is always the beginning of
/// what ran, never a sequence with gaps.
pub dropped: usize,
/// The size of `steps` and `ran` serialized as JSON.
/// The size of `steps` serialized as JSON.
bytes: usize,
/// What a step ran, by its index in `steps`, for a command that runs steps stored in the
/// workbook (`automate.runScript`, `macro.run`): the commands as they ran, so a recording
/// keeps them even when the script or macro is edited or deleted before the recording stops.
/// A run without an entry is left out when the recording is used.
pub ran: BTreeMap<usize, Vec<(String, Json)>>,
/// Which recording this is, and the number the next kept step gets: each step's number is in
/// `seqs`, beside it, so a mark stays right however many steps rollbacks take out.
generation: u64,
next_seq: u64,
seqs: Vec<u64>,
}
impl Default for Recorder {
fn default() -> Self {
let generation = NEXT_RECORDING.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Recorder { steps: Vec::new(), dropped: 0, bytes: 0, ran: BTreeMap::new(), generation, next_seq: 0, seqs: Vec::new() }
}
}
/// `params` with the state a toggle set (Bold's `on`, from the command's result), as Excel
/// records `Bold = True`: playing the step back sets that state rather than flipping it again.
pub(crate) fn with_toggle_state(mut params: Json, result: &Json) -> Json {
if let Some(on @ Json::Bool(_)) = result.get("on") {
if params.is_null() {
params = Json::Object(serde_json::Map::new());
}
if let Json::Object(p) = &mut params
&& !p.contains_key("on")
{
p.insert("on".into(), on.clone());
}
}
params
}
impl Recorder {
/// Adds a step, or counts it as dropped once the buffer is full; `true` when it was kept.
pub fn push(&mut self, step: RecordedStep) -> bool {
/// Adds a step, or counts it as dropped once the buffer is full.
pub fn push(&mut self, step: RecordedStep) {
// Once a step has been dropped, or the step count is full, the rest are counted without
// being serialized (a `file.open` step holds a whole file).
if self.dropped > 0 || self.steps.len() >= RECORD_LIMIT {
self.dropped = self.dropped.saturating_add(1);
return false;
return;
}
let size = serde_json::to_vec(&step).map_or(0, |v| v.len());
if self.bytes.saturating_add(size) <= RECORD_MAX_BYTES {
self.bytes = self.bytes.saturating_add(size);
self.steps.push(step);
self.seqs.push(self.next_seq);
self.next_seq = self.next_seq.saturating_add(1);
true
} else {
self.dropped = self.dropped.saturating_add(1);
false
}
}
/// Attaches what the last kept step ran ([`Recorder::ran`]), within the byte budget: past it,
/// the step stays without it, and the recording leaves the run out and lists it as skipped.
pub fn attach_ran(&mut self, steps: Vec<(String, Json)>) {
let Some(index) = self.steps.len().checked_sub(1) else { return };
let size = serde_json::to_vec(&steps).map_or(0, |v| v.len());
if self.bytes.saturating_add(size) <= RECORD_MAX_BYTES {
self.bytes = self.bytes.saturating_add(size);
self.ran.insert(index, steps);
}
}
/// Where the recording is now, for [`Recorder::cut_back`]: this recording and the number its
/// next step gets.
pub(crate) fn mark(&self) -> (u64, u64) {
(self.generation, self.next_seq)
}
/// Takes back the steps recorded in the document with uid `doc` since `mark` (an undo group
/// on it was rolled back, so playing them back would make edits it no longer has). Steps in
/// other documents stay: their edits do too. Steps the buffer dropped stay counted.
/// What a step ran ([`Recorder::ran`]) goes with its step, and stays with a kept one under its
/// new index. A mark from another recording (the recording started again inside the group)
/// means every step of this one came after it.
pub(crate) fn cut_back(&mut self, (generation, seq): (u64, u64), doc: u64) {
let from = if generation == self.generation { seq } else { 0 };
let gone = |i: usize, step: &RecordedStep| step.doc == doc && self.seqs.get(i).is_some_and(|&n| n >= from);
if !self.steps.iter().enumerate().any(|(i, s)| gone(i, s)) {
return;
}
let mut ran = std::mem::take(&mut self.ran);
let (mut steps, mut seqs) = (Vec::with_capacity(self.steps.len()), Vec::with_capacity(self.seqs.len()));
for (i, step) in self.steps.iter().enumerate() {
if gone(i, step) {
continue;
}
if let Some(r) = ran.remove(&i) {
self.ran.insert(steps.len(), r);
}
steps.push(step.clone());
seqs.push(self.seqs.get(i).copied().unwrap_or(0));
}
(self.steps, self.seqs) = (steps, seqs);
let size = |v: Result<Vec<u8>, serde_json::Error>| v.map_or(0, |v| v.len());
self.bytes = self.steps.iter().map(|s| size(serde_json::to_vec(s))).sum::<usize>()
+ self.ran.values().map(|r| size(serde_json::to_vec(r))).sum::<usize>();
}
}
-228
View File
@@ -1081,48 +1081,6 @@ fn sort_and_filter() {
assert_eq!(r["hiddenRows"], 1);
}
/// Delete and the Clear commands on a filtered selection clear only the rows showing, as in
/// Excel (#1419); rows hidden by hand are cleared with the rest.
#[test]
fn clearing_a_filtered_selection_leaves_the_rows_the_filter_hides() {
let mut s = s();
let rows = [["Name", "Status"], ["John", "Active"], ["Mary", "Inactive"], ["David", "Active"], ["Sarah", "Inactive"], ["Michael", "Active"]];
s.execute("range.setValues", json!({"range": "A1", "values": rows})).unwrap();
s.execute("home.bold", json!({"range": "A2:A6", "on": true})).unwrap();
s.execute("review.newNote", json!({"cell": "A3", "text": "note", "author": "Example"})).unwrap();
s.execute("selection.set", json!({"cell": "A1"})).unwrap();
s.execute("data.filter", json!({})).unwrap();
s.execute("data.filterBy", json!({"column": "B", "values": ["Active"]})).unwrap();
let bold = |s: &Session, a: &str| {
let d = s.doc().unwrap();
d.wb.styles.get(d.wb.active().unwrap().style_id(CellRef::parse(a).unwrap())).font.bold
};
s.execute("edit.clearContents", json!({"range": "A2:A6"})).unwrap();
for a in ["A2", "A4", "A6"] {
assert_eq!(v(&s, a), Value::Empty, "{a} shows and is cleared");
}
assert_eq!(v(&s, "A3"), Value::from("Mary"));
assert_eq!(v(&s, "A5"), Value::from("Sarah"));
s.execute("edit.clearFormats", json!({"range": "A2:A6"})).unwrap();
assert!(!bold(&s, "A2"));
assert!(bold(&s, "A3"), "a filtered-out row keeps its format");
s.execute("edit.clearAll", json!({"range": "A2:B6"})).unwrap();
assert_eq!(v(&s, "B2"), Value::Empty);
assert_eq!(v(&s, "B3"), Value::from("Inactive"));
assert_eq!(v(&s, "A5"), Value::from("Sarah"));
assert!(s.doc().unwrap().wb.active().unwrap().comments.contains_key(&CellRef::parse("A3").unwrap()));
// Without the filter, a row hidden by hand is cleared like the rows around it.
s.execute("data.clearFilter", json!({})).unwrap();
s.execute("home.hideRows", json!({"rows": "5:5"})).unwrap();
s.execute("edit.clearContents", json!({"range": "A3:A5"})).unwrap();
assert_eq!(v(&s, "A3"), Value::Empty);
assert_eq!(v(&s, "A5"), Value::Empty);
}
/// Top 10 keeps the top or bottom N items or percent, ties at the threshold included.
#[test]
fn filter_top10_items_and_percent() {
@@ -1400,17 +1358,6 @@ fn protection_refuses_edits_of_locked_cells() {
assert_eq!(v(&s, "A1"), Value::Number(1.0));
}
#[test]
fn locked_cell_message_matches_what_an_edit_would_be_refused_with() {
let s = protected(json!({}));
let locked = CellRef::parse("A1").unwrap();
let unlocked = CellRef::parse("D1").unwrap();
assert!(s.locked_cell_message(0, locked).is_some_and(|m| m.contains("protected sheet")));
assert_eq!(s.locked_cell_message(0, unlocked), None, "an unlocked cell stays editable");
assert_eq!(s.locked_cell_message(9, locked), None, "no such sheet");
}
#[test]
fn protection_allows_permitted_row_and_column_edits() {
let mut s = protected(json!({"insertRows": true, "deleteRows": true, "insertColumns": true}));
@@ -2018,15 +1965,6 @@ fn a_whole_column_table_without_headers_stays_on_the_sheet() {
assert!(e.to_string().contains("pushed off the end"), "{e}");
}
#[test]
fn inserting_a_column_next_to_far_right_data_names_gridcraft_in_the_refusal() {
let mut sess = s();
sess.execute("cell.set", json!({"cell": "XFD1", "input": "2"})).unwrap();
let e = sess.execute("home.insertColumns", json!({"cols": "A:A"})).unwrap_err();
assert!(e.to_string().contains("GridCraft cannot shift nonblank cells"), "{e}");
assert!(!e.to_string().contains("Excel"), "{e}");
}
#[test]
fn inserting_cells_refuses_to_push_values_off_the_sheet() {
for (shift, edge, last_fit, last_ok) in [("right", "XFD1", "XFC1", "XFD1"), ("down", "A1048576", "A1048575", "A1048576")] {
@@ -2035,7 +1973,6 @@ fn inserting_cells_refuses_to_push_values_off_the_sheet() {
sess.execute("cell.set", json!({"cell": edge, "input": "2"})).unwrap();
let e = sess.execute("home.insertCells", json!({"range": "A1", "shift": shift})).unwrap_err();
assert!(e.to_string().contains("loss of data"), "{e}");
assert!(e.to_string().contains("GridCraft") && !e.to_string().contains("Excel"), "{e}");
assert_eq!(v(&sess, "A1"), Value::Number(1.0));
assert_eq!(v(&sess, edge), Value::Number(2.0));
// A value that still fits moves to the last valid cell.
@@ -2414,36 +2351,6 @@ fn deleting_a_3d_span_end_sheet_keeps_the_rest() {
}
}
#[test]
fn workbook_view_roundtrips_after_sheet_deletion_and_reordering() {
let view = gridcraft_model::WorkbookView { show_sheet_tabs: false, show_horizontal_scroll: true, show_vertical_scroll: false, tab_ratio: 350 };
for (initial_active, command, params, expected_order, expected_active) in [
("Third", "home.deleteSheet", json!({"sheet": "First"}), vec!["Second", "Third"], 1),
("Third", "home.deleteSheet", json!({"sheet": "Third"}), vec!["First", "Second"], 1),
("First", "home.deleteSheet", json!({"sheet": "Third"}), vec!["First", "Second"], 0),
("Third", "sheet.move", json!({"sheet": "Third", "to": 0}), vec!["Third", "First", "Second"], 0),
("Third", "sheet.move", json!({"sheet": "First", "to": 2}), vec!["Second", "Third", "First"], 2),
] {
let mut session = s();
session.execute("sheet.rename", json!({"name": "First"})).unwrap();
for name in ["Second", "Third"] {
session.execute("home.insertSheet", json!({"name": name})).unwrap();
}
std::sync::Arc::make_mut(&mut session.doc_mut().unwrap().wb).view = view;
session.execute("sheet.activate", json!({"sheet": initial_active})).unwrap();
session.execute(command, params).unwrap();
let wb = &session.doc().unwrap().wb;
assert_eq!(wb.active_sheet(), expected_active, "{command}");
assert_eq!(wb.sheets.iter().map(|s| s.name.as_str()).collect::<Vec<_>>(), expected_order, "{command}");
let (back, _) = gridcraft_xlsx::read_xlsx(&gridcraft_xlsx::write_xlsx(wb).unwrap()).unwrap();
assert_eq!(back.sheets.iter().map(|s| s.name.as_str()).collect::<Vec<_>>(), expected_order, "{command} after reopening");
assert_eq!(back.active_sheet(), expected_active, "activeTab follows {command}");
assert_eq!(back.active().unwrap().name, expected_order[expected_active], "the active sheet is not a stale index");
assert_eq!(back.view, view, "workbook display settings survive {command}");
}
}
#[test]
fn xlsx_roundtrip_through_engine() {
let mut s = s();
@@ -2551,53 +2458,6 @@ fn saving_an_opened_semicolon_csv_keeps_semicolons() {
std::fs::remove_dir_all(dir).unwrap();
}
/// `file.exportCsv` writes displayed text by default (Excel's Save As CSV); `values: true` writes the
/// raw values for automation: full precision, ISO dates and times.
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn export_csv_values_writes_raw_values_and_the_default_stays_displayed_text() {
let mut s = s();
for (cell, input, code) in [
("A1", "1234.5", "$#,##0.00"),
("B1", "0.123456789", "0.00"),
("C1", "46023", "d-mmm-yy"),
("D1", "46023.5", "yyyy-mm-dd hh:mm"),
("E1", "0.75", "h:mm"),
("F1", "TRUE", "General"),
("G1", "a,b", "General"),
("H1", "=1/0", "General"),
("I1", "1", "h:mm"),
("J1", "1.5", "h:mm"),
("K1", "46023", "yyyy-mm-dd hh:mm"),
("L1", "1.5", "[h]:mm"),
("M1", "2.5", "[>=3]yyyy-mm-dd;[<3]hh:mm"),
] {
s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap();
s.execute("home.numberFormat", json!({"range": cell, "code": code})).unwrap();
}
#[allow(clippy::disallowed_methods)] // native-only test: a unique temp dir name
let nonce = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
let dir = std::env::temp_dir().join(format!("gridcraft-csv-values-{}-{nonce}", std::process::id()));
std::fs::create_dir(&dir).unwrap();
let displayed = dir.join("displayed.csv");
s.execute("file.exportCsv", json!({"path": displayed})).unwrap();
s.execute("file.exportCsv", json!({"path": dir.join("off.csv"), "values": false})).unwrap();
let raw = dir.join("raw.csv");
s.execute("file.exportCsv", json!({"path": raw, "values": true})).unwrap();
let saved = s.execute("file.saveBytes", json!({"format": "csv"})).unwrap();
let default = crate::io::base64_decode(saved["base64"].as_str().unwrap()).unwrap();
// The default export is byte for byte what Save As CSV writes, and `values: false` is the default.
assert_eq!(std::fs::read(&displayed).unwrap(), default);
assert_eq!(std::fs::read(dir.join("off.csv")).unwrap(), default);
let default = String::from_utf8(default).unwrap();
assert!(default.starts_with("\"$1,234.50\",0.12,1-Jan-26,"), "{default}");
assert_eq!(
std::fs::read_to_string(&raw).unwrap(),
"1234.5,0.123456789,2026-01-01,2026-01-01T12:00:00,18:00:00,TRUE,\"a,b\",#DIV/0!,00:00:00,12:00:00,2026-01-01T00:00:00,1.5,12:00:00\r\n"
);
std::fs::remove_dir_all(dir).unwrap();
}
/// A workbook whose B1 is `=[1]Sheet1!A1`, linked to `link` (an external relationship target).
fn linked_workbook(link: &str) -> Vec<u8> {
use std::io::{Cursor, Write};
@@ -2976,24 +2836,6 @@ fn checkboxes_keep_values_toggle_and_delete_in_two_passes() {
assert_ne!(fmt_of(&s, "B2"), "checkbox");
}
/// A checked box in a row the filter hides doesn't hold back Delete on the boxes showing, and
/// stays as it is (#1419).
#[test]
fn delete_on_filtered_checkboxes_skips_the_hidden_rows() {
let mut s = s();
s.execute("range.setValues", json!({"range": "A1", "values": [["Status"], ["on"], ["off"], ["on"]]})).unwrap();
s.execute("insert.checkbox", json!({"range": "B2:B4"})).unwrap();
s.execute("cell.toggleCheckbox", json!({"cell": "B3"})).unwrap();
s.execute("selection.set", json!({"cell": "A1"})).unwrap();
s.execute("data.filter", json!({})).unwrap();
s.execute("data.filterBy", json!({"column": "A", "values": ["on"]})).unwrap();
s.execute("edit.clearContents", json!({"range": "B2:B4"})).unwrap();
assert_ne!(fmt_of(&s, "B2"), "checkbox", "the unchecked boxes showing are removed at once");
assert_ne!(fmt_of(&s, "B4"), "checkbox");
assert_eq!(fmt_of(&s, "B3"), "checkbox");
assert_eq!(v(&s, "B3"), Value::Bool(true));
}
#[test]
fn checkboxes_survive_xlsx_as_cell_controls() {
let mut s = s();
@@ -5243,7 +5085,6 @@ fn a_script_cannot_switch_windows() {
assert_eq!(v(&s, "A1"), Value::number(2.0));
}
mod macros;
mod seams;
#[test]
@@ -5275,72 +5116,3 @@ fn unique_nonce() -> u64 {
static N: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
N.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
}
/// Normal, Page Layout and Page Break Preview are not undo steps (as in Excel), so undoing or
/// redoing an edit keeps each sheet in the view it is in now, instead of putting back the view
/// of the snapshot it swaps in. Found through the command-sequence fuzz (seed 177).
#[test]
fn undo_and_redo_keep_each_sheets_view() {
use gridcraft_model::SheetViewMode;
let view = |s: &Session, i: usize| s.doc().unwrap().wb.sheet(i).unwrap().view;
let mut s = s();
s.execute("cell.set", json!({"cell": "A1", "input": "1"})).unwrap();
s.execute("view.pageBreakPreview", json!({})).unwrap();
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(v(&s, "A1"), Value::Empty);
assert_eq!(view(&s, 0), SheetViewMode::PageBreakPreview, "undo leaves the view alone");
s.execute("view.pageLayout", json!({})).unwrap();
s.execute("edit.redo", json!({})).unwrap();
assert_eq!(v(&s, "A1"), Value::Number(1.0));
assert_eq!(view(&s, 0), SheetViewMode::PageLayout, "redo leaves the view alone");
// Undoing a rename: the sheet is found at its position under the restored name.
s.execute("sheet.rename", json!({"sheet": 0, "name": "Renamed"})).unwrap();
s.execute("view.normal", json!({})).unwrap();
s.execute("edit.undo", json!({})).unwrap();
assert_ne!(s.doc().unwrap().wb.sheet(0).unwrap().name, "Renamed");
assert_eq!(view(&s, 0), SheetViewMode::Normal);
// Undoing a new sheet: the other sheets keep theirs.
s.execute("view.pageBreakPreview", json!({})).unwrap();
s.execute("home.insertSheet", json!({"name": "Extra"})).unwrap();
s.execute("edit.undo", json!({})).unwrap();
assert_eq!(s.doc().unwrap().wb.sheets.len(), 1);
assert_eq!(view(&s, 0), SheetViewMode::PageBreakPreview);
}
#[test]
fn a_multi_step_undo_keeps_views_through_each_step() {
// A rename and a new sheet undone in one call: neither the name nor the sheet count matches
// between the first and last workbook, so the view is carried step by step.
let mut s = s();
let first = s.doc().unwrap().wb.sheet(0).unwrap().name.clone();
s.execute("sheet.rename", json!({"sheet": 0, "name": "Renamed"})).unwrap();
s.execute("view.pageBreakPreview", json!({"sheet": 0})).unwrap();
s.execute("home.insertSheet", json!({"name": "Extra"})).unwrap();
s.execute("edit.undo", json!({"steps": 2})).unwrap();
let d = s.doc().unwrap();
assert_eq!(d.wb.sheets.len(), 1);
assert_eq!(d.wb.sheet(0).unwrap().name, first);
assert_eq!(d.wb.sheet(0).unwrap().view, gridcraft_model::SheetViewMode::PageBreakPreview);
s.execute("edit.redo", json!({"steps": 2})).unwrap();
let d = s.doc().unwrap();
assert_eq!((d.wb.sheets.len(), d.wb.sheet(0).unwrap().name.as_str()), (2, "Renamed"));
assert_eq!(d.wb.sheet(0).unwrap().view, gridcraft_model::SheetViewMode::PageBreakPreview);
}
#[test]
fn a_view_kept_through_redo_doesnt_make_the_saved_workbook_dirty() {
let mut s = s();
s.execute("cell.set", json!({"cell": "A1", "input": "1"})).unwrap();
{
let d = s.doc_mut().unwrap();
d.saved = d.wb.clone();
}
s.execute("edit.undo", json!({})).unwrap();
s.execute("view.pageBreakPreview", json!({})).unwrap();
s.execute("edit.redo", json!({})).unwrap();
let d = s.doc().unwrap();
assert_eq!(d.wb.active().unwrap().view, gridcraft_model::SheetViewMode::PageBreakPreview);
assert!(!d.is_dirty(), "back at the saved edits, the view alone isn't a change");
}
File diff suppressed because it is too large Load Diff
-85
View File
@@ -820,91 +820,6 @@ fn a_call_that_only_opens_a_dialog_is_not_recorded() {
assert_eq!(s.stop_recording().unwrap().dropped, 0);
}
#[test]
fn stop_action_runs_only_on_its_own_and_is_not_recorded() {
// A step of an action in progress may not stop the action: nested, the command is refused
// and the action's group stays. Nor is Stop Action a step of the person's work: a recording
// leaves it out, so playing the recording back never stops the run it is part of.
let mut s = s();
s.start_recording();
let r: crate::Result<()> = s.grouped("Outer", |s| {
set(s, "A1", "one");
let err = s.execute_nested("automate.stopAction", json!({})).unwrap_err().to_string();
assert!(err.contains("on its own"), "{err}");
set(s, "A2", "two");
Ok(())
});
r.unwrap();
assert_eq!(v(&s, "A2").display(), "two");
assert_eq!(undo_labels(&s), ["Outer"], "the action kept its group");
// On its own it stops a group left open; the recording keeps neither the stop nor the edits
// the stop rolled back, so playing it back makes only the edits the workbook has.
let _left = s.group_begin("Left open").unwrap();
set(&mut s, "A3", "three");
assert_eq!(s.execute("automate.stopAction", json!({})).unwrap(), json!({"stopped": true}));
assert!(v(&s, "A3").is_empty());
assert_eq!(recorded_ids(&s), ["cell.set", "cell.set"]);
// An edit recorded in another workbook while the group was open stays, as its edit does.
let first = s.active_index();
let _left = s.group_begin("Left open").unwrap();
set(&mut s, "A4", "four");
s.new_workbook();
set(&mut s, "B1", "other");
s.set_active(first);
assert_eq!(s.execute("automate.stopAction", json!({})).unwrap(), json!({"stopped": true}));
assert_eq!(recorded_ids(&s), ["cell.set", "cell.set", "cell.set"], "the other workbook's edit stays");
assert_eq!(s.recorder.as_ref().unwrap().steps.last().map(|st| st.params["cell"].clone()), Some(json!("B1")));
assert_eq!(s.stop_recording().unwrap().dropped, 0);
}
#[test]
fn rollbacks_take_back_their_own_recorded_steps_in_any_order() {
// Groups open on two workbooks, each with a recorded edit: rolling back the first takes out
// only its edit, and the second's mark still finds its own after that.
let mut s = s();
s.start_recording();
let a = s.active_index();
let _in_a = s.group_begin("A").unwrap();
set(&mut s, "A1", "a");
s.new_workbook();
let b = s.active_index();
let _in_b = s.group_begin("B").unwrap();
set(&mut s, "B1", "b");
s.set_active(a);
s.execute("automate.stopAction", json!({})).unwrap();
let cells = |s: &Session| s.recorder.as_ref().unwrap().steps.iter().map(|st| st.params["cell"].clone()).collect::<Vec<_>>();
assert_eq!(cells(&s), [json!("B1")]);
s.set_active(b);
s.execute("automate.stopAction", json!({})).unwrap();
assert!(cells(&s).is_empty());
// A recording started again inside a group: every step of it came after the group began.
let _again = s.group_begin("Again").unwrap();
s.start_recording();
set(&mut s, "C1", "c");
s.execute("automate.stopAction", json!({})).unwrap();
assert!(cells(&s).is_empty(), "{:?}", cells(&s));
}
#[test]
fn a_run_that_edits_and_opens_a_dialog_is_recorded() {
// A script with an edit before a menu-style step: the edit happened, so the run is a step of
// the recording (the person's confirming call is another). Only a call that opened a dialog
// and changed nothing is left out.
let mut s = s();
let edit_then_dialog = json!([{"command": "cell.set", "params": {"cell": "B1", "input": "1"}}, {"command": "home.formatCells", "params": {}}]);
let dialog_only = json!([{"command": "home.formatCells", "params": {}}]);
s.execute("automate.newScript", json!({"name": "Edit", "commands": edit_then_dialog})).unwrap();
s.execute("automate.newScript", json!({"name": "Ask", "commands": dialog_only})).unwrap();
s.start_recording();
s.execute("automate.runScript", json!({"name": "Ask"})).unwrap();
assert!(s.take_ui_requests().iter().any(|q| matches!(q, crate::UiRequest::Dialog(..))));
assert!(recorded_ids(&s).is_empty(), "{:?}", recorded_ids(&s));
s.execute("automate.runScript", json!({"name": "Edit"})).unwrap();
assert!(s.take_ui_requests().iter().any(|q| matches!(q, crate::UiRequest::Dialog(..))));
assert_eq!(recorded_ids(&s), ["automate.runScript"], "the edit happened, so the run is a step");
assert_eq!(s.stop_recording().unwrap().dropped, 0);
}
#[test]
fn the_recorder_keeps_top_level_calls_with_where_they_ran() {
let mut s = s();
@@ -34,7 +34,7 @@ use std::sync::Arc;
use gridcraft_core::{CellRef, MAX_COLS, MAX_ROWS, RangeRef, Value, col_to_letters};
use gridcraft_engine::calc::Calc;
use gridcraft_engine::model::{CalcMode, Formula, Sheet, SheetViewMode, Workbook};
use gridcraft_engine::model::{CalcMode, Formula, Sheet, Workbook};
use gridcraft_engine::{EngineError, Session, command_specs, find_command, io};
use serde_json::{Value as Json, json};
@@ -486,9 +486,8 @@ fn watch_list(wb: &Workbook) -> Option<&str> {
wb.names.iter().find(|n| n.name == WATCH_NAME).map(|n| n.formula.as_str())
}
/// The workbook with view state (active sheet, scroll, active cell, Normal / Page Layout / Page
/// Break Preview) and the Watch Window list reset: undo restores view state as it was, but
/// switching sheets or views isn't an undoable edit.
/// The workbook with view state (active sheet, scroll, active cell) and the Watch Window list
/// reset: undo restores view state as it was, but switching sheets isn't an undoable edit.
fn canonical(wb: &Workbook) -> Workbook {
let mut wb = wb.clone();
wb.set_active_sheet(0);
@@ -497,7 +496,6 @@ fn canonical(wb: &Workbook) -> Workbook {
let sh = Arc::make_mut(sh);
sh.view_active = CellRef::default();
sh.view_top_left = CellRef::default();
sh.view = SheetViewMode::Normal;
}
wb
}
@@ -1091,13 +1089,3 @@ fn params_docs_parse_into_keys() {
assert!(find_command(id).is_some(), "CURATED names unknown command `{id}`");
}
}
/// Seed 177: switching a sheet to Page Break Preview isn't an undo step (as in Excel), so undoing
/// everything leaves the view mode alone and the undo check must not count it as an edit.
#[test]
fn view_mode_switch_is_not_an_undo_difference() {
for id in ["view.pageBreakPreview", "view.pageLayout"] {
let failure = replay("sales", &[(id.to_string(), json!({"sheet": 0}))]);
assert!(failure.is_none(), "`{id}`: {failure:?}");
}
}
-3
View File
@@ -22,13 +22,10 @@ mod percent_entry;
mod pivot_unknown_func;
mod pivotchart_link;
mod range_fill_formats;
mod recently_used;
mod row_autofit;
mod sheet_copy_spill;
mod sheet_name_quoting;
mod sparse_clear_invalidation;
mod stored_list_shape;
mod table_resize_overlap;
mod table_total_row_last_row;
mod workbook_author;
mod wrap_row_height;
-58
View File
@@ -1,58 +0,0 @@
//! Formulas › Recently Used: the functions most recently entered in a formula, from any command
//! that enters one, kept in the preferences rather than read back from the journal.
use gridcraft_engine::Session;
use serde_json::{Value as Json, json};
fn recent(s: &mut Session) -> Vec<String> {
let list = s.execute("formulas.recentlyUsed", json!({})).unwrap();
list.as_array().unwrap().iter().filter_map(Json::as_str).map(String::from).collect()
}
#[test]
fn formulas_entered_by_any_command_lead_the_list() {
let mut s = Session::new();
s.new_workbook();
s.execute("cell.set", json!({"cell": "A1", "input": "=NPV(0.1,1,2)"})).unwrap();
s.execute("cell.set", json!({"cell": "A2", "input": "=IRR({-10,6,6})+ROUND(1,0)"})).unwrap();
s.execute("range.setValues", json!({"range": "B1", "values": [["=FV(0.1,2,-1)", 4, "text"]]})).unwrap();
s.execute("cell.set", json!({"cell": "A3", "input": "PMT is just text"})).unwrap();
assert_eq!(&recent(&mut s)[..4], ["FV", "IRR", "ROUND", "NPV"]);
// A command that fails enters nothing.
assert!(s.execute("cell.set", json!({"cell": "not a cell", "input": "=DB(1,2,3,4)"})).is_err());
assert_ne!(recent(&mut s)[0], "DB");
}
#[test]
fn formulas_typed_with_a_plus_or_minus_count() {
let mut s = Session::new();
s.new_workbook();
s.execute("cell.set", json!({"cell": "A1", "input": "+ROUND(1.5,0)"})).unwrap();
s.execute("cell.set", json!({"cell": "A2", "input": "-SLN(100,10,5)"})).unwrap();
s.execute("cell.set", json!({"cell": "A3", "input": "-5"})).unwrap();
assert_eq!(&recent(&mut s)[..2], ["SLN", "ROUND"]);
}
#[test]
fn the_list_does_not_depend_on_the_journal() {
// The journal drops its oldest entries past its caps (and is public); the list stays.
let mut s = Session::new();
s.new_workbook();
s.execute("cell.set", json!({"cell": "A1", "input": "=NPV(0.1,1,2)"})).unwrap();
s.journal.clear();
assert_eq!(recent(&mut s)[0], "NPV");
s.execute("cell.set", json!({"cell": "A2", "input": "=SLN(100,10,5)"})).unwrap();
assert_eq!(&recent(&mut s)[..2], ["SLN", "NPV"]);
}
#[test]
fn the_list_is_kept_with_the_preferences() {
let mut s = Session::new();
s.new_workbook();
s.execute("cell.set", json!({"cell": "A1", "input": "=SYD(100,10,5,1)"})).unwrap();
let saved = serde_json::to_value(&s.prefs).unwrap();
let mut next = Session::new();
next.new_workbook();
next.prefs = serde_json::from_value(saved).unwrap();
assert_eq!(recent(&mut next)[0], "SYD");
}
@@ -1,33 +0,0 @@
use gridcraft_core::{CellRef, Value};
use gridcraft_engine::Session;
use gridcraft_engine::calc::Calc;
use serde_json::{Value as Json, json};
fn run(s: &mut Session, command: &str, params: Json) {
s.execute(command, params).unwrap();
}
fn value(s: &Session, sheet: &str, cell: &str) -> Value {
let wb = &s.doc().unwrap().wb;
wb.sheet(wb.sheet_index(sheet).unwrap()).unwrap().value(CellRef::parse(cell).unwrap())
}
/// Issue #2053: the copy of a sheet holding a spill keeps the spill's values, in memory and after a full recalc.
#[test]
fn copied_sheet_keeps_spill_values_through_full_recalc() {
let mut s = Session::new();
run(&mut s, "file.new", json!({"sample": "grades"}));
run(&mut s, "home.mergeCells", json!({"range": "F:H"}));
run(&mut s, "cell.set", json!({"cell": "J54", "input": "=SEQUENCE(5)*C6"}));
run(&mut s, "selection.set", json!({"range": "15:15"}));
run(&mut s, "sheet.move", json!({"copy": true, "sheet": 0, "to": 1}));
run(&mut s, "cell.set", json!({"array": true, "cell": "D3", "input": "=SUM('Grades (2)'!J54#)"}));
let before = value(&s, "Grades (2)", "J55");
assert!(matches!(before, Value::Number(_)), "J55 should hold a spilled number, got {before:?}");
let mut fresh = (*s.doc().unwrap().wb).clone();
Calc::new().recalc_all(&mut fresh);
let after = fresh.sheet(fresh.sheet_index("Grades (2)").unwrap()).unwrap().value(CellRef::parse("J55").unwrap());
assert_eq!(before, after);
}
-50
View File
@@ -1,50 +0,0 @@
//! Regression tests for the default author of newly created workbooks.
use gridcraft_engine::{DocState, Session, Trust, io, model::Workbook};
#[test]
fn new_workbook_stamps_the_configured_user_name_at_creation() {
let mut session = Session::new();
session.prefs.user_name = "Ada Lovelace".into();
let first = session.new_workbook();
assert_eq!(session.documents()[first].wb.props.author, "Ada Lovelace");
// Changing the preference must not rewrite an existing workbook.
session.prefs.user_name = "Grace Hopper".into();
assert_eq!(session.documents()[first].wb.props.author, "Ada Lovelace");
// A new workbook uses the preference as it exists at creation.
let second = session.new_workbook();
assert_eq!(session.documents()[second].wb.props.author, "Grace Hopper");
assert_eq!(session.documents()[first].wb.props.author, "Ada Lovelace");
}
#[test]
fn opening_and_resaving_preserves_the_existing_workbook_author() {
// An existing XLSX has its own author metadata.
let mut source = Workbook::new();
source.props.author = "Original Spreadsheet Author".into();
let bytes = io::save_bytes(&source, "source.xlsx").unwrap();
let (opened, _) = io::open_bytes("source.xlsx", &bytes).unwrap();
let mut session = Session::new();
session.prefs.user_name = "Current GridCraft User".into();
session.add_document(DocState::new(opened, None, "source".into(), Trust::FromFile));
// Opening an existing workbook must not replace its author.
assert_eq!(session.doc().unwrap().wb.props.author, "Original Spreadsheet Author");
// The original author must survive another XLSX save and reopen.
let saved = io::save_bytes(&session.doc().unwrap().wb, "saved.xlsx").unwrap();
let (reopened, _) = io::open_bytes("saved.xlsx", &saved).unwrap();
assert_eq!(reopened.props.author, "Original Spreadsheet Author");
}
+2 -2
View File
@@ -175,8 +175,8 @@ fn write_struct_ref(s: &StructRef, at_shorthand: bool) -> String {
let _ = write!(out, "[{}]", s.specifiers.first().map(item).unwrap_or(""));
}
(Some(c), None) if s.specifiers.is_empty() => {
// Names with spaces at their edges, or starting with `@` (this-row shorthand), use the double-bracket form.
if c.trim() == c && !c.starts_with('@') {
// Excel writes a name with spaces at its edges in the double-bracket form.
if c.trim() == c {
let _ = write!(out, "[{}]", esc(c));
} else {
let _ = write!(out, "[[{}]]", esc(c));
-9
View File
@@ -558,12 +558,3 @@ fn reference_shapes_print_unchanged() -> Result<(), Box<dyn std::error::Error>>
}
Ok(())
}
#[test]
fn column_starting_with_at_round_trips() {
for name in ["@a", "a@b"] {
let s = StructRef { table: "T".into(), specifiers: vec![], col_start: Some(name.into()), col_end: None };
let text = format!("={}", printer::struct_ref(&s));
assert_eq!(parse(&text).ok(), Some(Expr::Struct(s)), "printed as {text}");
}
}
+3 -10
View File
@@ -449,15 +449,13 @@ pub(crate) fn yearfrac_core(sys: DateSystem, s: i64, e: i64, basis: i64) -> R<f6
Ok(match basis {
0 => {
let (mut a, mut b) = (d1.day as i64, d2.day as i64);
// Excel tests the end-31 rule against the original start day, before a February end moves to 30.
let start_30 = a >= 30;
if last_of_feb(&d1) && last_of_feb(&d2) {
b = 30;
}
if last_of_feb(&d1) || a == 31 {
a = 30;
}
if start_30 && b == 31 {
if a == 30 && b == 31 {
b = 30;
}
((d2.year as i64 - d1.year as i64) * 360 + (d2.month as i64 - d1.month as i64) * 30 + b - a) as f64 / 360.0
@@ -819,13 +817,8 @@ mod tests {
#[test]
fn yearfrac_basis0_feb_end_to_31() {
// Excel 16: a February-end start keeps its original day for the end-31 test, so 31 days.
close(ev("YEARFRAC", vec![n(d(2021.0, 2.0, 28.0)), n(d(2021.0, 3.0, 31.0)), n(0.0)]), 0.086_111_111_111_111_11);
close(ev("YEARFRAC", vec![n(d(2020.0, 2.0, 29.0)), n(d(2020.0, 3.0, 31.0)), n(0.0)]), 0.086_111_111_111_111_11);
close(ev("YEARFRAC", vec![n(d(2021.0, 3.0, 31.0)), n(d(2021.0, 2.0, 28.0)), n(0.0)]), 0.086_111_111_111_111_11);
close(ev("YEARFRAC", vec![n(d(2021.0, 1.0, 31.0)), n(d(2021.0, 2.0, 28.0)), n(0.0)]), 0.077_777_777_777_777_78);
close(ev("YEARFRAC", vec![n(d(2021.0, 1.0, 30.0)), n(d(2021.0, 3.0, 31.0)), n(0.0)]), 1.0 / 6.0);
close(ev("YEARFRAC", vec![n(d(2020.0, 2.0, 29.0)), n(d(2021.0, 2.0, 28.0)), n(0.0)]), 1.0);
close(ev("YEARFRAC", vec![n(d(2021.0, 2.0, 28.0)), n(d(2021.0, 3.0, 31.0)), n(0.0)]), 30.0 / 360.0);
close(ev("YEARFRAC", vec![n(d(2020.0, 2.0, 29.0)), n(d(2020.0, 3.0, 31.0)), n(0.0)]), 30.0 / 360.0);
close(ev("YEARFRAC", vec![n(d(2021.0, 2.0, 28.0)), n(d(2021.0, 2.0, 28.0)), n(0.0)]), 0.0);
}
+14 -105
View File
@@ -25,7 +25,7 @@ fn base_source(a: &[Arg]) -> R<String> {
Ok(number_to_text(n))
}
Value::Empty => Ok(String::new()),
v => Ok(v.to_text()?.to_string()),
v => Ok(v.to_text()?.trim().to_string()),
}
}
@@ -62,15 +62,6 @@ fn format_base(v: i64, radix: u32, places: Option<f64>) -> R<Value> {
if v < min || v > max {
return Err(CellError::Num);
}
let places = places
.map(|p| {
let p = p.trunc();
if !(1.0..=10.0).contains(&p) {
return Err(CellError::Num);
}
Ok(p as usize)
})
.transpose()?;
if v < 0 {
let u = v + (1i64 << bits);
return text_val(to_radix(u, radix));
@@ -79,6 +70,11 @@ fn format_base(v: i64, radix: u32, places: Option<f64>) -> R<Value> {
match places {
None => text_val(s),
Some(p) => {
let p = p.trunc();
if !(1.0..=10.0).contains(&p) {
return Err(CellError::Num);
}
let p = p as usize;
if s.len() > p {
return Err(CellError::Num);
}
@@ -101,13 +97,7 @@ fn to_radix(mut v: i64, radix: u32) -> String {
}
fn places_arg(a: &[Arg]) -> R<Option<f64>> {
if !has(a, 1) {
return Ok(None);
}
if matches!(a.get(1).map(scalar), Some(Value::Bool(_))) {
return Err(CellError::Value);
}
Ok(Some(num(a, 1)?))
if has(a, 1) { Ok(Some(num(a, 1)?)) } else { Ok(None) }
}
fn convert_base(a: &[Arg], from: u32, to: u32) -> R<Value> {
@@ -185,9 +175,6 @@ fn bitxor(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
fn shift(a: &[Arg], left: bool) -> R<Value> {
let n = bit_operand(a, 0)?;
let s = num(a, 1)?.trunc();
if n == 0 {
return Ok(Value::Number(0.0));
}
if s.abs() > 53.0 {
return Err(CellError::Num);
}
@@ -261,26 +248,11 @@ im1!(imexp, |z: Cx| Ok(z.exp()));
im1!(imln, |z: Cx| z.ln());
im1!(imlog10, |z: Cx| z.ln().map(|l| Cx::new(l.re / std::f64::consts::LN_10, l.im / std::f64::consts::LN_10)));
im1!(imlog2, |z: Cx| z.ln().map(|l| Cx::new(l.re / std::f64::consts::LN_2, l.im / std::f64::consts::LN_2)));
// Excel takes the polar route, so IMSQRT("-4") is 1.22514845490862E-16+2i, not 2i. Its cosine
// reduces its argument with a 64-bit-mantissa pi, which is PI/2 + 2.5e-20 and shows up as the
// stray real part. `+ 0.0` turns a -0 imaginary part into +0, so the angle stays +pi.
// shortcut: parts above ~1E154 or below ~1E-155 keep the exact result (Excel gives #NUM! or 0), no known use.
const HALF_PI_EXCESS: f64 = 2.508_278_806_33e-20;
im1!(imsqrt, |z: Cx| {
let im = z.im + 0.0;
let theta = match z.re {
0.0 => {
if im == 0.0 {
0.0
} else {
std::f64::consts::FRAC_PI_2.copysign(im)
}
}
x if x > 0.0 => (im / x).atan(),
x => (im / x).atan() + if im < 0.0 { -std::f64::consts::PI } else { std::f64::consts::PI },
};
let (half, root) = (theta / 2.0, z.abs().sqrt());
Ok(Cx::new(root * (half.cos() + HALF_PI_EXCESS * half.abs().sin()), root * half.sin()))
let m = z.abs();
let re = ((m + z.re) / 2.0).sqrt();
let im = ((m - z.re) / 2.0).sqrt();
Ok(Cx::new(re, if z.im < 0.0 { -im } else { im }))
});
im1!(imsin, |z: Cx| Ok(z.sin()));
im1!(imcos, |z: Cx| Ok(z.cos()));
@@ -415,12 +387,6 @@ mod tests {
is_text(ev("BIN2OCT", vec![n(1111111111.0)]), "7777777777");
is_text(ev("DEC2BIN", vec![n(9.0), n(4.0)]), "1001");
is_text(ev("DEC2BIN", vec![n(-100.0)]), "1110011100");
is_err(ev("DEC2BIN", vec![n(-1.0), n(0.0)]), CellError::Num);
is_err(ev("DEC2HEX", vec![n(-1.0), n(11.0)]), CellError::Num);
is_err(ev("DEC2OCT", vec![n(-1.0), n(-1.0)]), CellError::Num);
is_err(ev("DEC2BIN", vec![n(1.0), crate::Arg::val(Value::Bool(true))]), CellError::Value);
is_text(ev("DEC2BIN", vec![n(-1.0), n(1.0)]), "1111111111");
is_text(ev("DEC2HEX", vec![n(-1.0), n(10.0)]), "FFFFFFFFFF");
is_err(ev("DEC2BIN", vec![n(512.0)]), CellError::Num);
is_text(ev("DEC2HEX", vec![n(100.0), n(4.0)]), "0064");
is_text(ev("DEC2HEX", vec![n(-54.0)]), "FFFFFFFFCA");
@@ -445,21 +411,6 @@ mod tests {
is_text(ev("OCT2HEX", vec![n(100.0), n(4.0)]), "0040");
is_text(ev("OCT2HEX", vec![n(7777777533.0)]), "FFFFFFFF5B");
is_err(ev("BIN2DEC", vec![n(102.0)]), CellError::Num);
for (f, input) in [
("BIN2DEC", " 10"),
("BIN2DEC", "10 "),
("BIN2DEC", "\t10"),
("BIN2HEX", " 10"),
("BIN2OCT", "10 "),
("HEX2DEC", " 10"),
("HEX2BIN", "10 "),
("HEX2OCT", "\t10"),
("OCT2DEC", " 10"),
("OCT2BIN", "10 "),
("OCT2HEX", "\t10"),
] {
is_err(ev(f, vec![t(input)]), CellError::Num);
}
is_err(ev("HEX2DEC", vec![t("GG")]), CellError::Num);
is_err(ev("BIN2DEC", vec![t("11111111111")]), CellError::Num);
is_err(ev("DEC2BIN", vec![t("abc")]), CellError::Value);
@@ -480,21 +431,6 @@ mod tests {
is_err(ev("BITAND", vec![n(1.5), n(5.0)]), CellError::Num);
is_err(ev("BITLSHIFT", vec![n(1.0), n(54.0)]), CellError::Num);
is_err(ev("BITLSHIFT", vec![n(2f64.powi(47)), n(1.0)]), CellError::Num);
for f in ["BITLSHIFT", "BITRSHIFT"] {
for shift in [-100.0, -54.0, -53.0, 53.0, 54.0, 100.0] {
close(ev(f, vec![n(0.0), n(shift)]), 0.0);
}
for shift in [-100.0, -54.0, 54.0, 100.0] {
is_err(ev(f, vec![n(1.0), n(shift)]), CellError::Num);
}
if f == "BITLSHIFT" {
is_err(ev(f, vec![n(1.0), n(53.0)]), CellError::Num);
} else {
close(ev(f, vec![n(1.0), n(53.0)]), 0.0);
}
is_err(ev(f, vec![n(0.0), t("bad")]), CellError::Value);
}
is_err(ev("BITRSHIFT", vec![n(-1.0), n(100.0)]), CellError::Num);
}
#[test]
@@ -573,36 +509,9 @@ mod tests {
cx(ev("IMLOG10", vec![t("3+4i")]), 0.698_970_004_336_018_8, 0.402_719_196_273_373_2);
cx(ev("IMLOG2", vec![t("3+4i")]), 2.321_928_094_887_362, 1.337_804_212_450_976_8);
cx(ev("IMSQRT", vec![t("1+i")]), 1.098_684_113_467_809_9, 0.455_089_860_562_227_3);
// Strings read back from Excel (16.0): its stray real part on negative reals included.
for (input, want) in [
("-4", "1.22514845490862E-16+2i"),
("-9", "1.83772268236293E-16+3i"),
("-1", "6.1257422745431E-17+i"),
("-2", "8.66310780426106E-17+1.4142135623731i"),
("-100", "6.1257422745431E-16+10i"),
("-1E10", "6.1257422745431E-12+100000i"),
("-1E-10", "6.1257422745431E-22+0.00001i"),
("-4+0j", "1.22514845490862E-16+2j"),
("-4-0i", "1.22514845490862E-16+2i"),
("-4-1E-20i", "1.22514845490862E-16-2i"),
("-1+1E-10i", "5.00000653944413E-11+i"),
("-0.5-1E-12i", "7.07212958930194E-13-0.707106781186548i"),
("4", "2"),
("2", "1.4142135623731"),
("0", "0"),
("3+4i", "2+i"),
("3+4j", "2+j"),
("-3+4i", "1+2i"),
("-3-4i", "1-2i"),
("-2i", "1-i"),
("2i", "1+i"),
("i", "0.707106781186548+0.707106781186547i"),
("5-12i", "3-2i"),
("-5+12i", "2+3i"),
("1E10+1E10i", "109868.411346781+45508.9860562227i"),
] {
is_text(ev("IMSQRT", vec![t(input)]), want);
}
cx(ev("IMSQRT", vec![t("-4")]), 0.0, 2.0);
is_text(ev("IMSQRT", vec![t("-4")]), "2i");
is_text(ev("IMSQRT", vec![t("-9")]), "3i");
is_text(ev("IMSQRT", vec![t("4")]), "2");
is_text(ev("IMSQRT", vec![t("-2i")]), "1-i");
is_text(ev("IMPOWER", vec![t("2+3i"), n(3.0)]), "-46+9i");
+5 -8
View File
@@ -311,8 +311,11 @@ fn hyperlink(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
let v = a.get(1).map(scalar).unwrap_or(Value::Empty);
return Ok(empty_to_zero(v));
}
// Excel returns the link as text, so HYPERLINK(1) is "1" and ISTEXT is TRUE.
Ok(Value::Text(text(a, 0)?.into()))
let link = a.first().map(scalar).unwrap_or(Value::Empty);
if let Value::Error(e) = link {
return Err(e);
}
Ok(link)
}
fn encodeurl(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
@@ -605,12 +608,6 @@ mod tests {
is_text(ev("HYPERLINK", vec![t("http://x.com")]), "http://x.com");
is_text(ev("HYPERLINK", vec![t("http://x.com"), t("Click")]), "Click");
close(ev("HYPERLINK", vec![t("http://x.com"), n(5.0)]), 5.0);
// Excel 16: without a friendly name the result is the link as text.
is_text(ev("HYPERLINK", vec![n(1.0)]), "1");
is_text(ev("HYPERLINK", vec![n(1.5)]), "1.5");
is_text(ev("HYPERLINK", vec![b(true)]), "TRUE");
is_text(ev("HYPERLINK", vec![rf(Value::Empty)]), "");
is_err(ev("HYPERLINK", vec![e(CellError::Div0)]), CellError::Div0);
is_text(ev("ENCODEURL", vec![t("a b&c/é")]), "a%20b%26c%2F%C3%A9");
is_err(ev("WEBSERVICE", vec![t("http://x")]), CellError::Value);
is_err(ev("FILTERXML", vec![t("<a/>"), t("//a")]), CellError::Value);
+1 -61
View File
@@ -58,35 +58,6 @@ fn reject_multi_cell_refs(spec: &FnSpec, args: &[Arg]) -> Option<Vec<Arg>> {
)
}
/// Lookups whose every call searches a lookup vector from its start, so a call lifted over `n`
/// needles does `n` x (vector length) work. Their search arguments don't lift.
const SCANS: &[&str] = &["VLOOKUP", "HLOOKUP", "LOOKUP", "MATCH", "XLOOKUP", "XMATCH"];
/// Work budget of a lifted [`SCANS`] lookup: needle cells times the length of the longest vector
/// it searches (the longest dimension of its largest non-lifted array). Each needle re-reads and
/// scans the vector (about 8 ns per entry), so without a cap a one-cell formula such as
/// `XMATCH(SEQUENCE(1048576), -SEQUENCE(1048576))` runs for hours and hangs opening the file.
/// Past the budget the call is `#NUM!`, as MMULT is past 2e9 multiply-adds. 5e8 keeps the worst
/// case to a few seconds and still admits 5,000 needles over a 100,000-row table.
const LIFTED_SCAN_BUDGET: usize = 500_000_000;
/// The length of the longest vector a lifted call of `spec` searches per needle: 0 unless `spec`
/// is one of the [`SCANS`] lookups and a non-lifted argument is an array.
fn scan_len(spec: &FnSpec, args: &[Arg]) -> usize {
if !SCANS.contains(&spec.name) {
return 0;
}
args.iter()
.enumerate()
.filter(|(i, _)| !spec.is_scalar_arg(*i))
.filter_map(|(_, a)| match &a.value {
Value::Array(arr) => Some(arr.rows.max(arr.cols)),
_ => None,
})
.max()
.unwrap_or(0)
}
pub(crate) fn call_lifted(spec: &FnSpec, args: &[Arg], ctx: &mut dyn Ctx) -> Value {
let rejected = if spec.value_args.is_empty() { None } else { reject_multi_cell_refs(spec, args) };
let args = rejected.as_deref().unwrap_or(args);
@@ -105,8 +76,7 @@ pub(crate) fn call_lifted(spec: &FnSpec, args: &[Arg], ctx: &mut dyn Ctx) -> Val
if !any {
return (spec.imp)(args, ctx);
}
let needles = rows.saturating_mul(cols);
if needles > MAX_CELLS || needles.saturating_mul(scan_len(spec, args)) > LIFTED_SCAN_BUDGET {
if rows.saturating_mul(cols) > MAX_CELLS {
return Value::Error(CellError::Num);
}
let mut cell_args: Vec<Arg> = args.to_vec();
@@ -179,36 +149,6 @@ mod tests {
is_err(huge, CellError::Num);
}
/// Security: a lifted lookup searches its whole lookup vector once per needle, so needles x
/// vector length past `LIFTED_SCAN_BUDGET` is `#NUM!` at once.
/// `XMATCH(SEQUENCE(1048576), -SEQUENCE(1048576))` used to run for hours (and hung opening
/// the 1.6 KB file holding it).
#[test]
fn lifted_lookups_stop_past_their_work_budget() {
use gridcraft_core::Array;
let tall = |n: usize| av(Array::filled(n, 1, nv(-1.0)).into());
let wide = |n: usize| av(Array::filled(1, n, nv(-1.0)).into());
is_err(ev("XMATCH", vec![tall(1_048_576), tall(1_048_576)]), CellError::Num);
// Just past the budget, for every lookup: 501 needles over 1,000,000 entries.
assert_eq!(super::LIFTED_SCAN_BUDGET, 500 * 1_000_000);
is_err(ev("XMATCH", vec![tall(501), tall(1_000_000)]), CellError::Num);
is_err(ev("MATCH", vec![tall(501), tall(1_000_000), n(0.0)]), CellError::Num);
is_err(ev("XLOOKUP", vec![tall(501), tall(1_000_000), tall(1_000_000)]), CellError::Num);
is_err(ev("VLOOKUP", vec![tall(501), tall(1_000_000), n(1.0), b(false)]), CellError::Num);
is_err(ev("HLOOKUP", vec![tall(501), wide(1_000_000), n(1.0), b(false)]), CellError::Num);
is_err(ev("LOOKUP", vec![tall(501), tall(1_000_000)]), CellError::Num);
// Within the budget lifted lookups work as before.
let keys = || av(col(&[10.0, 20.0, 30.0]));
let pos = ev("XMATCH", vec![av(col(&[30.0, 10.0, 99.0])), keys()]);
assert_eq!(rows_of(&pos), vec![vec![nv(3.0)], vec![nv(1.0)], vec![Value::Error(CellError::NA)]]);
let found = ev("XLOOKUP", vec![av(row(&[20.0, 30.0])), keys(), av(col(&[1.0, 2.0, 3.0]))]);
assert_eq!(rows_of(&found), vec![vec![nv(2.0), nv(3.0)]]);
// Other lifted functions aren't metered: INDEX reads one entry per position.
let picks: Vec<f64> = (1..=1000u32).map(f64::from).collect();
let out = ev("INDEX", vec![tall(1_048_576), av(col(&picks))]);
assert_eq!(rows_of(&out), vec![vec![nv(-1.0)]; 1000]);
}
#[test]
fn value_params_reject_multi_cell_references() {
let s = FnSpec { value_args: &[true, false], ..spec() };
-14
View File
@@ -314,10 +314,6 @@ fn modulo(x: f64, d: f64) -> R<f64> {
if d == 0.0 {
return Err(CellError::Div0);
}
// Excel returns #NUM! once the quotient magnitude reaches 1.1259E12 (checked against Excel 16).
if (x / d).abs() >= 1.1259e12 {
return Err(CellError::Num);
}
// `%` is an exact remainder (truncated), so |r| < |d| even for tiny divisors.
let r = x % d;
// Keep the sign of the divisor and avoid returning |d| from rounding.
@@ -698,16 +694,6 @@ pub(crate) fn specs() -> Vec<FnSpec> {
f!("ROUNDDOWN", 2, 2, MathTrig, S, "ROUNDDOWN(number, num_digits)", "Rounds a number toward zero to a given number of digits.", rounddown),
f!("MROUND", 2, 2, MathTrig, S, "MROUND(number, multiple)", "Rounds a number to the nearest multiple.", mround),
f!("CEILING", 2, 2, Compatibility, S, "CEILING(number, significance)", "Rounds a number up to a multiple of significance.", ceiling),
f!(
"ECMA.CEILING",
2,
2,
Compatibility,
S,
"ECMA.CEILING(number, significance)",
"Rounds a number up to a multiple of significance.",
ceiling
),
f!(
"CEILING.MATH",
1,
+9 -20
View File
@@ -101,11 +101,6 @@ fn rounding() {
close(ev2("CEILING", 1.5, 0.1), 1.5);
close(ev2("CEILING", 0.234, 0.01), 0.24);
is_err(ev2("CEILING", 2.5, -1.0), Num);
// ECMA.CEILING is CEILING under a second name (Excel table from #2776).
close(ev2("ECMA.CEILING", 1.5, 1.0), 2.0);
close(ev2("ECMA.CEILING", -2.5, -2.0), -4.0);
close(ev2("ECMA.CEILING", 2.5, 0.0), 0.0);
is_err(ev2("ECMA.CEILING", 1.5, -1.0), Num);
close(ev2("FLOOR", 3.7, 2.0), 2.0);
close(ev2("FLOOR", -2.5, -2.0), -2.0);
close(ev2("FLOOR", -2.5, 2.0), -4.0);
@@ -140,22 +135,16 @@ fn rounding() {
}
#[test]
fn mod_quotient_limit() {
// Excel 16: #NUM! whenever |number / divisor| >= 1.1259E12.
for (x, d) in [
(3.0, 1e-300),
(-1.0, 1e-300),
(3.0, -1e-300),
(-1.0, -1e-300),
(1e15, 7.0),
(1e10, 0.001),
(1_125_900_000_000.0, 1.0),
(-1_125_900_000_000.0, 1.0),
] {
is_err(ev2("MOD", x, d), Num);
fn mod_tiny_divisor_stays_in_range() {
for (x, d) in [(3.0, 1e-300), (-1.0, 1e-300), (3.0, -1e-300), (-1.0, -1e-300)] {
match ev2("MOD", x, d) {
Value::Number(r) => {
assert!(r == 0.0 || (r < 0.0) == (d < 0.0), "MOD({x}, {d}) = {r}");
assert!(r.abs() < d.abs(), "MOD({x}, {d}) = {r}");
}
other => panic!("MOD({x}, {d}) = {other:?}"),
}
}
close(ev2("MOD", 1e12 * 7.0 + 3.0, 7.0), 3.0);
close(ev2("MOD", 1_125_899_999_999.99, 1.0), 0.989_990_234_375);
}
#[test]
+1 -12
View File
@@ -336,10 +336,6 @@ pub(super) fn percentrank_impl(a: &[Arg], exc: bool) -> R<Value> {
if sig < 1.0 || s.is_empty() {
return Err(CellError::Num);
}
// Excel: 10^significance overflows from 309 up.
if sig >= 309.0 {
return Err(CellError::Div0);
}
let (first, last) = (s.first().copied().unwrap_or(0.0), s.last().copied().unwrap_or(0.0));
if x < first || x > last {
return Err(CellError::NA);
@@ -363,10 +359,6 @@ pub(super) fn percentrank_impl(a: &[Arg], exc: bool) -> R<Value> {
let (rl, rh) = (rank_of(lo), rank_of(hi));
rl + (x - lo) / (hi - lo) * (rh - rl)
};
// From significance 15 up Excel returns 15 significant digits, rounded.
if sig >= 15.0 {
return num_val(format!("{r:.14e}").parse().unwrap_or(r));
}
num_val(trunc_digits(r, sig))
}
@@ -428,10 +420,7 @@ pub(super) fn geomean(v: &[f64]) -> R<f64> {
}
pub(super) fn harmean(v: &[f64]) -> R<f64> {
if v.is_empty() {
return Err(CellError::NA);
}
if v.iter().any(|&x| x <= 0.0) {
if v.is_empty() || v.iter().any(|&x| x <= 0.0) {
return Err(CellError::Num);
}
Ok(v.len() as f64 / v.iter().map(|x| 1.0 / x).sum::<f64>())
+14 -40
View File
@@ -22,14 +22,7 @@ fn t_cdf(t: f64, v: f64) -> f64 {
}
fn t_pdf(t: f64, v: f64) -> f64 {
let z = v / 2.0;
let log_gamma_ratio = if z >= 16.0 {
let inv_z = 1.0 / z;
0.5 * z.ln() - inv_z / 8.0 + inv_z.powi(3) / 192.0 - inv_z.powi(5) / 640.0 + 17.0 * inv_z.powi(7) / 14_336.0
} else {
ln_gamma(z + 0.5) - ln_gamma(z)
};
(log_gamma_ratio - 0.5 * (v.ln() + std::f64::consts::PI.ln()) - (v + 1.0) / 2.0 * (t * t / v).ln_1p()).exp()
(ln_gamma((v + 1.0) / 2.0) - ln_gamma(v / 2.0) - 0.5 * (v * std::f64::consts::PI).ln() - (v + 1.0) / 2.0 * (1.0 + t * t / v).ln()).exp()
}
/// t ≥ 0 with P(T > t) = q, q in (0, 0.5].
@@ -58,12 +51,6 @@ fn f_cdf(x: f64, d1: f64, d2: f64) -> f64 {
if x <= 0.0 {
return 0.0;
}
if x == 1.0 && d1 == d2 {
return 0.5;
}
if d1 >= 1e8 && d2 / d1 <= 1e-8 {
return reg_gamma_q(d2 / 2.0, d2 / (2.0 * (x + d2 / d1)));
}
reg_inc_beta(d1 * x / (d1 * x + d2), d1 / 2.0, d2 / 2.0)
}
@@ -71,10 +58,6 @@ fn f_upper(x: f64, d1: f64, d2: f64) -> f64 {
if x <= 0.0 {
return 1.0;
}
if d1 >= 1e8 && d2 / d1 <= 1e-8 {
// With a much larger numerator degree, the scaled beta tail converges to a gamma tail.
return reg_gamma_p(d2 / 2.0, d2 / (2.0 * (x + d2 / d1)));
}
reg_inc_beta(d2 / (d2 + d1 * x), d2 / 2.0, d1 / 2.0)
}
@@ -91,8 +74,6 @@ fn f_pdf(x: f64, d1: f64, d2: f64) -> f64 {
((d1 / 2.0) * (d1 / d2).ln() + (d1 / 2.0 - 1.0) * x.ln() - ((d1 + d2) / 2.0) * (1.0 + d1 * x / d2).ln() - ln_beta(d1 / 2.0, d2 / 2.0)).exp()
}
const MAX_BINOM_TRIALS: f64 = 2_147_483_646.0;
fn binom_pmf(k: f64, n: f64, p: f64) -> f64 {
if k < 0.0 || k > n {
return 0.0;
@@ -195,14 +176,8 @@ fn df_arg(a: &[Arg], i: usize) -> R<f64> {
pub(super) fn t_dist(a: &[Arg], _: C) -> R<Value> {
let x = num(a, 0)?;
let v = num(a, 1)?.trunc();
let v = df_arg(a, 1)?;
let cum = boolean(a, 2)?;
if v == 0.0 {
return Err(if cum { CellError::Num } else { CellError::Div0 });
}
if v < 0.0 {
return Err(CellError::Num);
}
num_val(if cum { t_cdf(x, v) } else { t_pdf(x, v) })
}
@@ -234,9 +209,6 @@ pub(super) fn tdist_legacy(a: &[Arg], _: C) -> R<Value> {
pub(super) fn t_inv(a: &[Arg], _: C) -> R<Value> {
let p = num(a, 0)?;
let v = df_arg(a, 1)?;
if p == 0.0 {
return Err(CellError::Div0);
}
if !(p > 0.0 && p < 1.0) {
return Err(CellError::Num);
}
@@ -375,7 +347,7 @@ fn f_args(a: &[Arg]) -> R<(f64, f64, f64)> {
let x = num(a, 0)?;
let d1 = num(a, 1)?.trunc();
let d2 = num(a, 2)?.trunc();
if d1 < 1.0 || d2 < 1.0 || d1 > 1e10 || d2 > 1e10 {
if d1 < 1.0 || d2 < 1.0 || d1 >= 1e10 || d2 >= 1e10 {
return Err(CellError::Num);
}
Ok((x, d1, d2))
@@ -406,8 +378,7 @@ pub(super) fn f_inv(a: &[Arg], _: C) -> R<Value> {
if p == 0.0 {
return num_val(0.0);
}
let value = if p < 0.5 { invert(|x| f_cdf(x, d1, d2), p, 0.0, 1.0, 1e60) } else { invert(|x| f_upper(x, d1, d2), 1.0 - p, 0.0, 1.0, 1e60) };
num_val(value)
num_val(invert(|x| f_cdf(x, d1, d2), p, 0.0, 1.0, 1e60))
}
pub(super) fn f_inv_rt(a: &[Arg], _: C) -> R<Value> {
@@ -439,7 +410,7 @@ pub(super) fn binom_dist(a: &[Arg], _: C) -> R<Value> {
let n = num(a, 1)?.trunc();
let p = num(a, 2)?;
let cum = boolean(a, 3)?;
if k < 0.0 || n > MAX_BINOM_TRIALS || k > n || !(0.0..=1.0).contains(&p) {
if k < 0.0 || k > n || !(0.0..=1.0).contains(&p) {
return Err(CellError::Num);
}
num_val(if cum { binom_cdf(k, n, p) } else { binom_pmf(k, n, p) })
@@ -450,7 +421,7 @@ pub(super) fn binom_dist_range(a: &[Arg], _: C) -> R<Value> {
let p = num(a, 1)?;
let s1 = num(a, 2)?.trunc();
let s2 = if has(a, 3) { num(a, 3)?.trunc() } else { s1 };
if !(0.0..=MAX_BINOM_TRIALS).contains(&n) || !(0.0..=1.0).contains(&p) || s1 < 0.0 || s1 > n || s2 < s1 || s2 > n {
if !(0.0..=1e15).contains(&n) || !(0.0..=1.0).contains(&p) || s1 < 0.0 || s1 > n || s2 < s1 || s2 > n {
return Err(CellError::Num);
}
if s2 - s1 > 10_000.0 {
@@ -528,7 +499,13 @@ pub(super) fn gamma_dist(a: &[Arg], _: C) -> R<Value> {
return num_val(reg_gamma_p(al, x / be));
}
if x == 0.0 {
return if al <= 1.0 { Err(CellError::Num) } else { num_val(0.0) };
return if al < 1.0 {
Err(CellError::Num)
} else if al == 1.0 {
num_val(1.0 / be)
} else {
num_val(0.0)
};
}
num_val(((al - 1.0) * x.ln() - x / be - al * be.ln() - ln_gamma(al)).exp())
}
@@ -571,7 +548,7 @@ pub(super) fn beta_dist(a: &[Arg], _: C) -> R<Value> {
if cum {
return num_val(reg_inc_beta(z, al, be));
}
if (z == 0.0 && al <= 1.0) || (z == 1.0 && be <= 1.0) {
if (z == 0.0 && al < 1.0) || (z == 1.0 && be < 1.0) {
return Err(CellError::Num);
}
let lp = if z == 0.0 || z == 1.0 {
@@ -631,9 +608,6 @@ pub(super) fn weibull_dist(a: &[Arg], _: C) -> R<Value> {
if x < 0.0 || al <= 0.0 || be <= 0.0 {
return Err(CellError::Num);
}
if x == 0.0 {
return num_val(0.0);
}
let t = (x / be).powf(al);
num_val(if cum { -(-t).exp_m1() } else { al / be.powf(al) * x.powf(al - 1.0) * (-t).exp() })
}
+4 -115
View File
@@ -151,19 +151,6 @@ fn spread() {
let g = [4.0, 5.0, 8.0, 7.0, 11.0, 4.0, 3.0];
close_tol(ev("GEOMEAN", vec![c(&g)]), 5.476986969, 1e-9);
close_tol(ev("HARMEAN", vec![c(&g)]), 5.028375962, 1e-9);
let text_values = arr(vec![vec![tv("1"), tv("2")]]);
let bool_values = arr(vec![vec![Value::Bool(true), Value::Bool(false)]]);
let mixed_values = arr(vec![vec![nv(2.0), tv("ignored"), Value::Bool(true)]]);
is_err(ev("HARMEAN", vec![av(text_values.clone())]), CellError::NA);
is_err(ev("HARMEAN", vec![av(bool_values.clone())]), CellError::NA);
is_err(ev("HARMEAN", vec![rf(text_values)]), CellError::NA);
is_err(ev("HARMEAN", vec![rf(bool_values)]), CellError::NA);
close(ev("HARMEAN", vec![av(mixed_values.clone())]), 2.0);
close(ev("HARMEAN", vec![rf(mixed_values)]), 2.0);
close(ev("HARMEAN", vec![b(true)]), 1.0);
close(ev("HARMEAN", vec![n(2.0), n(4.0)]), 8.0 / 3.0);
is_err(ev("HARMEAN", vec![av(arr(vec![vec![nv(0.0)]]))]), CellError::Num);
is_err(ev("HARMEAN", vec![av(arr(vec![vec![nv(-1.0)]]))]), CellError::Num);
is_err(ev("GEOMEAN", vec![c(&[1.0, 0.0])]), CellError::Num);
close_tol(ev("TRIMMEAN", vec![c(&[4.0, 5.0, 6.0, 7.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0]), n(0.2)]), 3.777777778, 1e-9);
is_err(ev("TRIMMEAN", vec![c(&[1.0]), n(1.0)]), CellError::Num);
@@ -227,19 +214,10 @@ fn percentiles() {
close(ev("PERCENTRANK.EXC", vec![e9.clone(), n(5.43)]), 0.381);
close(ev("PERCENTRANK.EXC", vec![e9, n(5.43), n(1.0)]), 0.3);
let t = c(&[1.0, 2.0, 3.0]);
// Excel 16: significance 309 and up is #DIV/0!, 308 still works.
for name in ["PERCENTRANK", "PERCENTRANK.INC", "PERCENTRANK.EXC"] {
is_err(ev(name, vec![t.clone(), n(2.0), n(309.0)]), CellError::Div0);
is_err(ev(name, vec![t.clone(), n(1.0), n(400.0)]), CellError::Div0);
close(ev(name, vec![t.clone(), n(2.0), n(308.0)]), 0.5);
}
// From significance 15 up the result has 15 significant digits.
let k = c(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0]);
for sig in [15.0, 16.0, 308.0] {
close_tol(ev("PERCENTRANK.INC", vec![k.clone(), n(2.123_456_789), n(sig)]), 0.187_242_798_166_667, 1e-16);
}
close_tol(ev("PERCENTRANK.INC", vec![k.clone(), n(2.123_456_789), n(14.0)]), 0.187_242_798_166_66, 1e-15);
close(ev("PERCENTRANK.EXC", vec![k, n(2.123_456_789), n(16.0)]), 0.265_432_098_625);
close(ev("PERCENTRANK", vec![t.clone(), n(2.0), n(400.0)]), 0.5);
close(ev("PERCENTRANK.INC", vec![t.clone(), n(2.0), n(400.0)]), 0.5);
close(ev("PERCENTRANK.INC", vec![t.clone(), n(1.0), n(400.0)]), 0.0);
close(ev("PERCENTRANK.EXC", vec![t, n(2.0), n(400.0)]), 0.5);
}
#[test]
@@ -382,7 +360,6 @@ fn normal() {
#[test]
fn t_chi_f() {
close_tol(ev("T.DIST", vec![n(0.0), n(6e307), b(false)]), 0.3989422804, 1e-10);
close_tol(ev("T.DIST", vec![n(60.0), n(1.0), b(true)]), 0.99469533, D7);
close_tol(ev("T.DIST", vec![n(8.0), n(3.0), b(false)]), 0.00073691, 1e-5);
close_tol(ev("T.DIST.2T", vec![n(1.959999998), n(60.0)]), 0.054644930, D7);
@@ -420,7 +397,6 @@ fn t_chi_f() {
close_tol(ev("F.DIST.RT", vec![n(15.2069), n(6.0), n(4.0)]), 0.01, 1e-6);
close_tol(ev("FDIST", vec![n(15.2069), n(6.0), n(4.0)]), 0.01, 1e-6);
close_tol(ev("F.INV", vec![n(0.01), n(6.0), n(4.0)]), 0.10930991, D7);
close_tol(ev("F.INV", vec![n(0.99), n(6.0), n(4.0)]), 15.20686, D7);
close_tol(ev("F.INV.RT", vec![n(0.01), n(6.0), n(4.0)]), 15.20686, D7);
close_tol(ev("FINV", vec![n(0.01), n(6.0), n(4.0)]), 15.20686, D7);
close_tol(ev("F.TEST", vec![c(&[6.0, 7.0, 9.0, 15.0, 21.0]), c(&[20.0, 28.0, 31.0, 38.0, 40.0])]), 0.64831785, D7);
@@ -428,48 +404,6 @@ fn t_chi_f() {
is_err(ev("F.DIST", vec![n(1.0), n(0.0), n(4.0), b(true)]), CellError::Num);
}
#[test]
fn f_distributions_accept_ten_billion_degrees() {
close_tol(ev("F.DIST", vec![n(0.0), n(10_000_000_000.0), n(1.0), b(false)]), 0.0, 0.0);
close_tol(ev("F.DIST.RT", vec![n(0.0), n(1.0), n(10_000_000_000.0)]), 1.0, 0.0);
close_tol(ev("F.DIST", vec![n(0.01), n(1_000_000_000.0), n(1.0), b(true)]), 1.52397e-23, 1e-28);
close_tol(ev("F.DIST", vec![n(1.0), n(10_000_000_000.0), n(10_000_000_000.0), b(true)]), 0.5, 0.0);
close_tol(ev("F.DIST", vec![n(1_000_000.0), n(10_000_000_000.0), n(1.0), b(true)]), 0.999202115, 1e-8);
close_tol(ev("F.INV", vec![n(0.5), n(10_000_000_000.0), n(1.0)]), 2.198110953542527, 2e-6);
close_tol(ev("F.DIST.RT", vec![n(0.0), n(9_999_999_999.0), n(1.0)]), 1.0, 0.0);
is_err(ev("F.DIST.RT", vec![n(0.0), n(10_000_000_001.0), n(1.0)]), CellError::Num);
is_err(ev("F.DIST.RT", vec![n(0.0), n(1.0), n(10_000_000_001.0)]), CellError::Num);
}
#[test]
fn t_dist_zero_degrees_error_depends_on_mode() {
for df in [0.0, 0.9, -0.1] {
is_err(ev("T.DIST", vec![n(0.0), n(df), b(false)]), CellError::Div0);
is_err(ev("T.DIST", vec![n(0.0), n(df), b(true)]), CellError::Num);
}
is_err(ev("T.DIST", vec![n(0.0), n(-1.0), b(false)]), CellError::Num);
close_tol(ev("T.DIST", vec![n(0.0), n(1.99), b(false)]), 1.0 / std::f64::consts::PI, 1e-15);
close_tol(ev("T.DIST", vec![n(0.0), n(1.0), b(false)]), 1.0 / std::f64::consts::PI, 1e-15);
}
#[test]
fn t_density_is_stable_for_large_degrees() {
close_tol(ev("T.DIST", vec![n(0.0), n(100_000_000.0), b(false)]), 0.398942279404077, 1e-12);
close_tol(ev("T.DIST", vec![n(0.0), n(10_000_000_000.0), b(false)]), 0.3989422803914591, 1e-12);
close_tol(ev("T.DIST", vec![n(1.0), n(10_000_000_000_000_000.0), b(false)]), 0.24197072451914337, 1e-12);
}
#[test]
fn t_inv_zero_probability_returns_div0() {
is_err(ev("T.INV", vec![n(-0.1), n(1.0)]), CellError::Num);
is_err(ev("T.INV", vec![n(0.0), n(1.0)]), CellError::Div0);
close_tol(ev("T.INV", vec![n(0.75), n(2.0)]), 0.8164966, D7);
is_err(ev("T.INV", vec![n(1.0), n(1.0)]), CellError::Num);
is_err(ev("T.INV", vec![n(1.1), n(1.0)]), CellError::Num);
is_err(ev("T.INV", vec![n(0.0), n(0.0)]), CellError::Num);
is_err(ev("T.INV.2T", vec![n(0.0), n(1.0)]), CellError::Num);
}
#[test]
fn binom_inv_huge_trials_returns() {
// Above 2^53 neighbouring f64 values are more than 1 apart; the bisection must still end.
@@ -493,15 +427,7 @@ fn discrete() {
close_tol(ev("BINOM.DIST", vec![n(6.0), n(10.0), n(0.5), b(false)]), 0.205078125, 1e-12);
close_tol(ev("BINOM.DIST", vec![n(6.0), n(10.0), n(0.5), b(true)]), 0.828125, 1e-12);
close_tol(ev("BINOMDIST", vec![n(6.0), n(10.0), n(0.5), b(false)]), 0.205078125, 1e-12);
close(ev("BINOM.DIST", vec![n(1.0), n(2_147_483_646.0), n(0.0), b(false)]), 0.0);
close(ev("BINOM.DIST", vec![n(1.0), n(2_147_483_646.9), n(0.0), b(false)]), 0.0);
is_err(ev("BINOM.DIST", vec![n(1.0), n(2_147_483_647.0), n(0.0), b(false)]), CellError::Num);
is_err(ev("BINOM.DIST", vec![n(1.0), n(2_147_483_647.9), n(0.0), b(false)]), CellError::Num);
is_err(ev("BINOM.DIST", vec![n(11.0), n(10.0), n(0.5), b(false)]), CellError::Num);
close(ev("BINOM.DIST.RANGE", vec![n(2_147_483_646.0), n(0.0), n(1.0)]), 0.0);
close(ev("BINOM.DIST.RANGE", vec![n(2_147_483_646.9), n(0.0), n(1.0)]), 0.0);
is_err(ev("BINOM.DIST.RANGE", vec![n(2_147_483_647.0), n(0.0), n(1.0)]), CellError::Num);
is_err(ev("BINOM.DIST.RANGE", vec![n(2_147_483_647.9), n(0.0), n(1.0)]), CellError::Num);
close_tol(ev("BINOM.DIST.RANGE", vec![n(60.0), n(0.75), n(48.0)]), 0.083974967, 1e-6);
close_tol(ev("BINOM.DIST.RANGE", vec![n(60.0), n(0.75), n(45.0), n(50.0)]), 0.523629793, 1e-6);
close(ev("BINOM.INV", vec![n(6.0), n(0.5), n(0.75)]), 4.0);
@@ -540,19 +466,6 @@ fn continuous() {
is_err(ev("GAMMA", vec![n(0.0)]), CellError::Num);
close_tol(ev("BETA.DIST", vec![n(2.0), n(8.0), n(10.0), b(true), n(1.0), n(3.0)]), 0.6854706, D7);
close_tol(ev("BETA.DIST", vec![n(2.0), n(8.0), n(10.0), b(false), n(1.0), n(3.0)]), 1.4837646, D7);
is_err(ev("BETA.DIST", vec![n(0.0), n(1.0), n(1.0), b(false)]), CellError::Num);
is_err(ev("BETA.DIST", vec![n(1.0), n(1.0), n(1.0), b(false)]), CellError::Num);
is_err(ev("BETA.DIST", vec![n(0.0), n(0.5), n(2.0), b(false)]), CellError::Num);
is_err(ev("BETA.DIST", vec![n(1.0), n(2.0), n(0.5), b(false)]), CellError::Num);
close(ev("BETA.DIST", vec![n(0.0), n(2.0), n(1.0), b(false)]), 0.0);
close(ev("BETA.DIST", vec![n(1.0), n(1.0), n(2.0), b(false)]), 0.0);
close(ev("BETA.DIST", vec![n(0.0), n(1.0), n(1.0), b(true)]), 0.0);
close(ev("BETA.DIST", vec![n(1.0), n(1.0), n(1.0), b(true)]), 1.0);
is_err(ev("BETA.DIST", vec![n(2.0), n(1.0), n(2.0), b(false), n(2.0), n(6.0)]), CellError::Num);
is_err(ev("BETA.DIST", vec![n(6.0), n(2.0), n(1.0), b(false), n(2.0), n(6.0)]), CellError::Num);
close(ev("BETA.DIST", vec![n(4.0), n(1.0), n(1.0), b(false), n(2.0), n(6.0)]), 0.25);
close(ev("BETA.DIST", vec![n(2.0), n(1.0), n(1.0), b(true), n(2.0), n(6.0)]), 0.0);
close(ev("BETA.DIST", vec![n(6.0), n(1.0), n(1.0), b(true), n(2.0), n(6.0)]), 1.0);
close_tol(ev("BETADIST", vec![n(2.0), n(8.0), n(10.0), n(1.0), n(3.0)]), 0.6854706, D7);
close_tol(ev("BETA.INV", vec![n(0.685470581), n(8.0), n(10.0), n(1.0), n(3.0)]), 2.0, D7);
close_tol(ev("BETAINV", vec![n(0.685470581), n(8.0), n(10.0), n(1.0), n(3.0)]), 2.0, D7);
@@ -566,30 +479,6 @@ fn continuous() {
close_tol(r[0][1].clone(), 0.841344746, 1e-8);
}
#[test]
fn gamma_density_at_zero_shape_one_is_num() {
for scale in [0.1, 0.9, 1.0, 1.9, 2.0, 10_000_000_000.0] {
is_err(ev("GAMMA.DIST", vec![n(0.0), n(1.0), n(scale), b(false)]), CellError::Num);
}
is_err(ev("GAMMA.DIST", vec![n(0.0), n(0.5), n(1.0), b(false)]), CellError::Num);
close_tol(ev("GAMMA.DIST", vec![n(0.0), n(2.0), n(1.0), b(false)]), 0.0, 0.0);
close_tol(ev("GAMMA.DIST", vec![n(0.0), n(1.0), n(2.0), b(true)]), 0.0, 0.0);
close_tol(ev("GAMMA.DIST", vec![n(2.0), n(1.0), n(2.0), b(false)]), 0.5 * (-1.0f64).exp(), 1e-15);
close_tol(ev("GAMMA.DIST", vec![n(2.0), n(1.0), n(2.0), b(true)]), 1.0 - (-1.0f64).exp(), 1e-15);
is_err(ev("GAMMA.DIST", vec![n(-0.1), n(1.0), n(2.0), b(false)]), CellError::Num);
}
#[test]
fn weibull_density_at_zero_is_zero() {
for shape in [0.5, 1.0, 2.0] {
close_tol(ev("WEIBULL.DIST", vec![n(0.0), n(shape), n(1.0), b(false)]), 0.0, 0.0);
close_tol(ev("WEIBULL.DIST", vec![n(0.0), n(shape), n(1.0), b(true)]), 0.0, 0.0);
}
close_tol(ev("WEIBULL.DIST", vec![n(1.0), n(1.0), n(1.0), b(false)]), (-1.0f64).exp(), 1e-15);
close_tol(ev("WEIBULL.DIST", vec![n(1.0), n(1.0), n(1.0), b(true)]), 1.0 - (-1.0f64).exp(), 1e-15);
is_err(ev("WEIBULL.DIST", vec![n(-0.1), n(1.0), n(1.0), b(false)]), CellError::Num);
}
#[test]
fn aggregate_helper() {
let vals = vec![nv(1.0), nv(2.0), tv("x"), Value::Empty, Value::Bool(true), nv(6.0)];
+12 -113
View File
@@ -111,31 +111,12 @@ fn len(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
num_val(text(a, 0)?.chars().count() as f64)
}
fn lower_char(ch: char) -> char {
match ch.to_lowercase().next() {
Some(lower) => lower,
None => ch,
}
}
fn upper_char(ch: char) -> char {
let mut uppercase = ch.to_uppercase();
match uppercase.next() {
Some(upper) if uppercase.next().is_none() => upper,
_ => ch,
}
}
fn proper_upper_char(ch: char) -> char {
if ch == '\u{130}' { 'I' } else { upper_char(ch) }
}
fn lower(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
text_val(text(a, 0)?.chars().map(lower_char).collect::<String>())
text_val(text(a, 0)?.to_lowercase())
}
fn upper(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
text_val(text(a, 0)?.chars().map(upper_char).collect::<String>())
text_val(text(a, 0)?.to_uppercase())
}
fn proper(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
@@ -145,9 +126,9 @@ fn proper(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
for ch in s.chars() {
if ch.is_alphabetic() {
if prev_letter {
out.push(lower_char(ch));
out.extend(ch.to_lowercase());
} else {
out.push(proper_upper_char(ch));
out.extend(ch.to_uppercase());
}
prev_letter = true;
} else {
@@ -300,11 +281,7 @@ fn unichar(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
if !(1.0..=1_114_111.0).contains(&n) {
return Err(CellError::Value);
}
let cp = n as u32;
if (0xFDD0..=0xFDEF).contains(&cp) || cp & 0xFFFE == 0xFFFE {
return Err(CellError::NA);
}
match char::from_u32(cp) {
match char::from_u32(n as u32) {
Some(c) => text_val(c.to_string()),
None => Err(CellError::NA),
}
@@ -373,10 +350,10 @@ fn numbervalue(a: &[Arg], c: &mut dyn Ctx) -> R<Value> {
/// Formats `x` (already rounded) with `decimals` places and optional thousands separators.
fn format_fixed(x: f64, decimals: i64, commas: bool) -> String {
let d = decimals.max(0) as usize;
let s = x.abs().to_string();
let s = format!("{:.*}", d, x.abs());
let (int_part, frac) = match s.split_once('.') {
Some((i, f)) => (i, f),
None => (s.as_str(), ""),
Some((i, f)) => (i.to_string(), Some(f.to_string())),
None => (s.clone(), None),
};
let int_part = if commas {
let b = int_part.as_bytes();
@@ -389,14 +366,12 @@ fn format_fixed(x: f64, decimals: i64, commas: bool) -> String {
}
o
} else {
int_part.to_string()
int_part
};
let mut out = int_part;
if d > 0 {
let frac_len = frac.len().min(d);
if let Some(f) = frac {
out.push('.');
out.push_str(&frac[..frac_len]);
out.extend(std::iter::repeat_n('0', d - frac_len));
out.push_str(&f);
}
out
}
@@ -419,21 +394,13 @@ fn fixed(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
let (r, d) = rounded(a, 2.0)?;
let no_commas = opt_bool(a, 2, false)?;
let body = format_fixed(r, d, !no_commas);
if body.len() + if r < 0.0 { 1 } else { 0 } > 255 {
return Err(CellError::Value);
}
text_val(if r < 0.0 { format!("-{body}") } else { body })
}
fn dollar(a: &[Arg], _c: &mut dyn Ctx) -> R<Value> {
let (r, d) = rounded(a, 2.0)?;
let negative = r < 0.0 || (r == 0.0 && d >= 0 && num(a, 0)? < 0.0);
let body = format_fixed(r, d, true);
let out = if negative { format!("(${body})") } else { format!("${body}") };
if out.len() > 254 {
return Err(CellError::Value);
}
text_val(out)
text_val(if r < 0.0 { format!("(${body})") } else { format!("${body}") })
}
// ---------------------------------------------------------------------------------------------
@@ -1156,20 +1123,6 @@ mod tests {
fn case_and_cleanup() {
is_text(ev("UPPER", vec![t("abc")]), "ABC");
is_text(ev("LOWER", vec![t("ABC")]), "abc");
is_text(ev("UPPER", vec![t("Straße")]), "STRAßE");
is_text(ev("LOWER", vec![t("İ")]), "i");
is_text(ev("PROPER", vec![t("İSTANBUL")]), "Istanbul");
is_text(ev("PROPER", vec![t("ß")]), "ß");
is_text(ev("PROPER", vec![t("ÉCOLE")]), "École");
let proper = ev("PROPER", vec![t("İSTANBUL Straße")]);
is_text(proper.clone(), "Istanbul Straße");
assert_eq!(
match proper {
Value::Text(s) => s.chars().count(),
_ => 0,
},
"Istanbul Straße".chars().count()
);
is_text(ev("PROPER", vec![t("this is a TITLE")]), "This Is A Title");
is_text(ev("PROPER", vec![t("2-way street's")]), "2-Way Street'S");
is_text(ev("TRIM", vec![t(" a b c ")]), "a b c");
@@ -1228,15 +1181,8 @@ mod tests {
close(ev("CODE", vec![t("€")]), 128.0);
is_err(ev("CODE", vec![t("")]), CellError::Value);
is_text(ev("UNICHAR", vec![n(9731.0)]), "☃");
for cp in [0xFDD0, 0xFDEF, 0xFFFE, 0xFFFF, 0x1FFFE, 0x1FFFF, 0x10FFFE, 0x10FFFF] {
is_err(ev("UNICHAR", vec![n(f64::from(cp))]), CellError::NA);
}
for (cp, expected) in [(0xFDCF, "\u{FDCF}"), (0xFDF0, "\u{FDF0}"), (0xFFFD, "\u{FFFD}"), (0x1FFFD, "\u{1FFFD}")] {
is_text(ev("UNICHAR", vec![n(f64::from(cp))]), expected);
}
close(ev("UNICODE", vec![t("☃")]), 9731.0);
is_err(ev("UNICHAR", vec![n(0.0)]), CellError::Value);
is_err(ev("UNICHAR", vec![n(1_114_112.0)]), CellError::Value);
is_err(ev("UNICHAR", vec![n(55296.0)]), CellError::NA);
}
@@ -1291,57 +1237,10 @@ mod tests {
is_text(ev("DOLLAR", vec![n(0.5), n(0.0)]), "$1");
is_text(ev("USDOLLAR", vec![n(1.0)]), "$1.00");
is_text(ev("USDOLLAR", vec![n(-1234.567), n(1.0)]), "($1,234.6)");
let fixed_16 = format!("1.1{}", "0".repeat(15));
let dollar_16 = format!("$1.1{}", "0".repeat(15));
let fixed_20 = format!("1.005{}", "0".repeat(17));
let dollar_20 = format!("$1.005{}", "0".repeat(17));
let fixed_127 = format!("1.1{}", "0".repeat(126));
let dollar_127 = format!("$1.1{}", "0".repeat(126));
is_text(ev("FIXED", vec![n(1.1), n(16.0)]), &fixed_16);
is_text(ev("DOLLAR", vec![n(1.1), n(16.0)]), &dollar_16);
is_text(ev("FIXED", vec![n(1.005), n(20.0)]), &fixed_20);
is_text(ev("DOLLAR", vec![n(1.005), n(20.0)]), &dollar_20);
is_text(ev("FIXED", vec![n(1.1), n(127.0)]), &fixed_127);
is_text(ev("DOLLAR", vec![n(1.1), n(127.0)]), &dollar_127);
is_text(ev("T", vec![t("x")]), "x");
is_text(ev("T", vec![n(1.0)]), "");
}
#[test]
fn fixed_and_dollar_output_limits() {
assert!(matches!(ev("FIXED", vec![n(1e191), n(0.0)]), Value::Text(ref s) if s.len() == 255));
is_err(ev("FIXED", vec![n(1e192), n(0.0)]), CellError::Value);
assert!(matches!(ev("FIXED", vec![n(-1e253), n(0.0), b(true)]), Value::Text(ref s) if s.starts_with('-')));
is_err(ev("FIXED", vec![n(-1e191), n(0.0)]), CellError::Value);
assert!(matches!(ev("FIXED", vec![n(1e189), n(1.0)]), Value::Text(ref s) if s.len() == 255));
is_err(ev("FIXED", vec![n(1e190), n(1.0)]), CellError::Value);
assert!(matches!(ev("DOLLAR", vec![n(1e189), n(0.0)]), Value::Text(ref s) if s.len() == 254));
is_err(ev("DOLLAR", vec![n(1e190), n(0.0)]), CellError::Value);
assert!(matches!(ev("DOLLAR", vec![n(-1e188), n(0.0)]), Value::Text(ref s) if s.len() == 254));
is_err(ev("DOLLAR", vec![n(-1e189), n(0.0)]), CellError::Value);
assert!(matches!(ev("DOLLAR", vec![n(1.23456789e187), n(2.0)]), Value::Text(ref s) if s.len() == 254));
is_err(ev("DOLLAR", vec![n(1.23456789e188), n(2.0)]), CellError::Value);
is_err(ev("DOLLAR", vec![n(-1.23456789e186), n(2.0)]), CellError::Value);
assert!(matches!(ev("DOLLAR", vec![n(1e189), n(-1.0)]), Value::Text(ref s) if s.len() == 254));
is_err(ev("DOLLAR", vec![n(1e190), n(-1.0)]), CellError::Value);
}
#[test]
fn dollar_keeps_negative_sign_when_nonnegative_precision_rounds_to_zero() {
is_text(ev("DOLLAR", vec![n(-0.0001), n(0.0)]), "($0)");
is_text(ev("DOLLAR", vec![n(-0.0001), n(2.0)]), "($0.00)");
is_text(ev("DOLLAR", vec![n(0.0), n(0.0)]), "$0");
is_text(ev("DOLLAR", vec![n(-0.0), n(0.0)]), "$0");
is_text(ev("DOLLAR", vec![n(0.0001), n(0.0)]), "$0");
is_text(ev("DOLLAR", vec![n(-0.0001), n(-1.0)]), "$0");
is_text(ev("DOLLAR", vec![n(-4.0), n(-1.0)]), "$0");
is_text(ev("DOLLAR", vec![n(-5.0), n(-1.0)]), "($10)");
is_text(ev("DOLLAR", vec![n(-0.4), n(0.0)]), "($0)");
is_text(ev("DOLLAR", vec![n(-0.5), n(0.0)]), "($1)");
is_text(ev("FIXED", vec![n(-0.0001), n(0.0)]), "0");
}
#[test]
fn before_after() {
is_text(ev("TEXTBEFORE", vec![t("Red riding hood"), t(" ")]), "Red");
-16
View File
@@ -252,22 +252,6 @@ fn save_and_open_roundtrip() {
let _ = std::fs::remove_file(path);
}
/// #2255: `parse: false` on `write_range` stores strings as the text they are, so a dd.mm.yyyy
/// date or a thousands separator survives, and the result names the strings the default read.
#[test]
fn write_range_can_store_strings_as_text() {
let mut s = server();
let row = json!([["05.04.2026", "2.500", "4.25 %", "=1+1"]]);
let stored = ok(&mut s, "write_range", json!({"range": "A1", "parse": false, "values": row}));
assert_eq!(stored["convertedCount"], 0, "nothing was read: {stored}");
let v = ok(&mut s, "read_range", json!({"range": "A1:D1"}));
assert_eq!(v["values"], json!([["05.04.2026", "2.500", "4.25 %", 2.0]]), "{v}");
// Without it the strings are read like typed input, and the result says which.
let read = ok(&mut s, "write_range", json!({"range": "A3", "values": row}));
assert_eq!(read["convertedCount"], 3, "{read}");
assert_eq!(read["converted"][0], json!({"cell": "A3", "input": "05.04.2026", "stored": "5/4/2026"}), "{read}");
}
/// An agent builds a small monthly budget through MCP tool calls only.
#[test]
fn agent_builds_a_budget() {
+2 -4
View File
@@ -190,13 +190,11 @@ fn base_tool_definitions() -> Vec<Value> {
"Write range",
"Write a 2-D array of values starting at the top-left cell of `range` (rows of cells). Numbers, booleans, null (clears) \
and strings are accepted; strings starting with = are formulas (\"=SUM(B2:B4)\"), other strings are parsed like typed \
input (\"12%\", \"1/2/2024\", \"$5\") unless `parse` is false, which stores them as the text they are. The result's \
`converted` lists the strings that were read as something else. One undo step. Example: {\"range\":\"A1\",\"values\":[[\"Item\",\"Cost\"],[\"Rent\",1200]]}.",
input (\"12%\", \"1/2/2024\", \"$5\"). One undo step. Example: {\"range\":\"A1\",\"values\":[[\"Item\",\"Cost\"],[\"Rent\",1200]]}.",
obj(
json!({
"range": string("Top-left cell or range, e.g. \"A1\" or \"Sheet2!B3\""),
"values": {"type": "array", "description": "Rows of cell values", "items": {"type": "array", "items": {"type": ["string", "number", "boolean", "null"]}}},
"parse": boolean("Read strings not starting with = like typed input, so \"05.04.2026\" becomes a date (default true); false stores them as the text they are, and \"\" clears the cell"),
}),
&["range", "values"],
),
@@ -639,7 +637,7 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result<Value, String>
"list_windows" => exec(b, "window.list", json!({})),
"inspect_workbook" => b.call("document.inspect", json!({})),
"read_range" => b.call("sheet.read", pick(a, &["range", "sheet", "formulas", "formatted"])),
"write_range" => exec(b, "range.setValues", pick(a, &["range", "values", "parse"])),
"write_range" => exec(b, "range.setValues", pick(a, &["range", "values"])),
"set_cell" => exec(b, "cell.set", pick(a, &["cell", "input"])),
"get_cell" => exec(b, "cell.get", pick(a, &["cell"])),
"evaluate_formula" => {
+3 -1
View File
@@ -13,5 +13,7 @@ gridcraft-core = { workspace = true }
sha2 = { workspace = true }
gridcraft-formula = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
[dev-dependencies]
serde_json = { workspace = true }
+1 -3
View File
@@ -8,7 +8,6 @@ mod b64;
pub mod cell;
mod cellmap;
pub mod features;
pub mod macros;
pub mod password;
pub mod preserved;
pub mod sheet;
@@ -18,7 +17,6 @@ pub mod workbook;
pub use cell::{Cell, CellPicture, Formula};
pub use features::*;
pub use macros::*;
pub use password::PasswordHash;
pub use preserved::{Preserved, PreservedPart, PreservedRel, PreservedSheet, PreservedXml};
pub use sheet::{DEFAULT_COL_WIDTH, DEFAULT_ROW_HEIGHT, LineIndex, Sheet, SheetViewMode, Visibility, points_to_chars};
@@ -27,7 +25,7 @@ pub use style::{
Alignment, BorderLine, BorderStyle, Borders, Color, Fill, Font, HAlign, NumFmt, PatternType, Style, StyleId, StyleTable, Theme, Underline,
VAlign, VertAlign,
};
pub use workbook::{CalcMode, CalcSettings, DefinedName, DocProps, PhoneticProps, PhoneticRun, PhoneticString, ShowObjects, Workbook, WorkbookView};
pub use workbook::{CalcMode, CalcSettings, DefinedName, DocProps, PhoneticProps, PhoneticRun, PhoneticString, ShowObjects, Workbook};
/// Font used for new workbooks. Only a name: renderers substitute an available open font
/// (e.g. the metric-compatible Carlito) when it isn't installed.
-539
View File
@@ -1,539 +0,0 @@
//! GridCraft macros: typed, declarative command lists that run as one undo step. They are not VBA:
//! a VBA project in a file is carried through unchanged (`Workbook::vba_project`) and never run.
use serde::{Deserialize, Serialize};
/// The macro format version written by this build.
pub const MACRO_VERSION: u32 = 1;
/// The most macros a workbook keeps.
pub const MAX_MACROS: usize = 1_000;
/// The most steps in one macro, counting the steps inside Repeat and If.
pub const MAX_MACRO_STEPS: usize = 10_000;
/// How deeply Repeat and If may nest (a top-level step is at depth 1).
pub const MAX_MACRO_DEPTH: usize = 4;
/// The longest macro or input name, in characters.
pub const MAX_MACRO_NAME: usize = 255;
/// The longest macro description, step note, Stop message or If value (as JSON), in characters.
pub const MAX_MACRO_TEXT: usize = 4_096;
/// The longest reference or token in a macro (an output anchor, an input's default range, a
/// Repeat's range, an If's cell), in characters.
pub const MAX_MACRO_REF: usize = 1_024;
/// The longest shortcut text (`Ctrl+Shift+M`), in characters.
pub const MAX_MACRO_SHORTCUT: usize = 64;
/// The most commands a macro's permissions list.
pub const MAX_MACRO_PERMISSIONS: usize = 256;
/// The most column types an input lists: one per column of a sheet.
pub const MAX_MACRO_COLUMNS: usize = 16_384;
/// A recorded or hand-made macro: a list of typed steps that runs as one undo step. Most steps run
/// a GridCraft command; Repeat, If and Stop add bounded control flow. References in step
/// parameters can be tokens (`"Input.col(2)"`) that are resolved against the declared inputs when
/// the macro runs, so a macro recorded on one block of data works on another.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct Macro {
/// Format version ([`MACRO_VERSION`]).
pub v: u32,
pub name: String,
pub description: String,
/// Keyboard shortcut (`Ctrl+Shift+M`) for a user interface to offer; the engine only stores it.
#[serde(skip_serializing_if = "Option::is_none")]
pub shortcut: Option<String>,
/// The ranges the macro works on, checked before it runs.
pub inputs: Vec<NamedInput>,
pub steps: Vec<Step>,
/// Where results go and what to do when they would land on existing data.
pub output: OutputRule,
/// Commands that need permission (reading or writing files) which the macro may run.
pub permissions: Vec<String>,
}
impl Default for Macro {
fn default() -> Self {
Macro {
v: MACRO_VERSION,
name: String::new(),
description: String::new(),
shortcut: None,
inputs: vec![],
steps: vec![],
output: OutputRule::default(),
permissions: vec![],
}
}
}
impl Macro {
/// Steps in the macro, counting those inside Repeat and If.
pub fn step_count(&self) -> usize {
fn count(steps: &[Step]) -> usize {
steps.iter().map(|s| 1usize.saturating_add(s.kind.children().map(|b| count(b)).sum::<usize>())).sum()
}
count(&self.steps)
}
/// Checks every field a file can set: the format version ([`check_version`]); the lengths of
/// the name, description, shortcut, notes, messages, references and command ids; the counts of
/// steps ([`MAX_MACRO_STEPS`]), inputs, permissions and column types; the nesting depth
/// ([`MAX_MACRO_DEPTH`]); and each input's declaration. Command parameters are bounded only by
/// the size of the whole macro, which [`MAX_MACRO_STEPS`] and the workbook's item limit cap.
/// Every struct is taken apart field by field, so a field added later doesn't compile here
/// until someone decides its limit.
pub fn check_limits(&self) -> Result<(), String> {
let Macro { v, name, description, shortcut, inputs, steps, output, permissions } = self;
check_version(u64::from(*v))?;
let name = name.trim();
if name.is_empty() {
return Err("A macro needs a name.".into());
}
if name.chars().count() > MAX_MACRO_NAME {
return Err(format!("A macro name can be at most {MAX_MACRO_NAME} characters long."));
}
if name.chars().any(char::is_control) {
return Err("A macro name can't hold line breaks or other control characters.".into());
}
check_text("A macro description", description, MAX_MACRO_TEXT, false)?;
if let Some(shortcut) = shortcut {
check_text("A shortcut", shortcut, MAX_MACRO_SHORTCUT, true)?;
}
let OutputRule { anchor, on_conflict: _ } = output;
if let Some(anchor) = anchor {
check_text("The output anchor", anchor, MAX_MACRO_REF, true)?;
}
if permissions.len() > MAX_MACRO_PERMISSIONS {
return Err(format!("A macro can list at most {MAX_MACRO_PERMISSIONS} permissions."));
}
for p in permissions {
check_id("A permission", p)?;
}
// Count without recursing past the depth limit (a hostile file can nest deeper).
// A Repeat or If at the top is nested 1 deep; one inside it, 2 deep.
fn walk(steps: &[Step], level: usize, count: &mut usize) -> Result<(), String> {
for s in steps {
*count = count.saturating_add(1);
if *count > MAX_MACRO_STEPS {
return Err(format!("A macro can have at most {MAX_MACRO_STEPS} steps."));
}
let Step { kind, enabled: _, breakpoint: _, note } = s;
check_text("A step note", note, MAX_MACRO_TEXT, false)?;
match kind {
StepKind::Command { id, params: _ } => check_id("A step's command", id)?,
StepKind::Repeat { over, of: _, max: _, body: _ } => check_text("A Repeat step's range", over, MAX_MACRO_REF, true)?,
StepKind::If { cell, op: _, value, then: _, otherwise: _ } => {
check_text("An If step's cell", cell, MAX_MACRO_REF, true)?;
// Empty and NotEmpty ignore the value, which is then null.
if serde_json::to_string(value).map_or(usize::MAX, |t| t.chars().count()) > MAX_MACRO_TEXT {
return Err(format!("An If step's value can be at most {MAX_MACRO_TEXT} characters long."));
}
}
StepKind::Stop { message, rollback: _ } => check_text("A Stop message", message, MAX_MACRO_TEXT, false)?,
}
if kind.children().next().is_some() && level > MAX_MACRO_DEPTH {
return Err(format!("Repeat and If steps can nest at most {MAX_MACRO_DEPTH} deep."));
}
for body in kind.children() {
walk(body, level + 1, count)?;
}
}
Ok(())
}
walk(steps, 1, &mut 0)?;
if inputs.len() > 64 {
return Err("A macro can declare at most 64 inputs.".into());
}
for (k, i) in inputs.iter().enumerate() {
let NamedInput { name, kind, rows, cols, header, col_types, non_empty: _, default_ref } = i;
let n = name.as_str();
let ident = n.chars().next().is_some_and(char::is_alphabetic) && n.chars().all(|c| c.is_alphanumeric() || c == '_');
if !ident || n.chars().count() > MAX_MACRO_NAME {
return Err(format!("'{n}' can't name an input: use letters, digits and _, starting with a letter."));
}
if ["Output", "Row", "Column", "Name"].iter().any(|r| r.eq_ignore_ascii_case(n)) {
return Err(format!("'{n}' is a token of its own and can't name an input."));
}
// A step written with plain A1 text (a recorded one) must keep meaning the cell.
if gridcraft_core::RangeRef::parse(n).is_some() {
return Err(format!("'{n}' reads as a cell reference and can't name an input."));
}
if inputs.iter().take(k).any(|o| o.name.eq_ignore_ascii_case(n)) {
return Err(format!("Two inputs are called '{n}'."));
}
for (what, [least, most]) in [("rows", rows), ("columns", cols)] {
if *most != 0 && least > most {
return Err(format!("Input '{n}' allows {least} to {most} {what}: the least can't be more than the most."));
}
}
// A cell is one row and one column ([1, 1], or the default [1, 0]) with no header.
let one = |b: &[u32; 2]| *b == [1, 1] || *b == [1, 0];
if *kind == InputKind::Cell && (!one(rows) || !one(cols) || *header || col_types.len() > 1) {
return Err(format!("Input '{n}' is a single cell: one row and one column, no header, at most one column type."));
}
let most = if cols[1] == 0 { MAX_MACRO_COLUMNS } else { usize::try_from(cols[1]).unwrap_or(usize::MAX).min(MAX_MACRO_COLUMNS) };
if col_types.len() > most {
return Err(format!("Input '{n}' lists the types of {} columns, but has at most {most}.", col_types.len()));
}
if let Some(r) = default_ref {
check_text(&format!("The default range of input '{n}'"), r, MAX_MACRO_REF, true)?;
}
}
Ok(())
}
}
/// Refuses `text` longer than `max` characters, and, for a one-line field, one that holds line
/// breaks or other control characters.
fn check_text(what: &str, text: &str, max: usize, one_line: bool) -> Result<(), String> {
if text.chars().count() > max {
return Err(format!("{what} can be at most {max} characters long."));
}
if one_line && text.chars().any(char::is_control) {
return Err(format!("{what} can't hold line breaks or other control characters."));
}
Ok(())
}
/// Refuses a command id that isn't one: empty, longer than a name, or holding anything other than
/// letters, digits, `.` and `_` (every command id is of that form).
fn check_id(what: &str, id: &str) -> Result<(), String> {
let ok = !id.is_empty() && id.len() <= MAX_MACRO_NAME && id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'.' || b == b'_');
if ok { Ok(()) } else { Err(format!("{what} must be a command id (letters, digits, . and _): '{}'", id.chars().take(64).collect::<String>())) }
}
/// The macros of a workbook. Workbook snapshots share the list until one of them changes it, so
/// an undo entry or a copy made for a cell edit holds the same list rather than a copy of every
/// step. Reads go through `Deref` to the list; a write through `DerefMut` first copies the list
/// when another snapshot shares it.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Macros(std::sync::Arc<Vec<Macro>>);
impl Macros {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Whether both hold the one list, with no copy made between them.
pub fn shares(&self, other: &Macros) -> bool {
std::sync::Arc::ptr_eq(&self.0, &other.0)
}
/// A rough size of the list in memory, for the undo budget: each macro's JSON, which holds
/// every name, text and step parameter, plus its structs. An undo step that keeps a copy of
/// the list (after a save, rename or delete) is charged for all of it. The JSON is counted as
/// it is written, without building it.
pub fn estimated_bytes(&self) -> usize {
/// Counts the bytes written to it.
struct Count(usize);
impl std::io::Write for Count {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0 = self.0.saturating_add(buf.len());
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
self.0.iter().fold(0usize, |bytes, m| {
let mut json = Count(0);
// Writing to a counter fails only for a value JSON can't hold, which a macro never is.
let _ = serde_json::to_writer(&mut json, m);
let steps = m.step_count().saturating_mul(std::mem::size_of::<Step>());
bytes.saturating_add(std::mem::size_of::<Macro>()).saturating_add(json.0).saturating_add(steps)
})
}
}
impl From<Vec<Macro>> for Macros {
fn from(list: Vec<Macro>) -> Macros {
Macros(std::sync::Arc::new(list))
}
}
impl std::ops::Deref for Macros {
type Target = Vec<Macro>;
fn deref(&self) -> &Vec<Macro> {
&self.0
}
}
impl std::ops::DerefMut for Macros {
fn deref_mut(&mut self) -> &mut Vec<Macro> {
std::sync::Arc::make_mut(&mut self.0)
}
}
impl IntoIterator for Macros {
type Item = Macro;
type IntoIter = std::vec::IntoIter<Macro>;
fn into_iter(self) -> Self::IntoIter {
std::sync::Arc::unwrap_or_clone(self.0).into_iter()
}
}
impl<'a> IntoIterator for &'a Macros {
type Item = &'a Macro;
type IntoIter = std::slice::Iter<'a, Macro>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl FromIterator<Macro> for Macros {
fn from_iter<I: IntoIterator<Item = Macro>>(iter: I) -> Macros {
Macros(std::sync::Arc::new(iter.into_iter().collect()))
}
}
/// Refuses a format version newer than [`MACRO_VERSION`]: this build would drop the fields it
/// doesn't know, and a save would then write the rest back as if it were the whole macro.
pub fn check_version(v: u64) -> Result<(), String> {
if v > u64::from(MACRO_VERSION) {
return Err(format!("This macro was made by a newer version of GridCraft (macro format {v}); this version reads format {MACRO_VERSION}."));
}
Ok(())
}
/// Reads the `macros` field of a JSON workbook as raw entries, whatever they hold, so they can be
/// checked one at a time.
pub(crate) fn entries<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<serde_json::Value>, D::Error> {
Ok(match serde_json::Value::deserialize(d)? {
serde_json::Value::Array(list) => list,
serde_json::Value::Null => vec![],
other => vec![other],
})
}
/// A range the macro works on, with the shape and content it expects.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct NamedInput {
/// The token that stands for it in step parameters (`Input`, `Rates`).
pub name: String,
pub kind: InputKind,
/// Allowed data rows (header excluded), `[min, max]`; a max of 0 means no limit.
pub rows: [u32; 2],
/// Allowed columns, `[min, max]`; a max of 0 means no limit.
pub cols: [u32; 2],
/// The first row holds column names.
pub header: bool,
/// What each column must hold, first column first; columns past the list may hold anything.
pub col_types: Vec<ColType>,
/// Every data cell must have something in it.
pub non_empty: bool,
/// The range used when a run doesn't name one (else the current selection, for the first input).
#[serde(skip_serializing_if = "Option::is_none")]
pub default_ref: Option<String>,
}
impl Default for NamedInput {
fn default() -> Self {
NamedInput {
name: "Input".into(),
kind: InputKind::Range,
rows: [1, 0],
cols: [1, 0],
header: false,
col_types: vec![],
non_empty: false,
default_ref: None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum InputKind {
/// A single cell.
Cell,
#[default]
Range,
}
/// What a column of an input must hold. Empty cells pass unless the input is `non_empty`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ColType {
Number,
Text,
/// A number shown with a date or time format.
Date,
Bool,
#[default]
Any,
}
impl ColType {
/// The plural noun used in messages ("must hold numbers").
pub fn noun(self) -> &'static str {
match self {
ColType::Number => "numbers",
ColType::Text => "text",
ColType::Date => "dates",
ColType::Bool => "TRUE or FALSE",
ColType::Any => "anything",
}
}
}
/// One macro step and its editor flags.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Step {
#[serde(flatten)]
pub kind: StepKind,
/// A disabled step is skipped.
#[serde(default = "yes", skip_serializing_if = "is_true")]
pub enabled: bool,
/// The debugger stops before this step.
#[serde(default, skip_serializing_if = "is_false")]
pub breakpoint: bool,
#[serde(default, skip_serializing_if = "String::is_empty")]
pub note: String,
}
fn yes() -> bool {
true
}
fn is_true(b: &bool) -> bool {
*b
}
fn is_false(b: &bool) -> bool {
!*b
}
impl Step {
pub fn new(kind: StepKind) -> Step {
Step { kind, enabled: true, breakpoint: false, note: String::new() }
}
/// A step that runs command `id` with `params`.
pub fn command(id: impl Into<String>, params: serde_json::Value) -> Step {
Step::new(StepKind::Command { id: id.into(), params })
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum StepKind {
/// Runs a GridCraft command. String parameters may hold tokens, resolved when the step runs.
Command {
id: String,
#[serde(default)]
params: serde_json::Value,
},
/// Runs `body` once for each row (or column) of the range `over`, a token or reference; inside
/// it, `Row` (or `Column`) stands for the current one. At most `max` times.
Repeat {
over: String,
#[serde(default)]
of: RepeatOf,
#[serde(default = "repeat_max")]
max: u32,
#[serde(default)]
body: Vec<Step>,
},
/// Compares the value of `cell` (a token or reference to one cell) with `value` and runs
/// `then` or `else`.
If {
cell: String,
op: CompareOp,
#[serde(default)]
value: serde_json::Value,
#[serde(default)]
then: Vec<Step>,
#[serde(default, rename = "else")]
otherwise: Vec<Step>,
},
/// Ends the run with a message. The changes made so far are kept, or undone with `rollback`.
Stop {
#[serde(default)]
message: String,
#[serde(default, skip_serializing_if = "is_false")]
rollback: bool,
},
}
fn repeat_max() -> u32 {
10_000
}
impl StepKind {
/// The step lists inside this step (Repeat's body, If's branches).
pub fn children(&self) -> impl Iterator<Item = &Vec<Step>> {
let (a, b) = match self {
StepKind::Repeat { body, .. } => (Some(body), None),
StepKind::If { then, otherwise, .. } => (Some(then), Some(otherwise)),
_ => (None, None),
};
a.into_iter().chain(b)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum RepeatOf {
#[default]
Rows,
Columns,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum CompareOp {
Eq,
Ne,
Gt,
Ge,
Lt,
Le,
/// The cell is empty (`value` is ignored).
Empty,
NotEmpty,
/// The cell's text contains `value`'s text, ignoring case.
Contains,
}
/// Where a macro's results go.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase")]
pub struct OutputRule {
/// What the `Output` token stands for: a token (`Input.offset(0,4)`), a reference or `Name:X`.
#[serde(skip_serializing_if = "Option::is_none")]
pub anchor: Option<String>,
/// What to do when the cells the macro writes, outside its inputs, already hold data.
pub on_conflict: OnConflict,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum OnConflict {
/// Ask first: overwrite, shift the data down or right, or cancel. Programmatic runs fail.
#[default]
Ask,
Fail,
Overwrite,
ShiftDown,
ShiftRight,
}
impl OnConflict {
pub fn parse(s: &str) -> Option<OnConflict> {
Some(match s.to_ascii_lowercase().replace(['_', '-', ' '], "").as_str() {
"ask" => OnConflict::Ask,
"fail" | "cancel" => OnConflict::Fail,
"overwrite" => OnConflict::Overwrite,
"shiftdown" => OnConflict::ShiftDown,
"shiftright" => OnConflict::ShiftRight,
_ => return None,
})
}
pub fn name(self) -> &'static str {
match self {
OnConflict::Ask => "ask",
OnConflict::Fail => "fail",
OnConflict::Overwrite => "overwrite",
OnConflict::ShiftDown => "shiftDown",
OnConflict::ShiftRight => "shiftRight",
}
}
}
-66
View File
@@ -30,51 +30,6 @@ pub enum ShowObjects {
None,
}
/// The workbook window's display settings from `<workbookView>` (Excel: File › Options ›
/// Advanced, "Display options for this workbook"): whether the window shows the sheet tabs
/// and the two scroll bars, and how much of the horizontal bar the tabs take (`tabRatio`,
/// thousandths of the bar; the format's default is 600). XLSX I/O normalizes this
/// share to 0–1000, including values supplied directly through the model.
///
/// The values are modelled so an open + save keeps them — a workbook Excel saved with the
/// tabs hidden must not come back with them shown and the settings gone. The app does not
/// act on them yet and there is no Options control for them; that is the other half of
/// storytold/gridcraft#3057.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct WorkbookView {
#[serde(default = "yes")]
pub show_sheet_tabs: bool,
#[serde(default = "yes")]
pub show_horizontal_scroll: bool,
#[serde(default = "yes")]
pub show_vertical_scroll: bool,
#[serde(default = "default_tab_ratio")]
pub tab_ratio: u32,
}
fn yes() -> bool {
true
}
fn default_tab_ratio() -> u32 {
600
}
impl Default for WorkbookView {
fn default() -> Self {
WorkbookView { show_sheet_tabs: true, show_horizontal_scroll: true, show_vertical_scroll: true, tab_ratio: default_tab_ratio() }
}
}
impl WorkbookView {
/// The entire bar, in thousandths: the upper bound used by XLSX I/O.
pub const MAX_TAB_RATIO: u32 = 1000;
fn is_default(&self) -> bool {
*self == WorkbookView::default()
}
}
/// File › Options › Formulas › Calculation options, stored per workbook (`<calcPr>`).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CalcSettings {
@@ -195,9 +150,6 @@ pub struct Workbook {
pub show_objects: ShowObjects,
/// Read and set with [`Workbook::active_sheet`] and [`Workbook::set_active_sheet`].
pub active_sheet: ActiveSheet,
/// The workbook window's display settings (`<workbookView>`), kept across an open + save.
#[serde(default, skip_serializing_if = "WorkbookView::is_default")]
pub view: WorkbookView,
pub props: DocProps,
/// Workbook structure protection.
pub protected_structure: bool,
@@ -229,20 +181,6 @@ pub struct Workbook {
/// separator back (a `;` file stays a `;` file). `None` = not read from CSV: CSV saves use `,`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub csv_delimiter: Option<u8>,
/// GridCraft macros (typed command lists, not VBA), in the order the macro list shows them.
/// The JSON format keeps them as this field; xlsx keeps them in hidden defined names.
#[serde(default, skip_serializing_if = "crate::macros::Macros::is_empty", skip_deserializing)]
pub macros: crate::macros::Macros,
/// The `macros` entries of a JSON file as read, before the engine checks each one on its own
/// and moves it into `macros`, so one damaged or newer macro doesn't stop the file opening.
/// Never written.
#[serde(rename = "macros", default, skip_serializing, deserialize_with = "crate::macros::entries")]
pub macros_read: Vec<serde_json::Value>,
/// Macros in a newer format than this build reads, as the file held them. They are never read,
/// listed or run; a save writes them back unchanged, so the newer build gets them back whole.
/// Shared between snapshots, as nothing changes them.
#[serde(skip)]
pub macros_unread: Arc<Vec<serde_json::Value>>,
/// Unmodelled file content kept for saving (package root and workbook level). Not in JSON.
#[serde(skip)]
pub preserved: Option<Arc<crate::preserved::Preserved>>,
@@ -332,7 +270,6 @@ impl Workbook {
calc: CalcSettings::default(),
show_objects: ShowObjects::All,
active_sheet: ActiveSheet::default(),
view: WorkbookView::default(),
props: DocProps::default(),
protected_structure: false,
structure_password: None,
@@ -341,9 +278,6 @@ impl Workbook {
phonetics: vec![],
vba_project: None,
csv_delimiter: None,
macros: crate::macros::Macros::default(),
macros_read: vec![],
macros_unread: Arc::default(),
preserved: None,
source_dir: None,
source_path: None,
+1 -13
View File
@@ -15,7 +15,7 @@ use gridcraft_core::{CellError, DateSystem, Value};
pub use builtin::{builtin_format, builtin_id};
pub use decimal::format_general_fit;
use parse::{DatePart, SecKind, Section, Tok};
use parse::{SecKind, Section, Tok};
use render::Out;
/// Width (in characters) of General output in a cell.
@@ -193,18 +193,6 @@ impl NumberFormat {
self.num_sections().iter().any(|s| s.kind == SecKind::Date)
}
/// For a date or time format, whether the section that formats `value` shows calendar parts (year, month,
/// day) and whether it shows time parts, as `(date, time)`. `None` for any other format and for
/// elapsed-time codes like `[h]:mm`, whose value is a duration, not a moment.
pub fn date_shape(&self, value: f64) -> Option<(bool, bool)> {
let s = self.select(value).map(|(s, _)| s).filter(|s| s.kind == SecKind::Date)?;
let parts = || s.toks.iter().filter_map(|t| if let Tok::Date(d) = t { Some(*d) } else { None });
if parts().any(|d| matches!(d, DatePart::ElapsedH(_) | DatePart::ElapsedM(_) | DatePart::ElapsedS(_))) {
return None;
}
Some((parts().any(DatePart::is_calendar), parts().any(|d| !d.is_calendar())))
}
pub fn is_percent(&self) -> bool {
self.num_sections().iter().any(|s| s.kind == SecKind::Number && s.percent > 0)
}
+1 -1
View File
@@ -11,7 +11,7 @@ workspace = true
[dependencies]
# The shared capture slot (`craft_ui::capture`) comes from the one craft-libs revision every
# craft-libs dependency uses (see the root Cargo.toml).
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "c5a1cbfb5f9049f9270955caffb5bee5997aebf7" }
craft-ui = { git = "https://github.com/storytold/craft-libs", rev = "92d392cff132eaf53e13afff77f38b5c11b73aa0" }
gridcraft-engine = { workspace = true }
gridcraft-chart = { workspace = true }
# `monochrome_emoji_fonts` restores the Noto Emoji and emoji-icon faces that 0.37 moved behind
+4 -3
View File
@@ -1,7 +1,8 @@
//! Bake the contributor credits into the binary as static Rust tables. Nothing is read at run time.
//! Bake the contributor credits into the binary as static Rust tables (craftrules
//! standards/contributors.md). Nothing is read at run time.
//!
//! - contributors/contributors.json: commit stats per GitHub username, with a mark for whose
//! people.toml entry is verified.
//! - contributors/contributors.json: commit stats per GitHub username, generated by craftrules
//! scripts/contributors.py, which also marks whose people.toml entry it verified.
//! - contributors/people.toml: the names people chose for themselves. A name is used only when the
//! JSON says the entry was verified (`people_entry`).
//!
+1 -5
View File
@@ -8,7 +8,7 @@ use super::updates_ui;
use super::{
CellRef, HashMap, INTERFACE_SCALE_MAX, INTERFACE_SCALE_MIN, Json, Notice, Perf, RangeRef, Session, SheetApp, UiRequest, UiState, backstage,
clamp_interface_scale, clean_error, control, desktop_theme, dialogs, editor, floating_pictures, formula_bar, formula_locale, grid, i18n, json,
keytips, macro_ui, panes, pivot_pane, printing, ribbon, tabs, text_box, theme, widgets,
keytips, panes, pivot_pane, printing, ribbon, tabs, text_box, theme, widgets,
};
mod clipboard;
@@ -72,10 +72,8 @@ impl SheetApp {
clipboard_text_at_copy: None,
cut_text: None,
tab_run: None,
macros: macro_ui::MacroUi::default(),
started: now_ms(),
allow_close: false,
bring_forward: false,
open_switch_windows: false,
ui_window: None,
parked: HashMap::new(),
@@ -237,8 +235,6 @@ impl SheetApp {
});
}
dialogs::show(self, &ctx);
macro_ui::forget_closed_editor(self);
macro_ui::question_box(self, &ctx);
widgets::message_box(self, &ctx);
widgets::toast(self, &ctx);
// Requests are admitted in `logic`, for the root viewport, so only its pass issues them.
-17
View File
@@ -55,15 +55,6 @@ impl SheetApp {
// Requests the command made (a dialog, a draft) belong to the window it left active.
self.follow_active_window();
self.after_engine();
if id == "formulas.autoSum"
&& let Some(ed) = self.editor.as_mut()
&& ed.text.starts_with('=')
&& ed.text.ends_with("()")
{
ed.caret = ed.text.chars().count().saturating_sub(1);
ed.anchor = ed.caret;
ed.sync_caret = true;
}
if id == "view.arrangeAll"
&& self.native_windows
&& let Ok(r) = &r
@@ -339,14 +330,6 @@ impl SheetApp {
let page = params.get("page").and_then(Json::as_str).and_then(backstage::Page::parse).unwrap_or(backstage::Page::Home);
self.open_backstage(page);
}
// The macro step editor stays until it closes itself (its draft, overlays and debug
// run go with it), so another dialog can't replace it.
_ if self.dialog.as_ref().is_some_and(|d| d.name == "macroEditor") => {
self.message = Some((
"Macro".into(),
"Close the macro step editor first: it keeps its window until it closes itself, so its steps and debug run are not lost.".into(),
));
}
other => self.dialog = dialogs::Dialog::open(self, other, params),
}
}
-88
View File
@@ -15,13 +15,6 @@ impl SheetApp {
let Some(d) = self.session.active() else { return };
let Some(sh) = d.wb.active() else { return };
let at = sh.merge_at(d.selection.active).map(|m| m.start).unwrap_or(d.selection.active);
// A locked cell on a protected sheet can't be entered (Excel's warning, and the engine
// refuses `cell.set` anyway): refuse before a draft exists, rather than leaving one that
// can never be committed and blocks Save (#2945).
if let Some(m) = self.session.locked_cell_message(d.wb.active_sheet(), at) {
self.message = Some(("GridCraft".into(), m));
return;
}
// A picture has no editable scalar text. Selecting it, F2, or clicking
// the formula bar must not turn its fallback value into a text edit.
// Typing still starts an intentional replacement.
@@ -109,13 +102,6 @@ impl SheetApp {
}
return true;
}
// A draft on a cell sheet protection now locks (the sheet was protected while it was
// open) can't be entered: drop it with the warning instead of keeping it, which would
// block Save with a draft that can never be committed (#2945).
if let Some(m) = self.session.locked_cell_message(ed.sheet, ed.cell) {
self.message = Some(("GridCraft".into(), m));
return true;
}
let text = match self.session.active().map(|d| formula_locale::canonical(&text, ed.locale, &d.wb, ed.sheet, ed.cell)) {
Some(Ok(text)) => text,
Some(Err(error)) => {
@@ -223,77 +209,3 @@ impl SheetApp {
self.grid.ensure_visible = true;
}
}
#[cfg(test)]
mod protected_cell_tests {
use super::*;
use gridcraft_engine::Session;
use gridcraft_engine::core::{CellRef, Value};
/// A protected sheet whose C2 is unlocked; C2 holds "keep" as the last undo step.
fn protected_book() -> SheetApp {
let mut session = Session::new();
session.new_workbook();
let mut app = SheetApp::new(session, Default::default());
app.run("cell.set", json!({"cell": "C2", "input": "keep"})).unwrap();
app.run("home.formatCells", json!({"range": "C2", "style": {"protection": {"locked": false}}})).unwrap();
app.run("review.protectSheet", json!({})).unwrap();
app
}
fn value(app: &SheetApp, a1: &str) -> Value {
let at = CellRef::parse(a1).unwrap();
app.session.active().and_then(|d| d.wb.sheet(0)).map(|sh| sh.value(at)).unwrap_or_default()
}
fn protected_message(app: &SheetApp) -> Option<&str> {
app.message.as_ref().map(|(_, m)| m.as_str())
}
#[test]
fn typing_in_a_locked_cell_does_not_leave_a_draft_that_blocks_undo_and_save() {
let mut app = protected_book();
assert_eq!(app.session.active().unwrap().selection.active.a1(), "A1", "the locked A1 is the active cell");
let undo = app.session.active().unwrap().undo.len();
let dirty = app.session.active().unwrap().is_dirty();
app.begin_edit(Some("locked".into()), false);
assert!(app.editor.is_none(), "a locked cell must not open an editor");
assert!(protected_message(&app).is_some_and(|m| m.contains("protected sheet")), "the warning names the protected sheet");
assert_eq!(app.session.active().unwrap().undo.len(), undo, "a refused edit adds no undo step");
assert_eq!(app.session.active().unwrap().is_dirty(), dirty, "a refused edit leaves the workbook as it was");
assert!(app.run("file.saveBytes", json!({"format": "xlsx"})).is_ok(), "Save is not blocked by a draft that can never be entered");
}
#[test]
fn an_unlocked_cell_on_a_protected_sheet_still_edits_undoes_and_saves() {
let mut app = protected_book();
app.run("selection.set", json!({"cell": "C2"})).unwrap();
app.begin_edit(Some("typed".into()), false);
assert!(app.editor.is_some(), "an unlocked cell opens its editor");
assert!(app.commit_edit(0, 0, false, false));
assert_eq!(value(&app, "C2"), Value::text("typed"));
assert!(app.run("edit.undo", json!({})).is_ok());
assert_eq!(value(&app, "C2"), Value::text("keep"), "Undo of the typed edit still works");
assert!(app.run("file.saveBytes", json!({"format": "xlsx"})).is_ok());
}
#[test]
fn a_draft_that_becomes_locked_is_cancelled_instead_of_staying_open() {
let mut app = protected_book();
app.run("selection.set", json!({"cell": "C2"})).unwrap();
app.begin_edit(Some("typed".into()), false);
// The sheet is unprotected, C2 locked, and the sheet protected again while the draft is open.
app.run("review.unprotectSheet", json!({})).unwrap();
app.run("home.formatCells", json!({"range": "C2", "style": {"protection": {"locked": true}}})).unwrap();
app.run("review.protectSheet", json!({})).unwrap();
assert!(app.commit_edit(0, 0, false, false), "the refused draft is handled, not left open");
assert!(app.editor.is_none());
assert!(protected_message(&app).is_some_and(|m| m.contains("protected sheet")));
assert_eq!(value(&app, "C2"), Value::text("keep"));
assert!(app.run("file.saveBytes", json!({"format": "xlsx"})).is_ok());
}
}
-8
View File
@@ -12,13 +12,6 @@ impl SheetApp {
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
return;
}
// The macro step editor asks about its own unsaved steps and debug run first; once it has
// closed, the window close goes on.
if self.dialog.as_ref().is_some_and(|d| d.name == "macroEditor") {
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
crate::macro_editor::request_close(self, true);
return;
}
if !self.session.documents().iter().any(|d| d.is_dirty()) {
return;
}
@@ -42,7 +35,6 @@ impl SheetApp {
self.enter_window(window);
let title = self.session.active().map(|d| d.display_title()).unwrap_or_default();
self.open_dialog("saveChanges", json!({"title": title, "quit": true}));
self.bring_forward = true;
}
None => {
self.allow_close = true;
+1 -5
View File
@@ -114,8 +114,6 @@ pub struct Backstage {
pub page: Page,
/// Info: property drafts while a field is being edited (committed when it loses focus).
props: [String; 6],
/// Focus at the end of the previous frame, before egui clears it on Esc.
text_edit_focused: bool,
/// Print: the whole workbook rather than the active sheet.
pub print_all: bool,
/// Print: fit each sheet on one page.
@@ -140,7 +138,6 @@ impl Backstage {
Backstage {
page,
props: Default::default(),
text_edit_focused: false,
print_all: false,
fit_to_page: false,
preview_page: 0,
@@ -163,7 +160,7 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui) {
let Some(mut bs) = app.backstage.take() else { return };
let ctx = ui.ctx().clone();
// Esc inside a text field leaves the field; it closes the view only from outside one.
let typing = bs.text_edit_focused;
let typing = ctx.text_edit_focused();
let t = Tokens::get(&ctx);
let lang = app.ui.language;
let mut out = Outcome::default();
@@ -232,7 +229,6 @@ pub fn show(app: &mut SheetApp, ui: &mut Ui) {
});
});
});
bs.text_edit_focused = ctx.text_edit_focused();
if !typing
&& app.dialog.is_none()
&& app.message.is_none()
+16 -17
View File
@@ -264,23 +264,6 @@ pub(crate) fn paint_objects(app: &SheetApp, p: &Painter, geo: &Geo, wb: &Workboo
if objects_hidden(wb) {
return painted;
}
// Source outlines stay in cell space beneath every floating object.
if let Some(ch) = sh.charts.iter().find(|ch| app.selected_chart == Some(ch.id))
&& anchor_rect(geo, sh, &ch.anchor).intersects(p.clip_rect())
{
for s in &ch.series {
if let Ok(gridcraft_engine::formula::Expr::Ref(rf)) = gridcraft_engine::formula::parse(&s.values)
&& rf.sheet_name().is_none_or(|n| n.eq_ignore_ascii_case(&sh.name))
{
let rr = geo.range_rect(sh, rf.range());
let color = Tokens::get(p.ctx()).ref_color(0, true);
for pane in geo.range_panes(rf.range()) {
let pp = p.with_clip_rect(pane.intersect(p.clip_rect()));
pp.rect_stroke(rr, 0.0, Stroke::new(1.5, color), StrokeKind::Inside);
}
}
}
}
let mut done = |start: egui::layers::ShapeIdx, rect: Rect| painted.push(PaintedObject { shapes: start.0..p.add(egui::Shape::Noop).0, rect });
// The grid paints the active workbook: its pictures are cached under it.
let doc = app.session.active().map_or(0, |d| d.uid);
@@ -382,7 +365,23 @@ pub(crate) fn paint_objects(app: &SheetApp, p: &Painter, geo: &Geo, wb: &Workboo
if app.selected_chart == Some(ch.id) {
selection_frame(p, r);
}
// The source-data highlight below is in cell space: mirrored with the cells.
done(start, r);
if app.selected_chart == Some(ch.id) {
// Highlight the source data like Excel does.
for s in &ch.series {
if let Ok(gridcraft_engine::formula::Expr::Ref(rf)) = gridcraft_engine::formula::parse(&s.values)
&& rf.sheet_name().is_none_or(|n| n.eq_ignore_ascii_case(&sh.name))
{
let rr = geo.range_rect(sh, rf.range());
let color = Tokens::get(p.ctx()).ref_color(0, true);
for pane in geo.range_panes(rf.range()) {
let pp = p.with_clip_rect(pane.intersect(p.clip_rect()));
pp.rect_stroke(rr, 0.0, Stroke::new(1.5, color), StrokeKind::Inside);
}
}
}
}
}
paint_slicers(app, p, geo, wb, si, sh);
painted
-7
View File
@@ -282,13 +282,6 @@ pub fn handle(app: &mut SheetApp, ctx: &egui::Context, method: &str, p: &Json) -
None => err("no dialog is open"),
},
"ui.dialog.cancel" => {
// The macro step editor's draft, overlays and debug run go with its own close.
if app.dialog.as_ref().is_some_and(|d| d.name == "macroEditor") {
return match crate::macro_editor::close_now(app) {
Ok(()) => ok(Json::Null),
Err(e) => err(e),
};
}
app.dialog = None;
ok(Json::Null)
}
+2 -1
View File
@@ -1,6 +1,7 @@
//! The About window's credits: every contributor (GitHub login always; display and real names only
//! with recorded consent) and the AI models that helped. The tables are baked into the binary by
//! build.rs from contributors/contributors.json; nothing is read at run time.
//! build.rs from contributors/contributors.json (craftrules standards/contributors.md); nothing is
//! read at run time.
use std::cmp::Ordering;
+19 -42
View File
@@ -22,6 +22,7 @@ mod data;
mod find;
mod format_cells;
mod insert_function;
mod macros;
mod names;
mod paste;
mod show;
@@ -72,8 +73,6 @@ pub struct Dialog {
focus_initialized: bool,
/// The formula box that takes references clicked or dragged on the grid (New Formatting Rule).
pick: Option<RefPick>,
/// The linked cell's initial display text; unchanged text must not replace a number or formula.
original_link_text: Option<String>,
}
/// A formula box in point mode, as in Excel: a click on the grid writes the cell's absolute
@@ -167,7 +166,6 @@ impl Dialog {
self.values.insert(pick.key.into(), json!(ed.text));
}
/// Creates a command-backed form with its initial field values.
fn form(name: &str, title: &str, command: &'static str, fields: Vec<Field>, defaults: Json) -> Dialog {
Dialog {
name: name.into(),
@@ -186,12 +184,10 @@ impl Dialog {
back: None,
focus_initialized: false,
pick: None,
original_link_text: None,
}
}
/// Creates a dialog whose body handles its own controls instead of a standard form.
pub(crate) fn custom(name: &str, title: &str, defaults: Json) -> Dialog {
fn custom(name: &str, title: &str, defaults: Json) -> Dialog {
Dialog {
name: name.into(),
title: title.into(),
@@ -209,11 +205,9 @@ impl Dialog {
back: None,
focus_initialized: false,
pick: None,
original_link_text: None,
}
}
/// Opens the named dialog with defaults from the active workbook, including a cell's stored hyperlink.
pub fn open(app: &mut SheetApp, name: &str, params: Json) -> Option<Dialog> {
let sel = app.session.active().map(|d| d.selection.current().a1()).unwrap_or_default();
let active = app.session.active().map(|d| d.selection.active.a1()).unwrap_or_default();
@@ -501,30 +495,17 @@ impl Dialog {
json!({"url": url}),
)
}
"insertLink" => {
let mut d = Dialog::form(
"insertLink",
"Insert Hyperlink",
"insert.link",
vec![
Text { key: "text", label: "Display:" },
Text { key: "target", label: "Link to (web address, e-mail or Sheet!A1):" },
Text { key: "tooltip", label: "ScreenTip:" },
],
json!({"target": "https://"}),
);
if let Some(doc) = app.session.active()
&& let Some(sheet) = doc.wb.active()
&& let Some(link) = sheet.hyperlinks.get(&doc.selection.active)
{
let text = gridcraft_engine::display::cell_text(&doc.wb, sheet, doc.selection.active);
d.values.insert("target".into(), json!(link.target));
d.values.insert("text".into(), json!(text));
d.values.insert("tooltip".into(), json!(link.tooltip));
d.original_link_text = Some(text);
}
d
}
"insertLink" => Dialog::form(
"insertLink",
"Insert Hyperlink",
"insert.link",
vec![
Text { key: "text", label: "Display:" },
Text { key: "target", label: "Link to (web address, e-mail or Sheet!A1):" },
Text { key: "tooltip", label: "ScreenTip:" },
],
json!({"target": "https://"}),
),
"sparkline" => Dialog::form(
"sparkline",
"Create Sparklines",
@@ -853,13 +834,7 @@ impl Dialog {
}
d
}
"insertFunction" => {
// A Function Library menu opens it on its own category.
let category = p("category").as_str().unwrap_or(crate::function_library::MOST_RECENTLY_USED).to_string();
let mut d = Dialog::custom("insertFunction", "Insert Function", json!({"category": category}));
d.focus_first_field = true;
d
}
"insertFunction" => Dialog::custom("insertFunction", "Formula Builder", json!({"category": "Most Recently Used"})),
"find" => {
let mut d = Dialog::custom("find", "Find and Replace", json!({"what": "", "with": "", "within": "sheet", "lookIn": "formulas"}));
d.tab = if p("replace").as_bool() == Some(true) { "Replace".into() } else { "Find".into() };
@@ -971,9 +946,11 @@ impl Dialog {
)
}
"journal" => Dialog::custom("journal", "Action Journal", json!({})),
"macros" => crate::macro_ui::open_dialog(app, &params),
"recordMacro" => return crate::macro_ui::open_record(app, &params),
"macroEditor" => return crate::macro_editor::open(app, &params),
"macros" => {
let mut d = Dialog::custom("macros", "Macros", json!({}));
d.result = app.session.run("view.macros", json!({})).ok();
d
}
"statistics" => {
let mut d = Dialog::custom("statistics", "Workbook Statistics", json!({}));
d.result = app.session.run("review.workbookStatistics", json!({})).ok();
+38 -94
View File
@@ -1,97 +1,33 @@
use super::show::{ok_cancel, take_first_focus};
use super::{Dialog, Json, SheetApp, json, vec2};
use super::show::ok_cancel;
use super::{Dialog, Json, SheetApp, vec2};
/// Insert Function: search names (as the formula language spells them) and descriptions, or pick
/// a category (Most Recently Used, All or one category), then a function; each hit shows its
/// category and description. OK starts editing `=NAME(` (or appends to the formula).
/// Insert Function: searches names (as the formula language spells them) and descriptions, shows
/// each hit's category and description, and starts editing `=NAME(` (or appends to the formula).
pub(super) fn insert_function(app: &mut SheetApp, ui: &mut egui::Ui, d: &mut Dialog, confirm: &mut bool) {
use crate::function_library::{self as lib, ALL, MOST_RECENTLY_USED};
let language = app.ui.language;
ui.horizontal(|ui| {
ui.label(language.tr("Search:"));
let r = ui.add(egui::TextEdit::singleline(&mut d.search).desired_width(300.0).hint_text(language.tr("e.g. lookup, average, date")));
// Once only, so Tab can move on to the category list.
take_first_focus(d, &r);
r.request_focus();
});
// A new search (typed, or set through the control channel) looks through every function, as
// Excel's does.
if d.values.get("searched").and_then(Json::as_str) != Some(d.search.as_str()) {
d.list_index = 0;
if !d.search.trim().is_empty() {
d.values.insert("category".into(), json!(ALL));
}
d.values.insert("searched".into(), json!(d.search));
}
// The category may be given by label or by engine id (a Function Library menu, `ui.dialog.set`).
let saved = d.values.get("category").and_then(Json::as_str).unwrap_or(MOST_RECENTLY_USED).to_string();
let mut category = if saved.eq_ignore_ascii_case(ALL) {
ALL.to_string()
} else {
lib::category_id(&saved).map(lib::category_label).unwrap_or(MOST_RECENTLY_USED).to_string()
};
// The category list is the dialog's only menu, so a menu open as the frame starts is its own
// (Enter on a row picks it and closes the menu in this frame).
let menu_was_open = egui::Popup::is_any_open(ui.ctx());
let picker = ui.horizontal(|ui| {
let label = ui.label(language.tr("Or select a category:"));
craft_ui::ComboBox::from_id_salt("insertFunctionCategory")
.width(220.0)
.selected_text(language.tr(&category))
.show_ui(ui, |ui| {
for c in [MOST_RECENTLY_USED, ALL].into_iter().chain(lib::CATEGORIES.iter().map(|(_, l)| *l)) {
ui.selectable_value(&mut category, c.to_string(), language.tr(c));
}
})
.response
.labelled_by(label.id)
});
// Enter on the category list or in its menu opens it or picks a category, as Space does
// (egui clicks a focused control on either key); it does not also confirm the dialog.
let picker = picker.inner;
if menu_was_open || picker.has_focus() || craft_ui::ComboBox::is_open(ui.ctx(), picker.id) {
ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Enter));
}
if category != saved {
d.list_index = 0;
}
d.values.insert("category".into(), json!(category));
let locale = language.formula_locale();
let list = gridcraft_engine::cmd::formulas::function_list();
// The names and descriptions searched are the ones shown, so in the interface language.
let q = d.search.trim().to_lowercase();
let matches = |f: &lib::FnEntry| {
q.is_empty()
|| locale.function_name(&f.name).to_lowercase().contains(&q)
|| language.function_description(&f.description).unwrap_or(&f.description).to_lowercase().contains(&q)
};
let hits: Vec<&lib::FnEntry> = if category == MOST_RECENTLY_USED {
lib::recent(app).iter().filter_map(|n| lib::find(n)).filter(|f| matches(f)).collect()
} else {
let id = lib::category_id(&category);
// The catalog is fixed (some 500 functions), so All lists every one.
lib::all().iter().filter(|f| id.is_none_or(|id| f.category == id) && matches(f)).collect()
};
let t = crate::theme::Tokens::get(ui.ctx());
let font = egui::TextStyle::Body.resolve(ui.style());
let rows: Vec<(&str, &str)> = hits.iter().map(|f| (locale.function_name(&f.name), language.tr(lib::category_label(&f.category)))).collect();
// The category column starts after the widest name shown, so a long name in any formula
// language or font never runs into it.
let name_w = rows.iter().map(|(n, _)| ui.painter().layout_no_wrap(n.to_string(), font.clone(), t.text).size().x).fold(0.0_f32, f32::max);
egui::ScrollArea::vertical().max_height(260.0).auto_shrink([false, false]).show(ui, |ui| {
if rows.is_empty() {
ui.label(egui::RichText::new(language.tr("No functions match.")).italics());
}
let row_h = ui.text_style_height(&egui::TextStyle::Body) + 4.0;
for (i, (name, category)) in rows.iter().enumerate() {
let (rect, r) = ui.allocate_exact_size(vec2(ui.available_width().max(name_w + 120.0), row_h), egui::Sense::click());
if i == d.list_index {
ui.painter().rect_filled(rect, 3.0, ui.visuals().selection.bg_fill);
} else if r.hovered() {
ui.painter().rect_filled(rect, 3.0, t.hover);
}
let color = if i == d.list_index { ui.visuals().selection.stroke.color } else { t.text };
ui.painter().text(rect.left_center() + vec2(6.0, 0.0), egui::Align2::LEFT_CENTER, *name, font.clone(), color);
ui.painter().text(rect.left_center() + vec2(6.0 + name_w + 16.0, 0.0), egui::Align2::LEFT_CENTER, *category, font.clone(), t.text_dim);
r.widget_info(|| egui::WidgetInfo::selected(egui::Role::Button, true, i == d.list_index, format!("{name} ({category})")));
let q = d.search.to_lowercase();
let hits: Vec<&Json> = list
.iter()
.filter(|f| {
q.is_empty()
|| f["name"].as_str().is_some_and(|n| locale.function_name(n).to_lowercase().contains(&q))
|| f["description"].as_str().is_some_and(|n| language.function_description(n).unwrap_or(n).to_lowercase().contains(&q))
})
.take(300)
.collect();
egui::ScrollArea::vertical().max_height(260.0).show(ui, |ui| {
for (i, f) in hits.iter().enumerate() {
let name = locale.function_name(f["name"].as_str().unwrap_or(""));
let category = language.tr(function_category_label(f["category"].as_str().unwrap_or("")));
let r = ui.add(egui::Button::selectable(i == d.list_index, format!("{name:<18} {category}")).min_size(vec2(480.0, 20.0)));
if r.clicked() {
d.list_index = i;
}
@@ -102,19 +38,15 @@ pub(super) fn insert_function(app: &mut SheetApp, ui: &mut egui::Ui, d: &mut Dia
}
});
if let Some(f) = hits.get(d.list_index) {
let name = f["name"].as_str().unwrap_or("");
ui.separator();
ui.label(egui::RichText::new(crate::formula_locale::signature(&f.name, locale, language).unwrap_or_default()).strong());
ui.label(crate::formula_locale::description(&f.name, locale, language).unwrap_or_default());
ui.label(egui::RichText::new(crate::formula_locale::signature(name, locale, language).unwrap_or_default()).strong());
ui.label(crate::formula_locale::description(name, locale, language).unwrap_or_default());
}
let mut open = true;
ok_cancel(ui, confirm, &mut open);
// The function counts as recently used once the formula is entered (the engine notes it).
if *confirm && let Some(f) = hits.get(d.list_index) {
let n = app
.session
.active()
.map(|doc| crate::formula_locale::completion_name(&f.name, locale, &doc.wb, doc.wb.active_sheet()))
.unwrap_or(&f.name);
if *confirm && let Some(n) = hits.get(d.list_index).and_then(|f| f["name"].as_str()) {
let n = app.session.active().map(|doc| crate::formula_locale::completion_name(n, locale, &doc.wb, doc.wb.active_sheet())).unwrap_or(n);
let cur = app.editor.as_ref().map(|e| e.text.clone());
let text = match cur {
Some(t) if t.starts_with('=') => format!("{t}{n}("),
@@ -126,3 +58,15 @@ pub(super) fn insert_function(app: &mut SheetApp, ui: &mut egui::Ui, d: &mut Dia
*confirm = true;
}
}
/// Excel's name for a function category, given the catalog's id (`MathTrig`).
fn function_category_label(id: &str) -> &str {
match id {
"DateTime" => "Date & Time",
"Lookup" => "Lookup & Reference",
"MathTrig" => "Math & Trig",
"AddIn" => "Add-in and Automation",
// Financial, Logical, Text, Statistical, Engineering, Database, Information, …
other => other,
}
}
+40
View File
@@ -0,0 +1,40 @@
use super::{Dialog, Json, SheetApp, json};
/// The Macros dialog: lists GridCraft's own recorded scripts and (if the file carries one) the
/// preserved VBA project. Mirrors Excel's Developer ▸ Macros list, but is honest that VBA is
/// preserved and not executed.
pub(super) fn macros(app: &mut SheetApp, ui: &mut egui::Ui, d: &mut Dialog) {
ui.label("Automation in this workbook. GridCraft runs its own scripts; a file's VBA project is preserved but not executed.");
ui.add_space(6.0);
let items: Vec<Json> = d.result.as_ref().and_then(Json::as_array).cloned().unwrap_or_default();
if items.is_empty() {
ui.weak("This workbook has no scripts or macros.");
return;
}
// Scripts that came with a file stay disabled until the user trusts the workbook (#2178).
if items.iter().any(|it| it.get("trusted").and_then(Json::as_bool) == Some(false)) {
ui.horizontal(|ui| {
ui.weak("Scripts in this workbook came from a file and are disabled.");
if ui.button("Trust Scripts").on_hover_text("Let this workbook's scripts run for the rest of this session").clicked() {
let _ = app.run("automate.trustScripts", json!({"trusted": true}));
d.result = app.session.run("view.macros", json!({})).ok();
}
});
ui.add_space(6.0);
}
egui::ScrollArea::vertical().max_height(300.0).show(ui, |ui| {
for it in &items {
let name = it.get("name").and_then(Json::as_str).unwrap_or("(unnamed)");
let is_vba = it.get("kind").and_then(Json::as_str) == Some("vba");
ui.horizontal(|ui| {
ui.label(egui::RichText::new(name).strong());
if is_vba {
ui.weak("— VBA project (preserved, not run)");
} else {
let steps = it.get("steps").and_then(Json::as_u64).unwrap_or(0);
ui.weak(format!("— {steps} step{}", if steps == 1 { "" } else { "s" }));
}
});
}
});
}
+4 -28
View File
@@ -7,6 +7,7 @@ use super::data::{advanced_filter_form, data_validation_form, sort};
use super::find::{find, go_to};
use super::format_cells::format_cells;
use super::insert_function::insert_function;
use super::macros::macros;
use super::names::{name_manager, paste_name};
use super::paste::{paste_special, paste_special_shortcuts};
use super::spelling::spelling;
@@ -15,17 +16,9 @@ use super::{Dialog, Field, Json, Key, Map, RefPick, SheetApp, json, vec2};
/// Builds the command params from form values for dialogs that need translation.
/// The parameters OK sends to the dialog's command: its field values, reshaped per dialog.
/// Unchanged hyperlink display text is omitted so its number or formula stays intact.
pub(crate) fn build_params(d: &Dialog) -> Json {
let mut v = Json::Object(d.values.clone());
match d.name.as_str() {
"insertLink" => {
if d.original_link_text.as_deref().is_some_and(|text| d.values.get("text").and_then(Json::as_str) == Some(text))
&& let Some(m) = v.as_object_mut()
{
m.remove("text");
}
}
"advancedFilter" => {
if let Some(m) = v.as_object_mut() {
m.remove("action");
@@ -114,21 +107,6 @@ pub(crate) fn build_params(d: &Dialog) -> Json {
pub fn show(app: &mut SheetApp, ctx: &egui::Context) {
let Some(mut d) = app.dialog.take() else { return };
let home = app.ui_window();
// The macro step editor is a large resizable window of its own.
if d.name == "macroEditor" {
let mut open = true;
crate::macro_editor::window(app, ctx, &mut d, &mut open);
if !open {
app.macros.editor = None;
app.grid.macro_overlay = None;
} else if app.ui_window() != home {
// Something run from the editor switched windows: the editor stays in its own.
app.return_dialog(home, d);
} else {
app.dialog = Some(d);
}
return;
}
if d.name == "pasteSpecial" {
paste_special_shortcuts(&mut d, ctx);
}
@@ -138,8 +116,7 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) {
let width = match d.name.as_str() {
"formatCells" => 560.0,
"about" => 660.0,
"insertFunction" | "commandSearch" | "nameManager" | "manageRules" | "journal" | "agents" => 520.0,
"macros" => 480.0,
"insertFunction" | "commandSearch" | "nameManager" | "manageRules" | "journal" | "agents" | "macros" => 520.0,
"recommendedCharts" => 600.0,
"analysisToolPak" => 420.0,
"solver" => 460.0,
@@ -199,8 +176,7 @@ pub fn show(app: &mut SheetApp, ctx: &egui::Context) {
}
});
}
"macros" => crate::macro_ui::macros_dialog(app, ui, &mut d, &mut open),
"recordMacro" => crate::macro_ui::record_dialog(app, ui, &mut d, &mut open),
"macros" => macros(app, ui, &mut d),
"statistics" | "accessibility" | "errorChecking" | "evaluateFormula" | "comments" => {
let r = if d.name == "comments" {
app.session.active().and_then(|doc| doc.wb.active().map(|s| json!(s.comments.iter().map(|(c, m)| json!({"cell": c.a1(), "author": m.author, "text": m.text})).collect::<Vec<_>>())))
@@ -464,7 +440,7 @@ fn form(ui: &mut egui::Ui, d: &mut Dialog, confirm: &mut bool, open: &mut bool)
/// Enter sets the width, as in Excel.
/// Keeps asking until the field really has focus: a new window's first frame is an invisible
/// sizing pass where a focus request is ignored.
pub(super) fn take_first_focus(d: &mut Dialog, r: &egui::Response) {
fn take_first_focus(d: &mut Dialog, r: &egui::Response) {
if !d.focus_first_field {
return;
}
-71
View File
@@ -1,71 +0,0 @@
//! List drag and drop shared by the PivotTable Fields pane and the macro step editor: a drag
//! source that carries a payload, where a drop lands among a list's rows, the line that marks it,
//! and the label that follows the pointer.
use egui::{DragAndDrop, Id, Pos2, Rect, Sense, Stroke, Ui, pos2, vec2};
use crate::theme::{self, Tokens};
/// Where a drop at `y` lands among rows laid out top to bottom.
pub fn insert_index(rows: &[Rect], y: f32) -> usize {
rows.iter().filter(|r| r.center().y < y).count()
}
/// Starts dragging `payload` when `r` (a widget sensing drags) begins a drag, and shows the grab
/// cursor over it.
pub fn drag_source<T: std::any::Any + Send + Sync>(ui: &Ui, r: &egui::Response, payload: T) {
if r.drag_started() {
DragAndDrop::set_payload(ui.ctx(), payload);
}
if r.hovered() && !DragAndDrop::has_any_payload(ui.ctx()) {
ui.ctx().set_cursor_icon(egui::CursorIcon::Grab);
}
}
/// A drag handle under a widget that only senses clicks (a checkbox): it covers where the widget
/// was last frame (stored under `id`) and is added first, so clicks still reach the widget on top.
pub fn drag_under<T: std::any::Any + Send + Sync>(ui: &mut Ui, id: Id, payload: T) {
let Some(rect) = ui.ctx().data(|d| d.get_temp::<Rect>(id)) else { return };
let r = ui.interact(rect, id, Sense::drag());
drag_source(ui, &r, payload);
}
/// While dragging: outlines `zone` and draws the line where a drop at `y` would land among `rows`.
pub fn drop_marker(ui: &Ui, zone: Rect, rows: &[Rect], y: f32) {
let t = Tokens::get(ui.ctx());
let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Foreground, Id::unique("dnd_drop_marker")));
painter.rect_stroke(zone, 4.0, Stroke::new(2.0, t.accent), egui::StrokeKind::Inside);
let k = insert_index(rows, y);
let y = match (rows.get(k), k.checked_sub(1).and_then(|j| rows.get(j))) {
(Some(r), _) => r.top() - 2.0,
(None, Some(r)) => r.bottom() + 2.0,
(None, None) => zone.top() + 4.0,
};
painter.line_segment([pos2(zone.left() + 4.0, y), pos2(zone.right() - 4.0, y)], Stroke::new(2.0, t.accent));
}
/// The dragged item's label following the pointer.
pub fn ghost(ui: &Ui, text: &str, at: Pos2) {
let t = Tokens::get(ui.ctx());
let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Tooltip, Id::unique("dnd_ghost")));
let galley = painter.layout_no_wrap(text.to_string(), theme::ui_font(12.0), t.text);
let rect = Rect::from_min_size(at + vec2(12.0, 8.0), galley.size() + vec2(12.0, 6.0));
painter.rect_filled(rect, 3.0, t.input_bg);
painter.rect_stroke(rect, 3.0, Stroke::new(1.0, t.accent), egui::StrokeKind::Inside);
painter.galley(rect.min + vec2(6.0, 3.0), galley, t.text);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_drop_lands_before_the_first_row_below_it() {
let rows: Vec<Rect> = (0..3).map(|i| Rect::from_min_size(pos2(0.0, i as f32 * 20.0), vec2(100.0, 20.0))).collect();
assert_eq!(insert_index(&rows, -5.0), 0);
assert_eq!(insert_index(&rows, 15.0), 1);
assert_eq!(insert_index(&rows, 45.0), 2);
assert_eq!(insert_index(&rows, 500.0), 3);
assert_eq!(insert_index(&[], 10.0), 0);
}
}
-9
View File
@@ -160,15 +160,6 @@ impl DocWindows {
self.list.focus(id);
self.viewports.focus(ctx, id);
}
// macOS ignores Focus for a minimized window or a hidden app, so restore and show first.
if std::mem::take(&mut app.bring_forward)
&& let Some(id) = self.list.active()
{
let vp = self.viewports.viewport_id(id);
ctx.send_viewport_cmd_to(vp, egui::ViewportCommand::Visible(true));
ctx.send_viewport_cmd_to(vp, egui::ViewportCommand::Minimized(false));
ctx.send_viewport_cmd_to(vp, egui::ViewportCommand::Focus);
}
}
/// Draws every window. Call from the root viewport's UI each frame, in place of
+5 -24
View File
@@ -518,23 +518,9 @@ fn editor_arrow_keys(
ed: &mut crate::editor::EditState,
mut action: Option<EditorAction>,
) -> (Option<EditorAction>, bool) {
// The modifiers come with the key event: an arrow pressed and released within one frame (a
// control client's or an automation tool's keystroke) has already lost them from `i.modifiers`.
let arrow = ui.input(|i| {
i.events.iter().find_map(|e| match e {
egui::Event::Key { key: key @ (Key::ArrowUp | Key::ArrowDown | Key::ArrowLeft | Key::ArrowRight), pressed: true, modifiers, .. } => {
Some((*key, *modifiers))
}
_ => None,
})
let (up, down, left, right, shift) = ui.input(|i| {
(i.key_pressed(Key::ArrowUp), i.key_pressed(Key::ArrowDown), i.key_pressed(Key::ArrowLeft), i.key_pressed(Key::ArrowRight), i.modifiers.shift)
});
let (up, down, left, right) = (
arrow.is_some_and(|(k, _)| k == Key::ArrowUp),
arrow.is_some_and(|(k, _)| k == Key::ArrowDown),
arrow.is_some_and(|(k, _)| k == Key::ArrowLeft),
arrow.is_some_and(|(k, _)| k == Key::ArrowRight),
);
let (shift, jump) = arrow.map_or((false, false), |(_, m)| (m.shift, m.command));
if (up || down) && !ed.autocomplete.is_empty() {
ui.input_mut(|i| {
i.consume_key(egui::Modifiers::NONE, Key::ArrowUp);
@@ -559,18 +545,13 @@ fn editor_arrow_keys(
let rtl = app.session.active().and_then(|d| d.wb.active()).is_some_and(|s| s.right_to_left);
let dc = if rtl { -dc } else { dc };
if ed.can_point() {
// Ctrl+arrow (⌘arrow) jumps the pointed cell to the edge of its data, Ctrl+Shift+arrow
// extends the pointed range there, as the same keys do on the selection.
ui.input_mut(|i| {
for key in [Key::ArrowUp, Key::ArrowDown, Key::ArrowLeft, Key::ArrowRight] {
for modifiers in
[egui::Modifiers::NONE, egui::Modifiers::SHIFT, egui::Modifiers::COMMAND, egui::Modifiers::COMMAND | egui::Modifiers::SHIFT]
{
i.consume_key(modifiers, key);
}
i.consume_key(egui::Modifiers::NONE, key);
i.consume_key(egui::Modifiers::SHIFT, key);
}
});
crate::grid::point_move(app, ed, dr, dc, shift, jump);
crate::grid::point_move(app, ed, dr, dc, shift);
(action, false)
} else {
action = Some((dr, dc, false, false));
-191
View File
@@ -1,191 +0,0 @@
//! The Formulas tab's Function Library: the function categories, their menus, the AutoSum menu
//! (shared with Home › Editing), and the Recently Used menu, which lists what the engine's
//! `formulas.recentlyUsed` returns.
use std::sync::OnceLock;
use egui::{Ui, vec2};
use serde_json::json;
use crate::SheetApp;
/// The Insert Function category that lists [`recent`].
pub const MOST_RECENTLY_USED: &str = "Most Recently Used";
/// The Insert Function category that lists every function.
pub const ALL: &str = "All";
/// The function categories in the order the Insert Function dialog lists them: (the engine's
/// category id, the label people see).
pub const CATEGORIES: [(&str, &str); 14] = [
("Financial", "Financial"),
("DateTime", "Date & Time"),
("MathTrig", "Math & Trig"),
("Statistical", "Statistical"),
("Lookup", "Lookup & Reference"),
("Database", "Database"),
("Text", "Text"),
("Logical", "Logical"),
("Information", "Information"),
("Engineering", "Engineering"),
("Cube", "Cube"),
("Compatibility", "Compatibility"),
("Web", "Web"),
("AddIn", "Add-in and Automation"),
];
/// The categories under More Functions, each a cascading submenu.
pub const MORE_FUNCTIONS: [&str; 6] = ["Statistical", "Engineering", "Cube", "Information", "Compatibility", "Web"];
/// The label of an engine category id (the id itself if it is unknown).
pub fn category_label(id: &str) -> &str {
CATEGORIES.iter().find(|(c, _)| *c == id).map(|(_, l)| *l).unwrap_or(id)
}
/// The engine category id for a category given by id or label, in any case.
pub fn category_id(name: &str) -> Option<&'static str> {
CATEGORIES.iter().find(|(c, l)| c.eq_ignore_ascii_case(name) || l.eq_ignore_ascii_case(name)).map(|(c, _)| *c)
}
/// One function of the library.
#[derive(Clone, Debug)]
pub struct FnEntry {
pub name: String,
pub category: String,
pub description: String,
}
/// Every function, sorted by name (built once; the list is static).
pub fn all() -> &'static [FnEntry] {
static LIST: OnceLock<Vec<FnEntry>> = OnceLock::new();
LIST.get_or_init(|| {
let mut list: Vec<FnEntry> = gridcraft_engine::cmd::formulas::function_list()
.iter()
.filter_map(|f| {
Some(FnEntry {
name: f["name"].as_str()?.to_string(),
category: f["category"].as_str().unwrap_or("").to_string(),
description: f["description"].as_str().unwrap_or("").to_string(),
})
})
.collect();
list.sort_by(|a, b| a.name.cmp(&b.name));
list.dedup_by(|a, b| a.name == b.name);
list
})
}
/// The function named `name` (upper case), if the library knows it.
pub fn find(name: &str) -> Option<&'static FnEntry> {
let list = all();
list.binary_search_by(|f| f.name.as_str().cmp(name)).ok().and_then(|i| list.get(i))
}
/// The functions of one engine category, in name order.
pub fn in_category(id: &str) -> impl Iterator<Item = &'static FnEntry> + '_ {
all().iter().filter(move |f| f.category == id)
}
/// The name a function shows under in the interface language.
fn display_name<'a>(app: &SheetApp, name: &'a str) -> &'a str {
let locale = app.ui.language.formula_locale();
app.session.active().map(|d| crate::formula_locale::completion_name(name, locale, &d.wb, d.wb.active_sheet())).unwrap_or(name)
}
/// One menu row per function, with its description on hover; a click starts the formula.
fn function_items<'a>(app: &mut SheetApp, ui: &mut Ui, names: impl Iterator<Item = &'a str>) {
let language = app.ui.language;
let locale = language.formula_locale();
ui.set_max_height(420.0);
egui::ScrollArea::vertical().max_height(380.0).show(ui, |ui| {
for n in names {
let Some(entry) = find(n) else { continue };
let name = display_name(app, &entry.name);
let r = ui.add(egui::Button::new(name).frame(false).min_size(vec2(180.0, 20.0)));
// The description is looked up only while the row is hovered.
let r = r.on_hover_ui(|ui| {
ui.label(crate::formula_locale::description(&entry.name, locale, language).unwrap_or_default());
});
if r.clicked() {
app.run_or_alert("formulas.insertFunction", json!({"name": entry.name}));
ui.close();
}
}
});
}
/// The "Insert Function…" row that ends every category menu; it opens the dialog on `category`.
fn insert_function_item(app: &mut SheetApp, ui: &mut Ui, category: &str) {
ui.separator();
let label = app.ui.language.tr("Insert Function…");
if ui.add(egui::Button::new(label).frame(false).min_size(vec2(180.0, 22.0))).clicked() {
app.open_dialog("insertFunction", json!({"category": category}));
ui.close();
}
}
/// The menu of one category: its functions, then Insert Function….
pub fn category_menu(app: &mut SheetApp, ui: &mut Ui, id: &str) {
let names: Vec<&str> = in_category(id).map(|f| f.name.as_str()).collect();
function_items(app, ui, names.into_iter());
insert_function_item(app, ui, category_label(id));
}
/// The functions `formulas.recentlyUsed` lists, newest first (none without a workbook).
pub fn recent(app: &mut SheetApp) -> Vec<String> {
let list = app.session.execute("formulas.recentlyUsed", json!({})).unwrap_or_default();
list.as_array().map(|a| a.iter().filter_map(|n| n.as_str().map(String::from)).collect()).unwrap_or_default()
}
/// Recently Used: the most recent functions, newest first, then Insert Function….
pub fn recent_menu(app: &mut SheetApp, ui: &mut Ui) {
let recent = recent(app);
function_items(app, ui, recent.iter().map(String::as_str));
insert_function_item(app, ui, MOST_RECENTLY_USED);
}
/// More Functions: a cascading submenu per remaining category.
pub fn more_menu(app: &mut SheetApp, ui: &mut Ui) {
for id in MORE_FUNCTIONS {
let label = app.ui.language.tr(category_label(id)).to_string();
ui.menu_button(label, |ui| category_menu(app, ui, id));
}
}
/// The AutoSum menu, shared by Home › Editing and Formulas › Function Library: a function, or
/// the Insert Function dialog.
pub fn auto_sum_menu(app: &mut SheetApp, ui: &mut Ui) {
for (label, f) in [("Sum", "SUM"), ("Average", "AVERAGE"), ("Count Numbers", "COUNT"), ("Max", "MAX"), ("Min", "MIN")] {
let label = app.ui.language.tr(label);
if ui.add(egui::Button::new(label).frame(false).min_size(vec2(160.0, 22.0))).clicked() {
app.run_or_alert("formulas.autoSum", json!({"function": f}));
ui.close();
}
}
ui.separator();
let label = app.ui.language.tr("More Functions…");
if ui.add(egui::Button::new(label).frame(false).min_size(vec2(160.0, 22.0))).clicked() {
app.open_dialog("insertFunction", json!({}));
ui.close();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn categories_cover_every_function() {
for f in all() {
assert!(category_id(&f.category).is_some(), "{} is in an unknown category {}", f.name, f.category);
}
assert_eq!(category_id("date & time"), Some("DateTime"));
assert_eq!(category_id("Lookup"), Some("Lookup"));
assert_eq!(category_id("nope"), None);
for c in MORE_FUNCTIONS {
assert!(category_id(c).is_some());
}
assert!(all().windows(2).all(|w| w[0].name < w[1].name), "sorted by name, without duplicates");
assert_eq!(find("SUM").map(|f| f.category.as_str()), Some("MathTrig"));
assert!(find("NOT_A_FUNCTION").is_none());
}
}
-2
View File
@@ -248,8 +248,6 @@ pub struct GridState {
pub wheel_zoom: Option<WheelZoom>,
/// A one-finger touch drag in progress: it pans the sheet instead of selecting.
pub touch_pan: bool,
/// What the macro step editor shows on the grid (Check, debugging).
pub macro_overlay: Option<crate::macro_editor::MacroOverlay>,
}
/// Ctrl+wheel zoom for one (doc uid, sheet). egui eases a notch in over several frames and

Some files were not shown because too many files have changed in this diff Show More