gpt4 book ai didi

c# - 如何使用 Google Docs C# API 访问公共(public)电子表格?

转载 作者:太空狗 更新时间:2023-10-29 23:18:56 24 4
gpt4 key购买 nike

我使用 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();

现在我有兴趣扩展我的程序的功能,以便从公共(public) Google 电子表格中读取数据。也就是说,给定公共(public) Google 电子表格的 URL(例如“https://spreadsheets.google.com/ccc?key=BUNCH_OF_LETTERS_HERE&hl=en”),我想获取与该文档对应的 SpreadsheetEntry 对象。

到目前为止我一直在使用的方法显然没有扩展到允许这样做,所以我想知道是否有人知道通过他们的 API 访问公共(public) Google 文档的正确方法?

最佳答案

您可以简单地检索公共(public)页面,然后解析行的 HTML 源代码。

请求 header :

GET https://docs.google.com/spreadsheet/ccc?key=0Au1XehjMeFfYdEE2d0RDSk1FMEMtRjM0MmllUWdoTkE HTTP/1.1
Host: docs.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=68126eb9eb12adef:FF=0:TM=1331371284:LM=1331371284:S=Bkm9mYX8pYy9a4h9

电子表格网格 - 列标签:

<table class="waffle" cellspacing="0" cellpadding="0">

<thead><tr>
<th class="row-header freezebar-top-left"></th>
<th style="width:120px" class="column-headers-background">A</th>
<th style="width:120px" class="column-headers-background">B</th>
<th style="width:120px" class="column-headers-background">C</th>
</tr></thead>

<tbody>

第一行作为用户定义的列名:

<tr style='height:16px;'>
<th style="height: 16px;" class="row-headers-background"><div class="row-header-wrapper" style="height: 16px;">1</div></th>

<td class="g-0-GjugEgs0" dir="auto-ltr">username</td>
<td class="g-0-GjugEgs0" dir="auto-ltr">create</td>
<td class="g-0-GjugEgs0" dir="auto-ltr">expire</td>
</tr>

其余行作为用户数据:行号:

<tr style='height:16px;'>
<th style="height: 16px;" class="row-headers-background"><div class="row-header-wrapper" style="height: 16px;">2</div></th>

行数据单元格:

<td class="g-0-GjugEgs2">3600001</td>
<td class="g-0-GjugEgs2">2012</td>
<td class="g-0-GjugEgs2">2013</td>
</tr>

你可以使用 Html Agility Pack或您自己的解析器。

另一种选择是将公共(public)电子表格下载为 CSV 或文本格式的文件

文件下载请求头:

GET https://docs.google.com/spreadsheet/fm?id=tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644&fmcmd=23&gid=0 HTTP/1.1
Host: docs.google.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: https://docs.google.com/spreadsheet/ccc?key=0Au1XehjMeFfYdEE2d0RDSk1FMEMtRjM0MmllUWdoTkE
Accept-Encoding: gzip,deflate,sdch
Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-9,utf-8;q=0.7,*;q=0.3
Cookie: PREF=ID=68126eb9eb12adef:FF=0:TM=1331371284:LM=1331371284:S=Bkm9mYX8pYy9a4h9; lbcs=0

注意 id 参数。您必须像下面这样从 html 源中提取它:

 ...
var mergedConfig = {"formToken":"Kg2uOS1UniIe0yFks5zcDZDsGQ=","formStatus":false,"id":"tA6wDCJME0C-F342ieQghNA.PREF_08255578241116458508.3736592253424693644",
...

我用了Fiddler捕获包括 SSL 加密消息在内的通信。

关于c# - 如何使用 Google Docs C# API 访问公共(public)电子表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3443490/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com