Home > Backend Development > Golang > Google Form API - Error creating, updating items

Google Form API - Error creating, updating items

WBOY
Release: 2024-02-06 11:33:14
forward
772 people have browsed it

Google Form API - 创建、更新项目时出错

Question content

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

from the server

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
}
Copy after login

Note: I am using form api v1


Correct answer


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)
}
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template