javascript Google Docs 电子表格转 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23641492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Docs Spreadsheet to JSON
提问by user2727195
I've seen numerous articles on this but they seem outdated, for instance none of the Google Docs Spreadsheet urls has key parameter. I read this as well: JSON data from google spreadsheet
我已经看过很多关于此的文章,但它们似乎已经过时,例如,Google Docs 电子表格 url 都没有关键参数。我也读过这个: 来自谷歌电子表格的 JSON 数据
Then I read this to access data https://developers.google.com/gdata/samples/spreadsheet_sample
然后我读这个来访问数据 https://developers.google.com/gdata/samples/spreadsheet_sample
My spreadsheet exists at: https://docs.google.com/spreadsheets/d/1SKI5773_68HiSve1fsz7fr4gotjFWHB7KBuVsOlLz6I/edit#gid=0
我的电子表格存在于:https: //docs.google.com/spreadsheets/d/1SKI5773_68HiSve1fsz7fr4gotjFWHB7KBuVsOlLz6I/edit#gid=0
I've tried using this code, I think I have a problem with the key or syntax, please guide to fix.
我已经尝试使用此代码,我认为我的密钥或语法有问题,请指导修复。
<script src="http://spreadsheets.google.com/feeds/feed/1SKI5773_68HiSve1fsz7fr4gotjFWHB7KBuVsOlLz6I/worksheet/public/basic?alt=json-in-script&callback=importGSS"></script>
<script type="text/javascript">
function importGSS(json) {
console.log('finished');
}
</script>
采纳答案by sfletche
The src
attribute in your script
tag is an invalid link (and you can see this for yourself by viewing your link directly in a browser).
src
您script
标记中的属性是无效链接(您可以通过直接在浏览器中查看链接来亲自查看此信息)。
The feed/key/worksheet
section of the URL has the right key
but the wrong feed
and worksheet
.
feed/key/worksheet
URL的部分有正确key
但错误的feed
和worksheet
。
In the URL, replace "feed
" with either "cells
" (separate value for each cell) or "list
" (separate value for each row).
在 URL 中,将“ feed
”替换为“ cells
”(每个单元格的单独值)或“ list
”(每行的单独值)。
At the same time, replace "worksheet
" with "od6
" (indicating the leftmost, or default, sheet - see this blog postfor accessing other sheets).
同时,将“ worksheet
”替换为“ od6
”(表示最左边的或默认的工作表 - 请参阅此博客文章以访问其他工作表)。
If you view this new URL directly in a browser, you can see that it returns a meaningful value.
如果您直接在浏览器中查看这个新 URL,您可以看到它返回了一个有意义的值。
Your final script tag might look like this:
您的最终脚本标签可能如下所示:
<script src="https://spreadsheets.google.com/feeds/list/1SKI5773_68HiSve1fsz7fr4gotjFWHB7KBuVsOlLz6I/od6/public/values?alt=json-in-script&callback=importGSS"></script>
For more info, you can see an example on the Google Developers site
有关更多信息,您可以在Google Developers 网站上查看示例
回答by Jerome Louvel
APISpark PaaS has a feature to create and deploy a custom JSON API based on a GSpreadsheet. That might help and give you more control on the web API (CORS support, authentication, custom domain and so on).
APISpark PaaS 具有基于 GSpreadsheet 创建和部署自定义 JSON API 的功能。这可能有助于并让您更好地控制 Web API(CORS 支持、身份验证、自定义域等)。
See the tutorial here: https://apispark.com/docs/tutorials/google-spreadsheet
请参阅此处的教程:https: //apispark.com/docs/tutorials/google-spreadsheet
回答by drmartin
You may consider use an alternative to this request of your sheet data, because this method is deprecated. Anyway, you can still using another feed format, you can see this alternatives in: https://spreadsheets.google.com/feeds/worksheets/your-spreadsheet-id/private/full
您可以考虑使用对您的工作表数据的此请求的替代方法,因为此方法已被弃用。无论如何,您仍然可以使用另一种提要格式,您可以在以下位置看到此替代方案:https: //spreadsheets.google.com/feeds/worksheets/your-spreadsheet-id/private/full
In that result you can see any export formats are availables. Can help you an CSV or alt JSON visualization format?
在该结果中,您可以看到任何可用的导出格式。可以帮助您创建 CSV 或 alt JSON 可视化格式吗?
回答by owyongsk
Another potential solution here is to use this https://gist.github.com/ronaldsmartin/47f5239ab1834c47088eto wrap around your existing spreadsheet.
此处的另一个潜在解决方案是使用此https://gist.github.com/ronaldsmartin/47f5239ab1834c47088e来环绕您现有的电子表格。
Add the id
and sheet
html param to the URL below.
将id
和sheet
html 参数添加到下面的 URL。
https://script.google.com/macros/s/AKfycbzGvKKUIaqsMuCj7-A2YRhR-f7GZjl4kSxSN1YyLkS01_CfiyE/exec
https://script.google.com/macros/s/AKfycbzGvKKUIaqsMuCj7-A2YRhR-f7GZjl4kSxSN1YyLkS01_CfiyE/exec
Eg: your id
is your sheet id which is
例如:您id
的工作表 ID 是
1SKI5773_68HiSve1fsz7fr4gotjFWHB7KBuVsOlLz6I
1SKI5773_68HiSve1fsz7fr4gotjFWHB7KBuVsOlLz6I
and your sheet which is
和你的工作表
Sheet1
Sheet1
In your case you can actually see your data (it's actually working) here as json at
在您的情况下,您实际上可以在此处以 json 的形式查看您的数据(它实际上正在工作)
To be safe, you should deploy the code sheetAsJson.gs
in the github gist above as your own in your Google Drive.
为安全起见,您应该将sheetAsJson.gs
上面 github gist 中的代码作为您自己的代码部署在您的 Google Drive 中。
回答by Jorge Alcantara
You have plenty of possible answers above. For those that come anew, if you're looking for a more controlled JSON generator, check out this gist:
你上面有很多可能的答案。对于那些新来的人,如果您正在寻找更受控制的 JSON 生成器,请查看以下要点:
It takes in a Spreadsheet and returns an array of objects, with the lined that you decide as the headers (defaults to whichever line is frozen)
它接受一个电子表格并返回一个对象数组,其中包含您决定的行作为标题(默认为冻结的任何行)
Cheers,
干杯,