简体   繁体   中英

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 manage to send a webhook using this code but don't receive any data in the body.

    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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM