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

4.2 KB
Raw Blame
import {
	AppConnectionValueForAuthProperty,
	createAction,
	Property,
} from '@activepieces/pieces-framework';
import {
	AuthenticationType,
	httpClient,
	HttpMethod,
	HttpRequest,
} from '@activepieces/pieces-common';
import { drive as googleDrive } from '@googleapis/drive';
import { includeTeamDrivesProp } from '../common/props';
import { createGoogleClient, getAccessToken, googleSheetsAuth } from '../common/common';
import { isNil } from '@activepieces/pieces-framework';
import { AppConnectionType } from '@activepieces/pieces-framework';
import { createSpreadsheetActionOutputSchema } from '../output-schemas';

export const createSpreadsheetAction = createAction({
	auth: googleSheetsAuth,
	name: 'create-spreadsheet',
	classification: 'WRITE',
	displayName: 'Create Spreadsheet',
	description: 'Creates a blank spreadsheet.',
	audience: 'human',
	aiMetadata: {
		description:
			'Creates a new, empty Google Sheets spreadsheet with the given title, optionally inside a specified Drive folder. Use when an agent needs a fresh spreadsheet to populate. Not idempotent — each call creates a separate spreadsheet even if the title is identical.',
		idempotent: false,
	},
	props: {
		title: Property.ShortText({
			displayName: 'Title',
			description: 'The title of the new spreadsheet.',
			required: true,
		}),
		includeTeamDrives: includeTeamDrivesProp(),
		folder: Property.Dropdown({
			auth: googleSheetsAuth,
			displayName: 'Parent Folder',
			description:
				'Where to create the spreadsheet. Defaults to My Drive.',
			required: false,
			refreshers: ['auth', 'includeTeamDrives'],
			options: async ({ auth, includeTeamDrives }) => {
				if (!auth) {
					return {
						disabled: true,
						options: [],
						placeholder: 'Connect your account first',
					};
				}
				const authProp = auth;
				let folders: { id: string; name: string }[] = [];
				const isServiceAccountWithoutImpersonation = authProp.type === AppConnectionType.CUSTOM_AUTH && authProp.props.userEmail?.length === 0;
				let pageToken = null;
				do {
					const request: HttpRequest = {
						method: HttpMethod.GET,
						url: `https://www.googleapis.com/drive/v3/files`,
						queryParams: {
							q: "mimeType='application/vnd.google-apps.folder' and trashed = false",
							includeItemsFromAllDrives: includeTeamDrives || isServiceAccountWithoutImpersonation ? 'true' : 'false',
							supportsAllDrives: 'true',
							corpora: includeTeamDrives || isServiceAccountWithoutImpersonation ? 'allDrives' : 'user',
						},
						authentication: {
							type: AuthenticationType.BEARER_TOKEN,
							token: await getAccessToken(authProp),
						},
					};
					if (pageToken) {
						if (request.queryParams !== undefined) {
							request.queryParams['pageToken'] = pageToken;
						}
					}
					try {
						const response = await httpClient.sendRequest<{
							files: { id: string; name: string,teamDriveId?: string }[];
							nextPageToken: string;
						}>(request);
						folders = folders.concat(response.body.files.filter(file => !isNil(file.teamDriveId) || !isServiceAccountWithoutImpersonation));
						pageToken = response.body.nextPageToken;
					} catch (e) {
						throw new Error(`Failed to get folders\nError:${e}`);
					}
				} while (pageToken);

				return {
					disabled: false,
					options: folders.map((folder: { id: string; name: string }) => {
						return {
							label: folder.name,
							value: folder.id,
						};
					}),
				};
			},
			advanced: true,
		}),
	},
	outputSchema: createSpreadsheetActionOutputSchema,
	async run(context) {
		const { title, folder } = context.propsValue;
		const response = await createSpreadsheet(context.auth, title, folder);
		const newSpreadsheetId = response.id;

	

		return {
			id: newSpreadsheetId,
		};
	},
});

async function createSpreadsheet(
	auth: AppConnectionValueForAuthProperty<typeof googleSheetsAuth>,
	title: string,
	folderId?: string,
) {
	const googleClient = await createGoogleClient(auth);
  const driveApi = googleDrive({ version: 'v3', auth: googleClient });
  const response = await driveApi.files.create({
    requestBody: {
      name: title,
      mimeType: 'application/vnd.google-apps.spreadsheet',
      parents: folderId ? [folderId] : undefined,
    },
    supportsAllDrives: true,
  });
  return response.data;
}