I am trying to update an existing form using the google forms api. I filled out the location field in the request but still receive an error message create_item.location.index is invalid or not provided
Create request code
func UpdateForm(formId string, form *forms.Form) *forms.Form { var requestElements []*forms.Request // Update form info requestElements = append(requestElements, &forms.Request{ UpdateFormInfo: &forms.UpdateFormInfoRequest{ Info: form.Info, UpdateMask: "*", }, }) // Add items for i, item := range form.Items { element := &forms.Request{ CreateItem: &forms.CreateItemRequest{ Item: item, Location: &forms.Location{Index: int64(i)}, }, } requestElements = append(requestElements, element) } request := forms.BatchUpdateFormRequest{ IncludeFormInResponse: true, Requests: requestElements, } response, err := formService.Forms. BatchUpdate(formId, &request). Context(context.TODO()). Do() if err != nil { panic(err) } return response.Form }
Note: I am using form api
v1
Finally found the problem. When adding a new item to the form we have to start from index 0
, but 0
is the default value for int
in protobuf, so when the request is sent It will be ignored.
Solution: Force sending field index
// Add items for i, item := range form.Items { element := &forms.Request{ CreateItem: &forms.CreateItemRequest{ Item: item, Location: &forms.Location{ Index: int64(i), ForceSendFields: []string{"Index"}, }, }, } requestElements = append(requestElements, element) }
The above is the detailed content of Google Form API - Error creating, updating items. For more information, please follow other related articles on the PHP Chinese website!