google-apps-scriptgoogle-apigoogle-formsgoogle-forms-api

Create google form response through API


In AppScript, I wrote this snippet of code to create a google form response, and get the edit link:

function createResponseTest() {
   var test_form = FormApp.create('test-form').setAllowResponseEdits(true);
   // Add a required item
   test_form.addTextItem().setRequired(true);

   // Create a blank response that hasn't completed the item
   var FormResponse = test_form.createResponse();
   const res = FormResponse.submit();
   const link = res.getEditResponseUrl();

   // Get a unique link to the response
   console.log(link);
}

I have looked through the Google Forms documentation, but I don't see a method to create a google form response given a form ID, as is possible in Appscript (see docs here).

I want to run the line:

test_form.createResponse()

from my server, so I don't need to use AppScript at all, but I can't find how to do this.

Am I missing something, or is the Google Form API not feature-complete?

Edit:

I have requested this as a feature through the issue tracker. Track its progress here!


Solution

  • It's not possible. It seems that the Apps Script functions and Forms REST API are meant to be different. From the documentation:

    Both the REST API and the Forms Service on Apps Script support Forms features that the other does not. (...) However, certain features are unlikely to ever be supported, as described below.

    The REST API does not plan to support:

    • Submitting form responses
    • Including detailed form or response data in the body of pub/sub notifications

    I'm not sure why this is the case. The Forms API was launched recently compared to its cousins, so that could be a partial reason, but it doesn't seem like they ever intend to add this feature. Maybe they want users to actually use the Google Forms interface instead of it turning into just a backend to some other UI. They still offer the option in Apps Script, but this has more stringent quotas than the regular API, so it would be more difficult to implement at a large scale. This is just my conjecture, though.

    As a workaround you could still create an Apps Script Web App to act as a webhook receiving POST requests from your server and create the responses that way. You'd just need to create the initial script to fill out a response and then work from your server.

    Reference: