如何使用应用程序脚本创建Google表单响应

如何使用应用程序脚本创建Google表单响应

问题描述:

作为初始化表单记录的一种方法,我想用Apps脚本填写并提交Google表单.form.createResponse()文档的关键是这个

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

创建对表单的新响应.要回答问题,请创建来自项目的ItemResponse,然后通过以下方式将其附加到此表单响应中调用FormResponse.withItemResponse(response).保存组装好的响应,请调用FormResponse.submit().

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().

我需要新的FormResponse()还是如何做到这一点?

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

创建新表单

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

设置响应

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

创建并提交响应对象

    var FormResponse = test_form.createResponse();
    FormResponse.withItemResponse( qr );
    FormResponse.submit();