Google表单API – 创建、更新项目时出现错误

huangapple go评论80阅读模式
英文:

Google Form API - Error when Create, Update Item

问题

我尝试使用Google表单API更新我的现有表单。我在请求中填写了位置字段,但仍然收到服务器的错误消息create_item.location.index is invalid or was not provided

创建请求代码

func UpdateForm(formId string, form *forms.Form) *forms.Form {
   var requestElements []*forms.Request
   // 更新表单信息
   requestElements = append(requestElements, &forms.Request{
      UpdateFormInfo: &forms.UpdateFormInfoRequest{
         Info:       form.Info,
         UpdateMask: "*",
      },
   })
   // 添加项目
   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 api v1。

英文:

I try to update my existing form using google form API. I filled location field in request but still received error message from server create_item.location.index is invalid or was 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'm using form api v1

答案1

得分: 0

最后我找到了问题所在。当向表单中添加新项时,我们必须从索引0开始,但是在protobuf中,0int的默认值,所以当请求发送时会被忽略。
解决方案:强制发送字段Index

// 添加项
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)
}
英文:

Finally i found the issue. When add a new item into form we must start with index 0, but 0 is default value of int in protobuf so it will be ignore when request send.
Solution: Force send 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)
}

huangapple
  • 本文由 发表于 2022年11月19日 23:45:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/74501410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定