谷歌表格数据到 json

标签 go google-apps-script

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.

1年前关闭。




Improve this question




我在 Google 表格中有以下示例数据,有没有办法将 Google Apps 脚本与 Go Lang 结合起来,以便我可以在我的 GO 应用中将工作表数据作为 Json 处理?
enter image description here

最佳答案

就在这里:
1- 在 Google 驱动器上,创建新的脚本表并编写如下代码:

function doGet(){
  // Open Google Sheet using ID
  var ss = SpreadsheetApp.openById("1eLDCzOGyctqXgQmC5qULZ4thcNKsIOdKLJoYc762CTk");
  var sheet = ss.getSheetByName("Master");
  // Read all data rows from Google Sheet
  const values = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
  // Converts data rows in json format
  const result = JSON.stringify(values.map(([a,b]) => ({ProductId: a,ProductName:b,})));
  // Returns Result
  return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}
发布脚本,第一次发布时可能会被要求授权,所以需要接受(注意:每次修改,都必须在另一个版本号下发布)
enter image description here
设置对任何人的访问权限,甚至是匿名的。并部署。
enter image description here
使用您的谷歌帐户登录。使用下面的不安全选项。
enter image description here
允许获取您的网络应用 API 的 URL。
enter image description here
2- 在 Go Lang 中,使用之前发布步骤生成的链接编写以下代码:
package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
    "os"
)

type Product struct {
    ProductId   uint   `json:"ProductId"`
    ProductName string `json:"ProductName"`
}

func main() {
    // Read the returned response
    url := "https://script.googleusercontent.com/macros/echo?user_content_key=WBSJPDNSN6X1FCYeXsR6TDaDval0vdvmSoMmXFhGbt5sfK0ia80Dp7kPD27GLpZbYz8vrwfDiUecI2oGMjEtgfL5o8Da25T1m5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnGb6k9xaGtOX6M1tIiG811CRpk9nXl8ZKS7UJTno1dvQXMe1kqfAj8WxsSkLor-EqzOmbnRGq-tk&lib=M0B6GXYh0EOYMkP7qr1Xy9xw8GuJxFqGH"
    resp, err := http.Get(url)
    if err != nil {
        log.Fatal(err)
    }

    defer resp.Body.Close()

    // Read the body of the response
    htmlData, err := ioutil.ReadAll(resp.Body)

    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }

    // print out
    fmt.Println(string(htmlData)) // The data is returned as []byte, so string required to display it correctly

    // Unmarshall the returned []byte into json
    var products []Product
    json.Unmarshal([]byte(htmlData), &products)
    fmt.Printf("id: %v, description: %s", products[0].ProductId, products[0].ProductName)
}
运行上面的脚本,你会得到:
PS D:\> go run gsheet.go
[{"ProductId":1,"ProductName":"Helmet"},{"ProductId":2,"ProductName":"Glove"},{"ProductId":3,"ProductName":"Detecttor"}]
id: 1, description: Helmet
笔记:
在 go 代码中,您需要确保以下内容:
1-解码目标结构必须匹配数据
2- 字段类型应匹配,例如 ProductID应该是 uint如果定义为 string,它将失败
3- 输出中的 json OP 应该与使用的标签匹配,例如,使用 json:"id"而不是 json:"ProductId"对于 ProductId将失败

关于谷歌表格数据到 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64439336/

相关文章:

debugging - GoLand远程调试显示 "could not find <file>"

go - 如何避免这个 golang 程序中的死锁?

google-apps-script - 使用 Google App 脚本更改 Google Sheet 中部分单元格值的字体粗细

javascript - 更改 Google 日历中事件的颜色

mysql - 使用 JDBC executeBatch() 通过 appscript 批量插入 Cloud SQL 消耗太多时间

google-apps-script - 如何在 Gmail 过滤器中使用 "not"?

database - 无法使用 go_client PUT GridDB 容器

go - 在 GO 中使用构建器的继承

json - Golang Unmarshal slice 类型接口(interface)

curl - UrlFetch 相当于 CURL -u "<username>:<password>"