3

As a way of initializing a form record, I want to fill and submit a google form with apps script. The key bit of documentation for form.createResponse() is this

Creates a new response to the form. To answer a question item, create an ItemResponse from the item, then attach it to this form response by calling FormResponse.withItemResponse(response). To save the assembled response, call FormResponse.submit().

do I need to new FormResponse() or how do I make this happen?

Rob Foree
  • 375
  • 3
  • 15

1 Answers1

9

create new form

    var test_form = FormApp.create('test1');
    test_form.addTextItem();

get the first question as a text item

    var questions = test_form.getItems();
    var qt = questions[0].asTextItem();

set the response

    var qr = qt.createResponse('cats');

create and submit a response object

    var FormResponse = test_form.createResponse();
    FormResponse.withItemResponse( qr );
    FormResponse.submit();
Rob Foree
  • 375
  • 3
  • 15
  • 5
    There are two different `createResponse()` methods. Exact same name, but used on two different things. One is used on an item, with a parameter of what the answer will be: [Apps Script documentation](https://developers.google.com/apps-script/reference/forms/text-item#createresponseresponse) The other has no parameter and is used on the Form to create the response as a whole. [new response to the form](https://developers.google.com/apps-script/reference/forms/form#createresponse) – Alan Wells Jul 02 '16 at 01:01
  • this also can be used to produce prefilled urls. – icarus May 23 '22 at 16:09