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

Google Form API - Error creating, updating items

WBOY
WBOYforward
2024-02-06 11:33:14714browse

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
}

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)
}

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!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete