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

3.1 KB
Raw Blame
import {
	AppConnectionValueForAuthProperty,
	createTrigger,
	TriggerStrategy,
} from '@activepieces/pieces-framework';
import { googleSheetsAuth } from '../common/common';
import { DedupeStrategy, Polling, pollingHelper } from '@activepieces/pieces-common';

import dayjs from 'dayjs';
import { drive as googleDrive, drive_v3 } from '@googleapis/drive';
import { includeTeamDrivesProp } from '../common/props';
import { createGoogleClient, GoogleSheetsAuthValue } from '../common/common';
import { newSpreadsheetTriggerOutputSchema } from '../output-schemas';

type Props = {
	includeTeamDrives?: boolean;
};

const polling: Polling<AppConnectionValueForAuthProperty<typeof googleSheetsAuth>, Props> = {
	strategy: DedupeStrategy.TIMEBASED,
	async items({ auth, propsValue, lastFetchEpochMS }) {
		const authValue = auth as GoogleSheetsAuthValue;
		const q = ["mimeType='application/vnd.google-apps.spreadsheet'", 'trashed = false'];
		if (lastFetchEpochMS) {
			q.push(`createdTime > '${dayjs(lastFetchEpochMS).toISOString()}'`);
		}
		const authClient = await createGoogleClient(authValue);
		const drive = googleDrive({ version: 'v3', auth: authClient });
		let nextPageToken;
		const items = [];
		do {
			const response: any = await drive.files.list({
				q: q.join(' and '),
				fields: '*',
				orderBy: 'createdTime desc',
				supportsAllDrives: true,
				includeItemsFromAllDrives: propsValue.includeTeamDrives,
				corpora: propsValue.includeTeamDrives ? 'allDrives' : 'user',
				pageToken: nextPageToken,
			});
			const fileList: drive_v3.Schema$FileList = response.data;
			if (fileList.files) {
				items.push(...fileList.files);
			}
			if (lastFetchEpochMS === 0) break;
			nextPageToken = response.data.nextPageToken;
		} while (nextPageToken);
		return items.map((item) => ({
			epochMilliSeconds: dayjs(item.createdTime).valueOf(),
			data: item,
		}));
	},
};
export const newSpreadsheetTrigger = createTrigger({
	auth: googleSheetsAuth,
	name: 'new-spreadsheet',
	classification: 'READ',
	displayName: 'New Spreadsheet',
	description: 'Triggers when a new spreadsheet is created.',
	aiMetadata: {
		description:
			'Fires when a new Google Sheets spreadsheet is created in the connected Drive (optionally including shared drives), emitting one event per new spreadsheet with its Drive file metadata. Polls periodically rather than in real time.',
	},
	type: TriggerStrategy.POLLING,
	props: {
		includeTeamDrives: includeTeamDrivesProp({ advanced: false }),
	},
	outputSchema: newSpreadsheetTriggerOutputSchema,
	async onEnable(context) {
		await pollingHelper.onEnable(polling, context);
	},
	async onDisable(context) {
		await pollingHelper.onDisable(polling, context);
	},
	async test(context) {
		return await pollingHelper.test(polling, context);
	},
	async run(context) {
		return await pollingHelper.poll(polling, context);
	},
	sampleData: {
		kind: 'drive#file',
		mimeType: 'application/vnd.google-apps.spreadsheet',
		webViewLink: 'https://docs.google.com/document/d/1_9xjsrYFgHVvgqYwAJ8KcsDcNU/edit?usp=drivesdk',
		id: '1_9xjsrYFgHVvgqYwAJ8KcsDcN3AzPelsux',
		name: 'Test Document',
	},
});