添加新工作表

时间:2018-06-11 07:15:28

标签: go google-sheets-api

我尝试添加新表,但我不明白该怎么做。请帮帮我......

感谢。

  

Google表格示例代码   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")
}

1 个答案:

答案 0 :(得分:1)

我找到了方法......

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