-
Notifications
You must be signed in to change notification settings - Fork 589
feat: add sheets +write-image shortcut #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // Copyright (c) 2026 Lark Technologies Pte. Ltd. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package sheets | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/larksuite/cli/internal/output" | ||
| "github.com/larksuite/cli/internal/validate" | ||
| "github.com/larksuite/cli/internal/vfs" | ||
| "github.com/larksuite/cli/shortcuts/common" | ||
| ) | ||
|
|
||
| var SheetWriteImage = common.Shortcut{ | ||
| Service: "sheets", | ||
| Command: "+write-image", | ||
| Description: "Write an image into a spreadsheet cell", | ||
| Risk: "write", | ||
| Scopes: []string{"sheets:spreadsheet:write_only", "sheets:spreadsheet:read"}, | ||
| AuthTypes: []string{"user", "bot"}, | ||
| Flags: []common.Flag{ | ||
| {Name: "url", Desc: "spreadsheet URL"}, | ||
| {Name: "spreadsheet-token", Desc: "spreadsheet token"}, | ||
| {Name: "sheet-id", Desc: "sheet ID"}, | ||
| {Name: "range", Desc: "target cell (e.g. A1 or <sheetId>!A1). Start and end cell must be the same", Required: true}, | ||
| {Name: "image", Desc: "local image file path (supported formats: PNG, JPEG, JPG, GIF, BMP, JFIF, EXIF, TIFF, BPG, HEIC)", Required: true}, | ||
| {Name: "name", Desc: "image file name with extension (defaults to the basename of --image)"}, | ||
| }, | ||
| Validate: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| token := runtime.Str("spreadsheet-token") | ||
| if runtime.Str("url") != "" { | ||
| token = extractSpreadsheetToken(runtime.Str("url")) | ||
| } | ||
| if token == "" { | ||
| return common.FlagErrorf("specify --url or --spreadsheet-token") | ||
| } | ||
| if err := validateSheetRangeInput(runtime.Str("sheet-id"), runtime.Str("range")); err != nil { | ||
| return err | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if err := validateSingleCellRange(runtime.Str("range")); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
|
fangshuyu-768 marked this conversation as resolved.
|
||
| }, | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| DryRun: func(ctx context.Context, runtime *common.RuntimeContext) *common.DryRunAPI { | ||
| token := runtime.Str("spreadsheet-token") | ||
| if runtime.Str("url") != "" { | ||
| token = extractSpreadsheetToken(runtime.Str("url")) | ||
| } | ||
| pointRange := normalizePointRange(runtime.Str("sheet-id"), runtime.Str("range")) | ||
| imageName := runtime.Str("name") | ||
| if imageName == "" { | ||
| imageName = filepath.Base(runtime.Str("image")) | ||
| } | ||
| return common.NewDryRunAPI(). | ||
| Desc("JSON upload with inline image bytes"). | ||
| POST("/open-apis/sheets/v2/spreadsheets/:token/values_image"). | ||
| Body(map[string]interface{}{ | ||
| "range": pointRange, | ||
| "image": fmt.Sprintf("<binary: %s>", runtime.Str("image")), | ||
| "name": imageName, | ||
| }). | ||
| Set("token", token) | ||
| }, | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| Execute: func(ctx context.Context, runtime *common.RuntimeContext) error { | ||
| token := runtime.Str("spreadsheet-token") | ||
| if runtime.Str("url") != "" { | ||
| token = extractSpreadsheetToken(runtime.Str("url")) | ||
| } | ||
|
|
||
| // Resolve the target cell range (--range is required). | ||
| pointRange := normalizePointRange(runtime.Str("sheet-id"), runtime.Str("range")) | ||
|
|
||
| // Resolve image file. | ||
| imagePath := runtime.Str("image") | ||
| safePath, err := validate.SafeInputPath(imagePath) | ||
| if err != nil { | ||
| return output.ErrValidation("unsafe image path: %s", err) | ||
| } | ||
| stat, err := vfs.Stat(safePath) | ||
| if err != nil { | ||
| return output.ErrValidation("image file not found: %s", imagePath) | ||
| } | ||
| if !stat.Mode().IsRegular() { | ||
| return output.ErrValidation("image must be a regular file: %s", imagePath) | ||
| } | ||
| const maxImageSize int64 = 20 * 1024 * 1024 // 20 MB | ||
| if stat.Size() > maxImageSize { | ||
| return output.ErrValidation("image %.1fMB exceeds 20MB limit", float64(stat.Size())/1024/1024) | ||
| } | ||
|
|
||
| imageBytes, err := vfs.ReadFile(safePath) | ||
| if err != nil { | ||
| return output.ErrValidation("cannot read image file: %s", err) | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| imageName := runtime.Str("name") | ||
| if imageName == "" { | ||
| imageName = filepath.Base(imagePath) | ||
| } | ||
|
|
||
| fmt.Fprintf(runtime.IO().ErrOut, "Writing image: %s (%d bytes) → %s\n", imageName, stat.Size(), pointRange) | ||
|
|
||
| // The sheets v2 values_image API expects a JSON body with the image | ||
| // as an inline byte array, not multipart/form-data. | ||
| data, err := runtime.CallAPI("POST", fmt.Sprintf("/open-apis/sheets/v2/spreadsheets/%s/values_image", validate.EncodePathSegment(token)), nil, map[string]interface{}{ | ||
| "range": pointRange, | ||
| "image": imageBytes, | ||
| "name": imageName, | ||
| }) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| runtime.Out(data, nil) | ||
| return nil | ||
| }, | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.