From d7adc9c6afc1441c36dc91dda9d3834999b68780 Mon Sep 17 00:00:00 2001 From: ccchenhuohuo Date: Mon, 1 Jun 2026 11:27:29 +0800 Subject: [PATCH] fix(sheets): support file input for values --- shortcuts/sheets/lark_sheets_cell_data.go | 4 +- .../sheets/lark_sheets_sheet_ranges_test.go | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/shortcuts/sheets/lark_sheets_cell_data.go b/shortcuts/sheets/lark_sheets_cell_data.go index 8c654716f..f5f12d755 100644 --- a/shortcuts/sheets/lark_sheets_cell_data.go +++ b/shortcuts/sheets/lark_sheets_cell_data.go @@ -106,7 +106,7 @@ var SheetWrite = common.Shortcut{ {Name: "spreadsheet-token", Desc: "spreadsheet token"}, {Name: "range", Desc: "write range (!A1:D10, A1:D10 with --sheet-id, or a single cell like C2)"}, {Name: "sheet-id", Desc: "sheet ID"}, - {Name: "values", Desc: "2D array JSON", Required: true}, + {Name: "values", Desc: "2D array JSON (supports @file and - for stdin)", Required: true, Input: []string{common.File, common.Stdin}}, }, Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { if _, err := validateSheetManageToken(runtime); err != nil { @@ -182,7 +182,7 @@ var SheetAppend = common.Shortcut{ {Name: "spreadsheet-token", Desc: "spreadsheet token"}, {Name: "range", Desc: "append range (!A1:D10, A1:D10 with --sheet-id, or a single cell like C2)"}, {Name: "sheet-id", Desc: "sheet ID"}, - {Name: "values", Desc: "2D array JSON", Required: true}, + {Name: "values", Desc: "2D array JSON (supports @file and - for stdin)", Required: true, Input: []string{common.File, common.Stdin}}, }, Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { if _, err := validateSheetManageToken(runtime); err != nil { diff --git a/shortcuts/sheets/lark_sheets_sheet_ranges_test.go b/shortcuts/sheets/lark_sheets_sheet_ranges_test.go index a4b2a4eb4..cdd170181 100644 --- a/shortcuts/sheets/lark_sheets_sheet_ranges_test.go +++ b/shortcuts/sheets/lark_sheets_sheet_ranges_test.go @@ -6,9 +6,11 @@ package sheets import ( "context" "encoding/json" + "os" "strings" "testing" + "github.com/larksuite/cli/internal/cmdutil" "github.com/larksuite/cli/shortcuts/common" "github.com/spf13/cobra" ) @@ -126,6 +128,61 @@ func TestSheetAppendDryRunNormalizesEscapedSeparator(t *testing.T) { } } +func TestSheetWriteDryRunAcceptsValuesFileInput(t *testing.T) { + dir := t.TempDir() + cmdutil.TestChdir(t, dir) + if err := os.WriteFile("values.json", []byte(`[ + ["2025-01", "产品A"], + ["2025-02", "产品B"] + ]`), 0644); err != nil { + t.Fatal(err) + } + + f, stdout, _, _ := cmdutil.TestFactory(t, sheetsTestConfig()) + err := mountAndRunSheets(t, SheetWrite, []string{ + "+write", + "--spreadsheet-token", "shtTOKEN", + "--range", "sheet1!A1", + "--values", "@values.json", + "--dry-run", + "--as", "user", + }, f, stdout) + if err != nil { + t.Fatalf("dry-run should accept --values @file, got: %v", err) + } + got := stdout.String() + if !strings.Contains(got, `"range": "sheet1!A1:A1"`) { + t.Fatalf("dry-run should include normalized write range: %s", got) + } + if !strings.Contains(got, `"产品B"`) { + t.Fatalf("dry-run should include values loaded from file: %s", got) + } +} + +func TestSheetAppendDryRunAcceptsValuesStdinInput(t *testing.T) { + f, stdout, _, _ := cmdutil.TestFactory(t, sheetsTestConfig()) + f.IOStreams.In = strings.NewReader(`[["2025-01","产品A"]]`) + + err := mountAndRunSheets(t, SheetAppend, []string{ + "+append", + "--spreadsheet-token", "shtTOKEN", + "--range", "sheet1!A1", + "--values", "-", + "--dry-run", + "--as", "user", + }, f, stdout) + if err != nil { + t.Fatalf("dry-run should accept --values - stdin, got: %v", err) + } + got := stdout.String() + if !strings.Contains(got, `"range": "sheet1!A1:A1"`) { + t.Fatalf("dry-run should preserve append point range: %s", got) + } + if !strings.Contains(got, `"产品A"`) { + t.Fatalf("dry-run should include values loaded from stdin: %s", got) + } +} + func TestSheetFindDryRunNormalizesEscapedSeparator(t *testing.T) { t.Parallel()