Skip to content

Commit 9866c0b

Browse files
committed
fix(google-sheets): correct filter metadata for missing column and header-only sheets
- matchedRows is now 0 (not totalRows) when the filter column is not found, so it no longer contradicts applied=false / columnFound=false - columnFound now reflects an actual header lookup for empty/header-only sheets instead of being hardcoded true - add tests covering header-only and empty sheets with present/absent columns
1 parent 1689dca commit 9866c0b

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

apps/sim/tools/google_sheets/filter.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,39 @@ describe('filterSheetRows', () => {
172172
})
173173
expect(result.applied).toBe(false)
174174
expect(result.columnFound).toBe(false)
175+
expect(result.matchedRows).toBe(0)
175176
expect(result.values).toBe(VALUES)
176177
expect(result.totalRows).toBe(3)
177178
})
178179

179-
it('handles a header-only sheet without error', () => {
180+
it('handles a header-only sheet and reports the column as found when it exists', () => {
180181
const headerOnly: unknown[][] = [['Name', 'Status']]
181182
const result = filterSheetRows(headerOnly, { filterColumn: 'Status', filterValue: 'Active' })
182183
expect(result.applied).toBe(false)
184+
expect(result.columnFound).toBe(true)
185+
expect(result.matchedRows).toBe(0)
183186
expect(result.totalRows).toBe(0)
184187
expect(result.values).toBe(headerOnly)
185188
})
186189

190+
it('reports columnFound=false for a header-only sheet when the column is absent', () => {
191+
const headerOnly: unknown[][] = [['Name', 'Status']]
192+
const result = filterSheetRows(headerOnly, { filterColumn: 'Nonexistent', filterValue: 'x' })
193+
expect(result.applied).toBe(false)
194+
expect(result.columnFound).toBe(false)
195+
expect(result.matchedRows).toBe(0)
196+
expect(result.values).toBe(headerOnly)
197+
})
198+
199+
it('reports columnFound=false for an empty values array', () => {
200+
const empty: unknown[][] = []
201+
const result = filterSheetRows(empty, { filterColumn: 'Status', filterValue: 'Active' })
202+
expect(result.applied).toBe(false)
203+
expect(result.columnFound).toBe(false)
204+
expect(result.matchedRows).toBe(0)
205+
expect(result.totalRows).toBe(0)
206+
})
207+
187208
it('treats missing cells as empty strings', () => {
188209
const sparse: unknown[][] = [['Name', 'Status'], ['Alice'], ['Bob', 'Active']]
189210
const result = filterSheetRows(sparse, {

apps/sim/tools/google_sheets/filter.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,22 @@ export function filterSheetRows(
119119
return { values, applied: false, columnFound: true, matchedRows: totalRows, totalRows }
120120
}
121121

122-
if (values.length <= 1) {
123-
return { values, applied: false, columnFound: true, matchedRows: 0, totalRows: 0 }
124-
}
125-
126-
const headers = values[0]
122+
const headers = values[0] ?? []
127123
const normalizedColumn = filterColumn.trim().toLowerCase()
128124
const columnIndex = headers.findIndex(
129125
(header) => String(header).trim().toLowerCase() === normalizedColumn
130126
)
127+
const columnFound = columnIndex !== -1
128+
129+
// No data rows to evaluate (empty or header-only sheet): nothing matched, but
130+
// still report whether the requested column actually exists in the header.
131+
if (values.length <= 1) {
132+
return { values, applied: false, columnFound, matchedRows: 0, totalRows: 0 }
133+
}
131134

132-
if (columnIndex === -1) {
133-
return { values, applied: false, columnFound: false, matchedRows: totalRows, totalRows }
135+
// Column not found: leave rows untouched and report zero matches, not totalRows.
136+
if (!columnFound) {
137+
return { values, applied: false, columnFound: false, matchedRows: 0, totalRows }
134138
}
135139

136140
const matchType = filterMatchType ?? DEFAULT_MATCH_TYPE

apps/sim/tools/google_sheets/read.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ describe('readV2Tool.transformResponse', () => {
8181
expect(result.output.values).toEqual(SHEET_DATA.values)
8282
expect(result.output.filter?.columnFound).toBe(false)
8383
expect(result.output.filter?.applied).toBe(false)
84+
expect(result.output.filter?.matchedRows).toBe(0)
8485
})
8586

8687
it('handles a response with no values array', async () => {

0 commit comments

Comments
 (0)