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.9 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import {
	httpClient,
	HttpMethod,
	AuthenticationType,
	HttpRequest,
} from '@activepieces/pieces-common';
import { googleSheetsAuth } from '../common/common';
import { includeTeamDrivesProp } from '../common/props';
import { getAccessToken } from '../common/common';
import { findSpreadsheetsActionOutputSchema } from '../output-schemas';
import { driveQuery } from '../common/drive-query';

export const findSpreadsheets = createAction({
	name: 'find_spreadsheets',
	classification: 'SEARCH',
	displayName: 'Find Spreadsheet(s)',
	description: 'Find spreadsheet(s) by name.',
	audience: 'human',
	aiMetadata: {
		description:
			'Searches Google Drive for spreadsheets whose name matches a query (exact or contains) and returns the matches. Use to resolve a spreadsheet id from a human-readable name before acting on it. Read-only and idempotent.',
		idempotent: true,
	},
	auth: googleSheetsAuth,
	props: {
		includeTeamDrives: includeTeamDrivesProp({ advanced: false }),
		spreadsheet_name: Property.ShortText({
			displayName: 'Spreadsheet Name',
			description: 'Enter the name of the spreadsheet to search for',
			required: true,
		}),
		exact_match: Property.Checkbox({
			displayName: 'Exact Match',
			description:
				'Only return spreadsheets whose name matches exactly.',
			required: false,
			defaultValue: false,
		}),
	},
	outputSchema: findSpreadsheetsActionOutputSchema,
	async run({ propsValue, auth }) {
		const searchValue = propsValue.spreadsheet_name;
		const queries = ["mimeType='application/vnd.google-apps.spreadsheet'", 'trashed=false'];

		if (propsValue.exact_match) {
			queries.push(`name = '${driveQuery.escapeLiteral(searchValue)}'`);
		} else {
			queries.push(`name contains '${driveQuery.escapeLiteral(searchValue)}'`);
		}

		const files = [];
		let pageToken = null;

		do {
			const request: HttpRequest = {
				method: HttpMethod.GET,
				url: 'https://www.googleapis.com/drive/v3/files',
				queryParams: {
					q: queries.join(' and '),
					includeItemsFromAllDrives: propsValue.includeTeamDrives ? 'true' : 'false',
					supportsAllDrives: 'true',
					corpora: propsValue.includeTeamDrives ? 'allDrives' : 'user',
					fields: 'files(id,name,webViewLink,createdTime,modifiedTime),nextPageToken',
				},
				authentication: {
					type: AuthenticationType.BEARER_TOKEN,
					token: await getAccessToken(auth),
				},
			};
			if (pageToken) {
				if (request.queryParams !== undefined) {
					request.queryParams['pageToken'] = pageToken;
				}
			}
			try {
				const response = await httpClient.sendRequest<{
					files: { id: string; name: string }[];
					nextPageToken: string;
				}>(request);

				files.push(...response.body.files);
				pageToken = response.body.nextPageToken;
			} catch (e) {
				throw new Error(`Failed to get folders\nError:${e}`);
			}
		} while (pageToken);

		return {
			found: files.length > 0,
			spreadsheets: files,
		};
	},
});