import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleSheetsApi, normalizeConditionalFormatRule, normalizeSpreadsheetId, resolveSheet, } from "../lib/google-sheets" import { GOOGLE_SHEETS_WRITE_REQUIREMENT } from "../lib/scopes" import { CONDITIONAL_FORMAT_RULE_SCHEMA, SHEET_REFERENCE_SCHEMA, SPREADSHEET_REFERENCE_SCHEMA, } from "../lib/schemas" /** Deletes a conditional-format rule at a sheet's zero-based index. */ export const deleteGoogleSheetsConditionalFormatRule = defineAction( "Delete Google Sheets conditional format rule", ) .describe("Deletes one conditional-format rule from a sheet.") .account("google", GOOGLE_SHEETS_WRITE_REQUIREMENT) .input( z.object({ /** Zero-based rule index on the sheet. */ index: z.number().int().nonnegative(), /** Sheet title or immutable numeric sheet ID. */ sheet: SHEET_REFERENCE_SCHEMA, /** Spreadsheet ID or standard Google Sheets URL. */ spreadsheet: SPREADSHEET_REFERENCE_SCHEMA, }), ) .output(CONDITIONAL_FORMAT_RULE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const sheetsApi = getGoogleSheetsApi(account.secret) const spreadsheetId = normalizeSpreadsheetId(input.spreadsheet) const { data } = await sheetsApi.spreadsheets.batchUpdate({ requestBody: { requests: [ { deleteConditionalFormatRule: { index: input.index, sheetId: ( await resolveSheet(sheetsApi, spreadsheetId, input.sheet) ).properties!.sheetId!, }, }, ], }, spreadsheetId, }) const rule = data.replies?.[0]?.deleteConditionalFormatRule?.rule if (!rule) throw new Error("Google did not return the deleted rule.") return normalizeConditionalFormatRule(rule, input.index) })