2009-11-18 44 views
1

我目前正在測試谷歌API。看起來很有希望,但我陷入了一個「簡單」的問題。我想用本地副本更新現有文檔。使用google API從本地文件更新Google文檔可能嗎? (.NET/C#/ WPF)

我的想法是,使用doc下載將所有google文檔下載到文件夾中。這樣可行。在下一次運行中,我會檢查日期,如果遠程文檔較新,請再次抓取它。如果本地文檔較新,請上傳,並替換當前的在線版本。

我無法找到替換文檔的功能。有一個上傳(文件名,doctitle),但是這會創建一個新文檔。是否有人知道這是否可行,並指出我在糾正方向。我是否需要剖析原子提要(文檔內容是否在其中?)。該「字/上傳下載/更改」看上去那麼美好:-)

克里斯

而且任何人誰是有興趣的,它非常簡單,很好用的API。下面是一個簡短WPF例(無憑據,當然)

 var settings = new RequestSettings("GoogleDocumentsSample", _credentials); 
     AllDocuments = new ObservableCollection<Document>(); 

     settings.AutoPaging = true; 
     settings.PageSize = 10; 

     service = new DocumentsService("DocListUploader"); 
     ((GDataRequestFactory)service.RequestFactory).KeepAlive = false; 
     service.setUserCredentials(username, password); 

     //force the service to authenticate 
     var query = new DocumentsListQuery {NumberToRetrieve = 1}; 
     service.Query(query); 



     var request = new DocumentsRequest(settings); 


     Feed<Document> feed = request.GetEverything(); 
     // this takes care of paging the results in 
     foreach (Document entry in feed.Entries) 
     { 
      AllDocuments.Add(entry); 
      if (entry.Type == Document.DocumentType.Document) 
      { 
       var fI = new FileInfo(@"somepath" + entry.DocumentId + ".doc"); 

       if (!fI.Exists || fI.LastWriteTime < entry.Updated) 
       { 
        Debug.WriteLine("Download doc " + entry.DocumentId); 
        var type = Document.DownloadType.doc; 
        Stream stream = request.Download(entry, type); 

        if (fI.Exists) fI.Delete(); 

        Stream file = fI.OpenWrite(); 

        int nBytes = 2048; 
        int count = 0; 
        Byte[] arr = new Byte[nBytes]; 

        do 
        { 
         count = stream.Read(arr, 0, nBytes); 
         file.Write(arr, 0, count); 

        } while (count > 0); 
        file.Flush(); 
        file.Close(); 

        stream.Close(); 

        fI.CreationTimeUtc = entry.Updated; 
        fI.LastWriteTimeUtc = entry.Updated; 

       } 
       else 
       { 
        if (entry.Updated == fI.LastWriteTime) 
        { 
         Debug.WriteLine("Document up to date " + entry.DocumentId); 
        } 
        else 
        { 
         Debug.WriteLine(String.Format("Local version newer {0} [LOCAL {1}] [REMOTE {2}]", entry.DocumentId, fI.LastWriteTimeUtc, entry.Updated)); 
         service.UploadDocument(fI.FullName, entry.Title); 

        } 
       } 

      } 

     } 

回答

相關問題