c# - 使用 Google .NET API 复制 Google Docs 电子表格

标签 c# .net google-api google-sheets

我想将现有的 Google 文档电子表格复制到新的 Google 文档电子表格中。我不认为 v2.0 .NET API 可以本地处理(或者如果是的话我找不到类/方法),但是它看起来像 v3.0 protocol 可以,但我不确定如何在当前框架中实现它,或者即使使用当前的 .net api 也是可能的。例如。 ~DocumentsFeed.copy()(伪代码)。

导出到临时 excel 文件然后使用新名称上传是不可能的,因为一些复杂的公式在转换过程中会被弄乱。

我有点 .NET 菜鸟,所以任何信息将不胜感激,例如。如果我只能使用 v3 协议(protocol)(ajax 等)而不是 .NET API,我将如何在 .NET 中执行此操作。

谢谢

编辑:(最后一节课感谢@langsamu 的帮助!)

using System;
using Google.GData.Documents;
using Google.GData.Client;
using Google.GData.Extensions;


public class GoogleDocument
{
    private DocumentsService ds;
    private String username;
    private String password;

    public GoogleDocument(String username, String password)
    {
        this.ds = new DocumentsService("doc service name");
        this.username = username;
        this.password = password;

        this.ds.setUserCredentials(username, password);
        this.ds.QueryClientLoginToken();
    }

    public void copyDocument(String oldFileName, String newFileName)
    {
        SpreadsheetQuery query = new Google.GData.Documents.SpreadsheetQuery();
        query.Title = oldFileName;
        query.TitleExact = true;

        DocumentsFeed feed = this.ds.Query(query);
        AtomEntry entry = feed.Entries[0];

        entry.Title.Text = newFileName;

        var feedUri = new Uri(DocumentsListQuery.documentsBaseUri);
        this.ds.Insert(feedUri, entry);
    }
}

最佳答案

Google.GData.Documents.DocumentsService service = new Google.GData.Documents.DocumentsService("YOUR_APPLICATIONS_NAME");
service.setUserCredentials("YOUR_USERNAME", "YOUR_PASSWORD");

Google.GData.Documents.SpreadsheetQuery query = new Google.GData.Documents.SpreadsheetQuery();
query.Title = "YOUR_SPREADSHEETS_TITLE";
query.TitleExact = true;

Google.GData.Documents.DocumentsFeed feed = service.Query(query);
Google.GData.Client.AtomEntry entry = feed.Entries[0];

var feedUri = new Uri(Google.GData.Documents.DocumentsListQuery.documentsBaseUri);

service.Insert(feedUri, entry);

此解决方案基本上是关于使用 service.Query 检索现有电子表格 ( Document List API )并重新插入它(service.Insert)。

确保替换全部大写的应用程序名称、用户名、密码和电子表格标题。

添加对 Google.GData.Documents 的引用。

这是使用 .NET 4(应该也适用于较低版本)和 Google Documents List Data API v2.0 (DLL 说版本是 1.6.0.0: google-gdata ),似乎使用了 3.0 版本的协议(protocol)。

关于c# - 使用 Google .NET API 复制 Google Docs 电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3715132/

相关文章:

c# - try catch block 以显示错误消息

c# - 层和实体?

c# - IEnumerable IndexOutOfRangeException异常

c# - MVC2 中的可选和命名参数、语言规范

c# - 在没有外部 dll 的情况下制作图像底片的有效方法

javascript - Google Save To Drive API v3 停止工作

google-api - 间歇性 403 访问未配置

c# - 正则表达式获取自定义TAG之间的所有值

.net - 在程序中使用 SQL 数据库的要求

javascript - 无弹出窗口的Google Analytics(分析)API OAuth 2.0授权