1 / 10
Ahmed Bouchefra Profile Header
A.B

;Ahmed Bouchefra

1.2k منشورات
100k قارئ
9 كاتب

أنا أحمد بوشفرة، مبرمج ومؤلف تقني (Tech Author) متخصص في تبسيط مفاهيم البرمجة وتطوير الويب. منذ عام 2017، أقدّم محتوى موجّهًا للمبرمجين عبر موقع 10xdev blog، بالإضافة إلى منصّات مثل SitePoint وSmashing Magazine. أسلوبي عملي ويساعد المبرمجين على فهم التقنيات بسرعة وبناء مهارات قوية بثقة. كما تعاونت مع دار النشر Packt في إصدار كتاب Full Stack Development with Angular and GraphQL، مما يعكس جودة المحتوى الذي أقدمه للمبرمجين. يحتوي هذا الموقع على مقالات كتبتها للجمهور العربي، بالإضافة إلى مقالاتي المترجمة من موقع 10xdev blog ومقالات أخرى ساهم بها مبرمجون من مختلف الأنحاء.

Posting a Google Form to a Webhook

جاهز

In this tutorial, we’ll learn how to create a script for getting Google form submissions programmatically to integrate Google Forms with Webhooks.

If you are familiar with JavaScript, you need to write a Google Apps script and set up a trigger that will be invoked every time we submit the form.

Let’s see this step by step.

Heab back to your Google form, and click on the three vertical dots in the top right corner and select “Script Editor”.

Next, copy the following script into the editor:

var webhook = "enter your URL here";
function onSubmit(event) {
    const form = FormApp.getActiveForm();
    const allResponses = form.getResponses();
    const latestResponse = allResponses[allResponses.length - 1];
    var res = latestResponse.getItemResponses();
    var data = {};
    for (var i = 0; i < res.length; i++) {
        const qTitle = res[i].getItem().getTitle();
        var qAnswer = res[i].getResponse();
        data[qTitle] = qAnswer;
    }
UrlFetchApp.fetch(webhook, {
        "method": "post",
        "contentType": "application/json",
        "payload": JSON.stringify(data)
    });
};

Make sure to replace, the webhook variable with your own service URL.

Now, this is an important step, to actually get the form submissions; we need to set up a trigger. Click on Edit and select Current Project’s Triggers

Next, create a new trigger using the Add Trigger button on the bottom right-hand side.

You’ll be presented with the following window:

Simply make sure that Select event type is set to On form submit then click Save.

That’s it, if you go and submit your form, you will receive the data as a JSON response.