AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents
TypeScript ai-agentai-agent-toolsai-agentsai-agents-frameworkmcpmcp-servermcp-toolsmcpsn8n-alternativeno-code-automationworkflowworkflow-automationworkflows
import {
Property,
Store,
StoreScope,
createAction,
} from '@activepieces/pieces-framework';
import { googleSheetsAuth } from '../common/common';
import {
areSheetIdsValid,
GoogleSheetsAuthValue,
googleSheetsCommon,
mapRowsToHeaderNames,
} from '../common/common';
import { isNil } from '@activepieces/pieces-framework';
import { HttpError } from '@activepieces/pieces-common';
import * as z from 'zod/mini'
import { propsValidation } from '@activepieces/pieces-common';
import { getWorkSheetGridSize } from '../triggers/helpers';
import { commonProps } from '../common/props';
import { getNextRowsActionOutputSchema } from '../output-schemas';
async function getRows(
store: Store,
auth: GoogleSheetsAuthValue,
spreadsheetId: string,
sheetId: number,
memKey: string,
groupSize: number,
startRow: number,
headerRow: number,
useHeaderNames: boolean,
testing: boolean
) {
const sheetGridRange = await getWorkSheetGridSize(auth,spreadsheetId,sheetId);
const existingGridRowCount = sheetGridRange.rowCount ??0;
const memVal = await store.get(memKey, StoreScope.FLOW);
let startingRow;
if (isNil(memVal) || memVal === '') startingRow = startRow || 1;
else {
startingRow = parseInt(memVal as string);
if (isNaN(startingRow)) {
throw Error(
'The value stored in memory key : ' +
memKey +
' is ' +
memVal +
' and it is not a number'
);
}
}
if (startingRow < 1)
throw Error('Starting row : ' + startingRow + ' is less than 1' + memVal);
if(startingRow > existingGridRowCount-1){
return [];
}
const endRow = Math.min(startingRow + groupSize,existingGridRowCount);
if (testing == false) await store.put(memKey, endRow, StoreScope.FLOW);
const row = await googleSheetsCommon.getGoogleSheetRows({
auth,
sheetId: sheetId,
spreadsheetId: spreadsheetId,
rowIndex_s: startingRow,
rowIndex_e: endRow - 1,
headerRow: headerRow,
});
if (row.length == 0) {
const allRows = await googleSheetsCommon.getGoogleSheetRows({
spreadsheetId: spreadsheetId,
auth,
sheetId: sheetId,
rowIndex_s: undefined,
rowIndex_e: undefined,
headerRow: headerRow,
});
const lastRow = allRows.length + 1;
if (testing == false) await store.put(memKey, lastRow, StoreScope.FLOW);
}
const finalRows = await mapRowsToHeaderNames(
row,
useHeaderNames,
spreadsheetId,
sheetId,
headerRow,
auth,
);
return finalRows;
}
const notes = `
**Notes:**
- The memory key remembers which row was processed last, and is reused on the following runs.
- Republishing the flow **keeps** the memory key value. To start over, **change** the memory key.
`
export const getRowsAction = createAction({
auth: googleSheetsAuth,
name: 'get_next_rows',
classification: 'READ',
description: 'Read the next batch of rows from a worksheet, continuing where the last run left off.',
audience: 'human',
aiMetadata: {
description:
'Reads the next batch of rows from a worksheet, advancing a cursor stored under a memory key so successive calls walk through the sheet without reprocessing rows. Use to iterate a sheet in chunks across runs. Not idempotent — each non-test call moves the stored cursor forward and returns a different batch.',
idempotent: false,
},
displayName: 'Get Next Rows',
propertyGroups: [
{
key: 'spreadsheet',
display: 'section',
label: 'Spreadsheet',
icon: 'file',
props: ['spreadsheetId', 'sheetId'],
},
{
key: 'target_rows',
display: 'section',
label: 'Rows',
icon: 'markdown',
props: ['startRow', 'groupSize', 'headerRow'],
},
{
key: 'memory',
display: 'section',
label: 'Memory',
icon: 'inbox',
props: ['markdown', 'memKey'],
},
],
props: {
...commonProps,
startRow: Property.Number({
displayName: 'Start Row',
description: 'The row to start reading from on the first run.',
required: true,
defaultValue: 1,
}),
headerRow: Property.Number({
displayName: 'Header Row',
description: 'The row that contains the column names.',
required: true,
defaultValue: 1,
}),
useHeaderNames: Property.Checkbox({
displayName: 'Use Column Names',
description: 'Key each row by column name instead of A, B, C.',
required: false,
defaultValue: false,
advanced: true,
}),
markdown: Property.MarkDown({
value: notes,
}),
memKey: Property.ShortText({
displayName: 'Memory Key',
description: 'The key used to store the current row number in memory',
required: true,
defaultValue: 'row_number',
}),
groupSize: Property.Number({
displayName: 'Group Size',
description: 'The number of rows to get',
required: true,
defaultValue: 1,
}),
},
outputSchema: getNextRowsActionOutputSchema,
async run({ store, auth, propsValue }) {
const { startRow, groupSize, memKey, headerRow, spreadsheetId, sheetId, useHeaderNames} = propsValue;
if (!areSheetIdsValid(spreadsheetId, sheetId)) {
throw new Error('Please select a spreadsheet and sheet first.');
}
await propsValidation.validateZod(propsValue, {
startRow: z.number().check(z.minimum(1)),
groupSize: z.number().check(z.minimum(1)),
});
try {
return await getRows(
store,
auth,
spreadsheetId as string,
sheetId as number,
memKey,
groupSize,
startRow,
headerRow,
useHeaderNames as boolean,
false
);
} catch (error) {
if (error instanceof HttpError) {
const errorBody = error.response.body as any;
throw new Error(errorBody['error']['message']);
}
throw error;
}
},
async test({ store, auth, propsValue }) {
const { startRow, groupSize, memKey, headerRow, spreadsheetId, sheetId, useHeaderNames} = propsValue;
if (!areSheetIdsValid(spreadsheetId, sheetId)) {
throw new Error('Please select a spreadsheet and sheet first.');
}
try {
return await getRows(
store,
auth,
spreadsheetId as string,
sheetId as number,
memKey,
groupSize,
startRow,
headerRow,
useHeaderNames as boolean,
true
);
} catch (error) {
if (error instanceof HttpError) {
const errorBody = error.response.body as any;
throw new Error(errorBody['error']['message']);
}
throw error;
}
},
});