mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
fix(google-sheets): update google sheets tools to take in both an array of objects and an array of arrays
This commit is contained in:
@@ -82,7 +82,7 @@ export const GoogleSheetsBlock: BlockConfig<GoogleSheetsResponse> = {
|
||||
title: 'Values',
|
||||
type: 'long-input',
|
||||
layout: 'full',
|
||||
placeholder: 'Enter values as JSON array of arrays (e.g., [["A1", "B1"], ["A2", "B2"]])',
|
||||
placeholder: 'Enter values as JSON array of arrays (e.g., [["A1", "B1"], ["A2", "B2"]]) or an array of objects (e.g., [{"name":"John", "age":30}, {"name":"Jane", "age":25}])',
|
||||
condition: { field: 'operation', value: 'write' },
|
||||
},
|
||||
{
|
||||
@@ -102,7 +102,7 @@ export const GoogleSheetsBlock: BlockConfig<GoogleSheetsResponse> = {
|
||||
title: 'Values',
|
||||
type: 'long-input',
|
||||
layout: 'full',
|
||||
placeholder: 'Enter values as JSON array of arrays (e.g., [["A1", "B1"], ["A2", "B2"]])',
|
||||
placeholder: 'Enter values as JSON array of arrays (e.g., [["A1", "B1"], ["A2", "B2"]]) or an array of objects (e.g., [{"name":"John", "age":30}, {"name":"Jane", "age":25}])',
|
||||
condition: { field: 'operation', value: 'update' },
|
||||
},
|
||||
{
|
||||
@@ -122,7 +122,7 @@ export const GoogleSheetsBlock: BlockConfig<GoogleSheetsResponse> = {
|
||||
title: 'Values',
|
||||
type: 'long-input',
|
||||
layout: 'full',
|
||||
placeholder: 'Enter values as JSON array of arrays (e.g., [["A1", "B1"], ["A2", "B2"]])',
|
||||
placeholder: 'Enter values as JSON array of arrays (e.g., [["A1", "B1"], ["A2", "B2"]]) or an array of objects (e.g., [{"name":"John", "age":30}, {"name":"Jane", "age":25}])',
|
||||
condition: { field: 'operation', value: 'append' },
|
||||
},
|
||||
{
|
||||
|
||||
@@ -78,8 +78,40 @@ export const appendTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsAppendRe
|
||||
}
|
||||
}
|
||||
|
||||
// Validate that processedValues is actually an array of arrays
|
||||
if (!Array.isArray(processedValues)) {
|
||||
// New logic to handle array of objects
|
||||
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') {
|
||||
Object.keys(obj).forEach(key => allKeys.add(key))
|
||||
}
|
||||
})
|
||||
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)
|
||||
}
|
||||
return value === undefined ? '' : value
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
// Continue with existing logic for other array types
|
||||
else if (!Array.isArray(processedValues)) {
|
||||
processedValues = [[String(processedValues)]]
|
||||
} else if (!processedValues.every((item: any) => Array.isArray(item))) {
|
||||
// If it's an array but not all elements are arrays, wrap each element
|
||||
|
||||
@@ -44,9 +44,44 @@ export const updateTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsUpdateRe
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: (params) => {
|
||||
let processedValues: any = params.values || []
|
||||
|
||||
// Handle array of objects
|
||||
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') {
|
||||
Object.keys(obj).forEach(key => allKeys.add(key))
|
||||
}
|
||||
})
|
||||
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)
|
||||
}
|
||||
return value === undefined ? '' : value
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
|
||||
const body: Record<string, any> = {
|
||||
majorDimension: params.majorDimension || 'ROWS',
|
||||
values: params.values || [],
|
||||
values: processedValues,
|
||||
}
|
||||
|
||||
// Only include range if it's provided
|
||||
|
||||
@@ -44,9 +44,44 @@ export const writeTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsWriteResp
|
||||
'Content-Type': 'application/json',
|
||||
}),
|
||||
body: (params) => {
|
||||
let processedValues: any = params.values || []
|
||||
|
||||
// Handle array of objects
|
||||
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') {
|
||||
Object.keys(obj).forEach(key => allKeys.add(key))
|
||||
}
|
||||
})
|
||||
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)
|
||||
}
|
||||
return value === undefined ? '' : value
|
||||
})
|
||||
})
|
||||
|
||||
// Add headers as the first row, then add data rows
|
||||
processedValues = [headers, ...rows]
|
||||
}
|
||||
|
||||
const body: Record<string, any> = {
|
||||
majorDimension: params.majorDimension || 'ROWS',
|
||||
values: params.values || [],
|
||||
values: processedValues,
|
||||
}
|
||||
|
||||
// Only include range if it's provided
|
||||
|
||||
Reference in New Issue
Block a user