go - 添加新工作表

标签 go google-sheets-api

我尝试添加一个新工作表,但我不知道该怎么做。请帮助我...

谢谢。

Google sheet sample code https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate

AddSheetRequest https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#AddSheetRequest

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 spreadsheet to apply the updates to.
        spreadsheetId := "my-spreadsheet-id" // TODO: Update placeholder value.

        // A list of updates to apply to the spreadsheet.
        // Requests will be applied in the order they are specified.
        // If any request is not valid, no requests will be applied.
        requests := []*sheets.Request{} // TODO: Update placeholder value.

        rb := &sheets.BatchUpdateSpreadsheetRequest{
                Requests: requests,

                // TODO: Add desired fields of the request body.
        }

        resp, err := sheetsService.Spreadsheets.BatchUpdate(spreadsheetId, rb).Context(ctx).Do()
        if err != nil {
                log.Fatal(err)
        }

        // TODO: Change code below to process the `resp` object:
        fmt.Printf("%#v\n", resp)
}

func getClient(ctx context.Context) (*http.Client, error) {
        // TODO: Change placeholder below to get authentication credentials. See
        // 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")
}

最佳答案

我找到了方法...

    req := sheets.Request{
        AddSheet: &sheets.AddSheetRequest{
            Properties: &sheets.SheetProperties{
                Title: "test",
            },
        },
    }

    rbb := &sheets.BatchUpdateSpreadsheetRequest{
        Requests: []*sheets.Request{&req},
    }

    resp, err := srv.Spreadsheets.BatchUpdate(spreadsheetId, rbb).Context(context.Background()).Do()
    if err != nil {
        log.Fatal(err)
    }

关于go - 添加新工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50792237/

相关文章:

inheritance - 我如何组织这段从嵌入式类型重新定义方法的 Go 代码,以减少冗余并提高可维护性?

google-apps-script - 获取驱动器文件统计信息(查看/打开)|谷歌云端硬盘API

java - 使用 Google Sheet API V4 将数据写入 Google Sheet - Java 示例代码

http - Go lang 捕获带超时的重定向 url 和状态代码

string - 在 Golang 中生成固定长度的随机十六进制字符串的有效方法?

python - Google Sheets API Python - 清除表格

c# - 在 C# .NET 中通过服务帐户而不是个人帐户进行身份验证以使用 Google 表格

javascript - 未记录的 Sheet API 限制问题

go - 为什么我的 channel 需要缓冲区?

go - 简短的goroutine返回值