How to send a webhook using Office Script in Microsoft Excel?

Blagoy Nikushev 0 Reputation points
2023-01-21T08:28:39.2266667+00:00

I need to send webhooks using Office Script in Excel. Here's the flow:

  1. User fills some data in a table.
  2. User clicks a button in Excel.
  3. Script sends data via Webhook to external service (Zapier in this case)

Tried both Fetch and XMLHttpRequest but the body of the received webhooks is empty in both cases. Fetch gives an error Unable to fetch but still sends the webhook.

Here's a sample code I've tried:

//XMLHttpRequest
    function sendMessage() {
        var request = new XMLHttpRequest();
        request.open("POST", "webhookurl");

        request.setRequestHeader('Content-type', 'application/json');
                 
        var content = { "value1": "test data" };
        
        request.send(JSON.stringify(content));
    }
    sendMessage()
}

//Fetch:

function main(workbook: ExcelScript.Workbook) {
  interface WebhookData {
    message: string;
  }


async function sendWebhook(data: WebhookData) {
    const response: Response = await fetch('https://example.com/webhook', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data),
    });
    const json: unknown = await response.json();
    return json;
}
Microsoft 365 and Office Development Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.