javascripttypescriptoffice365office-scripts

How to send a webhook using Office Script?


I need to connect Excel to Zapier through a webhook, but can't make it work so far. I managed to send a webhook using this code, but I don't receive any data in the body.

function main(workbook: ExcelScript.Workbook) {
    function sendMessage() {
        var request = new XMLHttpRequest();
        request.open("POST", "https://hooks.zapier.com/hooks/catch/2251620/bv6gjw6/");

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

Tried every code snippet out there but couldn't make it work. Pasted JSON data as well.


Solution

  • I've found the culprit - mode: 'no-cors'needs to be added. Here's the end result, you can send webhooks from Excel Script with that:

    async function main(workbook: ExcelScript.Workbook) {
        async function sendWebhook(data:string) {
          
            let response = await fetch('https://hooks.zapier.com/hooks/catch/2251620/bv6gjw6/', {
                method: 'POST',
                mode: 'no-cors',
                headers: { 'Content-Type': 'application/json' },
                body: data,
            });
            return response;
            //const json = await response.json();
            //return json;
        }
        let data = JSON.stringify({ message: 'Hello, Excel!' });
        let response= await sendWebhook(data);