如何使用 Google Docs C# API 访问公共电子表格?
我使用 Google Docs .NET API 编写了一个 C# 程序,在给定用户名、密码、电子表格名称和工作表名称的情况下将 Google 工作表读入 DataTable。这一切都工作正常,但编程模型似乎围绕着为电子表格服务提供一组凭据,然后削减生成的提要以获得特定的电子表格/工作表,即
SpreadsheetsService service = new SpreadsheetsService("Application-Name");
service.setUserCredentials(userName, password);
SpreadsheetQuery spreadsheetQuery = new SpreadsheetQuery();
SpreadsheetFeed spreadsheetFeed = service.Query(spreadsheetQuery);
SpreadsheetEntry spreadsheetEntry = (SpreadsheetEntry)(from entries in spreadsheetFeed.Entries
where entries.Title.Text == spreadsheetName
select entries).SingleOrDefault();
现在我有兴趣扩展我的程序的功能从公共 Google 电子表格中读取。也就是说,给定公共 Google 电子表格的网址(例如“https:// Spreadsheets.google.com/ccc?key=BUNCH_OF_LETTERS_HERE&hl=en"),我想获取与该文档对应的 SpreadsheetEntry 对象。
到目前为止,我一直在使用的方法显然似乎无法扩展以允许这样做,所以我想知道是否有人知道通过他们的 API 访问公共 Google 文档的正确方法?
I've written a C# program using the Google Docs .NET API to read a Google worksheet into a DataTable given a user name, password, spreadsheet name, and worksheet name. This all works fine, but the programming model seems to revolve around giving the spreadsheet service a set of credentials, and then paring down the resulting feed to get a specific spreadsheet/worksheet, i.e.
SpreadsheetsService service = new SpreadsheetsService("Application-Name");
service.setUserCredentials(userName, password);
SpreadsheetQuery spreadsheetQuery = new SpreadsheetQuery();
SpreadsheetFeed spreadsheetFeed = service.Query(spreadsheetQuery);
SpreadsheetEntry spreadsheetEntry = (SpreadsheetEntry)(from entries in spreadsheetFeed.Entries
where entries.Title.Text == spreadsheetName
select entries).SingleOrDefault();
Now I'm interested in extending the functionality of my program to read from public Google spreadsheets. That is, given the URL of a public Google spreadsheet (e.g. "https://spreadsheets.google.com/ccc?key=BUNCH_OF_LETTERS_HERE&hl=en"), I want to get the SpreadsheetEntry object corresponding to that document.
The method I've been using thus far obviously does not seem to extend to allow this, so I was wondering if anyone knows the proper way to access a public Google document through their API?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以简单地检索公共页面,然后解析行的 HTML 源。
请求标头:
电子表格网格 - 列标签:
第一行作为用户定义的列名称:
其余行作为用户数据:
行号:
行数据单元格:
您可以使用 Html Agility Pack 或您自己的解析器。
另一种选择是将公共电子表格下载为 CSV 或文本格式的
文件 文件下载请求标头:
请注意 id 参数。你必须从 html 源中提取它,如下所示:
我使用 提琴手 捕获包括 SSL 加密消息在内的通信。
You could simply retrieve the public page and then parse HTML source for the rows.
Request headers:
Spreadsheet grid - Column labels:
First row as user defined column names:
Rest of the rows as user data:
Row number:
Row data cells:
You could use Html Agility Pack or your own parser.
Another option is downloading the public spreadsheet as file in CSV or text format
File download request headers:
Notice the id parameter. You have to extract it from the html source like below:
I used Fiddler to capture communication including SSL encrypted messages.
我试图阅读带有多个选项卡的电子表格,当我实现这些其他答案(即向页面发出直接网络请求)时返回的 html 和 javascript 几乎无法读取。经过进一步挖掘,我发现如果您确实想获取公共 Google 电子表格的值而不处理任何 OAuth 内容,您首先需要转到 https://console.developers.google.com/apis,创建一个项目,转到仪表板,然后转到凭据,单击“创建凭据”并设置创建一个新的服务帐户密钥(选择 json 并保存 json 文件。然后您可以使用它来获取任何公共电子表格):
I was trying to read a spreadsheet with multiple tabs and the html and javascript that came back when I implemented these other answers(i.e. making a direct web request to the page) was almost unreadable. After further digging, I found that if you really want to get the values of a public Google spreadsheet without having to deal with any OAuth stuff, you first need to go to https://console.developers.google.com/apis, create a project, go to the dashboard, and go to credentials, click 'create credentials' and set up a new service account key(choose json and save the json file. Then you can use it to get any public spreadsheet):
Google 文档列表 API 不支持似乎为此设置(参见ahab的回答)。
The Google Docs List API doesn't seem setup for that (see ahab's answer).