Golang google sheets API V4 - Write/Update example?

前端 未结 3 535
礼貌的吻别
礼貌的吻别 2021-01-02 06:24

Trying to write a simple three column table ([][]string) with Go, but can\'t. The quick start guide is very nice, I now can read sheets, but there no any examp

3条回答
  •  粉色の甜心
    2021-01-02 06:49

    Well after some tryouts, there is an answer. Everything is same as in https://developers.google.com/sheets/quickstart/go Just changes in the main function

    func write() {
        ctx := context.Background()
        b, err := ioutil.ReadFile("./Google_Sheets_API_Quickstart/client_secret.json")
        if err != nil {
           log.Fatalf("Unable to read client secret file: %v", err)
        }
    
    // If modifying these scopes, delete your previously saved credentials
    // at ~/.credentials/sheets.googleapis.com-go-quickstart.json
        config, err := google.ConfigFromJSON(b, "https://www.googleapis.com/auth/spreadsheets")
        if err != nil {
            log.Fatalf("Unable to parse client secret file to config: %v", err)
        }
        client := getClient(ctx, config)
    
        srv, err := sheets.New(client)
        if err != nil {
            log.Fatalf("Unable to retrieve Sheets Client %v", err)
        }
    
        spreadsheetId := "YOUR SPREADSHEET ID"
    
        writeRange := "A1"
    
        var vr sheets.ValueRange
    
        myval := []interface{}{"One", "Two", "Three"}
        vr.Values = append(vr.Values, myval)
    
        _, err = srv.Spreadsheets.Values.Update(spreadsheetId, writeRange, &vr).ValueInputOption("RAW").Do()
        if err != nil {
            log.Fatalf("Unable to retrieve data from sheet. %v", err)
        }
    
    }
    

提交回复
热议问题