我正在尝试在此处遵循 Google Sheets API 快速入门:
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate
(向下滚动到“示例”,然后单击“开始”)
这就是我尝试更新电子表格的方式:
package main
// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Google Sheets API
// and check the quota for your project at
// https://console.developers.google.com/apis/api/sheets
// 2. Install and update the Go dependencies by running `go get -u` in the
// project directory.
import (
"errors"
"fmt"
"log"
"net/http"
"golang.org/x/net/context"
"google.golang.org/api/sheets/v4"
)
func main() {
ctx := context.Background()
c, err := getClient(ctx)
if err != nil {
log.Fatal(err)
}
sheetsService, err := sheets.New(c)
if err != nil {
log.Fatal(err)
}
// The ID of the spreadsheet to update.
spreadsheetId := "1diQ943LGMDNkbCRGG4VqgKZdzyanCtT--V8o7r6kCR0"
var jsonPayloadVar []string
monthVar := "Apr"
thisCellVar := "A26"
thisLinkVar := "http://test.url"
jsonRackNumberVar := "\"RACKNUM01\""
jsonPayloadVar = append(jsonPayloadVar, fmt.Sprintf("(\"range\": \"%v!%v\", \"values\": [[\"%v,%v)\"]]),", monthVar, thisCellVar, thisLinkVar, jsonRackNumberVar))
rb := &sheets.BatchUpdateValuesRequest{"ValueInputOption": "USER_ENTERED", "data": jsonPayloadVar}
resp, err := sheetsService.Spreadsheets.Values.BatchUpdate(spreadsheetId, rb).Context(ctx).Do()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", resp)
}
func getClient(ctx context.Context) (*http.Client, error) {
// https://developers.google.com/sheets/quickstart/go#step_3_set_up_the_sample
//
// Authorize using the following scopes:
// sheets.DriveScope
// sheets.DriveFileScope
sheets.SpreadsheetsScope
return nil, errors.New("not implemented")
}
输出:
hello.go:43:结构初始化程序中的无效字段名称“ValueInputOption”
hello.go:43:结构初始化程序中的无效字段名称“数据”
hello.go:58:sheets.SpreadsheetsScope 已评估但未使用
有两件事不起作用:
- 如何将字段输入变量 rb 并不明显
- 我需要使用 sheet.SpreadsheetsScope
谁能提供一个执行 BatchUpdate 的工作示例?
参考资料:本文展示了如何进行不是 BatchUpdate 的更新:Golang google sheet API V4 - Write/Update example?
Google 的 API 参考 - 请参阅从第 1437 行开始的 ValueInputOption 部分:https ://github.com/google/google-api-go-client/blob/master/sheets/v4/sheets-gen.go
本文展示了如何在 Java 中执行 BatchUpdate:Write data to Google Sheet using Google Sheet API V4 - Java Sample Code