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.4 KB
Raw Blame
import { googleSheetsAuth } from '../common/common';
import { createAction, Property } from '@activepieces/pieces-framework';
import {
	areSheetIdsValid,
	columnToLabel,
	createGoogleClient,
	getHeaderRow,
	ValueInputOption,
} from '../common/common';
import { sheets as googleSheets } from '@googleapis/sheets';
import { getWorkSheetName } from '../triggers/helpers';
import { commonProps } from '../common/props';
import { createColumnActionOutputSchema } from '../output-schemas';

export const createColumnAction = createAction({
	auth: googleSheetsAuth,
	name: 'create-column',
	classification: 'WRITE',
	displayName: 'Create Column',
	description: 'Add a new column to a worksheet.',
	audience: 'human',
	aiMetadata: {
		description:
			'Inserts a new column into a worksheet and writes a header name into its first row, either at a given column index or after the last existing column. Use when an agent needs to add a field to a sheet. Not idempotent — each call inserts another column.',
		idempotent: false,
	},
	propertyGroups: [
		{
			key: 'spreadsheet',
			display: 'section',
			label: 'Spreadsheet',
			icon: 'file',
			props: ['spreadsheetId', 'sheetId'],
		},
		{
			key: 'new_column',
			display: 'section',
			label: 'New Column',
			icon: 'tag',
			props: ['columnName', 'columnIndex'],
		},
	],
	props: {
		...commonProps,
		columnName: Property.ShortText({
			displayName: 'Column Name',
			description: 'The header text for the new column.',
			required: true,
		}),
		columnIndex: Property.Number({
			displayName: 'Column Index',
			description:
				'Position of the new column, counting from 1. Empty adds it last.',
			required: false,
		}),
	},
	outputSchema: createColumnActionOutputSchema,
	async run(context) {
		const { spreadsheetId, sheetId, columnName, columnIndex } = context.propsValue;

		if (!areSheetIdsValid(spreadsheetId, sheetId)) {
			throw new Error('Please select a spreadsheet and sheet first.');
		}

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

		let columnLabel;

		if (columnIndex && columnIndex > 0) {
			await sheets.spreadsheets.batchUpdate({
				spreadsheetId,
				requestBody: {
					requests: [
						{
							insertDimension: {
								range: {
									sheetId,
									dimension: 'COLUMNS',
									startIndex: columnIndex - 1,
									endIndex: columnIndex,
								},
							},
						},
					],
				},
			});
			columnLabel = columnToLabel(columnIndex - 1);
		} else {
			const headers = await getHeaderRow({
				spreadsheetId: spreadsheetId as string,
				sheetId: sheetId as number,
				auth: context.auth,
			});

			const newColumnIndex = headers === undefined ? 0 : headers.length;

			await sheets.spreadsheets.batchUpdate({
				spreadsheetId,
				requestBody: {
					requests: [
						{
							insertDimension: {
								range: {
									sheetId,
									dimension: 'COLUMNS',
									startIndex: newColumnIndex,
									endIndex: newColumnIndex + 1,
								},
							},
						},
					],
				},
			});
			columnLabel = columnToLabel(newColumnIndex);
		}

		const sheetName = await getWorkSheetName(
			context.auth,
			spreadsheetId as string,
			sheetId as number,
		);

		const response = await sheets.spreadsheets.values.update({
			range: `${sheetName}!${columnLabel}1`,
			spreadsheetId,
			valueInputOption: ValueInputOption.USER_ENTERED,
			requestBody: {
				values: [[columnName]],
			},
		});

		return response.data;
	},
});