Excel to Oktopost Draft Agent Setup Guide

Excel to Oktopost Draft Agent Setup Guide

Trigger a Custom Agent in Oktopost to generate draft social posts directly from an Excel content calendar using Office Scripts.

Prerequisites

  • Access to Excel for the Web with Office Scripts enabled
  • Permission to create and activate Custom Agents in Oktopost

Step 1: Set Up Your Agent in Oktopost

  1. In Oktopost, open Agent Builder and click Create Agent (or open your existing draft agent)
  2. Add a Webhook node as your trigger
  3. In the Webhook node sidebar:
    • Vendor: Set to Generic
    • Label: Enter a name (for example, Draft Created)
  4. Copy the generated Webhook URL and Secret keys
  5. Connect your Webhook node to an AI Action node configured to create draft social posts based on the spreadsheet data
  6. Click Activate in the top-right corner to make your agent live

Step 2: Add the Office Script to Excel Web

  1. Open your content calendar spreadsheet in Excel for the Web
  2. Click the Automate tab in the top ribbon and select New Script

Replace any default code in the Script Editor with the following TypeScript code:

async function main(workbook: ExcelScript.Workbook) {
    const sheet = workbook.getActiveWorksheet();
    const activeCell = workbook.getActiveCell();
    const rowIndex = activeCell.getRowIndex();

    // Prevent sending Row 1 (Headers)
    if (rowIndex < 1) {
        console.log("Please select a data row (Row 2 or lower).");
        return;
    }

    const usedRange = sheet.getUsedRange();
    if (!usedRange) {
        console.log("No data found in worksheet.");
        return;
    }

    const columnCount = usedRange.getColumnCount();
    const headerRange = sheet.getRangeByRangeAddress(`1:1`).getResizedRange(0, columnCount - 1);
    const selectedRowRange = sheet.getRangeByIndexes(rowIndex, 0, 1, columnCount);

    const headers = headerRange.getValues()[0];
    const rowValues = selectedRowRange.getValues()[0];

    // Build JSON Payload
    const payload: { [key: string]: any } = {};
    headers.forEach((header, index) => {
        if (header) {
            payload[header.toString()] = rowValues[index];
        }
    });

    // Paste your Webhook URL and Secret from Step 1
    const webhookUrl = 'YOUR_OKTOPOST_WEBHOOK_URL';
    const secretKey = 'YOUR_OKTOPOST_SECRET_KEY';

    try {
        const response = await fetch(webhookUrl, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'x-webhook-secret': secretKey,
                'Authorization': secretKey
            },
            body: JSON.stringify(payload)
        });

        if (response.ok) {
            console.log(`Success! Sent Row ${rowIndex + 1} to Oktopost.`);
        } else {
            console.log(`Failed with status ${response.status}: ${await response.text()}`);
        }
    } catch (error) {
        console.log(`Error sending row: ${error}`);
    }
}
  1. Replace YOUR_OKTOPOST_WEBHOOK_URL and YOUR_OKTOPOST_SECRET_KEY with the credentials copied from Oktopost in Step 1
  2. Click Save script and rename it to Send Row to Oktopost

Step 3: Trigger the Agent

  1. In your Excel sheet, select any cell in the row containing the post details you want to draft (Row 2 or below)
  2. Open the Automate tab, select Send Row to Oktopost, and click Run
  3. The script packages the active row's data and sends it to your Oktopost Custom Agent to generate the draft post on your calendar

For automated spreadsheet integrations and workflow concepts, watch How to Create Email Drafts using Google Sheets, which explains triggering automated draft actions from spreadsheet rows.

Was this article helpful?
0 out of 0 found this helpful