diff --git a/apps/builder/src/features/blocks/integrations/googleSheets/components/GoogleSheetsSettings.tsx b/apps/builder/src/features/blocks/integrations/googleSheets/components/GoogleSheetsSettings.tsx index ce6ab0deb..41c065b07 100644 --- a/apps/builder/src/features/blocks/integrations/googleSheets/components/GoogleSheetsSettings.tsx +++ b/apps/builder/src/features/blocks/integrations/googleSheets/components/GoogleSheetsSettings.tsx @@ -21,6 +21,7 @@ import { GoogleSheetsInsertRowOptions, GoogleSheetsOptions, GoogleSheetsUpdateRowOptions, + totalRowsToExtractOptions, } from '@typebot.io/schemas' import React, { useMemo } from 'react' import { isDefined } from '@typebot.io/lib' @@ -184,6 +185,11 @@ const ActionOptions = ({ const handleFilterChange = (filter: GoogleSheetsGetOptions['filter']) => onOptionsChange({ ...options, filter } as GoogleSheetsOptions) + const updateTotalRowsToExtract = ( + totalRowsToExtract: GoogleSheetsGetOptions['totalRowsToExtract'] + ) => + onOptionsChange({ ...options, totalRowsToExtract } as GoogleSheetsOptions) + const UpdatingCellItem = useMemo( () => function Component(props: TableListItemProps) { @@ -273,59 +279,69 @@ const ActionOptions = ({ case GoogleSheetsAction.GET: return ( - {options.referenceCell && ( + + {options.referenceCell && ( + + + + Rows to select + + + + + + + + + )} + {!options.referenceCell && ( + <> + + + + Rows to filter + + + + + + + + + + + )} + - Rows to select + Columns to extract - + initialItems={options.cellsToExtract} + onItemsChange={handleExtractingCellsChange} + Item={ExtractingCellItem} + addLabel="Add a value" /> - )} - {!options.referenceCell && ( - - - - Rows to filter - - - - - - - - - )} - - - - Columns to extract - - - - - - - initialItems={options.cellsToExtract} - onItemsChange={handleExtractingCellsChange} - Item={ExtractingCellItem} - addLabel="Add a value" - /> - - + ) default: diff --git a/apps/viewer/src/features/blocks/integrations/googleSheets/getRow.ts b/apps/viewer/src/features/blocks/integrations/googleSheets/getRow.ts index 06d5804fb..8efa47a64 100644 --- a/apps/viewer/src/features/blocks/integrations/googleSheets/getRow.ts +++ b/apps/viewer/src/features/blocks/integrations/googleSheets/getRow.ts @@ -38,10 +38,13 @@ export const getRow = async ( await doc.loadInfo() const sheet = doc.sheetsById[sheetId] const rows = await sheet.getRows() - const filteredRows = rows.filter((row) => - referenceCell - ? row[referenceCell.column as string] === referenceCell.value - : matchFilter(row, filter as NonNullable) + const filteredRows = getTotalRows( + options.totalRowsToExtract, + rows.filter((row) => + referenceCell + ? row[referenceCell.column as string] === referenceCell.value + : matchFilter(row, filter as NonNullable) + ) ) if (filteredRows.length === 0) { log = { @@ -99,3 +102,20 @@ export const getRow = async ( } return { outgoingEdgeId, logs: log ? [log] : undefined } } + +const getTotalRows = ( + totalRowsToExtract: GoogleSheetsGetOptions['totalRowsToExtract'], + rows: T[] +): T[] => { + switch (totalRowsToExtract) { + case 'All': + case undefined: + return rows + case 'First': + return rows.slice(0, 1) + case 'Last': + return rows.slice(-1) + case 'Random': + return [rows[Math.floor(Math.random() * rows.length)]] + } +} diff --git a/packages/schemas/features/blocks/integrations/googleSheets/schemas.ts b/packages/schemas/features/blocks/integrations/googleSheets/schemas.ts index d0624f259..7763a9aee 100644 --- a/packages/schemas/features/blocks/integrations/googleSheets/schemas.ts +++ b/packages/schemas/features/blocks/integrations/googleSheets/schemas.ts @@ -35,6 +35,13 @@ const initialGoogleSheetsOptionsSchema = googleSheetsOptionsBaseSchema.merge( }) ) +export const totalRowsToExtractOptions = [ + 'All', + 'First', + 'Last', + 'Random', +] as const + const googleSheetsGetOptionsSchema = googleSheetsOptionsBaseSchema.merge( z.object({ action: z.enum([GoogleSheetsAction.GET]), @@ -48,6 +55,7 @@ const googleSheetsGetOptionsSchema = googleSheetsOptionsBaseSchema.merge( }) .optional(), cellsToExtract: z.array(extractingCellSchema), + totalRowsToExtract: z.enum(totalRowsToExtractOptions).optional(), }) )