-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathupdate.ts
More file actions
173 lines (154 loc) · 5.23 KB
/
update.ts
File metadata and controls
173 lines (154 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import type {
GoogleSheetsToolParams,
GoogleSheetsUpdateResponse,
} from '@/tools/google_sheets/types'
import type { ToolConfig } from '@/tools/types'
export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateResponse> = {
id: 'google_sheets_update',
name: 'Update Google Sheets',
description: 'Update data in a Google Sheets spreadsheet',
version: '1.0',
oauth: {
required: true,
provider: 'google-sheets',
},
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'The access token for the Google Sheets API',
},
spreadsheetId: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'The ID of the spreadsheet to update',
},
range: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'The range of cells to update',
},
values: {
type: 'array',
required: true,
visibility: 'user-or-llm',
description: 'The data to update in the spreadsheet',
},
valueInputOption: {
type: 'string',
required: false,
visibility: 'hidden',
description: 'The format of the data to update',
},
includeValuesInResponse: {
type: 'boolean',
required: false,
visibility: 'hidden',
description: 'Whether to include the updated values in the response',
},
},
request: {
url: (params) => {
// If range is not provided, use a default range for the first sheet, second row to preserve headers
const range = params.range || 'Sheet1!A2'
const url = new URL(
`https://sheets.googleapis.com/v4/spreadsheets/${params.spreadsheetId}/values/${encodeURIComponent(range)}`
)
// Default to USER_ENTERED if not specified
const valueInputOption = params.valueInputOption || 'USER_ENTERED'
url.searchParams.append('valueInputOption', valueInputOption)
if (params.includeValuesInResponse) {
url.searchParams.append('includeValuesInResponse', 'true')
}
return url.toString()
},
method: 'PUT',
headers: (params) => ({
Authorization: `Bearer ${params.accessToken}`,
'Content-Type': 'application/json',
}),
body: (params) => {
let processedValues: any = params.values || []
// Minimal shape enforcement: Google requires a 2D array
if (!Array.isArray(processedValues)) {
processedValues = [[processedValues]]
} else if (!processedValues.every((item: any) => Array.isArray(item))) {
processedValues = (processedValues as any[]).map((row: any) =>
Array.isArray(row) ? row : [row]
)
}
// Handle array of objects (existing behavior)
if (
Array.isArray(processedValues) &&
processedValues.length > 0 &&
typeof processedValues[0] === 'object' &&
!Array.isArray(processedValues[0])
) {
const allKeys = new Set<string>()
processedValues.forEach((obj: any) => {
if (obj && typeof obj === 'object') {
Object.keys(obj).forEach((key) => allKeys.add(key))
}
})
const headers = Array.from(allKeys)
const rows = processedValues.map((obj: any) => {
if (!obj || typeof obj !== 'object') {
return Array(headers.length).fill('')
}
return headers.map((key) => {
const value = obj[key]
if (value !== null && typeof value === 'object') {
return JSON.stringify(value)
}
return value === undefined ? '' : value
})
})
processedValues = [headers, ...rows]
}
const body: Record<string, any> = {
majorDimension: params.majorDimension || 'ROWS',
values: processedValues,
}
if (params.range) {
body.range = params.range
}
return body
},
},
transformResponse: async (response: Response) => {
const data = await response.json()
// Extract spreadsheet ID from the URL (guard if url is missing)
const urlParts = typeof response.url === 'string' ? response.url.split('/spreadsheets/') : []
const spreadsheetId = urlParts[1]?.split('/')[0] || ''
// Create a simple metadata object with just the ID and URL
const metadata = {
spreadsheetId,
properties: {},
spreadsheetUrl: `https://docs.google.com/spreadsheets/d/${spreadsheetId}`,
}
const result = {
success: true,
output: {
updatedRange: data.updatedRange,
updatedRows: data.updatedRows,
updatedColumns: data.updatedColumns,
updatedCells: data.updatedCells,
metadata: {
spreadsheetId: metadata.spreadsheetId,
spreadsheetUrl: metadata.spreadsheetUrl,
},
},
}
return result
},
outputs: {
updatedRange: { type: 'string', description: 'Range of cells that were updated' },
updatedRows: { type: 'number', description: 'Number of rows updated' },
updatedColumns: { type: 'number', description: 'Number of columns updated' },
updatedCells: { type: 'number', description: 'Number of cells updated' },
metadata: { type: 'json', description: 'Spreadsheet metadata including ID and URL' },
},
}