@@ -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