Alejandro Silvestri
Alejandro Silvestri

Reputation: 3774

create an ItemResponse in Form

I'm trying to send an automatic response from code to a form of mine. I can open it and send empty responses (I create a FormResponse and submit it empty).

Now I want to fill the itemResponses prior to submit. I'm trying to use

FormResponse.withItemResponse(itemResponse)

https://developers.google.com/apps-script/reference/forms/form-response?hl=es-ES#withItemResponse(ItemResponse) but I can't find a way to create the ItemResponse argument to fill my FormResponse. I searched the whole documentation, I know there must be a way, but I simply can't find it.

var form = FormApp.getActiveForm();
var response = form.createResponse();
var itemResponse = ????????????;
response.withItemResponse(itemResponse);
response.submit();

Any hint? Thank you!

Upvotes: 5

Views: 6236

Answers (2)

Min
Min

Reputation: 1

The Google Apps Script interface Item does not implement a createResponse method. However, the Item interface has as_Item methods (e.g., asTextItem()) that return an instance of TextItem, CheckboxItem, etc., that implements a createResponse method.

// This throws "TypeError: item.createResponse is not a function" if
// item is not an instance of a specific item class such as TextItem.
item.createResponse("response")

// The above will throw the error even if the item is a text box.
item.getType() == FormApp.ItemType.TEXT

// Once the item is cast into an **instance** of a specific item class,
// it will no longer throw the error.
item.asTextItem().createResponse("response")

Upvotes: 0

Alejandro Silvestri
Alejandro Silvestri

Reputation: 3774

Found it!

Because Google search "itemresponse" throw me nothing, I finally searched itemresponse in Google for the GAS documentation site...

The only way to create an ItemResponse is from the specific Item typed object. For example:

var itemResponse = TextItem.createResponse('my text');

https://developers.google.com/apps-script/reference/forms/text-item#createResponse(String)

Upvotes: 8

Related Questions