Skip to content

Commit 3205f59

Browse files
committed
improvement(google-sheets): parse input items
1 parent 8e0c72a commit 3205f59

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

sim/tools/google_sheets/append.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,47 @@ export const appendTool: ToolConfig<GoogleSheetsToolParams, GoogleSheetsAppendRe
5050
'Content-Type': 'application/json',
5151
}),
5252
body: (params) => {
53+
let processedValues: any = params.values || []
54+
55+
// Handle case where values might be a string (potentially JSON string)
56+
if (typeof processedValues === 'string') {
57+
try {
58+
// Try to parse it as JSON
59+
processedValues = JSON.parse(processedValues)
60+
} catch (error) {
61+
// If the input contains literal newlines causing JSON parse to fail,
62+
// try a more robust approach
63+
try {
64+
// Replace literal newlines with escaped newlines for JSON parsing
65+
const sanitizedInput = (processedValues as string)
66+
.replace(/\n/g, '\\n')
67+
.replace(/\r/g, '\\r')
68+
.replace(/\t/g, '\\t')
69+
// Fix any double backslashes that might occur
70+
.replace(/\\\\/g, '\\')
71+
72+
// Try to parse again with sanitized input
73+
processedValues = JSON.parse(sanitizedInput)
74+
} catch (secondError) {
75+
// If all parsing attempts fail, wrap as a single cell value
76+
processedValues = [[processedValues]]
77+
}
78+
}
79+
}
80+
81+
// Validate that processedValues is actually an array of arrays
82+
if (!Array.isArray(processedValues)) {
83+
processedValues = [[String(processedValues)]]
84+
} else if (!processedValues.every((item: any) => Array.isArray(item))) {
85+
// If it's an array but not all elements are arrays, wrap each element
86+
processedValues = (processedValues as any[]).map((row: any) =>
87+
Array.isArray(row) ? row : [String(row)]
88+
)
89+
}
90+
5391
const body: Record<string, any> = {
5492
majorDimension: params.majorDimension || 'ROWS',
55-
values: params.values || [],
93+
values: processedValues,
5694
}
5795

5896
// Only include range if it's provided

0 commit comments

Comments
 (0)