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.1 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import { areSheetIdsValid, createGoogleClient } from '../common/common';
import { googleSheetsAuth } from '../common/common';
import { commonProps } from '../common/props';
import { sheets as googleSheets } from '@googleapis/sheets';
import { isNil } from '@activepieces/pieces-framework';
import { formatRowActionOutputSchema } from '../output-schemas';

export const formatRowAction = createAction({
	auth: googleSheetsAuth,
	name: 'format-row',
	classification: 'WRITE',
	description: 'Apply colour and text styling to one or more rows.',
	audience: 'human',
	aiMetadata: {
		description:
			'Applies cell formatting (background color, text color, bold, italic, strikethrough) to a range of rows in a worksheet, leaving cell values untouched. Use when an agent needs to style rows rather than change data. Colors are given as HEX codes; idempotent — re-applying the same formatting produces the same appearance.',
		idempotent: true,
	},
	displayName: 'Format Row(s)',
	propertyGroups: [
		{
			key: 'spreadsheet',
			display: 'section',
			label: 'Spreadsheet',
			icon: 'file',
			props: ['spreadsheetId', 'sheetId'],
		},
		{
			key: 'target_rows',
			display: 'section',
			label: 'Rows',
			icon: 'markdown',
			props: ['startingRow', 'endingRow'],
		},
		{
			key: 'style',
			display: 'section',
			label: 'Style',
			icon: 'type',
			props: ['bgColor', 'textColor', 'bold', 'italic', 'strikethrough'],
		},
	],
	props: {
		...commonProps,
		startingRow: Property.Number({
			displayName: 'Starting Row',
			description: 'The first row number where formatting should begin.',
			required: true,
		}),
		endingRow: Property.Number({
			displayName: 'Ending Row',
			description:
				'Last row to format. Leave empty to format only the starting row.',
			required: false,
		}),
		bgColor: Property.ShortText({
			displayName: 'Background Color',
			description: 'Provide a HEX color code (example: #FFD966)',
			required: false,
			width: 'half',
		}),
		textColor: Property.ShortText({
			displayName: 'Text Color',
			description: 'Provide a HEX color code (example: #FFD966)',
			required: false,
			width: 'half',
		}),
		bold: Property.Checkbox({
			displayName: 'Bold',
			required: false,
		}),
		italic: Property.Checkbox({
			displayName: 'Italic',
			required: false,
		}),
		strikethrough: Property.Checkbox({
			displayName: 'Strikethrough',
			required: false,
		}),
	},
	outputSchema: formatRowActionOutputSchema,
	async run(context) {
		const { spreadsheetId, sheetId, startingRow,endingRow, bgColor, textColor, italic, bold, strikethrough } =
			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 });

		const response = await sheets.spreadsheets.batchUpdate({
			spreadsheetId: context.propsValue.spreadsheetId,
			requestBody: {
				requests: [
					{
						repeatCell: {
							range: {
								sheetId,
								startRowIndex: startingRow - 1,
								endRowIndex: endingRow ? endingRow : startingRow,
							},
							cell: {
								userEnteredFormat: {
									backgroundColor: hexToRgb(bgColor),
									textFormat: {
										bold,
										italic,
										strikethrough,
										foregroundColor: hexToRgb(textColor),
									},
								},
							},
							fields: 'userEnteredFormat(backgroundColor,textFormat)',
						},
					},
				],
			},
		});

		return {
			success: true,
			...response.data,
		};
	},
});

function hexToRgb(hex?: string) {
	// Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
	if (isNil(hex)) return undefined;
	const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
	hex = hex.replace(shorthandRegex, function (m, r, g, b) {
		return r + r + g + g + b + b;
	});

	const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
	return result
		? {
				red: parseInt(result[1], 16),
				green: parseInt(result[2], 16),
				blue: parseInt(result[3], 16),
		  }
		: undefined;
}