2010-08-09 137 views
5

我已經使用Google Docs .NET API編寫了一個C#程序,用於在給定用戶名,密碼,電子表格名稱和工作表名稱的情況下將Google工作表讀取到DataTable中。這一切工作正常,但編程模式似乎圍繞着給電子表格服務的一組憑據,然後削減下來得到的進料得到一個特定的電子表格/工作表,即如何使用Google Docs C#API訪問公用電子表格?

現在我很感興趣將我的程序的功能擴展爲從公開的Google電子表格中讀取。也就是說,考慮到Google公開電子表格的網址(例如「https://spreadsheets.google.com/ccc?key=BUNCH_OF_LETTERS_HERE&hl=en」),我想要獲取與該文檔相對應的SpreadsheetEntry對象。

到目前爲止,我一直在使用的方法顯然似乎沒有擴展到允許這個,所以我想知道是否有人知道通過他們的API訪問公共Google文檔的正確方法?

回答

4

您可以簡單地檢索公共頁面,然後解析行的HTML源代碼。

請求頭:

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或您自己的解析器。

另一種選擇是下載電子表格市民在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加密的郵件。

相關問題