-
Notifications
You must be signed in to change notification settings - Fork 95
feat(sheets): add sheets.appendRow write tool #342
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
base: main
Are you sure you want to change the base?
Changes from all commits
7ecb606
4760bdb
7f0efe4
e9ec351
85e4604
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,6 +194,82 @@ export class SheetsService { | |
| } | ||
| }; | ||
|
|
||
| public appendRows = async ({ | ||
| spreadsheetId, | ||
| range, | ||
| values, | ||
| }: { | ||
| spreadsheetId: string; | ||
| range: string; | ||
| values: (string | number | boolean | null)[][]; | ||
| }) => { | ||
| logToFile( | ||
| `[SheetsService] Starting appendRows for spreadsheet: ${spreadsheetId}, range: ${range}`, | ||
| ); | ||
| try { | ||
| const id = extractDocId(spreadsheetId) || spreadsheetId; | ||
|
|
||
| const sheets = await this.getSheetsClient(); | ||
| const response = await sheets.spreadsheets.values.append({ | ||
| spreadsheetId: id, | ||
| range: range, | ||
| valueInputOption: 'USER_ENTERED', | ||
| requestBody: { | ||
| values: values, | ||
| }, | ||
| }); | ||
|
|
||
| const updates = response.data.updates; | ||
| if (!updates) { | ||
| logToFile( | ||
| `[SheetsService] appendRows returned no update metadata for: ${id}`, | ||
| ); | ||
| return { | ||
| isError: true, | ||
| content: [ | ||
| { | ||
| type: 'text' as const, | ||
| text: JSON.stringify({ | ||
| error: | ||
| 'Append returned no update information; rows may not have been written.', | ||
| }), | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| logToFile(`[SheetsService] Finished appendRows for spreadsheet: ${id}`); | ||
| return { | ||
| content: [ | ||
| { | ||
| type: 'text' as const, | ||
| text: JSON.stringify({ | ||
| updatedRange: updates.updatedRange, | ||
| updatedRows: updates.updatedRows, | ||
| updatedColumns: updates.updatedColumns, | ||
| updatedCells: updates.updatedCells, | ||
| }), | ||
| }, | ||
| ], | ||
| }; | ||
| } catch (error) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This error result isn't flagged with return {
isError: true,
content: [{ type: 'text' as const, text: JSON.stringify({ error: errorMessage }) }],
};
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
| const errorMessage = | ||
| error instanceof Error ? error.message : String(error); | ||
| logToFile( | ||
| `[SheetsService] Error during sheets.appendRows: ${errorMessage}`, | ||
| ); | ||
| return { | ||
| isError: true, | ||
| content: [ | ||
| { | ||
| type: 'text' as const, | ||
| text: JSON.stringify({ error: errorMessage }), | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| }; | ||
|
|
||
| public getMetadata = async ({ spreadsheetId }: { spreadsheetId: string }) => { | ||
| logToFile( | ||
| `[SheetsService] Starting getMetadata for spreadsheet: ${spreadsheetId}`, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
values: []currently passes validation and forwards an emptyrequestBody.valuesto the API — a wasted no-op call. A.min(1)guard rejects it at the tool boundary with a clear message:z.array(z.array(z.union([...]))).min(1). (Don't enforce rectangular rows — ragged rows are legal in Sheets, where shorter rows just leave trailing cells untouched.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
.min(1)on the outer array. Left inner rows unconstrained as suggested — ragged rows are valid in Sheets.