Skip to content

Commit 9a6cc78

Browse files
authored
fix(sheets): fixed google sheets update (#1311)
Google sheets append was sending an invalid shape to the google sheets api. This PR fixes this by having similar logic to what is in append.ts
1 parent cff0a87 commit 9a6cc78

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

apps/sim/tools/google_sheets/append.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ export const appendTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsAppendRe
107107
.replace(/\n/g, '\\n')
108108
.replace(/\r/g, '\\r')
109109
.replace(/\t/g, '\\t')
110-
// Fix any double backslashes that might occur
111-
.replace(/\\\\/g, '\\')
112110

113111
// Try to parse again with sanitized input
114112
processedValues = JSON.parse(sanitizedInput)

apps/sim/tools/google_sheets/update.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,22 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
8282
body: (params) => {
8383
let processedValues: any = params.values || []
8484

85-
// Handle array of objects
85+
// Minimal shape enforcement: Google requires a 2D array
86+
if (!Array.isArray(processedValues)) {
87+
processedValues = [[processedValues]]
88+
} else if (!processedValues.every((item: any) => Array.isArray(item))) {
89+
processedValues = (processedValues as any[]).map((row: any) =>
90+
Array.isArray(row) ? row : [row]
91+
)
92+
}
93+
94+
// Handle array of objects (existing behavior)
8695
if (
8796
Array.isArray(processedValues) &&
8897
processedValues.length > 0 &&
8998
typeof processedValues[0] === 'object' &&
9099
!Array.isArray(processedValues[0])
91100
) {
92-
// It's an array of objects
93-
94-
// First, extract all unique keys from all objects to create headers
95101
const allKeys = new Set<string>()
96102
processedValues.forEach((obj: any) => {
97103
if (obj && typeof obj === 'object') {
@@ -100,36 +106,29 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
100106
})
101107
const headers = Array.from(allKeys)
102108

103-
// Then create rows with object values in the order of headers
104109
const rows = processedValues.map((obj: any) => {
105110
if (!obj || typeof obj !== 'object') {
106-
// Handle non-object items by creating an array with empty values
107111
return Array(headers.length).fill('')
108112
}
109113
return headers.map((key) => {
110114
const value = obj[key]
111-
// Handle nested objects/arrays by converting to JSON string
112115
if (value !== null && typeof value === 'object') {
113116
return JSON.stringify(value)
114117
}
115118
return value === undefined ? '' : value
116119
})
117120
})
118121

119-
// Add headers as the first row, then add data rows
120122
processedValues = [headers, ...rows]
121123
}
122124

123125
const body: Record<string, any> = {
124126
majorDimension: params.majorDimension || 'ROWS',
125127
values: processedValues,
126128
}
127-
128-
// Only include range if it's provided
129129
if (params.range) {
130130
body.range = params.range
131131
}
132-
133132
return body
134133
},
135134
},

0 commit comments

Comments
 (0)