mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-06 03:00:16 -04:00
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
This commit is contained in:
@@ -107,8 +107,6 @@ export const appendTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsAppendRe
|
||||
.replace(/\n/g, '\\n')
|
||||
.replace(/\r/g, '\\r')
|
||||
.replace(/\t/g, '\\t')
|
||||
// Fix any double backslashes that might occur
|
||||
.replace(/\\\\/g, '\\')
|
||||
|
||||
// Try to parse again with sanitized input
|
||||
processedValues = JSON.parse(sanitizedInput)
|
||||
|
||||
@@ -82,16 +82,22 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
|
||||
body: (params) => {
|
||||
let processedValues: any = params.values || []
|
||||
|
||||
// Handle array of objects
|
||||
// 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])
|
||||
) {
|
||||
// It's an array of objects
|
||||
|
||||
// First, extract all unique keys from all objects to create headers
|
||||
const allKeys = new Set<string>()
|
||||
processedValues.forEach((obj: any) => {
|
||||
if (obj && typeof obj === 'object') {
|
||||
@@ -100,15 +106,12 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
|
||||
})
|
||||
const headers = Array.from(allKeys)
|
||||
|
||||
// Then create rows with object values in the order of headers
|
||||
const rows = processedValues.map((obj: any) => {
|
||||
if (!obj || typeof obj !== 'object') {
|
||||
// Handle non-object items by creating an array with empty values
|
||||
return Array(headers.length).fill('')
|
||||
}
|
||||
return headers.map((key) => {
|
||||
const value = obj[key]
|
||||
// Handle nested objects/arrays by converting to JSON string
|
||||
if (value !== null && typeof value === 'object') {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
@@ -116,7 +119,6 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
|
||||
@@ -124,12 +126,9 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
|
||||
majorDimension: params.majorDimension || 'ROWS',
|
||||
values: processedValues,
|
||||
}
|
||||
|
||||
// Only include range if it's provided
|
||||
if (params.range) {
|
||||
body.range = params.range
|
||||
}
|
||||
|
||||
return body
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user