How to create and submit a response in a Form

276 views
Skip to first unread message

AdamD

unread,
Feb 25, 2022, 7:05:32 PM2/25/22
to Google Apps Script Community
I'm trying to use a script to add the response of "Smith" to the "Last Name" question in a Form.  The script runs without errors but I can't figure out why the formCreate.withItemResponse(itemResponse); line is not working properly.


The code is:

const form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var formCreate = form.createResponse();

function textResponse(){

var items = form.getItems();
for (i = 0; i < items.length; ++i){
var item = items[i];
if(item.getType() == 'TEXT' && item.getTitle() == 'Last Name') {
var textItem = item.asTextItem();
var itemResponse = textItem.createResponse('Davis');
formCreate.withItemResponse(itemResponse);
}
}
//formCreate.submit();
}

Thanks!

Adam

AdamD

unread,
Feb 26, 2022, 1:01:21 PM2/26/22
to Google Apps Script Community
Solved it. (I think.) I would appreciate it if someone could explain why this was the issue.

The issue is that the text in "var itemResponse = textItem.createResponse('Davis');" was not passing through correctly.  I added a variable of "var r = 'Davis'; " and changed the line to 
"var itemResponse = textItem.createResponse(r);" and the response was then added to the Form correctly.

Adam
Reply all
Reply to author
Forward
0 new messages