Golang google sheet API V4 - 写/更新示例? [英] Golang google sheets API V4 - Write/Update example?

查看:233
本文介绍了Golang google sheet API V4 - 写/更新示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试着用Go编写一个简单的三列表( [] [] string ),但不能。
快速入门指南非常好,我现在可以阅读工作表,但是没有任何关于如何将数据写入表单的例子,可能它是微不足道的,但对我而言似乎不是。
Golang图书馆我的大脑太复杂了。
并没有一个例子我可以谷歌...



C#示例非常接近,但我不确定自己清楚地理解C#

解决方案

一些试用后,有一个答案。所有内容与 https://developers.google.com/sheets/quickstart/go只是改变了主函数

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

//如果修改这些作用域,请删除以前保存的凭据
//〜〜/ .credentials / sheets.googleapis.com-go-quickstart.json
config,err:= google.ConfigFromJSON(b,https://www.googleapis.com/auth/spreadsheets)
if err!= nil {
log.Fatalf(无法解析客户端秘密文件配置:%v,err)
}
client:= getClient(ctx,config)

srv,err:= sheets.New客户端)
if err!= nil {
log.Fatalf(无法检索表格客户端% v,err)
}

spreadsheetId:=您的SPREADSHEET ID

writeRange:=A1

var vr sheets.ValueRange
$ b 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(无法从工作表检索数据。 %v,err)
}

}


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 example of how to write data to a sheet, maybe it is trivial, but not for me it seems. The Golang library for my brains is just too complicated to figure out. And there not a single example I could google...

This C# example very looks close, but I am not sure I clearly understand C#

解决方案

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

}

这篇关于Golang google sheet API V4 - 写/更新示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆