404尝试以json格式获取工作表数据时 [英] 404 while trying to fetch sheet data as json
本文介绍了404尝试以json格式获取工作表数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过将其作为json终结点调用来加载工作表内容。
fetch("https://spreadsheets.google.com/feeds/list/MYSHEETID/1/public/values?alt=json")
.then(res => res.json())
.then(json => {
const data = [] /* this array will eventually be populated with the contents of the spreadsheet's rows */
我正在使用https://benborgers.com/posts/google-sheets-json
中介绍的方法它大约30%的时间有效,另外70%的时间我得到的是404。我已经和它斗争了好几天了,我想不出它为什么失败了。
推荐答案
您需要共享电子表格,以便至少允许所有人阅读。然后
function getJson(id,gid){
var txt = UrlFetchApp.fetch(`https://docs.google.com/spreadsheets/d/${id}/gviz/tq?tqx=out:json&tq&gid=${gid}`).getContentText();
var jsonString = txt.match(/(?<="table":).*(?=});)/g)[0]
var json = JSON.parse(jsonString)
return(json)
}
- 使用电子表格的ID和工作表的GID(可以是0)
- 请注意,第一行专用于标签,然后行[0]对应于行2,c[0]对应于列A
- 如果要检索B1(第2列第1行):
json.cols[1].label
- 如果要检索B2(第2列,第2行):
json.rows[0].c[1].v
这篇关于404尝试以json格式获取工作表数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文