如何仅使用 Javascript 访问 Google Sheet 电子表格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4143901/
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
How can I access Google Sheet spreadsheets only with Javascript?
提问by Pratik
I want to access Google Spreadsheets using JavaScript only (no .NET, C#, Java, etc.)
我只想使用 JavaScript 访问 Google 电子表格(没有 .NET、C#、Java 等)
I came hereand was shocked to know that there is NO API for JavaScript to access Google Sheets.
我来到这里并惊讶地发现没有用于 JavaScript 的 API 来访问 Google 表格。
Please tell me how to access (CREATE/EDIT/DELETE) Google Sheets using JavaScript or any of its frameworks like jQuery.
请告诉我如何使用 JavaScript 或其任何框架(如 jQuery)访问(创建/编辑/删除)Google 表格。
采纳答案by Mike McKay
I have created a simple javascript library that retrieves google spreadsheet data (if they are published) via the JSON api:
我创建了一个简单的 javascript 库,它通过 JSON api 检索谷歌电子表格数据(如果它们已发布):
https://github.com/mikeymckay/google-spreadsheet-javascript
https://github.com/mikeymckay/google-spreadsheet-javascript
You can see it in action here:
你可以在这里看到它的实际效果:
http://mikeymckay.github.com/google-spreadsheet-javascript/sample.html
http://mikeymckay.github.com/google-spreadsheet-javascript/sample.html
回答by wescpy
Jan 2018 UPDATE: When I answered this question last year, I neglected to mention a thirdway to access Google APIs with JavaScript, and that would be from Node.js apps using itsclient library, so I added it below.
2018 年 1 月更新:当我去年回答这个问题时,我忽略了使用 JavaScript 访问 Google API的第三种方式,那就是使用其客户端库的Node.js 应用程序,所以我在下面添加了它。
It's Mar 2017, and most of the answers here are outdated -- the accepted answer now refers to a library that uses an older API version. A more current answer: you can access most Google APIswith JavaScript only. Google provides 3 ways to do this today:
现在是2017 年 3 月,这里的大多数答案都已过时——现在接受的答案是指使用旧 API 版本的库。一个更新的答案:您只能使用 JavaScript访问大多数 Google API。Google 今天提供了 3 种方法来做到这一点:
- As mentioned in the answer by Dan Dascalescu, you can use Google Apps Script, the JavaScript-in-Google's-cloud solution. That is, non-Node server-side JS apps outside the browser that run on Google servers.
- You code your apps in the Apps Script code editor, and they can access Google Sheets in two different ways:
- The Spreadsheet Service(native object support; usage guide); native is easier but is generally older than...
- The Google Sheets Advanced Service(directly access the latest Google Sheets REST API [see below]; usage guide)
- Apps Script also powers add-ons, and you can extend Sheets UI functionality with Sheets add-ons (like these)
- You can even write mobile add-onswhich extend the Sheets app on Android
- To learn more about using Apps Script, check out these videosI've created (most involve the use of Sheets)
- You code your apps in the Apps Script code editor, and they can access Google Sheets in two different ways:
- You can also use the Google APIs Client Library for JavaScriptto access the latest Google Sheets REST APIon the client side.
- Here are some generic samplesof using the client library
- The latest Sheets API (v4) was releasedat Google I/O 2016; it's much more powerful than all previous versions, giving developers programmatic access to most features found in the Sheets UI
- Here is the JavaScript quickstartfor the API to help you get started
- Here are sample "recipes"(JSON payloads) for core API requests
- If you're not "allergic" to Python (if you are, just pretend it's pseudocode ;) ), I made several videos with more "real-world" samples of using the API you can learn from and migrate to JS if desired (NOTE: even though it's Python code, most API requests have JSON & easily portable to JS):
- Migrating SQL data to a Sheet(code deep dive post)
- Formatting text using the Sheets API(code deep dive post)
- Generating slides from spreadsheet data(code deep dive post)
- Those and others in the Sheets API video library
- The 3rd way to access Google APIs with JavaScript is from Node.js apps using its client library. It works similarly to using the JavaScript (client) client library described just above, only you'll be accessing the same API from the server-side. Here's the Node.js Quickstart examplefor Sheets. You may find the Python-based videos above to be even more useful as they too access the API from the server-side.
- 正如Dan Dascalescu的回答中提到的,您可以使用Google Apps Script,即 JavaScript-in-Google's-cloud 解决方案。也就是说,在 Google 服务器上运行的浏览器之外的非 Node 服务器端 JS 应用程序。
- 您还可以使用适用于 JavaScript的Google API 客户端库在客户端访问最新的Google Sheets REST API。
- 以下是使用客户端库的一些通用示例
- 最新的床单API(V4)被发布在谷歌I / O 2016年 它比所有以前的版本都强大得多,使开发人员能够以编程方式访问 Sheets UI 中的大多数功能
- 这是API的JavaScript 快速入门,可帮助您入门
- 以下是核心 API 请求的示例“食谱”(JSON 有效负载)
- 如果您对 Python 不“过敏”(如果是,请假装它是伪代码;)),我制作了几个视频,其中包含更多使用 API 的“真实世界”示例,您可以从中学习并在需要时迁移到 JS(注意:即使它是 Python 代码,大多数 API 请求都有 JSON 并且很容易移植到 JS):
- 将 SQL 数据迁移到工作表(代码深入探讨帖子)
- 使用 Sheets API 格式化文本(代码深度挖掘帖子)
- 从电子表格数据生成幻灯片(代码深度挖掘帖子)
- Sheets API 视频库中的那些和其他
- 使用 JavaScript 访问 Google API 的第三种方式是从 Node.js 应用程序使用其客户端库。它的工作方式类似于使用上面描述的 JavaScript(客户端)客户端库,只是您将从服务器端访问相同的 API。这是表格的 Node.js 快速入门示例。您可能会发现上面基于 Python 的视频更有用,因为它们也从服务器端访问 API。
When using the REST API, you need to manage & store your source code as well as perform authorization by rolling your own auth code (see samples above). Apps Script handles this on your behalf, managing the data (reducing the "pain" as mentioned by Ape-inago in their answer), and your code is stored on Google's servers. But your functionality is restricted to what services App Script provides whereas the REST API gives developers much broader access to the API. But hey, it's good to have choices, right? In summary, to answer the OP original question, instead of zero, developers have threeways of accessing Google Sheets using JavaScript.
使用 REST API 时,您需要管理和存储源代码并通过滚动您自己的身份验证代码来执行授权(请参阅上面的示例)。Apps Script 代表您处理这个问题,管理数据(减少 Ape-inago 在他们的回答中提到的“痛苦” ),并且您的代码存储在 Google 的服务器上。但是您的功能仅限于 App Script 提供的服务,而 REST API 为开发人员提供了更广泛的 API 访问权限。但是,嘿,有选择是件好事,对吧?总之,要回答 OP 原始问题,而不是零,开发人员可以通过三种方式使用 JavaScript 访问 Google 表格。
回答by Evan Plaice
Here's the Gist.
这是要点。
You can create a spreadsheet using the Google Sheets API. There is currently no way to delete a spreadsheet using the API (read the documentation). Think of Google Docs API as the route to create and look-up documents.
您可以使用Google Sheets API创建电子表格。目前无法使用 API 删除电子表格(阅读文档)。将 Google Docs API 视为创建和查找文档的途径。
You can add/remove worksheets within the spreadsheet using the worksheet based feeds.
Updating a spreadsheet is done through either list based feedsor cell based feeds.
Reading the spreadsheet can be done through either the Google Spreadsheets APIs mentioned above or, for published sheets only, by using the Google Visualization API Query Languageto query the data (which can return results in CSV, JSON, or HTML table format).
可以通过上面提到的 Google 电子表格 API 读取电子表格,也可以通过使用Google 可视化 API 查询语言查询数据(可以返回 CSV、JSON 或 HTML 表格格式的结果)来完成,仅对于已发布的表格。
Forget jQuery. jQuery is only really valuable if you're traversing the DOM. Since GAS (Google Apps Scripting) doesn't use the DOM jQuery will add no value to your code. Stick to vanilla.
忘记 jQuery。jQuery 只有在遍历 DOM 时才真正有价值。由于 GAS(Google Apps Scripting)不使用 DOM,jQuery 不会为您的代码增加任何价值。坚持香草。
I'm really surprised that nobody has provided this information in an answer yet. Not only canit be done, but it's relatively easy to do using vanilla JS. The only exception being the Google Visualization API which is relatively new (as of 2011). The Visualization API also works exclusively through a HTTP query string URI.
我真的很惊讶还没有人在答案中提供这些信息。它不仅可以完成,而且使用 vanilla JS 相对容易。唯一的例外是 Google Visualization API,它相对较新(截至 2011 年)。可视化 API 还专门通过 HTTP 查询字符串 URI 工作。
回答by robsco
There's a solution that does not require one to publish the spreadsheet. However, the sheet does need to be 'Shared'. More specifically, one needs to share the sheet in a manner where anyone with the link can access the spreadsheet. Once this is done, one can use the Google Sheets HTTP API.
有一种不需要发布电子表格的解决方案。但是,该工作表确实需要“共享”。更具体地说,需要以任何拥有链接的人都可以访问电子表格的方式共享工作表。完成此操作后,就可以使用 Google Sheets HTTP API。
First up, you need an Google API key. Head here: https://developers.google.com/places/web-service/get-api-keyNB. Please be aware of the security ramifications of having an API key made available to the public: https://support.google.com/googleapi/answer/6310037
首先,您需要一个 Google API 密钥。前往此处:https: //developers.google.com/places/web-service/get-api-keyNB。请注意公开 API 密钥的安全后果:https: //support.google.com/googleapi/answer/6310037
Get alldata for a spreadsheet - warning, this can be a lot of data.
获取电子表格的所有数据 - 警告,这可能是大量数据。
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/?key={yourAPIKey}&includeGridData=true
Get sheet metadata
获取工作表元数据
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/?key={yourAPIKey}
Get a range of cells
获取一系列单元格
https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/{sheetName}!{cellRange}?key={yourAPIKey}
Now armed with this information, one can use AJAX to retrieve data and then manipulate it in JavaScript. I would recommend using axios.
现在有了这些信息,就可以使用 AJAX 来检索数据,然后在 JavaScript 中对其进行操作。我建议使用axios。
var url = "https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/?key={yourAPIKey}&includeGridData=true";
axios.get(url)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
回答by Dan Dascalescu
2016 update: The easiest way is to use the Google Apps Script API, in particular the SpreadSheet Service. This works for private sheets, unlike the other answers that require the spreadsheet to be published.
2016 更新:最简单的方法是使用 Google Apps Script API,尤其是SpreadSheet Service。这适用于私人工作表,不像其他需要发布电子表格的答案。
This will let you bind JavaScript code to a Google Sheet, and execute it when the sheet is opened, or when a menu item (that you can define) is selected.
这将允许您将 JavaScript 代码绑定到 Google 表格,并在打开表格或选择菜单项(您可以定义)时执行它。
Here's a Quickstart/Demo. The code looks like this:
这是一个快速入门/演示。代码如下所示:
// Let's say you have a sheet of First, Last, email and you want to return the email of the
// row the user has placed the cursor on.
function getActiveEmail() {
var activeSheet = SpreadsheetApp.getActiveSheet();
var activeRow = .getActiveCell().getRow();
var email = activeSheet.getRange(activeRow, 3).getValue();
return email;
}
You can also publish such scripts as web apps.
您还可以发布诸如 Web 应用程序之类的脚本。
回答by Ape-inago
edit:This was answered before the google doc's api was released. See Evan Plaice's answerand Dan Dascalescu's answerfor more up-to-date information.
编辑:这是在 google doc 的 api 发布之前回答的。有关更多最新信息,请参阅Evan Plaice 的回答和Dan Dascalescu 的回答。
It looks lke you can, but it's a pain to use. It involves using the Google data API.
看起来你可以,但使用起来很痛苦。它涉及使用 Google 数据 API。
http://gdatatips.blogspot.com/2008/12/using-javascript-client-library-w-non.html
http://gdatatips.blogspot.com/2008/12/using-javascript-client-library-w-non.html
"The JavaScript client library has helper methods for Calendar, Contacts, Blogger, and Google Finance. However, you can use it with just about any Google Data API to access authenticated/private feeds. This example uses the DocList API."
“JavaScript 客户端库具有用于日历、通讯录、Blogger 和 Google 财经的辅助方法。但是,您几乎可以将它与任何 Google 数据 API 结合使用,以访问经过身份验证的/私人提要。此示例使用 DocList API。”
and an example of writing a gadget that interfaces with spreadsheets: http://code.google.com/apis/spreadsheets/gadgets/
以及编写与电子表格交互的小工具的示例:http: //code.google.com/apis/spreadsheets/gadgets/
回答by Pratik
'JavaScript accessing Google Docs' would be tedious to implement and moreover Google documentation is also not that simple to get it. I have some good links to share by which you can achieve js access to gdoc:
'JavaScript 访问 Google Docs' 实施起来会很乏味,而且 Google 文档也不是那么容易获得。我有一些很好的链接可以分享,您可以通过它们实现对 gdoc 的 js 访问:
http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingDocs
http://code.google.com/apis/spreadsheets/gadgets/
http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UploadingDocs
http://code.google.com/apis/spreadsheets/gadgets/
May be these would help you out..
也许这些会帮助你..
回答by mjhm
Sorry, this is a lousy answer. Apparently this has been an issuefor almost two years so don't hold your breath.
对不起,这是一个糟糕的答案。显然,这已经是近两年的问题了,所以不要屏住呼吸。
Here is the official request that you can "star"
这是您可以“明星”的官方要求
Probably the closest you can come is rolling your own service with Google App Engine/Pythonand exposing whatever subset you need with your own JS library. Though I'd love to have a better solution myself.
可能最接近的是使用 Google App Engine/Python 推出您自己的服务,并使用您自己的 JS 库公开您需要的任何子集。虽然我很想自己有一个更好的解决方案。
回答by vladkras
回答by Tim
For this type of thing you should use Google Fusion Tables. The APIis designed for that purpose.
对于这种类型的事情,您应该使用Google Fusion Tables。该API是专为目的。