如何使用带有 spreadsheetid 、 spreadsheetname 、 sheetid 和 values 的 golang 在 googlesheet 中插入数据
How to insert data inside googlesheet using golang with spreadsheetid , spreadsheetname, sheetid and values
我正在尝试将数据插入或更新到 google 传播 sheet。我正在使用 golang 模块“google.golang.org/api/option”和“google.golang.org/api/sheets/v4”
订阅的包
我收到如下错误:- 1.
got HTTP response code 404 with body: <!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n </style>\n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n <p><b>404.</b> <ins>That’s an error.</ins>\n <p>The requested URL <code>/v4/spreadsheets/11dlAKQVB2Hb88fPtg7B9sdPkjmhbNxY8L6H-F1ZY5FI/values/:append?alt=json&insertDataOption=INSERT_ROWS&prettyPrint=false&valueInputOption=USER_ENTERED</code> was not found on this server. <ins>That’s all we know.</ins>\n"
当我尝试通过动态 sheetname 保存时。
2.
Error while updating records error: googleapi: Error 400: Unable to parse range: 655963475, badRequest"}
同时尝试使用 sheetId
如何在 golang 中使用 sheetname、sheetid 和 spreadsheetid 将值插入或更新到 googlsheet。
这是错误情况 2 的代码:-
func SaveRecords(reqId string, credentials, tokenConfig []byte, spreadsheetId, sheetname string, records [][]interface{}) error {
ctx := context.Background()
config, err := google.ConfigFromJSON(credentials, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
return err
}
token := oauth2.Token{}
json.Unmarshal(tokenConfig, &token)
client := config.Client(ctx, &token)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Print(err)
return err
}
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetname, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response.HTTPStatusCode != 200 {
return err
}
return nil
}
这是我针对错误情况 1 的代码:-
func SaveRecords(reqId string, credentials, tokenConfig []byte, spreadsheetId, sheetname string,sheetid string, records [][]interface{}) error {
ctx := context.Background()
config, err := google.ConfigFromJSON(credentials, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
return err
}
token := oauth2.Token{}
json.Unmarshal(tokenConfig, &token)
client := config.Client(ctx, &token)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Print(err)
return err
}
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetid, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response.HTTPStatusCode != 200 {
return err
}
return nil
}
我相信你的目标如下。
- 您想使用 googleapis for golang 将
records
的值附加到 Spreadsheet。
- 你想在这种情况下使用 sheet ID。
- 您已经能够使用表格 API.
获取值并将值放入 Google Spreadsheet
这样的话,下面的修改怎么样?
很遗憾,sheet ID 不能直接用于srv.Spreadsheets.Values.Append()
。我认为这就是你的问题的原因。所以,在这种情况下,首先需要将sheet ID转换为sheet名称。
修改后的脚本:
发件人:
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetname, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response.HTTPStatusCode != 200 {
return err
}
收件人:
sheetid := 0 // Please set sheet ID.
spreadsheetId := "###" // Please set Spreadsheet ID
records := [][]interface{}{{"a1", "b1", "c1"}} // This is a sample value.
// 1. Convert sheet ID to sheet name.
response1, err := srv.Spreadsheets.Get(spreadsheetId).Fields("sheets(properties(sheetId,title))").Do()
if err != nil || response1.HTTPStatusCode != 200 {
return err
}
sheetName := ""
for _, v := range response1.Sheets {
prop := v.Properties
sheetID := prop.SheetId
if sheetID == int64(sheetid) {
sheetName = prop.Title
break
}
}
// 2. Append value to the sheet.
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response2, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetName, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response2.HTTPStatusCode != 200 {
return err
}
- 当此脚本为 运行 时,sheet ID 将转换为 sheet 名称。并且,该值附加到 sheet.
注:
如果可以直接使用sheet名字,脚本就变成如下
sheetName := "Sheet1" // Please set sheet name.
spreadsheetId := "###" // Please set Spreadsheet ID
records := [][]interface{}{{"a1", "b1", "c1"}} // This is a sample value.
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response2, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetName, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response2.HTTPStatusCode != 200 {
return err
}
参考文献:
我正在尝试将数据插入或更新到 google 传播 sheet。我正在使用 golang 模块“google.golang.org/api/option”和“google.golang.org/api/sheets/v4”
订阅的包我收到如下错误:- 1.
got HTTP response code 404 with body: <!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content=\"initial-scale=1, minimum-scale=1, width=device-width\">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n </style>\n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n <p><b>404.</b> <ins>That’s an error.</ins>\n <p>The requested URL <code>/v4/spreadsheets/11dlAKQVB2Hb88fPtg7B9sdPkjmhbNxY8L6H-F1ZY5FI/values/:append?alt=json&insertDataOption=INSERT_ROWS&prettyPrint=false&valueInputOption=USER_ENTERED</code> was not found on this server. <ins>That’s all we know.</ins>\n"
当我尝试通过动态 sheetname 保存时。 2.
Error while updating records error: googleapi: Error 400: Unable to parse range: 655963475, badRequest"}
同时尝试使用 sheetId
如何在 golang 中使用 sheetname、sheetid 和 spreadsheetid 将值插入或更新到 googlsheet。
这是错误情况 2 的代码:-
func SaveRecords(reqId string, credentials, tokenConfig []byte, spreadsheetId, sheetname string, records [][]interface{}) error {
ctx := context.Background()
config, err := google.ConfigFromJSON(credentials, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
return err
}
token := oauth2.Token{}
json.Unmarshal(tokenConfig, &token)
client := config.Client(ctx, &token)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Print(err)
return err
}
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetname, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response.HTTPStatusCode != 200 {
return err
}
return nil
}
这是我针对错误情况 1 的代码:-
func SaveRecords(reqId string, credentials, tokenConfig []byte, spreadsheetId, sheetname string,sheetid string, records [][]interface{}) error {
ctx := context.Background()
config, err := google.ConfigFromJSON(credentials, "https://www.googleapis.com/auth/spreadsheets")
if err != nil {
return err
}
token := oauth2.Token{}
json.Unmarshal(tokenConfig, &token)
client := config.Client(ctx, &token)
srv, err := sheets.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Print(err)
return err
}
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetid, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response.HTTPStatusCode != 200 {
return err
}
return nil
}
我相信你的目标如下。
- 您想使用 googleapis for golang 将
records
的值附加到 Spreadsheet。- 你想在这种情况下使用 sheet ID。
- 您已经能够使用表格 API. 获取值并将值放入 Google Spreadsheet
这样的话,下面的修改怎么样?
很遗憾,sheet ID 不能直接用于srv.Spreadsheets.Values.Append()
。我认为这就是你的问题的原因。所以,在这种情况下,首先需要将sheet ID转换为sheet名称。
修改后的脚本:
发件人:
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetname, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response.HTTPStatusCode != 200 {
return err
}
收件人:
sheetid := 0 // Please set sheet ID.
spreadsheetId := "###" // Please set Spreadsheet ID
records := [][]interface{}{{"a1", "b1", "c1"}} // This is a sample value.
// 1. Convert sheet ID to sheet name.
response1, err := srv.Spreadsheets.Get(spreadsheetId).Fields("sheets(properties(sheetId,title))").Do()
if err != nil || response1.HTTPStatusCode != 200 {
return err
}
sheetName := ""
for _, v := range response1.Sheets {
prop := v.Properties
sheetID := prop.SheetId
if sheetID == int64(sheetid) {
sheetName = prop.Title
break
}
}
// 2. Append value to the sheet.
valueInputOption := "USER_ENTERED"
insertDataOption := "INSERT_ROWS"
rb := &sheets.ValueRange{
Values: records,
}
response2, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetName, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do()
if err != nil || response2.HTTPStatusCode != 200 {
return err
}
- 当此脚本为 运行 时,sheet ID 将转换为 sheet 名称。并且,该值附加到 sheet.
注:
如果可以直接使用sheet名字,脚本就变成如下
sheetName := "Sheet1" // Please set sheet name. spreadsheetId := "###" // Please set Spreadsheet ID records := [][]interface{}{{"a1", "b1", "c1"}} // This is a sample value. valueInputOption := "USER_ENTERED" insertDataOption := "INSERT_ROWS" rb := &sheets.ValueRange{ Values: records, } response2, err := srv.Spreadsheets.Values.Append(spreadsheetId, sheetName, rb).ValueInputOption(valueInputOption).InsertDataOption(insertDataOption).Context(ctx).Do() if err != nil || response2.HTTPStatusCode != 200 { return err }