1

我目前正在测试谷歌 API。这似乎很有希望,但我陷入了一个“简单”的问题。我想用本地副本更新现有文档。

我的想法是,使用 doc-download 将所有 google 文档下载到一个文件夹中。这样可行。在下一次运行时,我检查日期,如果远程文档较新,请再次获取它。如果本地文档较新,请上传,并替换当前在线版本。

我找不到替换文档的功能。有一个 Upload(filename, 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);

                    }
                }

            }

        }
4

1 回答 1

2

根据 Docs API docs ;) 您可以替换文档 http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#UpdatingContent的内容

于 2009-11-18T22:59:22.230 回答