import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGoogleSheetsApi, normalizeSpreadsheetId, normalizeTable, resolveTable, } from "../lib/google-sheets" import { GOOGLE_SHEETS_WRITE_REQUIREMENT } from "../lib/scopes" import { SPREADSHEET_REFERENCE_SCHEMA, TABLE_REFERENCE_SCHEMA, TABLE_SCHEMA, } from "../lib/schemas" /** Deletes a native table and all cell contents in its range. */ export const deleteGoogleSheetsTable = defineAction( "Delete Google Sheets table", ) .describe("Deletes a native table and all of its contents.") .account("google", GOOGLE_SHEETS_WRITE_REQUIREMENT) .input( z.object({ /** Spreadsheet ID or standard Google Sheets URL. */ spreadsheet: SPREADSHEET_REFERENCE_SCHEMA, /** Native table name or immutable table ID. */ table: TABLE_REFERENCE_SCHEMA, }), ) .output(TABLE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const spreadsheetId = normalizeSpreadsheetId(input.spreadsheet) const sheetsApi = getGoogleSheetsApi(account.secret) const { table } = await resolveTable(sheetsApi, spreadsheetId, input.table) if (!table.tableId) throw new Error("Google returned a table without an ID.") await sheetsApi.spreadsheets.batchUpdate({ requestBody: { requests: [{ deleteTable: { tableId: table.tableId } }], }, spreadsheetId, }) return normalizeTable(table) })