mirror of
https://github.com/larksuite/cli.git
synced 2026-09-14 18:42:53 +08:00
bd898a1d74
* feat(sheets): add +sheet-show-gridline / +sheet-hide-gridline shortcuts
* docs(sheets): strengthen lark-sheets references for common editing pitfalls
Add targeted guidance to six lark-sheets references to reduce frequent
mistakes when editing spreadsheets through the CLI:
- write-cells: sanity-check units / dimension conversion / quantity factors
before formula writes (formulas can run clean yet be off by a factor);
keep derived output off original data columns to avoid clobbering source
- core-operations: prefer live formulas for derived values even when "live
update" is not explicitly requested; scope rewrite/transform precisely so
rows/columns that should stay unchanged are kept 1:1; treat header-stated
format rules as checklist items; confirm the artifact file actually exists
before finishing; write back bare values from local scripts
- visual-standards: apply border/header formatting on explicit request and
identify the real header row; keep font size consistent with the source
- range-operations: keep total column width within A4 for printing
- read-data: dedup/compare long numbers via raw values, not csv formatted
display (scientific notation collapses distinct numbers and causes false
duplicates)
- chart: format date/number axes via source-cell number_format; place charts
outside the data area so they do not cover existing data
* feat(sheets): implement table-put/table-get and sync skill specs
- Add lark_sheet_table_io.go with +table-put / +table-get and tests
- Refactor read-data; extend workbook; register new shortcuts
- Sync generated flag defs/schemas (go:embed) from sheet-skill-spec
- Sync skill references (write-cells numeric-column guidance, plus
read-data / workbook / chart updates)
* docs(sheets): surface typed-write path at the write-decision point
Quick-ref table (SKILL.md, the first decision point) had no +table-put and
gated typed writes on "DataFrame", so a model holding a Counter/list/dict
would fall back to +csv-put and silently lose number/date fidelity.
- split csv-put row to plain-text values (no numeric/date semantics)
- add +table-put row for typed writes into an existing sheet
- add +workbook-create --sheets row for create + typed write in one shot
- add judgment note: number/amount/date/percent/count -> +table-put
(or +workbook-create --sheets when the workbook does not exist yet);
plain text -> +csv-put
- reframe write-cells scenario row to lead with numeric semantics
- point new-table writes at +workbook-create --sheets (one shot) instead
of the create-empty-then-table-put two-step
Synced from sheet-skill-spec canonical (generate:cli + sync:cli).
* docs(sheets): sync SKILL.md (drop "not for local Excel" caveat)
Mirror the upstream sheet-skill-spec change removing the "not applicable to local Excel files" tail from the sheets skill and reference descriptions.
* docs(sheets): sync SKILL.md (drop "Feishu sheets only" caveat)
Mirror the upstream sheet-skill-spec change removing the "applies to Feishu sheets only" tail from the 14 sheet reference descriptions.
* feat(sheets): add +workbook-import wrapping the drive import core
Import a local xlsx/xls/csv as a new spreadsheet by delegating to the shared drive import flow with the target type pinned to sheet. Refactor drive +import to expose ImportParams / ValidateImport / PlanImportDryRun / RunImport (behavior unchanged, existing drive tests still cover it); sheets reuses them. Regenerate flag_defs_gen.go and sync the spec mirror.
* refactor(sheets): reuse the drive export core in +workbook-export
Replace +workbook-export's parallel export-task implementation with the shared drive ExportParams/RunExport core (pinned to type=sheet). Drops ~90 lines of duplicated poll/download code; +workbook-export now inherits drive's ctx cancellation, resume-on-timeout, filename sanitize/overwrite, and the full set of export status labels. The output contract aligns with drive's (adds ready/downloaded/doc_type; saved_path preserved). Also normalize an empty drive --output-dir to "." so drive +export behavior is unchanged, and fix the sheets export e2e to call +workbook-export instead of a nonexistent +export.
* docs(sheets): keep original column widths; align chart axis with requested metric
- range-operations: only widen new / overflowing columns; never recompute or
shrink the widths of existing columns (any blanket resize, even by 1px,
breaks the original visual format)
- chart: when the user asks for a share / percentage, the value axis should be
a percentage (pie, or stack.percentage on bar/column) rather than raw counts
* docs(sheets): reword guidance to avoid eval-specific phrasing
Replace scoring-framework wording in the examples with plain functional
consequences (e.g. "not delivered", "goes stale when the source changes",
"breaks the original visual format"), so the references stay agent-facing.
* docs: add lark sheets financial modeling guidance
* docs(sheets): align write-cells reference with the generated output
Bring the hand-applied write-cells example in line with the spec-generated
reference so the CLI mirror is byte-identical to the canonical source.
* docs(sheets): align +csv-put help with formula support
Sync the formula-support wording from sheet-skill-spec (flag-defs, skill
references) and update the hand-authored cobra Description and comment for
+csv-put. +csv-put evaluates a leading-= cell as a formula via
set_range_from_csv; descriptions only, no behavior change.
* docs(sheets): fix invalid +dim-insert example in chart reference
The chart reference's placement example used non-existent flags
--dimension/--start/--end for +dim-insert. The real signature is
--position (required) + --count (required); copying the example
fails Validate with "--position is required". Replace it with
+dim-insert --position V --count 6 (insert 6 columns before V,
i.e. after U), aligning with the sheet-structure reference.
* docs(sheets): chart coordinate base / quoting + filter condition enums
Sync three reference-doc corrections from the spec source:
1. chart: label position.row as 0-based (first row = row:0), distinct
from the 1-based row numbers used by A1 ranges and +dim-insert
--position, removing the row-base ambiguity.
2. chart: convert the three runnable examples whose JSON contains a
quoted sheet prefix ('Sheet1'!A1) from inline single-quoted
--properties '{...}' to a stdin heredoc (--properties - <<'JSON').
Inside an inline single-quoted string bash strips the inner quotes
around the sheet name (and splits names with spaces into words),
corrupting the JSON; a quoted heredoc delimiter performs no shell
substitution and preserves it. Adds a short note on the pitfall.
3. filter / filter-view: add the full conditions[].type x compare_type
enum table (text / number / multiValue / color and their respective
compare_type values and values shape), and call out the
equals/notEquals (with s) vs equal/notEqual (no s) gotcha. The docs
previously only showed two values via examples.
* docs(sheets): label +sheet-create --index as 0-based
The base flag description for +sheet-create's --index omitted the
coordinate base, while its siblings +sheet-move ("Target position
(0-based)") and +sheet-copy already state 0-based. Align the description
so the index base is unambiguous. Synced from the spec source
(flag-defs.json + workbook reference).
* fix(sheets): regenerate flag defs and fix asasalint in table io
* feat(sheets): add counta to chart aggregateType enum
Add `counta` (count non-empty cells, incl. text) to manage_chart_object
dim2.series[].aggregateType in the chart flag schema. `count` only counts
numeric cells, so counting occurrences of a text/category column renders an
empty chart; `counta` enables category frequency counts. Synced from the
sheet-skill-spec canonical schema.
* feat(sheets): make --target-position and --range mutually exclusive on +pivot-create
Both flags map to the same wire field (properties.range), so passing
non-default values for both is ambiguous. Mirror the
--target-sheet-id / --target-sheet-name mutex pattern: --target-position
takes priority over --range, and supplying both with non-default values
is rejected up front with a typed FlagErrorf. --target-position=A1 is
the documented default and is treated as "not set".
Add a symmetric validateCreateInput hook on objectCRUDSpec (alongside
the existing validateUpdateInput), wire it into objectCreateInput, and
inject the pivot-specific check on pivotSpec.
* feat(sheets): rework +workbook-create flags and --styles
- --values builds a type-less typed payload, writing through --sheets' batched set_cell_range path (raw passthrough preserves auto-detect; large tables batch; big ints via json.Number)
- drop --headers (subsumed by --values first row) and --header-style (typed header no longer auto-bold; use --styles instead)
- styles: deep-merge overlapping cell_styles/border_styles fields (was wholesale-replace which dropped fields); add manual border_styles validation (style/weight enums + sides) since --styles is on parseJSONFlagSkip and bypasses the schema validator
- regenerate flag-defs/flag-schemas/skills mirror from sheet-skill-spec (--styles flag + full per-side border schema)
* fix(sheets): add mention_type enum to set_cell_range cells schema
Constrain rich_text mention_type to the proto MENTION_FILE_TYPE set so a
file @mention with an out-of-enum value (e.g. 6 = cloud shared folder) is
rejected by the schema validator before it reaches the server and fails
pb serialization ("mentionFileInfo.fileType: enum value expected").
- data/flag-schemas.json: mention_type gains enum + per-value description
- lark_sheet_write_cells_test.go: cover reject (6) + allow (0 / 2 / 22)
* feat(sheets): implement pandas-split --sheets protocol for +table-put/+table-get/+workbook-create
Synced from sheet-skill-spec canonical (cli:table_put schema +
references). +table-put/+workbook-create accept the new shape via a
tableSheetIn -> tableSheetSpec normalize step (dtype string -> internal
type/format mapping). +table-get emits the same shape so the writer's
df_to_sheet and the reader's sheet_to_df round-trip cleanly.
isoDateToSerial now accepts the full ISO datetime form
(2024-01-15T00:00:00.000, including timezone suffixes) emitted by
df.to_json(date_format="iso"), not just yyyy-mm-dd. End-to-end verified
by the spec repo's contracts/python_helper_roundtrip script against a
real Lark spreadsheet on pandas 2.2 and 3.0.
* feat(sheets): add --dataframe Arrow IPC input for +table-put/+table-get/+workbook-create
Introduce a binary-typed twin of --sheets: --dataframe accepts an Arrow IPC
(Feather v2) payload that pandas' df.to_feather() writes, deriving dtypes and
per-column number formats from the Arrow schema. The two producers are mutually
exclusive and funnel through a shared resolver so +table-put and
+workbook-create stay in lockstep; +table-get gains --dataframe-out for
single-sheet reads. Also auto-grow a sub-sheet's row/column count before
writing so blocks past the backend's default 200x20 bounds no longer fail with
range-exceeds-sheet-bounds.
* docs(lark-sheets): remove financial modeling standards reference
Drop the lark-sheets-financial-modeling-standards.md reference doc and all
pointers to it from SKILL.md, core-operations, and visual-standards. Bump
skill version to 3.0.0.
* docs(lark-sheets): clarify cell-image vs float-image routing and fix reference self-references
Synced from sheet-skill-spec.
- Add a binding-based decision (does the image belong to a record and move with its row?) to route +cells-set-image vs +float-image-create across the SKILL entry, float-image and write-cells references.
- Add routing rows to the SKILL command cheat-sheet and warn against defaulting to float-image out of familiarity.
- Replace mislabeled 本 skill / 子 skill / 跨 skill wording in references with 本文 / reference names, matching the existing convention.
* feat(sheets): add --styles to +table-put for one-step typed write with styling
+table-put now accepts --styles (same shape as +workbook-create's --styles):
cell_styles merge into the set_cell_range matrix, while cell_merges /
row_sizes / col_sizes apply as their own tool calls after the write. The
styles payload is name-matched against the written sheets and validated up
front, so a malformed or mismatched style fails before any write lands.
Also points +sheet-create users to +table-put (auto-creates missing sheets)
when they need data/styles, via a runtime Tip and the lark-sheets skill
references. Flag is sourced from the upstream Base table and regenerated
through sheet-skill-spec (flag-defs.json / flag-schemas.json / gen file).
Adds unit tests (dry-run styles, name-mismatch reject, execute) and a
dry-run E2E (tests/cli_e2e/sheets/sheets_table_put_dryrun_test.go).
* docs(lark-sheets): point read-data to +sheet-info for hidden row/col identification
skip-hidden defaults to false (lossless reads), but the read primitives don't mark which rows/cols are hidden. Cross-reference +sheet-info --include hidden_rows,hidden_cols + row_indices/col_indices so agents can identify hidden ranges when they need to filter or interpret hidden data.
Synced from sheet-skill-spec.
* feat(sheets): document link requirement for @document mentions in cells flag schema
@document mentions (mention_type != 0) must pass link (doc URL) to render a
clickable card; @user mentions (mention_type=0) don't need it. Synced from the
upstream tools-schema.
* fix(sheets): reject cond-format attrs whose shape mismatches rule_type
A conditional-format rule created with --rule-type colorScale but
cellIs-shaped attrs ({compare_type,value}, no color) was accepted by
the CLI and written through to the server, producing a color-less
color-scale segment. That dirty data crashes the frontend on snapshot
deserialization, so the spreadsheet can no longer be opened (5005).
The per-entry schema check can't catch this: properties.attrs.items is
a oneOf over all nine attr shapes and passes as soon as any branch
matches, blind to the sibling rule_type — {compare_type,value} matches
the cellIs branch even when rule_type says colorScale. The tool side
maps attrs blindly by rule_type and only validates dataBar count and
iconSet ordering, so the gap reaches the data layer.
Add a cross-field validator (validateCondFormatAttrs) wired into both
create and update via the new objectCRUDSpec.validateCreateInput hook
(twin of validateUpdateInput). It enforces, per rule_type, the keys
every attrs entry must carry — mirroring the tool's converter contract
— and treats an empty required string (notably color) as missing.
Rule types that take no attrs (duplicateValues / uniqueValues /
containsBlanks / notContainsBlanks) and updates that omit rule_type are
left to the server.
* test(sheets): guard condFormatAttrsRequired against flag-schemas drift
Add TestCondFormatAttrsRequired_MatchesSchemaOneOf, comparing the
hand-maintained condFormatAttrsRequired table against the embedded
flag-schemas.json attrs oneOf (multiset of required-key sets, for both
create and update). The cross-field validator only holds if its
per-rule_type required keys mirror the schema branches, and the two
share no compile-time link — this pins them together so a future schema
sync that adds/drops a required key can't silently desync the table.
* fix(sheets): default +table-get to full used range, not A1 current region
+table-get without --range anchored its current_region probe at A1, so an
internal blank row or column silently truncated everything past it — agents
then treated the partial data as complete (the pro016 / pro025 incident).
- Probe the used range over the full physical grid (row_count × column_count
from the workbook structure) so it spans internal blank rows/columns; fall
back to the legacy A1 anchor when dimensions are unknown.
- Emit the actually-read `range` on every sheet so callers can detect
truncation (get_cell_ranges has no has_more flag).
- Fix the same A1-anchor bug in append mode's last-data-row probe, which could
otherwise overwrite data past an internal blank row.
- Add unit + dry-run/live E2E coverage; refresh synced skill docs.
* docs(sheets): fix csv-get current_region guidance to cross-check row_count
current_region is a blank-row/column-bounded block, not the true sheet extent:
an internal blank row truncates it, so it can miss rows past the gap. The
read-data reference previously called it the "真实数据边界" and told agents to
prefer it over row_count — which drove the "read only to current_region's last
row, miss the tail" failure.
- current_region: warn it can be both smaller (internal blank rows truncate)
and larger (trailing summary/signature rows) than the real data range.
- csv-get output contract: clarify its row_count/col_count is the returned size
(= actual_range), not the physical sheet size; has_more only reflects the
current range, not whether the whole sheet was read.
- "确定数据范围的正确流程": add a step to cross-check against +workbook-info's
physical row_count and probe past current_region's last row for data beyond an
internal blank row.
* fix(sheets): collapse duplicate validateCreateInput from bad merge resolution
A prior merge kept both branches' independently-added validateCreateInput
fields on objectCRUDSpec with conflicting signatures (pivot's
func(rt, input) and cond-format's func(input)), plus both call sites in
objectCreateInput, which failed to compile (validateCreateInput redeclared).
Collapse to the single richer func(rt flagView, input) signature and one
call site. cond-format's validateCondFormatAttrs (func(input), still shared
with validateUpdateInput) is wrapped in a closure that ignores rt. Both
behaviors are preserved: pivot --target-position/--range mutex and
cond-format attrs-shape-vs-rule_type validation.
* refactor(sheets): migrate legacy error helpers to typed errs in sheets domain
golangci-lint forbidigo (errs-no-legacy-helper / errs-no-bare-wrap) flagged
the table I/O, workbook, and dataframe shortcuts that landed on this branch:
93 common.FlagErrorf and 48 fmt.Errorf calls.
- Replace every common.FlagErrorf with common.ValidationErrorf (typed
*errs.ValidationError, same signature) across workbook / table_io /
dataframe / object_crud.
- writeDataframeOut's two final --dataframe-out write failures become typed
errs.NewInternalError(SubtypeFileIO, ...).WithCause(err).
- applyWorkbookCreateVisualOps now passes the typed callTool error through
unchanged (re-wrapping would downgrade classification) and attaches the
failing op as a recovery hint only when none is set.
- The remaining fmt.Errorf are genuine intermediate errors that the command
layer re-wraps into typed validation errors (buildTypedCell / Arrow
decode-encode) or surfaces as a partial_success message string
(writeTypedSheets via tablePutPartial); each carries a //nolint:forbidigo
with that reason, per the lint guidance.
No behavior change: error messages and partial-success shapes are preserved;
gofmt, go vet, golangci-lint (0 issues) and sheets tests all pass.
* fix(shortcuts): clarify single-stdin constraint in flag help and error hint
Input flags advertised '(supports @file, - for stdin)' per flag, leading
AI agents to write '--a - <x --b - <y' where the second '<' silently
clobbers the first and the first flag reads the wrong payload. A process
has a single stdin, so at most one flag per call can use '-'.
- Reword the generated help hint to '- reads stdin (one flag per call;
use @file for others)'.
- Add an actionable .WithHint to the stdin-conflict validation error
pointing callers to @file for the extra flags.
- Assert the new hint in TestResolveInputFlags_DuplicateStdin.
* feat(sheets): +cells-get/+csv-get --max-chars 默认值 200000 → 500000
放宽默认防爆上限。flag_defs_gen.go 由 go generate 重生;flag_defs_test.go
的 expected default 同步;flag-schemas.json schema_version 2 → 3 是上游
spec-tables 架构调整带来的元数据 bump,与本业务改动无关、go:embed 不解析
该字段、无功能影响。
Synced from sheet-skill-spec@93f7a78.
* docs(lark-sheets): sync from spec — +csv-put 含逗号公式正例 + 收敛警示标签
源同步自 sheet-skill-spec:write-cells 补含逗号公式 RFC 4180 转义正例与结构化写入优先指引;全 reference 收敛「高频致命错误」类标签。
* docs(lark-sheets): sync from spec — --max-chars 放出为可见 flag + 落盘优先指引
源同步自 sheet-skill-spec:--max-chars 放出(默认 500000,可调小避免大输出被 Bash/终端转存为文件、改 has_more 分页);read-data 增「大数据优先落盘」指引。
* feat(sheets): 写操作报错增强 + --token 别名
- 复合 JSON shape 校验失败时报错附 --print-schema 提示,agent 可直接拿到精确结构(pro26 头号:+cells-set --cells 反复猜 shape)
- JSON 解析失败且该 flag 支持 stdin 时提示改用 stdin(公式/引号/逗号内联到 shell 被转义弄坏 JSON)
- --token 作为 --spreadsheet-token 的解析期别名:复用 sheets 已有 PostMount 钩子 + pflag normalize,仅 sheets 包,common 零改动
* docs(lark-sheets): sync from spec — set+H 改单引号 / 速查表补臆造命令名 / workbook-import 引导
* fix(sheets): migrate +table-put to typed error contract
The merge from main brought in #1449 (retire legacy error envelopes),
which removed output.ExitError / output.ErrDetail and forbids
constructing them. Port tablePutPartial off the legacy envelope:
- no sheets written -> typed errs.APIError (plain failure)
- some sheets written -> ok:false result via runtime.OutPartialFailure
carrying written_sheets, returning the partial-failure exit signal
Also fix two drifts the same merge introduced:
- regenerate flag_defs_gen.go to match the committed flag-defs.json
- update the --max-chars flag test to assert visible (no longer hidden)
* docs(lark-sheets): sync from spec — set+H 告诫通则化(移入 stdin 段)
* feat(sheets): styles 接受 halign/valign 等对齐字段别名
把模型常幻觉的 horizontal_align / halign / vertical_align / valign 映射到
规范字段 horizontal_alignment / vertical_alignment,覆盖 --styles 与 typed
--cells;与规范字段冲突时报错而非静默择一。同步 lark-sheets skill 文档补
对齐字段说明 + --print-schema --flag-name styles 提示。
* feat(sheets): resolve wiki URLs to the backing spreadsheet for --url
Sheets shortcuts only accepted /sheets/ and /spreadsheets/ URLs via --url.
A /wiki/<node_token> URL was rejected with "must be a spreadsheet URL"
because the wiki node_token is not a spreadsheet token: resolving it to the
backing spreadsheet needs a wiki get_node call, which Validate/DryRun (kept
network-free) must not make.
Mirror the existing slides/doc/drive two-stage pattern:
- parseSpreadsheetRef classifies --url / --spreadsheet-token network-free
into a sheet token or an (unresolved) wiki node_token.
- resolveSpreadsheetTokenExec (Execute only) resolves a /wiki/ node_token
via wiki get_node, verifies obj_type=sheet, and returns the obj_token.
The wiki:node:read scope is enforced on this path only, so non-wiki
invocations are unaffected.
- resolveSpreadsheetToken stays network-free for Validate/DryRun, passing
the node_token through unchanged.
All 47 Execute paths (including +batch-update and +workbook-export) switch
to the Exec resolver; Validate/DryRun keep the network-free one. No tool
schema change: the CLI feeds the resolved spreadsheet token as excel_id, so
this is a pure CLI-layer change.
Tested: unit (parse classification + wiki get_node e2e via httpmock) and
live end-to-end against a real wiki spreadsheet (read: +workbook-info,
+cells-get, +csv-get; write: +sheet-create, +sheet-rename, +csv-put).
* docs(sheets): note --url accepts wiki URLs (synced from spec)
* fix(sheets): match --url path segment via url.Parse, not substring
parseSpreadsheetRef classified /wiki/ with strings.Index over the whole URL, so a /sheets/ link whose query or fragment merely contained /wiki/ (e.g. .../sheets/sht?from=/wiki/x) was hijacked into a get_node call. Now parse the URL and match /sheets/, /spreadsheets/, /wiki/ only as a path prefix, mirroring slides parsePresentationRef which already fixed this class. Drop the substring helpers. Also align wiki resolution with slides: CallAPITyped (typed error + log_id) and classify an incomplete get_node response as InternalError instead of a --url validation error. Add regression tests for query/fragment /wiki/ and incomplete node.
* fix(sheets): satisfy errorlint/copyloopvar + regen flag defs
- helpers_test.go: drop the Go 1.22+ redundant `tc := tc` loop copy
(copyloopvar).
- lark_sheet_dataframe.go, lark_sheet_table_io.go: switch the
intermediate-error fmt.Errorf calls from %v to %w so errorlint passes.
Behavior unchanged — these errors are always rewrapped into typed
validation errors at the command layer.
- flag_defs_gen.go: regenerate from data/flag-defs.json (drift from the
wiki-URL merge).
* ci: allow Apache Arrow module in license check
Arrow is Apache-2.0 overall, but it vendors c-ares (LicenseRef-C-Ares,
ISC-like) inside the module which go-licenses classifies as Unknown and
the strict disallowed_types=...,unknown gate rejects.
Pass --ignore github.com/apache/arrow/go/v17 since Arrow is required by
sheets +table-put / +table-get / +workbook-create --dataframe (Arrow IPC
ingest) and the vendored c-ares is not redistributed by us.
* fix(sheets): resolve wiki URL in +range-move/+range-copy Execute
transformExecuteFn (the named Execute helper shared by +range-move and +range-copy) still called the network-free resolveSpreadsheetToken, so a /wiki/ URL reached transform_range as an unresolved node_token and failed. #1519's sweep over Execute hooks only rewrote inline closures; this is the only Execute backed by a named helper. Switch it to resolveSpreadsheetTokenExec (Validate/DryRun stay network-free) and add a +range-move wiki-URL regression test.
* refactor(sheets): drop +table-put manual capacity grow; rely on set_cell_range auto-grow
set_cell_range now auto-grows the sub-sheet to fit the write, so the
ensureSheetCapacity helper (and its modify_sheet_structure dim-insert
call before each write) is no longer needed. This also closes a data-
safety hole flagged in review: inserting before the last existing row
could push real data down into the area set_cell_range was about to
write, and allow_overwrite=false could not protect against it because
the structural insert had already mutated the sheet by the time the
write-collision check ran.
Verified end-to-end against a real spreadsheet: +table-put writing
300x25 into a fresh Sheet1 (default 200x20) succeeds in one write and
the sheet ends up 301x25.
* fix(sheets): close --dataframe stdin guard hole
--dataframe is binary and bypasses the common Input resolver, which is
where the existing single-stdin guard lives. Result: an invocation like
+table-put --dataframe - --styles - was accepted, then one of the two
consumers raced for stdin and the other silently saw an empty stream.
Add a stdinConsumed marker on RuntimeContext that both consumers share:
common.resolveInputFlags sets it when an Input flag uses '-', and
readDataframeBytes both checks and sets it. A second consumer is
rejected up front with an actionable hint pointing at @file.
Flagged in code review (lark_sheet_dataframe.go:93).
* fix(sheets): harden +table-put / +table-get input validation and round-trip safety
Four review-flagged correctness gaps in table I/O, all bundled because
they touch the same file:
1. --sheets accepted trailing data after the first JSON value
(json.Decoder does not surface that, unlike json.Unmarshal). A new
decoderExpectEOF helper rejects e.g. `--sheets '{...} oops'` with a
typed validation error instead of letting the leading object pass
through and surface as a confusing downstream failure.
2. +table-get with a duplicate header (e.g. `amount, amount`) used to
read back successfully — the dtypes map silently collapsed to one
entry — and only failed later on +table-put because the writer
rejects duplicate column names. Fail fast at read time with an
actionable hint to rename or pass --no-header. --no-header mode is
exempt (fallback col<N> names are always unique).
3. +table-put dry-run rendered an invalid range like A1:C0 when
header=false with rows=[]. tablePutFullRange returns "" for an
empty matrix or zero columns instead of building a degenerate
rectangle.
4. +table-get with --sheet-id and a get_workbook_structure miss (read
failure or selector mismatch) used to return a target with
name="", which then broke +table-get → +table-put round-trip (the
writer requires a non-empty sheet name). Fall back to using the id
as the name.
End-to-end verified against a real spreadsheet: trailing data, duplicate
header, and --no-header fallback all behave as advertised.
* fix(sheets): apply +workbook-create style-only ops instead of silently dropping them
A +workbook-create call carrying only cell_merges / row_sizes / col_sizes
(no --values / --sheets and no cell_styles) used to create the workbook
but silently drop the requested visual ops. Two reasons, both fixed:
- workbookCreateStyleDimensions only counted cell_styles when computing
the write extent, so cell_merges / row_sizes / col_sizes always
contributed 0 → buildValuesPayload returned a nil payload → Execute
skipped writeTypedSheets entirely → no visual ops ran. Extend the
helper to fold the merge / resize ranges in.
- Pure row_sizes / col_sizes payloads can never expand a cell rectangle
(they are dimension ranges, not cell ranges), so even with the extent
fix Execute would still skip the write path. Add a no-data branch:
when payload == nil but a styles item is present, look up the default
sheet and apply visual ops directly via applyWorkbookCreateVisualOps.
The dry-run plan mirrors this so the preview shows the visual ops.
Also picks up the --values trailing-JSON-data EOF check (mirror of the
--sheets one in lark_sheet_table_io.go).
End-to-end verified against a real spreadsheet: a cell_merges-only
+workbook-create now produces a sheet with merged_cells_count: 1.
* fix(sheets): preserve causes and render messages cleanly for typed validation errors
common.ValidationErrorf goes through fmt.Sprintf, which does not support
%w — the seven call sites that used `%w` were rendering the cause as
literal `%!w(*fmt.wrapError=&{...})` and dropping the cause from the
typed-error chain (so callers couldn't errors.As back to the underlying
error).
Switch each to `%v` for clean rendering and attach the cause via
.WithCause(err) so the typed contract is preserved. Touched call sites:
- lark_sheet_dataframe.go: --dataframe Arrow decode / stdin read / file
read failures (3 call sites).
- lark_sheet_table_io.go: --sheets invalid JSON, payload-validate
per-cell coercion error, buildSheetMatrix per-cell error,
--dataframe-out arrow encode failure (4 call sites).
End-to-end verified against a real spreadsheet: both invalid-JSON and
typed-cell errors now render readable messages instead of %!w(...).
* sync(sheets): pick up +sheet-{show,hide}-gridline in +batch-update schema
Mirror of the sheet-skill-spec change adding the two gridline shortcuts
to cli-schemas.json batch_update.operations.shortcut enum. Synced from
the upstream canonical via generate:cli + sync:cli.
Verified end-to-end on a real spreadsheet — +batch-update with a
+sheet-hide-gridline op passes schema validation and the backend run
returns succeeded: 1.
* sync(sheets): pick up +workbook-export UX clarification from spec
Mirror of the sheet-skill-spec update that documents +workbook-export's
default-no-download behavior and its relationship to drive +export
--doc-type sheet. Synced from canonical via generate:cli + sync:cli +
go generate.
End-to-end verified against a real spreadsheet:
- Omit --output-path → ok:true, downloaded:false, file_token returned
- Pass --output-path ./crfix_test.xlsx → ok:true, file saved
(17892 bytes), saved_path returned
The --help output for +workbook-export now states the default behavior
and points callers at `drive +export --doc-type sheet` when they need
the --output-dir / --file-name / --overwrite split.
* test(sheets): assert typed errs.Problem instead of err.Error() substrings
Per the coding guideline "Error-path tests must assert typed metadata via
errs.ProblemOf (category / subtype / param) and cause preservation, not
message substrings alone." — sweep through every error-path assertion in
the sheets domain and replace the
`strings.Contains(stdout+stderr+err.Error(), ...)` pattern with two
small helpers landed in helpers_test.go:
requireProblem(t, err, wantCategory, wantSubtype, msgContains)
-> *errs.Problem
requireValidation(t, err, msgContains)
-> *errs.ValidationError // shorthand for CategoryValidation +
// SubtypeInvalidArgument; lets callers
// also assert .Param / .Params / .Cause
~60 assertion sites across 18 test files now check the typed envelope
shape, with message-substring checks moved onto the returned Problem
(.Message / .Hint / .Param). The substring is preserved as a sanity
check rather than the sole assertion, so a category drift like
validation → internal would now fail loudly instead of slipping past.
Cases intentionally left as substring (each with a one-line reason):
- Errors that come straight from cobra's native flag parser (untyped
*errors.errorString — e.g. "required flag(s) ... not set", mutually-
exclusive groups). Re-typing these needs a custom FlagErrorFunc and
is out of scope here.
- Intermediate errors from decodeArrowToSheet that the caller wraps
into a typed envelope (`//nolint:forbidigo` reason). Those unit
tests assert the unwrapped intermediate directly.
One production tweak:
- shortcuts/sheets/flag_schema.go: printFlagSchemaFor returns typed
*errs.ValidationError (with WithParam("--flag-name") on the
unknown-flag branch) instead of raw fmt.Errorf. The framework
already wraps this when called via --print-schema, so user-facing
behaviour is unchanged; direct callers (and tests) now get the
typed envelope.
Verified: go test ./shortcuts/sheets/... passes; golangci-lint
--new-from-rev=origin/main reports 0 issues.
* test(common): assert typed errs.Problem instead of err.Error() substrings
Mirror of the sweep just landed in shortcuts/sheets: replace error-path
substring assertions with typed-envelope checks via two small helpers
landed in a new shortcuts/common/typed_error_assertions_test.go:
requireProblem(t, err, wantCategory, wantSubtype, msgContains)
-> *errs.Problem
requireValidation(t, err, msgContains)
-> *errs.ValidationError // shorthand for CategoryValidation +
// SubtypeInvalidArgument; lets callers
// also assert .Param / .Params / .Cause
8 sites moved to typed assertions across runner_jq_test.go,
mcp_client_test.go, drive_media_upload_typed_test.go, and
runner_input_test.go (the input tests already used a typed-param helper;
this just retargets the substring follow-up onto the typed Message).
Sites intentionally left as substring + comment (production returns raw
fmt.Errorf, not a typed envelope):
- runner_botinfo_test.go (6 sites): BotInfo / fetchBotInfo wrap upstream
errors with fmt.Errorf so the SDK-level message ([99991], 403,
invalid character, etc.) shows through.
- runner_args_test.go (4 sites in 2 tests): rejectPositionalArgs returns
raw fmt.Errorf to satisfy cobra's PositionalArgs contract.
- permission_grant_test.go (2 sites): assert on stderr / hint strings,
not error messages — already out of the err.Error() substring class.
No production code changes.
Verified: go test ./shortcuts/common/... passes;
golangci-lint --new-from-rev=origin/main ./shortcuts/common/... reports
0 issues.
* fix(sheets): plug four +table-put / +table-get correctness gaps flagged in CR
Four review-flagged bugs, all in lark_sheet_table_io.go (bundled because
they touch the same file and the same +table-put / +table-get domain):
1. +table-get --dry-run dropped the --sheet-id / --sheet-name selector
from the get_cell_ranges body, while Execute always passed it. Agents
that validate the dry-run shape and then run live would see a request
shape mismatch. The dry-run now calls sheetSelectorForToolInput so
the body matches Execute.
2. isDateNumberFormat used a simple `strings.ContainsRune(_, 'y')` so
number formats like "JPY #,##0" (a currency prefix that happens to
contain a lone 'Y') were misread as date formats — round-tripping
integer cells out as ISO dates. The detector is now token-aware:
it skips quoted "...", `\\x`-escaped, and `[...]` bracket sections,
and only fires on an unescaped `yy` (a real Excel year token).
3. sheetCreateDims sized new append-mode sheets by `headerOn(s)` only,
but writeSheetData forces a header on empty append sheets when
Header == nil. Near 50000 rows / 200 cols this created the sheet one
row short and the follow-up set_cell_range bounced off the backend
ceiling. Size now matches the forced-header logic exactly.
4. tableGetTargets fallback paths (read-failure / selector mismatch on
--sheet-id) returned a target with name="" — already corrected for
--sheet-id structure-success path in 086876d2, but the structure-
failure fallback still left it empty. Use the id as the name there
too so the +table-get → +table-put round-trip never breaks on a
nameless sheet.
End-to-end verified against a real spreadsheet:
- table-get --dry-run with --sheet-name / --sheet-id both render the
selector field in the get_cell_ranges body
- A real round-trip (typed put → get) preserves dtypes + formats
* fix(sheets): bound --dataframe memory use with byte / row / column caps
readDataframeBytes used to read the whole Arrow file unbounded — a
stdin / file > 1 GiB would OOM the CLI long before the backend
per-sheet ceilings kicked in. decodeArrowToSheet then materialized
every record into [][]interface{} regardless of size.
Three caps now match the backend's per-sheet hard ceilings:
- byte cap: 256 MiB (covers worst-case 200×50000 cells × ~25 B Arrow
overhead). File path pre-Stat()s before opening; both file and stdin
paths read through io.LimitReader so an oversized input is rejected
without allocating the full payload.
- column cap: 200, checked at schema-decode time before allocating any
per-column slices.
- row cap: 50000, checked during record-batch iteration so a 1M-row
Arrow file is rejected mid-stream instead of fully decoding first.
End-to-end verified against PPE — a 257 MiB file is rejected at file-
Stat with a typed validation error before any read happens.
* fix(drive): wrap +export ctx cancellation/deadline as typed errs.NetworkError
The poll loop in RunExport returned ctx.Err() directly in two places —
on the inter-attempt sleep cancel and on the pre-attempt deadline check.
That let context.Canceled / context.DeadlineExceeded escape as untyped
errors at the cobra layer, bypassing the typed-error contract every
other failure path already honors.
Add wrapExportContextErr that maps both into errs.NewNetworkError with
SubtypeNetworkTransport / SubtypeNetworkTimeout respectively and
preserves the cause via .WithCause(err), so callers can still
errors.Is(err, context.Canceled) downstream.
CR-flagged at drive_export.go:229 / :234.
* ci(license): narrow Apache Arrow workaround with a follow-up assertion
The dependency-license check still has to --ignore Apache Arrow wholesale
because go-licenses' classifier parses its LICENSE.txt as a single license
and mis-reports the module as LicenseRef-C-Ares / Unknown (Arrow inlines
the c-ares 3rdparty notice alongside its own Apache-2.0). Re-classifying
on our side isn't possible without changing go-licenses itself.
The CR concern was that --ignore is too wide — a future Arrow re-license
or new inlined dep would silently sail through. Add a follow-up step that
re-checks Arrow's LICENSE.txt independently: it must still open with
"Apache License" AND must still inline the c-ares 3rdparty notice (the
two facts that make the --ignore safe today). If either invariant breaks,
CI fails here and forces a human to re-evaluate the ignore.
Verified locally — both assertions pass against the current pinned
Arrow v17.
* sync(sheets): pick up +table-put payload-shape doc corrections from spec
Mirror of the sheet-skill-spec change that fixes three places teaching
an invalid +table-put payload shape — the typed protocol only has
columns / data / dtypes / formats (no formula field) and must always
be wrapped in an outer {"sheets":[...]} envelope. write-cells and the
SKILL.md decision table previously used the wrong field names (type /
format) and pointed users at +table-put for formula writes, which the
shortcut can't actually accept.
Synced from upstream canonical via generate:cli + sync:cli.
* test(sheets/e2e): add E2E coverage for new shortcuts + typed workbook-create
AGENTS.md requires a dry-run E2E for every new shortcut and a live E2E
for new flows. Three new files cover the four shortcuts this branch
adds or materially changes:
- sheets_gridline_dryrun_test.go — pins +sheet-show-gridline /
+sheet-hide-gridline as a single modify_workbook_structure call with
the right operation name (show_gridline / hide_gridline) and
sheet_id, so an op-name typo would trip CI before any live run.
- sheets_workbook_import_dryrun_test.go — pins +workbook-import as a
two-step plan (drive media upload + drive import-task create) with
the doc type hard-coded to "sheet" — the wrapper's whole reason for
existing on top of generic drive +import. --name reaches file_name
on the wire; file_extension is sniffed from the local file.
- sheets_table_put_typed_workflow_test.go — two live workflows running
against a freshly created spreadsheet. The first runs the full
typed +table-put → +table-get round-trip (date / numeric / object
columns with custom number_format) and asserts the dtype + format
contract holds end-to-end. The second exercises the typed
+workbook-create --sheets path: create + write in one shortcut, the
payload sheet name adopts the workbook's default sheet (no empty
"Sheet1" left behind), and the typed contract still survives the
read-back.
End-to-end verified locally (user identity): typed put round-trips
preserve dtypes (date → datetime64[ns], numeric → float64, object →
object) + formats verbatim; workbook-create adopts the named sheet as
the first sheet with the same typed shape intact.
* sync(sheets): pick up sheets_df.py — pandas ↔ JSON skill script from spec
Mirror of the sheet-skill-spec change that adds a DataFrame ↔ JSON
bridge as a skill-bundled Python script instead of inside the CLI
binary. Per PR #1355 review (docx NcmxdRo2yoZ4OXxoMUZcxRZ7nHd, §4.2):
keep the CLI a thin JSON/REST client; pandas / Arrow editing lives in
the caller's Python process. Synced from canonical via generate:cli +
sync:cli.
- skills/lark-sheets/scripts/sheets_df.py (new): pandas DataFrame ↔
one sheet, .parquet / .feather / .arrow / .csv / .json. Shells out to
`+table-put` / `+table-get` over typed JSON — no CLI changes.
- SKILL.md decision tree + write-cells.md +table-put section: explicit
pointers so pandas users land on the script instead of hand-rolling
the `--sheets` payload.
End-to-end verified against PPE: 3-row DataFrame (datetime / float /
object) round-trips parquet → script put → real sheet → script get →
parquet with dtypes preserved.
* Revert "sync(sheets): pick up sheets_df.py — pandas ↔ JSON skill script from spec"
This reverts commit 2964983b92.
* sync(sheets): pick up sheets_df.py + doc DRY cleanup from spec
Mirror of the sheet-skill-spec change that ships a 32-line helper-only
sheets_df.py (df_to_sheet + sheet_to_df) and removes the corresponding
inline `def` blocks from three reference docs.
- skills/lark-sheets/scripts/sheets_df.py (new): pandas DataFrame ↔
one +table-put / +table-get sheet, importable as a library. Same
helper pair the docs already taught, lifted out of the prose so
callers can `from sheets_df import df_to_sheet, sheet_to_df`.
- lark-sheets-write-cells.md / lark-sheets-read-data.md /
lark-sheets-workbook.md: drop the inline helper definitions; keep
the usage examples (single/multi-sheet, round-trip) and switch them
to import-from-script. workbook reference's +workbook-create
--sheets section now points pandas users at the helper directly
(was previously a textual reference back to write-cells).
End-to-end verified against PPE (--as user):
- +workbook-create with df_to_sheet for three sheets (income / balance
/ cashflow): create ok, dtypes (datetime64[ns] / float64) + formats
(#,##0 / 0.0% / yyyy-mm-dd) survive on read-back through sheet_to_df.
- read → pandas mutate → write-back round-trip preserves both data
and formats.
* chore: drop accidentally-committed __pycache__/ and gitignore .pyc
The previous commit (5fac9c39) shipped sheets_df.py and inadvertently
included its `__pycache__/sheets_df.cpython-312.pyc` — local Python
import created the bytecode cache during PPE round-trip verification and
`git add skills/lark-sheets/` swept it in.
Remove the pyc and add Python bytecode patterns to .gitignore so the
skill-bundled helper scripts don't pull cache files into future commits.
* refactor(sheets): drop --dataframe / --dataframe-out + apache/arrow dep
Per the design review at NcmxdRo2yoZ4OXxoMUZcxRZ7nHd, the Arrow IPC binary
input/output channel adds a heavy columnar runtime to the CLI for no new
capability — the typed JSON --sheets path already covers everything, and
the column-major / zero-copy advantages collapse the moment the CLI re-
encodes into the row-oriented sheets OpenAPI JSON body. Removing it also
lets us drop the `--ignore github.com/apache/arrow/go/v17` license-check
escape hatch.
Deleted:
- shortcuts/sheets/lark_sheet_dataframe.go (+ test)
- --dataframe branches in +table-put / +workbook-create
- --dataframe-out branch in +table-get
- StdinConsumed / MarkStdinConsumed exported methods (the binary stdin
reader was the only out-of-band consumer); internal stdinConsumed
guard against duplicate `-` input flags stays
- apache/arrow/go/v17 + transitive deps via `go mod tidy`
- CI go-licenses --ignore for arrow and the LICENSE.txt assertion step
- --dataframe / --dataframe-out coverage in skill references
Pandas users keep the round-trip via the existing skill script
skills/lark-sheets/scripts/sheets_df.py over the JSON path.
The full pre-removal state is preserved on branch feat/sheets-arrow-stash.
Upstream sheet-skill-spec follow-up: the two flag rows in the canonical
spec + base table tblV2F6fqIjyCFQW must also be dropped so the next sync
does not re-add them.
* sync(sheets): pick up --sheets one-liner fix from spec
Mirrors sheet-skill-spec 5562f83. The +table-put / +workbook-create
--sheets flag descriptions (and the --print-schema description on the
sheets array) now point at the existing df_to_sheet helper instead of
the previous misleading one-liner that produced a dict missing the
outer {"sheets":[...]} envelope and the per-sheet `name`. Agents that
copy-paste the description verbatim now build a valid payload.
Auto-synced via spec's generate:cli + sync:consumers; go generate
./shortcuts/sheets/... regenerated flag_defs_gen.go so its embedded
flagDefs stays byte-equal to data/flag-defs.json.
* test(sheets/e2e): close E2E coverage gaps for newly added shortcuts
AGENTS.md requires both dry-run and live E2E for every newly registered
shortcut, and behavior-changing refactors need at least the matching
half. Three gaps remained on feat/lark-sheets-develop:
- +sheet-show-gridline / +sheet-hide-gridline (new): only dry-run E2E.
Add sheets_gridline_workflow_test.go — create a real spreadsheet,
toggle hide then show against a live sub-sheet, assert ok=true on
both (gridline state is write-only — there is no read-back field on
+sheet-info / +workbook-info — so a successful envelope is the
meaningful signal; the dry-run E2E already pins the wire shape).
- +workbook-import (new): only dry-run E2E. Add
sheets_workbook_import_workflow_test.go — write a local CSV, run
the full upload → create-task → poll, assert ready=true with a
sheet token, +info confirms the imported workbook is reachable,
cleanup deletes the spreadsheet.
- +workbook-export refactor (no-download default changed): had live
E2E but no dry-run E2E in tests/cli_e2e/. Add
sheets_workbook_export_dryrun_test.go — pin the three sheet-
specific differences vs drive +export: type=sheet hard-coded,
csv mode routes --sheet-id onto sub_id (xlsx mode omits it), and
--output-path maps onto the dry-run plan's top-level output_dir.
Also pins the csv-without-sheet-id validation error.
* refactor(sheets): unify workbookCreatedButFillFailed with OutPartialFailure
Three "made it halfway and stopped" exits in the sheets domain previously
disagreed on shape, which made the post-failure recovery flow hard for
agents to predict from one command to another:
- +table-put partial write → exit 1, stdout ok:false envelope
- +table-put zero-sheet write → exit 1, stderr api/server_error
- +workbook-create create-but-fill → exit 2, stderr validation/failed_precondition
OutPartialFailure exists exactly for "the side effect landed but the
follow-up didn't" — it stamps an ok:false result envelope on stdout
(carrying the state the caller needs to recover) and returns the bare
partial-failure exit signal. The workbook-create fill-failure path was
the odd one out: it surfaced as a typed failed_precondition error on
stderr, which agents couldn't tell apart from a plain validation refusal
even though the spreadsheet really did exist and a retry / cleanup was
possible.
Migrate workbookCreatedButFillFailed onto OutPartialFailure so the four
call sites in +workbook-create's Execute (sheet-resolve failure, initial
fill failure, style-only resolve failure, style-only apply failure) emit
the same envelope shape +table-put's partial write does:
{
"ok": false,
"data": {
"spreadsheet_token": "shtNEW",
"reason": "spreadsheet shtNEW created but initial fill failed",
"hint": "the spreadsheet exists; retry the fill … or delete it",
"cause": {"category": "...", "subtype": "...", "message": "..."}
}
}
The inner failure's typed problem (category / subtype / message) is
flattened into the `cause` field so agents stay diagnosable from the JSON
envelope alone, instead of having to errors.Unwrap a Go error.
Updated TestExecute_WorkbookCreate_FillFailureKeepsToken to assert the
new shape (ok:false envelope on stdout, *output.PartialFailureError exit
signal, structured cause carrying the underlying invalid_response
subtype) — preserving the original test intent (token must survive for
recovery; inner cause must stay diagnosable) under the new contract.
* chore(sheets): three review nits — WithCause + stale comment + unexport
- shortcuts/sheets/flag_schema_validate.go:106 — composite-JSON shape
validation was wrapping vErr's message into a typed sheets validation
error without preserving vErr as the typed cause; add the missing
.WithCause(vErr) so errors.Unwrap and ProblemOf still find the
underlying validator error (matches every other typed-error chain
helper in the file).
- shortcuts/sheets/lark_sheet_batch_update.go:92 — comment claimed
batchUpdateInput returns "FlagErrorf-typed errors", but FlagErrorf no
longer exists (the typed-error migration replaced it with
common.ValidationErrorf / errs.ValidationError); update the comment
to reflect what is actually returned.
- shortcuts/drive/drive_export.go:121 — drop the ValidateExport public
alias and rename to validateExport. sheets +workbook-export reuses
RunExport / PlanExportDryRun from this package but inlines its own
(sheet-specific) Validate, so there is no cross-package call site —
ValidateExport was a misleading sibling of the genuinely-shared
ValidateImport. Comment added to record the asymmetry so future
readers do not export it back.
* chore(deps): drop stale indirect bumps left by the arrow removal
The earlier --dataframe / --dataframe-out + apache/arrow/go/v17 removal
deleted the arrow consumer but left two indirect lines in go.mod pinned
to the versions arrow had pulled in:
- github.com/kr/text v0.2.0
- golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
With arrow gone, larksuite/cli was the only requirer of those exact
versions; every real consumer needs lower ones (kr/pretty wants
kr/text v0.1.0; charmbracelet/huh wants x/exp …20231006; xo/terminfo
wants x/exp …20220909). Removing the two indirect lines and running
`go mod tidy` lets MVS pick the real-consumer versions and drops the
explicit indirect entries entirely — go.mod net-diff against main is
now zero for this branch.
Verified locally: go build ./...; go test ./shortcuts/sheets/...
./shortcuts/drive/... ./shortcuts/common/... ./internal/auth/...
./cmd/auth/... — all green.
---------
Co-authored-by: zhengzhijie <zhengzhijie.j@bytedance.com>
Co-authored-by: Chenweifeng-bd <chenweifeng.1534@bytedance.com>
1770 lines
64 KiB
Go
1770 lines
64 KiB
Go
// Copyright (c) 2026 Lark Technologies Pte. Ltd.
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
package sheets
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"path/filepath"
|
||
"strings"
|
||
|
||
"github.com/larksuite/cli/errs"
|
||
"github.com/larksuite/cli/extension/fileio"
|
||
"github.com/larksuite/cli/internal/util"
|
||
"github.com/larksuite/cli/shortcuts/common"
|
||
"github.com/larksuite/cli/shortcuts/drive"
|
||
)
|
||
|
||
// ─── lark_sheet_workbook ──────────────────────────────────────────────
|
||
//
|
||
// Wraps two tools behind the One-OpenAPI: get_workbook_structure (read) and
|
||
// modify_workbook_structure (write, dispatched by `operation` enum).
|
||
//
|
||
// CLI Risk tiers diverge intentionally from the tool's single endpoint:
|
||
// - +sheet-delete is high-risk-write (irreversible)
|
||
// - everything else is plain write
|
||
//
|
||
// +sheet-create only carries --url / --spreadsheet-token (no sheet selector):
|
||
// the create tool path needs no existing-sheet anchor, so the public sheet
|
||
// selector pair is dropped here to avoid a misleading XOR requirement.
|
||
|
||
// WorkbookInfo wraps get_workbook_structure: list a workbook's sub-sheets
|
||
// with their metadata (sheet_id, title, dimensions, freeze rows and cols,
|
||
// index, hidden). First step for every sheets task — downstream sheet-level
|
||
// operations all depend on the sheet_id returned here.
|
||
var WorkbookInfo = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+workbook-info",
|
||
Description: "List sub-sheets of a spreadsheet with metadata (sheet_id, title, dimensions, freeze, hidden).",
|
||
Risk: "read",
|
||
Scopes: []string{"sheets:spreadsheet:read"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+workbook-info"),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
_, err := resolveSpreadsheetToken(runtime)
|
||
return err
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
return invokeToolDryRun(token, ToolKindRead, "get_workbook_structure", map[string]interface{}{
|
||
"excel_id": token,
|
||
})
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindRead, "get_workbook_structure", map[string]interface{}{
|
||
"excel_id": token,
|
||
})
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
Tips: []string{
|
||
"First step for every sheets task — capture sheet_id from the result before doing any sheet-level operation.",
|
||
},
|
||
}
|
||
|
||
// SheetCreate creates a new sub-sheet. --title is the new sheet's name;
|
||
// --index inserts at a specific position (omitted → appended). Default
|
||
// dimensions match the canonical schema (rows=100, cols=26 when omitted —
|
||
// tool's defaults differ but CLI surface stays predictable).
|
||
var SheetCreate = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+sheet-create",
|
||
Description: "Create a new sub-sheet with an optional position and initial dimensions.",
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+sheet-create"),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetToken(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
_, err = sheetCreateInput(runtime, token)
|
||
return err
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
input, _ := sheetCreateInput(runtime, token)
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
input, err := sheetCreateInput(runtime, token)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
Tips: []string{
|
||
"+sheet-create makes an empty sub-sheet. To create a sub-sheet and fill it with typed data and/or styles in one step, use +table-put (missing sheets named in the payload are created automatically) with its --sheets and --styles flags.",
|
||
},
|
||
}
|
||
|
||
func sheetCreateInput(runtime flagView, token string) (map[string]interface{}, error) {
|
||
if strings.TrimSpace(runtime.Str("title")) == "" {
|
||
return nil, common.ValidationErrorf("--title is required")
|
||
}
|
||
if n := runtime.Int("row-count"); n < 0 || n > 50000 {
|
||
return nil, common.ValidationErrorf("--row-count must be between 0 and 50000")
|
||
}
|
||
if n := runtime.Int("col-count"); n < 0 || n > 200 {
|
||
return nil, common.ValidationErrorf("--col-count must be between 0 and 200")
|
||
}
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"operation": "create",
|
||
"sheet_name": strings.TrimSpace(runtime.Str("title")),
|
||
}
|
||
if runtime.Changed("index") {
|
||
input["target_index"] = runtime.Int("index")
|
||
}
|
||
if n := runtime.Int("row-count"); n > 0 {
|
||
input["rows"] = n
|
||
}
|
||
if n := runtime.Int("col-count"); n > 0 {
|
||
input["columns"] = n
|
||
}
|
||
return input, nil
|
||
}
|
||
|
||
// sheetDeleteInput / sheetRenameInput / sheetVisibilityInput /
|
||
// sheetSetTabColorInput build the modify_workbook_structure body for the
|
||
// matching shortcut. Shared by standalone DryRun/Execute and by the
|
||
// +batch-update sub-op dispatch so both paths emit an identical body and the
|
||
// same friendly error when --sheet-id/--sheet-name (or the shortcut's own
|
||
// required flags) are missing.
|
||
func sheetDeleteInput(runtime flagView, token, sheetID, sheetName string) (map[string]interface{}, error) {
|
||
if err := requireSheetSelector(sheetID, sheetName); err != nil {
|
||
return nil, err
|
||
}
|
||
input := map[string]interface{}{"excel_id": token, "operation": "delete"}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
return input, nil
|
||
}
|
||
|
||
func sheetRenameInput(runtime flagView, token, sheetID, sheetName string) (map[string]interface{}, error) {
|
||
if err := requireSheetSelector(sheetID, sheetName); err != nil {
|
||
return nil, err
|
||
}
|
||
if strings.TrimSpace(runtime.Str("title")) == "" {
|
||
return nil, common.ValidationErrorf("--title is required")
|
||
}
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"operation": "rename",
|
||
"new_name": strings.TrimSpace(runtime.Str("title")),
|
||
}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
return input, nil
|
||
}
|
||
|
||
func sheetVisibilityInput(runtime flagView, token, sheetID, sheetName, op string) (map[string]interface{}, error) {
|
||
if err := requireSheetSelector(sheetID, sheetName); err != nil {
|
||
return nil, err
|
||
}
|
||
input := map[string]interface{}{"excel_id": token, "operation": op}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
return input, nil
|
||
}
|
||
|
||
func sheetSetTabColorInput(runtime flagView, token, sheetID, sheetName string) (map[string]interface{}, error) {
|
||
if err := requireSheetSelector(sheetID, sheetName); err != nil {
|
||
return nil, err
|
||
}
|
||
if !runtime.Changed("color") {
|
||
return nil, common.ValidationErrorf("--color is required (empty string clears)")
|
||
}
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"operation": "set_tab_color",
|
||
"tab_color": runtime.Str("color"),
|
||
}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
return input, nil
|
||
}
|
||
|
||
// SheetDelete deletes a sub-sheet. high-risk-write — framework rejects
|
||
// without --yes. Always preview with --dry-run first to confirm the target.
|
||
var SheetDelete = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+sheet-delete",
|
||
Description: "Delete a sub-sheet (irreversible).",
|
||
Risk: "high-risk-write",
|
||
Scopes: []string{"sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+sheet-delete"),
|
||
Validate: validateViaInput(sheetDeleteInput),
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
sheetID, sheetName, _ := resolveSheetSelector(runtime)
|
||
input, _ := sheetDeleteInput(runtime, token, sheetID, sheetName)
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID, sheetName, err := resolveSheetSelector(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
input, err := sheetDeleteInput(runtime, token, sheetID, sheetName)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
Tips: []string{
|
||
"Sheet deletion is irreversible. Always run with --dry-run first to verify the target sheet_id/sheet_name.",
|
||
},
|
||
}
|
||
|
||
// SheetRename renames a sub-sheet via --title (mapped to tool's new_name).
|
||
var SheetRename = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+sheet-rename",
|
||
Description: "Rename a sub-sheet.",
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+sheet-rename"),
|
||
Validate: validateViaInput(sheetRenameInput),
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
sheetID, sheetName, _ := resolveSheetSelector(runtime)
|
||
input, _ := sheetRenameInput(runtime, token, sheetID, sheetName)
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID, sheetName, err := resolveSheetSelector(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
input, err := sheetRenameInput(runtime, token, sheetID, sheetName)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
}
|
||
|
||
// SheetMove moves a sub-sheet to a new index. The tool requires sheet_id
|
||
// and source_index in addition to target_index. The CLI accepts:
|
||
// - --sheet-id / --sheet-name to identify the sheet
|
||
// - --source-index (optional) for explicit source position
|
||
//
|
||
// When --source-index is omitted, or when --sheet-name is used instead of
|
||
// --sheet-id, Execute issues a single get_workbook_structure read to derive
|
||
// the missing pieces. DryRun stays network-free: it uses <resolve> placeholders
|
||
// for any field that would need that read.
|
||
var SheetMove = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+sheet-move",
|
||
Description: "Move a sub-sheet to a new position.",
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:read", "sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+sheet-move"),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
if _, err := resolveSpreadsheetToken(runtime); err != nil {
|
||
return err
|
||
}
|
||
if _, _, err := resolveSheetSelector(runtime); err != nil {
|
||
return err
|
||
}
|
||
if !runtime.Changed("index") {
|
||
return common.ValidationErrorf("--index is required")
|
||
}
|
||
if runtime.Int("index") < 0 {
|
||
return common.ValidationErrorf("--index must be >= 0")
|
||
}
|
||
if runtime.Changed("source-index") && runtime.Int("source-index") < 0 {
|
||
return common.ValidationErrorf("--source-index must be >= 0")
|
||
}
|
||
return nil
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
sheetID, sheetName, _ := resolveSheetSelector(runtime)
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"operation": "move",
|
||
"sheet_id": sheetSelectorPlaceholder(sheetID, sheetName),
|
||
"target_index": runtime.Int("index"),
|
||
"source_index": sourceIndexOrPlaceholder(runtime),
|
||
}
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID, sheetName, err := resolveSheetSelector(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
resolvedID := sheetID
|
||
var sourceIndex int
|
||
needIDLookup := sheetID == ""
|
||
needIndexLookup := !runtime.Changed("source-index")
|
||
if needIDLookup || needIndexLookup {
|
||
lookedID, lookedIdx, err := lookupSheetIndex(ctx, runtime, token, sheetID, sheetName)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
resolvedID = lookedID
|
||
sourceIndex = lookedIdx
|
||
}
|
||
if runtime.Changed("source-index") {
|
||
sourceIndex = runtime.Int("source-index")
|
||
}
|
||
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"operation": "move",
|
||
"sheet_id": resolvedID,
|
||
"source_index": sourceIndex,
|
||
"target_index": runtime.Int("index"),
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
Tips: []string{
|
||
"Pass --source-index when you already know it to avoid the extra read; otherwise CLI derives it from --sheet-id/--sheet-name.",
|
||
},
|
||
}
|
||
|
||
// sourceIndexOrPlaceholder returns the user-supplied source-index, or the
|
||
// string "<resolve>" when DryRun should signal that Execute will derive it.
|
||
func sourceIndexOrPlaceholder(runtime *common.RuntimeContext) interface{} {
|
||
if runtime.Changed("source-index") {
|
||
return runtime.Int("source-index")
|
||
}
|
||
return "<resolve>"
|
||
}
|
||
|
||
// SheetCopy duplicates a sub-sheet. --title (optional) names the copy;
|
||
// --index (optional) places it.
|
||
var SheetCopy = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+sheet-copy",
|
||
Description: "Duplicate a sub-sheet, optionally renaming and repositioning the copy.",
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+sheet-copy"),
|
||
Validate: validateViaInput(sheetCopyInput),
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
sheetID, sheetName, _ := resolveSheetSelector(runtime)
|
||
input, _ := sheetCopyInput(runtime, token, sheetID, sheetName)
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID, sheetName, err := resolveSheetSelector(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
input, err := sheetCopyInput(runtime, token, sheetID, sheetName)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
}
|
||
|
||
func sheetCopyInput(runtime flagView, token, sheetID, sheetName string) (map[string]interface{}, error) {
|
||
if err := requireSheetSelector(sheetID, sheetName); err != nil {
|
||
return nil, err
|
||
}
|
||
input := map[string]interface{}{"excel_id": token, "operation": "duplicate"}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
if t := strings.TrimSpace(runtime.Str("title")); t != "" {
|
||
input["new_name"] = t
|
||
}
|
||
if runtime.Changed("index") {
|
||
input["target_index"] = runtime.Int("index")
|
||
}
|
||
return input, nil
|
||
}
|
||
|
||
// SheetHide / SheetUnhide toggle visibility. Visible bool semantics live in
|
||
// the operation enum so callers don't need a --visible flag.
|
||
var SheetHide = newSheetVisibilityShortcut(
|
||
"+sheet-hide", "Hide a sub-sheet from the tabs bar.", "hide",
|
||
)
|
||
|
||
var SheetUnhide = newSheetVisibilityShortcut(
|
||
"+sheet-unhide", "Restore a hidden sub-sheet.", "unhide",
|
||
)
|
||
|
||
func newSheetVisibilityShortcut(command, desc, op string) common.Shortcut {
|
||
return common.Shortcut{
|
||
Service: "sheets",
|
||
Command: command,
|
||
Description: desc,
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor(command),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetToken(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID := strings.TrimSpace(runtime.Str("sheet-id"))
|
||
sheetName := strings.TrimSpace(runtime.Str("sheet-name"))
|
||
_, err = sheetVisibilityInput(runtime, token, sheetID, sheetName, op)
|
||
return err
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
sheetID, sheetName, _ := resolveSheetSelector(runtime)
|
||
input, _ := sheetVisibilityInput(runtime, token, sheetID, sheetName, op)
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID, sheetName, err := resolveSheetSelector(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
input, err := sheetVisibilityInput(runtime, token, sheetID, sheetName, op)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
}
|
||
}
|
||
|
||
// SheetSetTabColor sets the tab color of a sub-sheet. --color "" clears.
|
||
var SheetSetTabColor = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+sheet-set-tab-color",
|
||
Description: "Set or clear the tab color of a sub-sheet.",
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+sheet-set-tab-color"),
|
||
Validate: validateViaInput(sheetSetTabColorInput),
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
token, _ := resolveSpreadsheetToken(runtime)
|
||
sheetID, sheetName, _ := resolveSheetSelector(runtime)
|
||
input, _ := sheetSetTabColorInput(runtime, token, sheetID, sheetName)
|
||
return invokeToolDryRun(token, ToolKindWrite, "modify_workbook_structure", input)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
token, err := resolveSpreadsheetTokenExec(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
sheetID, sheetName, err := resolveSheetSelector(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
input, err := sheetSetTabColorInput(runtime, token, sheetID, sheetName)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
out, err := callTool(ctx, runtime, token, ToolKindWrite, "modify_workbook_structure", input)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
runtime.Out(out, nil)
|
||
return nil
|
||
},
|
||
}
|
||
|
||
// SheetShowGridline / SheetHideGridline toggle a sub-sheet's gridline display.
|
||
// Gridline show/hide is the same two-state-via-operation shape as
|
||
// +sheet-hide/+sheet-unhide (no --visible flag), so they reuse
|
||
// newSheetVisibilityShortcut; only the operation enum differs.
|
||
var SheetShowGridline = newSheetVisibilityShortcut(
|
||
"+sheet-show-gridline", "Show gridlines on a sub-sheet.", "show_gridline",
|
||
)
|
||
|
||
var SheetHideGridline = newSheetVisibilityShortcut(
|
||
"+sheet-hide-gridline", "Hide gridlines on a sub-sheet.", "hide_gridline",
|
||
)
|
||
|
||
// ─── +workbook-create (legacy OAPI, cli_status: cli-only) ────────────
|
||
//
|
||
// Creates a brand-new spreadsheet via POST /sheets/v3/spreadsheets, then
|
||
// optionally fills the first sheet's header row and initial data block
|
||
// via a follow-up callTool(set_cell_range). Not exposed as an MCP tool —
|
||
// hence the direct legacy OAPI call instead of going through callTool.
|
||
|
||
// WorkbookCreate creates a brand-new spreadsheet in the user's drive
|
||
// (optionally inside --folder-token) and can pre-fill the first row of
|
||
// headers and an initial data block.
|
||
var WorkbookCreate = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+workbook-create",
|
||
Description: "Create a new spreadsheet, optionally pre-filled with untyped --values or typed --sheets (type-faithful one-step create + write).",
|
||
Risk: "write",
|
||
Scopes: []string{"sheets:spreadsheet:create", "sheets:spreadsheet:write_only"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+workbook-create"),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
if strings.TrimSpace(runtime.Str("title")) == "" {
|
||
return common.ValidationErrorf("--title is required")
|
||
}
|
||
// --sheets (typed JSON) is the typed data entry, mutually exclusive
|
||
// with the untyped --values. Gating on Changed (not just non-empty)
|
||
// catches an explicitly-given but empty payload as an error instead
|
||
// of letting it fall through to creating an empty workbook.
|
||
sheetsGiven := runtime.Changed("sheets")
|
||
if sheetsGiven && runtime.Str("values") != "" {
|
||
return common.ValidationErrorf("--values is mutually exclusive with --sheets")
|
||
}
|
||
if sheetsGiven {
|
||
if strings.TrimSpace(runtime.Str("sheets")) == "" {
|
||
return common.ValidationErrorf("--sheets was given but resolved to empty (empty stdin/file?); pass a typed payload, or drop --sheets to create an empty workbook")
|
||
}
|
||
payload, err := parseTablePutPayload(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
_, err = parseWorkbookCreateSheetStyles(runtime, payload)
|
||
return err
|
||
}
|
||
// Untyped --values path: parse (and validate) --styles as a single sheet
|
||
// style item, then synthesize --values into a type-less typed payload —
|
||
// the same construction buildValuesPayload runs at execute time, so any
|
||
// malformed --values / --styles is caught here before a workbook is made.
|
||
sheetStyles, err := parseValuesSheetStyles(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if _, err := buildValuesPayload(runtime, sheetStyles); err != nil {
|
||
return err
|
||
}
|
||
return nil
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
body := map[string]interface{}{"title": strings.TrimSpace(runtime.Str("title"))}
|
||
if v := strings.TrimSpace(runtime.Str("folder-token")); v != "" {
|
||
body["folder_token"] = v
|
||
}
|
||
dry := common.NewDryRunAPI().
|
||
POST("/open-apis/sheets/v3/spreadsheets").
|
||
Desc("create spreadsheet").
|
||
Body(body)
|
||
// Both data entries (typed --sheets and untyped --values) resolve to the
|
||
// same typed payload and preview through the same set_cell_range path: one
|
||
// write per sheet, the first adopting the new workbook's default sheet.
|
||
// Mirrors +table-put's dry-run against a placeholder token.
|
||
payload, sheetStyles, _ := workbookCreateData(runtime)
|
||
if payload == nil {
|
||
// Style-only payload with no cell-rectangle extent (e.g. only
|
||
// row_sizes or col_sizes). No set_cell_range to render, but the
|
||
// visual ops (merges / row+col sizes) still run in Execute, so
|
||
// they should show up in the dry-run plan too.
|
||
if styles := sheetStyles.styleFor(0); styles != nil {
|
||
appendWorkbookCreateVisualOpsDryRun(dry, "<new-token>", "", valuesSheetName, styles)
|
||
}
|
||
return dry
|
||
}
|
||
for i := range payload.Sheets {
|
||
s := &payload.Sheets[i]
|
||
matrix, _ := buildSheetMatrix(s, headerOn(s))
|
||
_, col0, row0, _ := sheetAnchor(s)
|
||
_ = applyWorkbookCreateStylesToMatrix(matrix, sheetStyles.styleFor(i), col0, row0, fmt.Sprintf("--styles for sheet %q", s.Name))
|
||
input := map[string]interface{}{
|
||
"excel_id": "<new-token>",
|
||
"sheet_name": s.Name,
|
||
"range": tablePutFullRange(s, len(matrix)),
|
||
"cells": matrix,
|
||
}
|
||
wireBody, _ := buildToolBody("set_cell_range", input)
|
||
dry.POST("/open-apis/sheet_ai/v2/spreadsheets/<new-token>/tools/invoke_write").
|
||
Desc(fmt.Sprintf("write sheet %q (%d data rows × %d cols) via set_cell_range", s.Name, len(s.Rows), len(s.Columns))).
|
||
Body(wireBody)
|
||
appendWorkbookCreateVisualOpsDryRun(dry, "<new-token>", "", s.Name, sheetStyles.styleFor(i))
|
||
}
|
||
return dry
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
body := map[string]interface{}{"title": strings.TrimSpace(runtime.Str("title"))}
|
||
if v := strings.TrimSpace(runtime.Str("folder-token")); v != "" {
|
||
body["folder_token"] = v
|
||
}
|
||
data, err := runtime.CallAPITyped("POST", "/open-apis/sheets/v3/spreadsheets", nil, body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
ss := common.GetMap(data, "spreadsheet")
|
||
token := common.GetString(ss, "spreadsheet_token")
|
||
if token == "" {
|
||
token = common.GetString(ss, "token")
|
||
}
|
||
if token == "" {
|
||
return errs.NewInternalError(errs.SubtypeInvalidResponse, "spreadsheet created but token missing in response")
|
||
}
|
||
|
||
result := map[string]interface{}{"spreadsheet": ss}
|
||
|
||
// Both data entries resolve to the same typed payload: --sheets directly,
|
||
// --values synthesized into a type-less payload. Both write through
|
||
// writeTypedSheets, adopting the brand-new workbook's default sheet as the
|
||
// first payload sheet so no empty "Sheet1" is left behind.
|
||
payload, sheetStyles, err := workbookCreateData(runtime)
|
||
if err != nil {
|
||
return err // already validated; defensive
|
||
}
|
||
if payload != nil {
|
||
firstSheetID, err := lookupFirstSheetID(ctx, runtime, token)
|
||
if err != nil {
|
||
return workbookCreatedButFillFailed(runtime, token, "resolving its default sheet for the write failed", err)
|
||
}
|
||
written, err := writeTypedSheets(ctx, runtime, token, payload, firstSheetID, sheetStyles)
|
||
if err != nil {
|
||
return workbookCreatedButFillFailed(runtime, token, "initial fill failed", err)
|
||
}
|
||
result["sheets"] = written
|
||
} else if styles := sheetStyles.styleFor(0); styles != nil {
|
||
// Style-only payloads (e.g. --styles with only row_sizes or col_sizes
|
||
// and no --values/--sheets) don't write any cells but still need their
|
||
// visual ops applied — otherwise the merges/sizes would be silently
|
||
// dropped. workbookCreateStyleDimensions can't expand a row-only or
|
||
// column-only range into a cell rectangle, so the no-data branch lives
|
||
// here.
|
||
firstSheetID, err := lookupFirstSheetID(ctx, runtime, token)
|
||
if err != nil {
|
||
return workbookCreatedButFillFailed(runtime, token, "resolving its default sheet for the write failed", err)
|
||
}
|
||
if err := applyWorkbookCreateVisualOps(ctx, runtime, token, firstSheetID, styles); err != nil {
|
||
return workbookCreatedButFillFailed(runtime, token, "applying visual styles failed", err)
|
||
}
|
||
}
|
||
runtime.Out(result, nil)
|
||
return nil
|
||
},
|
||
Tips: []string{
|
||
"--values is an optional untyped fill (one JSON 2D array). It writes through the same batched set_cell_range path as --sheets; pair it with --styles to set number formats, colors, merges, and row/col sizes. Partial failure leaves the spreadsheet created but empty.",
|
||
"--sheets writes typed, type-faithful data (dates → real dates, numbers keep precision) in one step — the create + typed write that +table-put can't do on its own. Mutually exclusive with --values; the new workbook's default sheet becomes the first sheet (no empty Sheet1 left behind).",
|
||
},
|
||
}
|
||
|
||
// workbookCreatedButFillFailed reports a workbook-create where the spreadsheet
|
||
// POST succeeded but the follow-up initial fill did not. It is the same
|
||
// partial-state shape as +table-put's multi-sheet half-write: stdout carries an
|
||
// ok:false envelope with the new spreadsheet_token (so the caller can retry the
|
||
// fill via +cells-set / +csv-put, or delete the orphan), and the process exits
|
||
// with the partial-failure signal — keeping a single sheets-domain contract for
|
||
// "the side effect landed but the follow-up didn't" instead of two (this used to
|
||
// surface as a typed failed_precondition on stderr, which agents couldn't tell
|
||
// apart from a plain validation refusal). The underlying cause's typed shape is
|
||
// flattened into a structured `cause` field so the inner subtype / category /
|
||
// message stays diagnosable from the JSON envelope alone.
|
||
func workbookCreatedButFillFailed(runtime *common.RuntimeContext, token, reason string, cause error) error {
|
||
data := map[string]interface{}{
|
||
"spreadsheet_token": token,
|
||
"reason": fmt.Sprintf("spreadsheet %s created but %s", token, reason),
|
||
"hint": "the spreadsheet exists; retry the fill with the returned spreadsheet_token (+cells-set / +csv-put), or delete it",
|
||
}
|
||
if cause != nil {
|
||
if p, ok := errs.ProblemOf(cause); ok {
|
||
data["cause"] = map[string]interface{}{
|
||
"category": string(p.Category),
|
||
"subtype": string(p.Subtype),
|
||
"message": p.Message,
|
||
}
|
||
} else {
|
||
data["cause"] = map[string]interface{}{"message": cause.Error()}
|
||
}
|
||
}
|
||
return runtime.OutPartialFailure(data, nil)
|
||
}
|
||
|
||
// valuesSheetName is the synthesized sheet name for the untyped --values path.
|
||
// It matches a freshly created workbook's default sheet, so writeTypedSheets
|
||
// adopts that sheet in place (no rename, no stray sheet) — see its adopt logic.
|
||
// Lark Sheets names the default sheet "Sheet1" on create.
|
||
const valuesSheetName = "Sheet1"
|
||
|
||
// workbookCreateData resolves the data to write into a freshly created workbook:
|
||
// typed --sheets directly, or untyped --values synthesized as a single sheet of
|
||
// type-less (raw passthrough) columns. Both go through writeTypedSheets so the
|
||
// two entries share one batched set_cell_range writer. Returns (nil, nil, nil)
|
||
// when there's nothing to fill (no --sheets, and no --values/--styles extent).
|
||
func workbookCreateData(runtime *common.RuntimeContext) (*tablePayload, *workbookCreateSheetStyles, error) {
|
||
if runtime.Changed("sheets") {
|
||
payload, err := parseTablePutPayload(runtime)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
styles, err := parseWorkbookCreateSheetStyles(runtime, payload)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return payload, styles, nil
|
||
}
|
||
styles, err := parseValuesSheetStyles(runtime)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
payload, err := buildValuesPayload(runtime, styles)
|
||
if err != nil {
|
||
return nil, nil, err
|
||
}
|
||
return payload, styles, nil
|
||
}
|
||
|
||
// parseValuesSheetStyles parses --styles for the untyped --values path and wraps
|
||
// the single style item as a one-sheet workbookCreateSheetStyles, so --values
|
||
// reuses writeSheetData's styleFor application. The item's name is ignored (the
|
||
// synthesized sheet is always index 0). Returns nil when --styles is absent.
|
||
func parseValuesSheetStyles(runtime flagView) (*workbookCreateSheetStyles, error) {
|
||
p, err := parseWorkbookCreateStyles(runtime)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if p == nil {
|
||
return nil, nil
|
||
}
|
||
return &workbookCreateSheetStyles{ByIndex: []*workbookCreateStylePayload{p}}, nil
|
||
}
|
||
|
||
// buildValuesPayload turns untyped --values into a single-sheet typed payload of
|
||
// type-less columns (Header=false), so --values shares --sheets' batched
|
||
// set_cell_range writer. Rows are normalized to a rectangle wide/long enough to
|
||
// also cover any --styles cell ranges (matching the old buildInitialFillInput,
|
||
// where a style on B3 extends the written block). Returns (nil, nil) when there
|
||
// is nothing to write — no --values rows and no style-driven extent.
|
||
func buildValuesPayload(runtime flagView, sheetStyles *workbookCreateSheetStyles) (*tablePayload, error) {
|
||
rows, err := parseValuesRows(runtime)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
maxCols := 0
|
||
for _, r := range rows {
|
||
if len(r) > maxCols {
|
||
maxCols = len(r)
|
||
}
|
||
}
|
||
var styleRows, styleCols int
|
||
if sheetStyles != nil {
|
||
styleRows, styleCols = workbookCreateStyleDimensions(sheetStyles.styleFor(0), 0, 0)
|
||
}
|
||
if styleCols > maxCols {
|
||
maxCols = styleCols
|
||
}
|
||
nrows := len(rows)
|
||
if styleRows > nrows {
|
||
nrows = styleRows
|
||
}
|
||
if maxCols == 0 || nrows == 0 {
|
||
return nil, nil // nothing to write (e.g. --values '[]' with no styles)
|
||
}
|
||
// Pad to a rectangle; nil cells become empty cells in buildTypedCell.
|
||
for len(rows) < nrows {
|
||
rows = append(rows, nil)
|
||
}
|
||
for i := range rows {
|
||
for len(rows[i]) < maxCols {
|
||
rows[i] = append(rows[i], nil)
|
||
}
|
||
}
|
||
cols := make([]tableColumnSpec, maxCols)
|
||
for i := range cols {
|
||
cols[i] = tableColumnSpec{Name: fmt.Sprintf("col%d", i+1)} // type-less
|
||
}
|
||
noHeader := false
|
||
return &tablePayload{Sheets: []tableSheetSpec{{
|
||
Name: valuesSheetName,
|
||
Mode: "overwrite",
|
||
Header: &noHeader,
|
||
Columns: cols,
|
||
Rows: rows,
|
||
}}}, nil
|
||
}
|
||
|
||
// parseValuesRows decodes --values (JSON 2D array, with @file/stdin already
|
||
// resolved by the flag layer) using UseNumber so numeric cells keep full
|
||
// precision (large order IDs survive). Empty --values yields no rows.
|
||
func parseValuesRows(runtime flagView) ([][]interface{}, error) {
|
||
raw := strings.TrimSpace(runtime.Str("values"))
|
||
if raw == "" {
|
||
return nil, nil
|
||
}
|
||
dec := json.NewDecoder(strings.NewReader(raw))
|
||
dec.UseNumber()
|
||
var v interface{}
|
||
if err := dec.Decode(&v); err != nil {
|
||
return nil, common.ValidationErrorf("--values: invalid JSON: %v", err)
|
||
}
|
||
// Reject trailing non-whitespace after the first JSON value: see
|
||
// decoderExpectEOF in lark_sheet_table_io.go for the rationale.
|
||
if err := decoderExpectEOF(dec); err != nil {
|
||
return nil, common.ValidationErrorf("--values: %v", err).WithCause(err)
|
||
}
|
||
arr, ok := v.([]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("--values must be a JSON 2D array")
|
||
}
|
||
rows := make([][]interface{}, len(arr))
|
||
for i, r := range arr {
|
||
cells, ok := r.([]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("--values[%d] must be an array", i)
|
||
}
|
||
rows[i] = cells
|
||
}
|
||
return rows, nil
|
||
}
|
||
|
||
type workbookCreateStylePayload struct {
|
||
CellStyles []workbookCreateCellStyleOp
|
||
RowSizes []workbookCreateResizeOp
|
||
ColSizes []workbookCreateResizeOp
|
||
CellMerges []workbookCreateMergeOp
|
||
}
|
||
|
||
type workbookCreateCellStyleOp struct {
|
||
Range string
|
||
Style map[string]interface{}
|
||
}
|
||
|
||
type workbookCreateMergeOp struct {
|
||
Range string
|
||
MergeType string
|
||
}
|
||
|
||
type workbookCreateResizeOp struct {
|
||
Range string
|
||
ResizeType string
|
||
Size int
|
||
}
|
||
|
||
type workbookCreateSheetStyles struct {
|
||
ByIndex []*workbookCreateStylePayload
|
||
ByName map[string]*workbookCreateStylePayload
|
||
}
|
||
|
||
func (s *workbookCreateSheetStyles) styleFor(index int) *workbookCreateStylePayload {
|
||
if s == nil {
|
||
return nil
|
||
}
|
||
if index >= 0 && index < len(s.ByIndex) && s.ByIndex[index] != nil {
|
||
return s.ByIndex[index]
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// parseWorkbookCreateStyles parses --styles for +workbook-create's untyped
|
||
// initial-fill path. The outer protocol is always {"styles":[...]}; untyped
|
||
// initial fill consumes exactly one item from that array.
|
||
func parseWorkbookCreateStyles(runtime flagView) (*workbookCreateStylePayload, error) {
|
||
if strings.TrimSpace(runtime.Str("styles")) == "" {
|
||
return nil, nil
|
||
}
|
||
v, err := parseJSONFlag(runtime, "styles")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
items, err := parseWorkbookCreateStylesItems(v)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(items) != 1 {
|
||
return nil, common.ValidationErrorf("--styles.styles must contain exactly one item when using --values")
|
||
}
|
||
return parseWorkbookCreateStyleItem(items[0], "--styles.styles[0]")
|
||
}
|
||
|
||
// parseWorkbookCreateSheetStyles parses --styles for the typed --sheets path.
|
||
// The outer protocol is always {"styles":[...]}, and the array is aligned with
|
||
// --sheets.sheets. Each item must name the same sheet at the same index.
|
||
func parseWorkbookCreateSheetStyles(runtime flagView, payload *tablePayload) (*workbookCreateSheetStyles, error) {
|
||
if strings.TrimSpace(runtime.Str("styles")) == "" {
|
||
return nil, nil
|
||
}
|
||
v, err := parseJSONFlag(runtime, "styles")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
items, err := parseWorkbookCreateStylesItems(v)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(items) != len(payload.Sheets) {
|
||
return nil, common.ValidationErrorf("--styles.styles has %d items, want %d to match --sheets.sheets", len(items), len(payload.Sheets))
|
||
}
|
||
out := &workbookCreateSheetStyles{ByName: map[string]*workbookCreateStylePayload{}}
|
||
out.ByIndex = make([]*workbookCreateStylePayload, len(payload.Sheets))
|
||
for i, item := range items {
|
||
name, _ := item["name"].(string)
|
||
if strings.TrimSpace(name) == "" {
|
||
return nil, common.ValidationErrorf("--styles.styles[%d].name is required", i)
|
||
}
|
||
if name != payload.Sheets[i].Name {
|
||
return nil, common.ValidationErrorf("--styles.styles[%d].name %q must match --sheets.sheets[%d].name %q", i, name, i, payload.Sheets[i].Name)
|
||
}
|
||
style, err := parseWorkbookCreateStyleItem(item, fmt.Sprintf("--styles.styles[%d]", i))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
out.ByIndex[i] = style
|
||
out.ByName[name] = style
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func parseWorkbookCreateStylesItems(v interface{}) ([]map[string]interface{}, error) {
|
||
root, ok := v.(map[string]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("--styles must be a JSON object shaped as {\"styles\":[...]}")
|
||
}
|
||
rawItems, ok := root["styles"]
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("--styles.styles is required")
|
||
}
|
||
arr, ok := rawItems.([]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("--styles.styles must be an array")
|
||
}
|
||
items := make([]map[string]interface{}, len(arr))
|
||
for i, raw := range arr {
|
||
item, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("--styles.styles[%d] must be an object", i)
|
||
}
|
||
items[i] = item
|
||
}
|
||
return items, nil
|
||
}
|
||
|
||
func parseWorkbookCreateStyleItem(item map[string]interface{}, path string) (*workbookCreateStylePayload, error) {
|
||
payload := &workbookCreateStylePayload{}
|
||
var err error
|
||
if raw, ok := item["cell_styles"]; ok {
|
||
payload.CellStyles, err = parseWorkbookCreateCellStyleOps(raw, path+".cell_styles")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
if raw, ok := item["row_sizes"]; ok {
|
||
payload.RowSizes, err = parseWorkbookCreateResizeOps(raw, path+".row_sizes", "row")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
if raw, ok := item["col_sizes"]; ok {
|
||
payload.ColSizes, err = parseWorkbookCreateResizeOps(raw, path+".col_sizes", "column")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
if raw, ok := item["cell_merges"]; ok {
|
||
payload.CellMerges, err = parseWorkbookCreateMergeOps(raw, path+".cell_merges")
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
}
|
||
if len(payload.CellStyles) == 0 && len(payload.RowSizes) == 0 && len(payload.ColSizes) == 0 && len(payload.CellMerges) == 0 {
|
||
return nil, common.ValidationErrorf("%s must include at least one of cell_styles/row_sizes/col_sizes/cell_merges", path)
|
||
}
|
||
return payload, nil
|
||
}
|
||
|
||
func parseWorkbookCreateCellStyleOps(v interface{}, path string) ([]workbookCreateCellStyleOp, error) {
|
||
arr, ok := v.([]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s must be an array", path)
|
||
}
|
||
ops := make([]workbookCreateCellStyleOp, 0, len(arr))
|
||
for i, raw := range arr {
|
||
op, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s[%d] must be an object", path, i)
|
||
}
|
||
rangeStr, err := requireWorkbookCreateRange(op, fmt.Sprintf("%s[%d]", path, i))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if _, _, _, _, err := workbookCreateStyleRangeBounds(rangeStr); err != nil {
|
||
return nil, common.ValidationErrorf("%s[%d].range %q: %v", path, i, rangeStr, err)
|
||
}
|
||
styleObj := make(map[string]interface{}, len(op)-1)
|
||
for k, v := range op {
|
||
if k == "range" {
|
||
continue
|
||
}
|
||
styleObj[k] = v
|
||
}
|
||
style, err := normalizeWorkbookCreateStyleObject(styleObj, fmt.Sprintf("%s[%d]", path, i))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if len(style) == 0 {
|
||
return nil, common.ValidationErrorf("%s[%d] must include at least one style field", path, i)
|
||
}
|
||
ops = append(ops, workbookCreateCellStyleOp{Range: rangeStr, Style: style})
|
||
}
|
||
return ops, nil
|
||
}
|
||
|
||
func parseWorkbookCreateMergeOps(v interface{}, path string) ([]workbookCreateMergeOp, error) {
|
||
arr, ok := v.([]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s must be an array", path)
|
||
}
|
||
ops := make([]workbookCreateMergeOp, 0, len(arr))
|
||
for i, raw := range arr {
|
||
op, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s[%d] must be an object", path, i)
|
||
}
|
||
rangeStr, err := requireWorkbookCreateRange(op, fmt.Sprintf("%s[%d]", path, i))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if _, _, _, _, err := workbookCreateStyleRangeBounds(rangeStr); err != nil {
|
||
return nil, common.ValidationErrorf("%s[%d].range %q: %v", path, i, rangeStr, err)
|
||
}
|
||
mergeType := "all"
|
||
if raw, ok := op["merge_type"]; ok {
|
||
v, ok := raw.(string)
|
||
if !ok || strings.TrimSpace(v) == "" {
|
||
return nil, common.ValidationErrorf("%s[%d].merge_type must be a non-empty string", path, i)
|
||
}
|
||
mergeType = strings.TrimSpace(v)
|
||
}
|
||
switch mergeType {
|
||
case "all", "rows", "columns":
|
||
default:
|
||
return nil, common.ValidationErrorf("%s[%d].merge_type %q is invalid (want all/rows/columns)", path, i, mergeType)
|
||
}
|
||
if err := rejectUnexpectedWorkbookStyleFields(op, fmt.Sprintf("%s[%d]", path, i), "range", "merge_type"); err != nil {
|
||
return nil, err
|
||
}
|
||
ops = append(ops, workbookCreateMergeOp{Range: rangeStr, MergeType: mergeType})
|
||
}
|
||
return ops, nil
|
||
}
|
||
|
||
func parseWorkbookCreateResizeOps(v interface{}, path, dimension string) ([]workbookCreateResizeOp, error) {
|
||
arr, ok := v.([]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s must be an array", path)
|
||
}
|
||
ops := make([]workbookCreateResizeOp, 0, len(arr))
|
||
for i, raw := range arr {
|
||
op, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s[%d] must be an object", path, i)
|
||
}
|
||
rangeStr, err := requireWorkbookCreateRange(op, fmt.Sprintf("%s[%d]", path, i))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
parsedDim, _, _, err := parseA1Range(rangeStr)
|
||
if err != nil {
|
||
want := "row numbers like 2:10"
|
||
if dimension == "column" {
|
||
want = "column letters like A:E"
|
||
}
|
||
return nil, common.ValidationErrorf("%s[%d].range %q must use %s: %v", path, i, rangeStr, want, err)
|
||
}
|
||
if parsedDim != dimension {
|
||
want := "row numbers like 2:10"
|
||
if dimension == "column" {
|
||
want = "column letters like A:E"
|
||
}
|
||
return nil, common.ValidationErrorf("%s[%d].range %q must use %s", path, i, rangeStr, want)
|
||
}
|
||
resizeType, _ := op["type"].(string)
|
||
resizeType = strings.TrimSpace(resizeType)
|
||
if resizeType == "" {
|
||
return nil, common.ValidationErrorf("%s[%d].type is required (pixel/standard%s)", path, i, autoSuffix(dimension))
|
||
}
|
||
if dimension == "column" && resizeType == "auto" {
|
||
return nil, common.ValidationErrorf("%s[%d].type auto is rows-only", path, i)
|
||
}
|
||
switch resizeType {
|
||
case "pixel", "standard", "auto":
|
||
default:
|
||
return nil, common.ValidationErrorf("%s[%d].type %q is invalid (want pixel/standard%s)", path, i, resizeType, autoSuffix(dimension))
|
||
}
|
||
size := 0
|
||
if raw, ok := op["size"]; ok {
|
||
n, ok := util.ToFloat64(raw)
|
||
if !ok || n <= 0 {
|
||
return nil, common.ValidationErrorf("%s[%d].size must be a positive number", path, i)
|
||
}
|
||
size = int(n)
|
||
}
|
||
if resizeType == "pixel" && size <= 0 {
|
||
return nil, common.ValidationErrorf("%s[%d].type pixel requires size", path, i)
|
||
}
|
||
if resizeType != "pixel" && size > 0 {
|
||
return nil, common.ValidationErrorf("%s[%d].size is only valid with type pixel", path, i)
|
||
}
|
||
if err := rejectUnexpectedWorkbookStyleFields(op, fmt.Sprintf("%s[%d]", path, i), "range", "type", "size"); err != nil {
|
||
return nil, err
|
||
}
|
||
ops = append(ops, workbookCreateResizeOp{Range: normalizeWorkbookResizeRange(rangeStr), ResizeType: resizeType, Size: size})
|
||
}
|
||
return ops, nil
|
||
}
|
||
|
||
func requireWorkbookCreateRange(op map[string]interface{}, path string) (string, error) {
|
||
rangeRaw, ok := op["range"]
|
||
if !ok {
|
||
return "", common.ValidationErrorf("%s.range is required", path)
|
||
}
|
||
rangeStr, ok := rangeRaw.(string)
|
||
if !ok || strings.TrimSpace(rangeStr) == "" {
|
||
return "", common.ValidationErrorf("%s.range must be a non-empty string", path)
|
||
}
|
||
return strings.TrimSpace(rangeStr), nil
|
||
}
|
||
|
||
func rejectUnexpectedWorkbookStyleFields(op map[string]interface{}, path string, allowed ...string) error {
|
||
allow := map[string]struct{}{}
|
||
for _, k := range allowed {
|
||
allow[k] = struct{}{}
|
||
}
|
||
for k := range op {
|
||
if _, ok := allow[k]; !ok {
|
||
return common.ValidationErrorf("%s.%s is not valid here", path, k)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func normalizeWorkbookResizeRange(rangeStr string) string {
|
||
rangeStr = strings.TrimSpace(rangeStr)
|
||
if !strings.Contains(rangeStr, ":") {
|
||
return rangeStr + ":" + rangeStr
|
||
}
|
||
return rangeStr
|
||
}
|
||
|
||
func normalizeWorkbookCreateStyleObject(in map[string]interface{}, path string) (map[string]interface{}, error) {
|
||
if len(in) == 0 {
|
||
return nil, nil
|
||
}
|
||
if err := normalizeCellStyleAliases(in, path); err != nil {
|
||
return nil, err
|
||
}
|
||
out := map[string]interface{}{}
|
||
cellStyle := map[string]interface{}{}
|
||
for k, v := range in {
|
||
switch k {
|
||
case "cell_styles":
|
||
return nil, common.ValidationErrorf("%s.cell_styles is not supported inside cell_styles[]; put style fields directly on the item", path)
|
||
case "border_styles":
|
||
m, ok := v.(map[string]interface{})
|
||
if !ok {
|
||
return nil, common.ValidationErrorf("%s.border_styles must be a JSON object", path)
|
||
}
|
||
if err := validateWorkbookBorderStyles(m, path); err != nil {
|
||
return nil, err
|
||
}
|
||
out["border_styles"] = m
|
||
case "value", "formula", "rich_text", "multiple_values", "note", "data_validation":
|
||
return nil, common.ValidationErrorf("%s is for styles only; put content in --values or use --sheets for typed cell objects", path)
|
||
default:
|
||
if !workbookCreateCellStyleField(k) {
|
||
return nil, common.ValidationErrorf("%s.%s is not a supported style field", path, k)
|
||
}
|
||
cellStyle[k] = v
|
||
}
|
||
}
|
||
if len(cellStyle) > 0 {
|
||
out["cell_styles"] = cellStyle
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func workbookCreateCellStyleField(name string) bool {
|
||
switch name {
|
||
case "font_color", "font_size", "font_weight", "font_style", "font_line",
|
||
"background_color", "horizontal_alignment", "vertical_alignment",
|
||
"number_format", "word_wrap":
|
||
return true
|
||
default:
|
||
return false
|
||
}
|
||
}
|
||
|
||
// validateWorkbookBorderStyles checks a border_styles object's internal shape
|
||
// (per-side style/weight enums + color) at parse time. --styles is on
|
||
// parseJSONFlagSkip so it bypasses the generic schema validator; this keeps
|
||
// border errors caught in the CLI (mirroring +cells-set-style) rather than being
|
||
// passed straight through to the backend.
|
||
func validateWorkbookBorderStyles(m map[string]interface{}, path string) error {
|
||
for side, raw := range m {
|
||
switch side {
|
||
case "top", "bottom", "left", "right":
|
||
default:
|
||
return common.ValidationErrorf("%s.border_styles.%s is not a valid side (want top/bottom/left/right)", path, side)
|
||
}
|
||
spec, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
return common.ValidationErrorf("%s.border_styles.%s must be a JSON object", path, side)
|
||
}
|
||
for k, v := range spec {
|
||
switch k {
|
||
case "style":
|
||
if s, _ := v.(string); !workbookBorderStyleEnum(s) {
|
||
return common.ValidationErrorf("%s.border_styles.%s.style %q is invalid (want solid/dashed/dotted/double/none)", path, side, s)
|
||
}
|
||
case "weight":
|
||
if w, _ := v.(string); w != "thin" && w != "medium" && w != "thick" {
|
||
return common.ValidationErrorf("%s.border_styles.%s.weight %q is invalid (want thin/medium/thick)", path, side, w)
|
||
}
|
||
case "color":
|
||
if _, ok := v.(string); !ok {
|
||
return common.ValidationErrorf("%s.border_styles.%s.color must be a string", path, side)
|
||
}
|
||
default:
|
||
return common.ValidationErrorf("%s.border_styles.%s.%s is not valid (want style/weight/color)", path, side, k)
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func workbookBorderStyleEnum(s string) bool {
|
||
switch s {
|
||
case "solid", "dashed", "dotted", "double", "none":
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
func workbookCreateStyleDimensions(styles *workbookCreateStylePayload, baseCol, baseRow int) (rows, cols int) {
|
||
if styles == nil {
|
||
return 0, 0
|
||
}
|
||
expandCellRange := func(rng string) {
|
||
startCol, startRow, endCol, endRow, err := workbookCreateStyleRangeBounds(rng)
|
||
if err != nil {
|
||
return
|
||
}
|
||
if startCol < baseCol || startRow < baseRow {
|
||
return
|
||
}
|
||
if endCol-baseCol+1 > cols {
|
||
cols = endCol - baseCol + 1
|
||
}
|
||
if endRow-baseRow+1 > rows {
|
||
rows = endRow - baseRow + 1
|
||
}
|
||
}
|
||
expandRowRange := func(rng string) {
|
||
dim, _, endIdx, err := parseA1Range(rng)
|
||
if err != nil || dim != "row" || endIdx < baseRow {
|
||
return
|
||
}
|
||
if endIdx-baseRow+1 > rows {
|
||
rows = endIdx - baseRow + 1
|
||
}
|
||
}
|
||
expandColRange := func(rng string) {
|
||
dim, _, endIdx, err := parseA1Range(rng)
|
||
if err != nil || dim != "column" || endIdx < baseCol {
|
||
return
|
||
}
|
||
if endIdx-baseCol+1 > cols {
|
||
cols = endIdx - baseCol + 1
|
||
}
|
||
}
|
||
for _, op := range styles.CellStyles {
|
||
expandCellRange(op.Range)
|
||
}
|
||
// cell_merges / row_sizes / col_sizes also contribute to the write extent —
|
||
// without this, a style-only payload (e.g. just cell_merges) would compute
|
||
// extent 0 and the Execute path would skip writeTypedSheets entirely,
|
||
// silently dropping the visual ops.
|
||
for _, op := range styles.CellMerges {
|
||
expandCellRange(op.Range)
|
||
}
|
||
for _, op := range styles.RowSizes {
|
||
expandRowRange(op.Range)
|
||
}
|
||
for _, op := range styles.ColSizes {
|
||
expandColRange(op.Range)
|
||
}
|
||
return rows, cols
|
||
}
|
||
|
||
func applyWorkbookCreateStylesToMatrix(rows [][]interface{}, styles *workbookCreateStylePayload, baseCol, baseRow int, label string) error {
|
||
if styles == nil {
|
||
return nil
|
||
}
|
||
for i, op := range styles.CellStyles {
|
||
startCol, startRow, endCol, endRow, err := workbookCreateStyleRangeBounds(op.Range)
|
||
if err != nil {
|
||
return common.ValidationErrorf("%s[%d].range %q: %v", label, i, op.Range, err)
|
||
}
|
||
if startCol < baseCol || startRow < baseRow || endRow-baseRow >= len(rows) || len(rows) == 0 || endCol-baseCol >= len(rows[0]) {
|
||
return common.ValidationErrorf("%s[%d].range %q is outside the write range %s%d:%s%d",
|
||
label, i, op.Range,
|
||
columnIndexToLetter(baseCol), baseRow+1,
|
||
columnIndexToLetter(baseCol+len(rows[0])-1), baseRow+len(rows))
|
||
}
|
||
for r := startRow - baseRow; r <= endRow-baseRow; r++ {
|
||
for c := startCol - baseCol; c <= endCol-baseCol; c++ {
|
||
mergeWorkbookCreateStyle(rows[r][c], op.Style)
|
||
}
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func appendWorkbookCreateVisualOpsDryRun(dry *common.DryRunAPI, token, sheetID, sheetName string, styles *workbookCreateStylePayload) {
|
||
if dry == nil || styles == nil {
|
||
return
|
||
}
|
||
for _, op := range workbookCreateVisualOps(styles) {
|
||
input, toolName := workbookCreateVisualOpInput(token, sheetID, sheetName, op)
|
||
if toolName == "" {
|
||
continue
|
||
}
|
||
wireBody, _ := buildToolBody(toolName, input)
|
||
dry.POST(toolInvokePath(token, ToolKindWrite)).
|
||
Desc(fmt.Sprintf("apply %s %s", op.Kind, op.Range)).
|
||
Body(wireBody)
|
||
}
|
||
}
|
||
|
||
func applyWorkbookCreateVisualOps(ctx context.Context, runtime *common.RuntimeContext, token, sheetID string, styles *workbookCreateStylePayload) error {
|
||
if styles == nil {
|
||
return nil
|
||
}
|
||
for _, op := range workbookCreateVisualOps(styles) {
|
||
input, toolName := workbookCreateVisualOpInput(token, sheetID, "", op)
|
||
if toolName == "" {
|
||
continue
|
||
}
|
||
if _, err := callTool(ctx, runtime, token, ToolKindWrite, toolName, input); err != nil {
|
||
// callTool already returns a typed error; pass it through unchanged
|
||
// (re-wrapping would downgrade its classification) and attach the
|
||
// failing op as a recovery hint when one isn't already set.
|
||
if p, ok := errs.ProblemOf(err); ok {
|
||
if p.Hint == "" {
|
||
p.Hint = fmt.Sprintf("failed while applying %s on %s", op.Kind, op.Range)
|
||
}
|
||
return err
|
||
}
|
||
return errs.NewInternalError(errs.SubtypeUnknown, "%s %s failed", op.Kind, op.Range).WithCause(err)
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func workbookCreateVisualOps(styles *workbookCreateStylePayload) []workbookCreateStyleOp {
|
||
if styles == nil {
|
||
return nil
|
||
}
|
||
ops := make([]workbookCreateStyleOp, 0, len(styles.CellMerges)+len(styles.RowSizes)+len(styles.ColSizes))
|
||
for _, op := range styles.CellMerges {
|
||
ops = append(ops, workbookCreateStyleOp{Kind: "cell_merge", Range: op.Range, MergeType: op.MergeType})
|
||
}
|
||
for _, op := range styles.RowSizes {
|
||
ops = append(ops, workbookCreateStyleOp{Kind: "row_size", Range: op.Range, ResizeType: op.ResizeType, Size: op.Size})
|
||
}
|
||
for _, op := range styles.ColSizes {
|
||
ops = append(ops, workbookCreateStyleOp{Kind: "col_size", Range: op.Range, ResizeType: op.ResizeType, Size: op.Size})
|
||
}
|
||
return ops
|
||
}
|
||
|
||
type workbookCreateStyleOp struct {
|
||
Kind string
|
||
Range string
|
||
MergeType string
|
||
ResizeType string
|
||
Size int
|
||
}
|
||
|
||
func workbookCreateVisualOpInput(token, sheetID, sheetName string, op workbookCreateStyleOp) (map[string]interface{}, string) {
|
||
switch op.Kind {
|
||
case "cell_merge":
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"range": op.Range,
|
||
"operation": "merge",
|
||
"merge_type": op.MergeType,
|
||
}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
return input, "merge_cells"
|
||
case "row_size", "col_size":
|
||
input := map[string]interface{}{
|
||
"excel_id": token,
|
||
"range": op.Range,
|
||
}
|
||
sheetSelectorForToolInput(input, sheetID, sheetName)
|
||
block := map[string]interface{}{"type": op.ResizeType}
|
||
if op.ResizeType == "pixel" {
|
||
block["value"] = op.Size
|
||
}
|
||
if op.Kind == "row_size" {
|
||
input["resize_height"] = block
|
||
} else {
|
||
input["resize_width"] = block
|
||
}
|
||
return input, "resize_range"
|
||
default:
|
||
return nil, ""
|
||
}
|
||
}
|
||
|
||
func workbookCreateStyleRangeBounds(rangeStr string) (startCol, startRow, endCol, endRow int, err error) {
|
||
if idx := strings.Index(rangeStr, "!"); idx >= 0 {
|
||
rangeStr = rangeStr[idx+1:]
|
||
}
|
||
rangeStr = strings.TrimSpace(rangeStr)
|
||
if rangeStr == "" {
|
||
return 0, 0, 0, 0, fmt.Errorf("empty range") //nolint:forbidigo // intermediate error; callers wrap it into a typed validation error with flag/param context
|
||
}
|
||
parts := strings.SplitN(rangeStr, ":", 2)
|
||
if len(parts) == 1 {
|
||
col, row, ok := splitCellRef(parts[0])
|
||
if !ok {
|
||
return 0, 0, 0, 0, fmt.Errorf("invalid cell ref %q", parts[0]) //nolint:forbidigo // intermediate error; callers wrap it into a typed validation error with flag/param context
|
||
}
|
||
return col, row, col, row, nil
|
||
}
|
||
startCol, startRow, ok1 := splitCellRef(parts[0])
|
||
endCol, endRow, ok2 := splitCellRef(parts[1])
|
||
if !ok1 || !ok2 {
|
||
return 0, 0, 0, 0, fmt.Errorf("unsupported range form %q (need rectangular A1:B2)", rangeStr) //nolint:forbidigo // intermediate error; callers wrap it into a typed validation error with flag/param context
|
||
}
|
||
if endRow < startRow || endCol < startCol {
|
||
return 0, 0, 0, 0, fmt.Errorf("end %q must be at or after start %q", parts[1], parts[0]) //nolint:forbidigo // intermediate error; callers wrap it into a typed validation error with flag/param context
|
||
}
|
||
return startCol, startRow, endCol, endRow, nil
|
||
}
|
||
|
||
// mergeWorkbookCreateStyle merges one cell_styles op's style map into a cell.
|
||
// cell_styles / border_styles are nested submaps: they are deep-merged one level
|
||
// (field-wise, last write wins) so overlapping cell_styles ops accumulate fields
|
||
// rather than the later op's submap wholesale-replacing the earlier one. A fresh
|
||
// submap is allocated each merge so the op.Style shared across the range's cells
|
||
// is never mutated.
|
||
func mergeWorkbookCreateStyle(cell interface{}, style map[string]interface{}) {
|
||
if len(style) == 0 {
|
||
return
|
||
}
|
||
m, ok := cell.(map[string]interface{})
|
||
if !ok {
|
||
return
|
||
}
|
||
for k, v := range style {
|
||
if k == "cell_styles" || k == "border_styles" {
|
||
if incoming, ok := v.(map[string]interface{}); ok {
|
||
merged := map[string]interface{}{}
|
||
if existing, ok := m[k].(map[string]interface{}); ok {
|
||
for sk, sv := range existing {
|
||
merged[sk] = sv
|
||
}
|
||
}
|
||
for sk, sv := range incoming {
|
||
merged[sk] = sv
|
||
}
|
||
m[k] = merged
|
||
continue
|
||
}
|
||
}
|
||
m[k] = v
|
||
}
|
||
}
|
||
|
||
// ─── +workbook-export (legacy OAPI, cli_status: cli-only) ────────────
|
||
//
|
||
// Drives the three-step export flow against the classic drive endpoints:
|
||
// create export task → poll task status → optional binary download.
|
||
// Not exposed as an MCP tool.
|
||
|
||
// WorkbookExport drives the three-step export flow: create task → poll →
|
||
// optionally download. CSV mode requires --sheet-id (the API exports one
|
||
// sheet at a time as csv).
|
||
var WorkbookExport = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+workbook-export",
|
||
Description: "Export a spreadsheet to xlsx or a single sheet to csv (async + poll + optional download).",
|
||
Risk: "read",
|
||
Scopes: []string{"sheets:spreadsheet:read", "docs:document:export", "drive:drive.metadata:readonly"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+workbook-export"),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
if _, err := resolveSpreadsheetToken(runtime); err != nil {
|
||
return err
|
||
}
|
||
ext := runtime.Str("file-extension")
|
||
if ext == "" {
|
||
ext = "xlsx"
|
||
}
|
||
if ext == "csv" && strings.TrimSpace(runtime.Str("sheet-id")) == "" {
|
||
return common.ValidationErrorf("--sheet-id is required when --file-extension=csv")
|
||
}
|
||
return nil
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
p, _ := workbookExportParams(runtime)
|
||
p.OutputDir = strings.TrimSpace(runtime.Str("output-path"))
|
||
return drive.PlanExportDryRun(runtime, p)
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
p, err := workbookExportParams(runtime)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// workbookExportParams resolves --url network-free (DryRun shares it); a
|
||
// /wiki/ URL carries a node_token that needs the get_node step only
|
||
// Execute may take, so re-resolve the token here.
|
||
if p.Token, err = resolveSpreadsheetTokenExec(runtime); err != nil {
|
||
return err
|
||
}
|
||
applyWorkbookOutputPath(&p, runtime.FileIO(), runtime.Str("output-path"))
|
||
return drive.RunExport(ctx, runtime, p)
|
||
},
|
||
Tips: []string{
|
||
"Polls for a bounded window; if the export is still running it returns a resume reference instead of blocking. Pass --output-path to download the file once ready (omit it to only create the export task and get the file token back).",
|
||
},
|
||
}
|
||
|
||
// workbookExportParams builds the shared drive export request for
|
||
// +workbook-export: spreadsheet token + sheet locator, pinned to type=sheet.
|
||
// workbook-export has always overwritten the target, so Overwrite is set. The
|
||
// --output-path → OutputDir/FileName split (which needs a Stat) is applied
|
||
// separately by applyWorkbookOutputPath so Validate/DryRun stay I/O-free.
|
||
func workbookExportParams(runtime *common.RuntimeContext) (drive.ExportParams, error) {
|
||
token, err := resolveSpreadsheetToken(runtime)
|
||
if err != nil {
|
||
return drive.ExportParams{}, err
|
||
}
|
||
ext := runtime.Str("file-extension")
|
||
if ext == "" {
|
||
ext = "xlsx"
|
||
}
|
||
return drive.ExportParams{
|
||
Token: token,
|
||
DocType: "sheet",
|
||
FileExtension: ext,
|
||
SubID: strings.TrimSpace(runtime.Str("sheet-id")),
|
||
Overwrite: true,
|
||
}, nil
|
||
}
|
||
|
||
// applyWorkbookOutputPath maps the single --output-path flag onto the drive
|
||
// export OutputDir/FileName pair, preserving the legacy behavior: empty = no
|
||
// download (return the ready file token only); an existing directory = download
|
||
// into it under the server-provided name; otherwise treat it as a file path and
|
||
// split into dir + base name.
|
||
func applyWorkbookOutputPath(p *drive.ExportParams, fio fileio.FileIO, outputPath string) {
|
||
outputPath = strings.TrimSpace(outputPath)
|
||
if outputPath == "" {
|
||
return
|
||
}
|
||
if info, err := fio.Stat(outputPath); err == nil && info.IsDir() {
|
||
p.OutputDir = outputPath
|
||
return
|
||
}
|
||
p.OutputDir = filepath.Dir(outputPath)
|
||
p.FileName = filepath.Base(outputPath)
|
||
}
|
||
|
||
// lookupSheetIndex finds a sub-sheet by id or name and returns its canonical
|
||
// id + current 0-based index. Caller is responsible for ensuring at least one
|
||
// of sheetID/sheetName is non-empty.
|
||
func lookupSheetIndex(ctx context.Context, runtime *common.RuntimeContext, token, sheetID, sheetName string) (resolvedID string, index int, err error) {
|
||
out, err := callTool(ctx, runtime, token, ToolKindRead, "get_workbook_structure", map[string]interface{}{
|
||
"excel_id": token,
|
||
})
|
||
if err != nil {
|
||
return "", 0, err
|
||
}
|
||
m, ok := out.(map[string]interface{})
|
||
if !ok {
|
||
return "", 0, errs.NewInternalError(errs.SubtypeInvalidResponse, "get_workbook_structure returned non-object output")
|
||
}
|
||
sheets, _ := m["sheets"].([]interface{})
|
||
for _, raw := range sheets {
|
||
sm, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
continue
|
||
}
|
||
id, _ := sm["sheet_id"].(string)
|
||
// get_workbook_structure surfaces the sub-sheet's display name as
|
||
// "title"; older/alt payloads use "sheet_name". Match either so a
|
||
// --sheet-name lookup resolves regardless of the field name.
|
||
name, _ := sm["sheet_name"].(string)
|
||
if name == "" {
|
||
name, _ = sm["title"].(string)
|
||
}
|
||
if (sheetID != "" && id == sheetID) || (sheetName != "" && name == sheetName) {
|
||
idx, ok := util.ToFloat64(sm["index"])
|
||
if !ok {
|
||
return "", 0, errs.NewInternalError(errs.SubtypeInvalidResponse, "sheet entry missing index field")
|
||
}
|
||
return id, int(idx), nil
|
||
}
|
||
}
|
||
target := sheetID
|
||
if target == "" {
|
||
target = sheetName
|
||
}
|
||
return "", 0, errs.NewValidationError(errs.SubtypeFailedPrecondition, "sheet %q not found in workbook", target)
|
||
}
|
||
|
||
// lookupFirstSheetID returns the sheet_id of the sub-sheet at index 0 (the
|
||
// default sheet of a freshly created workbook). Used by +workbook-create to
|
||
// target the initial-fill set_cell_range write — set_cell_range rejects an
|
||
// empty sheet selector ("sheet_id or sheet_name is required"), and the v3
|
||
// create-spreadsheet response does not echo the default sheet's id.
|
||
func lookupFirstSheetID(ctx context.Context, runtime *common.RuntimeContext, token string) (string, error) {
|
||
out, err := callTool(ctx, runtime, token, ToolKindRead, "get_workbook_structure", map[string]interface{}{
|
||
"excel_id": token,
|
||
})
|
||
if err != nil {
|
||
return "", err
|
||
}
|
||
m, ok := out.(map[string]interface{})
|
||
if !ok {
|
||
return "", errs.NewInternalError(errs.SubtypeInvalidResponse, "get_workbook_structure returned non-object output")
|
||
}
|
||
sheets, _ := m["sheets"].([]interface{})
|
||
bestID := ""
|
||
bestIdx := -1
|
||
for _, raw := range sheets {
|
||
sm, ok := raw.(map[string]interface{})
|
||
if !ok {
|
||
continue
|
||
}
|
||
id, _ := sm["sheet_id"].(string)
|
||
if id == "" {
|
||
continue
|
||
}
|
||
idx, ok := util.ToFloat64(sm["index"])
|
||
if !ok {
|
||
// No index field — fall back to first encountered sheet.
|
||
if bestID == "" {
|
||
bestID = id
|
||
}
|
||
continue
|
||
}
|
||
if bestIdx < 0 || int(idx) < bestIdx {
|
||
bestIdx = int(idx)
|
||
bestID = id
|
||
}
|
||
}
|
||
if bestID == "" {
|
||
return "", errs.NewInternalError(errs.SubtypeInvalidResponse, "get_workbook_structure returned no sheets")
|
||
}
|
||
return bestID, nil
|
||
}
|
||
|
||
// ─── +workbook-import (reuses drive import core, cli_status: cli-only) ──
|
||
//
|
||
// Imports a local xlsx/xls/csv file as a brand-new spreadsheet. The full
|
||
// upload → create-task → poll flow is the shared drive import core
|
||
// (drive.RunImport); this shortcut only pins the target type to "sheet" and
|
||
// omits the bitable-only --target-token. Symmetric with +workbook-export.
|
||
// Not exposed as an MCP tool.
|
||
|
||
// WorkbookImport imports a local spreadsheet file as a new Feishu spreadsheet
|
||
// by delegating to the shared drive import core with type fixed to "sheet".
|
||
var WorkbookImport = common.Shortcut{
|
||
Service: "sheets",
|
||
Command: "+workbook-import",
|
||
Description: "Import a local xlsx/xls/csv file as a new spreadsheet (async + poll). Reuses the drive import core with type fixed to sheet.",
|
||
Risk: "write",
|
||
Scopes: []string{"docs:document.media:upload", "docs:document:import"},
|
||
AuthTypes: []string{"user", "bot"},
|
||
HasFormat: true,
|
||
Flags: flagsFor("+workbook-import"),
|
||
Validate: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
return drive.ValidateImport(workbookImportParams(runtime))
|
||
},
|
||
DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI {
|
||
return drive.PlanImportDryRun(runtime, workbookImportParams(runtime))
|
||
},
|
||
Execute: func(ctx context.Context, runtime *common.RuntimeContext) error {
|
||
return drive.RunImport(ctx, runtime, workbookImportParams(runtime))
|
||
},
|
||
}
|
||
|
||
// workbookImportParams builds the drive import request for +workbook-import,
|
||
// pinning DocType to "sheet". The bitable-only --target-token is intentionally
|
||
// not exposed here — use drive +import for non-sheet import targets.
|
||
func workbookImportParams(runtime *common.RuntimeContext) drive.ImportParams {
|
||
return drive.ImportParams{
|
||
File: runtime.Str("file"),
|
||
DocType: "sheet",
|
||
FolderToken: runtime.Str("folder-token"),
|
||
Name: runtime.Str("name"),
|
||
}
|
||
}
|