【问题标题】:Convert mimeType: from application/octate-stream to text/csv - Google Script转换 mime 类型:从 application/octet-stream 到 text/csv - Google Script
【发布时间】:2019-12-04 15:12:06
【问题描述】:

我想从电子邮件中获取 csv 文件附件 (msg_att),获取数据,然后将其复制到电子表格文件中。我正在研究谷歌脚本。 到目前为止,我所做的如下:

  var newFile=DriveApp.createFile(msg_att).setName("GDriveData")
  var attachmentID = newFile.getId()
  var date = new Date();
  var files = DriveApp.getFileById(attachmentID)
  var xlsFile = files.getMimeType();  
  Logger.log(xlsFile);
  var newSheet =  Drive.Files.copy({}, attachmentID, {convert: true});

不幸的是,最后一行返回以下错误:

对 drive.files.copy 的 API 调用失败并出现错误:不支持请求的转换

并且 MimeType 是 application/octate-stream

我调查了这个问题,发现 mimeType 应该是“text/csv”,因为我想获取 csv 附件。

有什么方法可以将 mimeType: 从 application/octate-stream 转换为 text/csv 吗?

我试过了,但它不起作用:

 var newFile=DriveApp.createFile(msg_att).setName("GDriveData").setMimeType('text/csv')

我也试过了,还是不行:

var newFile = DriveApp.createFile('name',msg_att,'text/csv');

【问题讨论】:

  • Logger.log(msg_att.getDataAsString())Logger.log(newFile.getBlob().getDataAsString())放在var newFile=DriveApp.createFile(msg_att).setName("GDriveData")之后运行脚本,你会在日志中看到什么值?

标签: javascript google-apps-script google-sheets


【解决方案1】:

假设msg_att实际上是一个CSV,可以在createFile()中设置MIME类型:createFile(name, content, mimeType)

【讨论】:

  • 您能否提供一些有关如何定义/获取msg_att 的详细信息?
  • 我感觉问题可能出在文件本身上。我刚刚使用您的代码版本进行了测试,并在记录 MIME 类型时得到了“文本/csv”。您能否确认该文件确实是 CSV,而不是 .xls 文件或类似文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-15
  • 2011-09-19
  • 2017-11-01
  • 2012-03-31
  • 2012-04-16
  • 1970-01-01
相关资源
最近更新 更多