【问题标题】:How to Upload Excel file and convert that to Google spreadsheet programmatically?如何上传 Excel 文件并以编程方式将其转换为 Google 电子表格?
【发布时间】:2014-09-18 04:45:56
【问题描述】:

我正在编写桌面程序以与Google spreadsheets 交互,为此我需要上传excel sheet 并通过编程方式将其转换为Google spreadsheet。我检查了谷歌驱动器和谷歌电子表格 API 找不到这样做的方法。如果有人可以建议这样做的方法,那将是非常有帮助的。

这是我现在拥有的代码,它可以正确上传 excel 文件,但没有转换为 Google 电子表格。

            Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
            body.Title = "My document";
            body.Description = "A test document";
            body.MimeType = "application/vnd.ms-excel";

            byte[] byteArray = System.IO.File.ReadAllBytes("test.xlsx");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "application/vnd.google-apps.spreadsheet");
            request.Convert = true;
            request.Upload();

            Google.Apis.Drive.v2.Data.File file = request.ResponseBody;
            Console.WriteLine("File id: " + file.Id);
            Console.WriteLine("Press Enter to end this process.");
            Console.ReadLine();

【问题讨论】:

标签: c# google-apps-script google-drive-api google-spreadsheet-api


【解决方案1】:

我使用 Drive API,下面是我用来转换 XLS 文件的 sn-p 代码

function convert(xlsxFileId, name) { 
  var xlsxBlob = xlsxFileId;
  var file = {
    title: name,
    //Which Drive Folder do you want the file to be placed in
    parents: [{'id':'FOLDER_ID'}],
    key: 'XXXX',
    value: 'XXXX',
    visibility: 'PRIVATE'
  };

  file = Drive.Files.insert(file, xlsxBlob, {
    convert: true
  });
}

【讨论】:

  • 这是 goole 应用脚本吗?
  • 是的,但 Drive 是必须为该项目激活的扩展服务。转到脚本编辑器中的资源菜单并按照说明进行操作。
  • 感谢cmets,我需要使用C#从客户端进行转换
猜你喜欢
  • 1970-01-01
  • 2014-06-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-19
  • 2021-07-21
  • 1970-01-01
  • 2011-01-30
  • 1970-01-01
相关资源
最近更新 更多