我尝试使用谷歌表单 API 更新我现有的表单。我在请求中填写了位置字段,但仍然收到来自服务器的错误消息create_item.location.index is invalid or was not provided创建请求代码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}注意:我使用的是form apiv1
1 回答
翻翻过去那场雪
TA贡献2065条经验 获得超13个赞
最后我发现了问题。当向表单中添加一个新项目时,我们必须从 index 开始0,但0它是 protobuf 中的默认值int,因此在请求发送时它将被忽略。解决方案:强制发送字段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)
}
- 1 回答
- 0 关注
- 93 浏览
添加回答
举报
0/150
提交
取消