Skip to content

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

2.1 KB
Raw Blame
import { googleSheetsAuth } from '../common/common';
import { createAction, Property } from '@activepieces/pieces-framework';
import { sheets as googleSheets } from '@googleapis/sheets';
import { includeTeamDrivesProp, spreadsheetIdProp } from '../common/props';
import { createGoogleClient } from '../common/common';
import { findWorksheetActionOutputSchema } from '../output-schemas';

export const findWorksheetAction = createAction({
	auth: googleSheetsAuth,
	name: 'find-worksheet',
	classification: 'SEARCH',
	displayName: 'Find Worksheet(s)',
	description: 'Find worksheets in a spreadsheet by title.',
	audience: 'human',
	aiMetadata: {
		description:
			'Searches the worksheets (tabs) of a given spreadsheet for ones whose title matches a query (exact or contains). Use to resolve a worksheet/sheet id from its tab name before acting on it. Read-only and idempotent.',
		idempotent: true,
	},
	props: {
		includeTeamDrives: includeTeamDrivesProp(),
		spreadsheetId: spreadsheetIdProp('Spreadsheet', 'The spreadsheet to search in.'),
		title: Property.ShortText({
			displayName: 'Title',
			description: 'The worksheet title to search for.',
			required: true,
		}),
		exact_match: Property.Checkbox({
			displayName: 'Exact Match',
			description:
				'Only return worksheets whose title matches exactly.',
			required: false,
			defaultValue: false,
		}),
	},
	outputSchema: findWorksheetActionOutputSchema,
	async run(context) {
		const spreadsheetId = context.propsValue.spreadsheetId;
		const title = context.propsValue.title;
		const exactMatch = context.propsValue.exact_match ?? false;

		const authClient = await createGoogleClient(context.auth);

		const sheets = googleSheets({ version: 'v4', auth: authClient });

		const response = await sheets.spreadsheets.get({
			spreadsheetId,
		});

		const sheetsData = response.data.sheets ?? [];

		const matchedSheets = sheetsData.filter((sheet) => {
			const sheetTitle = sheet.properties?.title ?? '';
			return exactMatch ? sheetTitle === title : sheetTitle.includes(title);
		});

		return {
			found: matchedSheets.length > 0,
			worksheets: matchedSheets,
		};
	},
});