Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 谷歌应用程序脚本错误';错误请求';将.xlsm文件传输到GSheet时_Google Apps Script_Google Sheets_Gmail_Email Attachments_Xlsm - Fatal编程技术网

Google apps script 谷歌应用程序脚本错误';错误请求';将.xlsm文件传输到GSheet时

Google apps script 谷歌应用程序脚本错误';错误请求';将.xlsm文件传输到GSheet时,google-apps-script,google-sheets,gmail,email-attachments,xlsm,Google Apps Script,Google Sheets,Gmail,Email Attachments,Xlsm,我的收件箱中有一个Excel文件(.xlsm)作为Gmail附件,我想把它传送到GDrive。应将其保存为GSheet。我正在尝试使用应用程序脚本自动化此过程 不幸的是,启动脚本时出错(API调用drive.files.insert失败,错误为:请求错误)这很奇怪,因为脚本运行了几次,文件可以毫无问题地转换。上周,当我将带有附件的邮件转发给某人时(不要问我上下文在哪里),它就起作用了。但现在,这一切都成了历史,我不知道如何修复错误 我是StackOverflow的新手,我真的很期待你的每一个回答

我的收件箱中有一个Excel文件(.xlsm)作为Gmail附件,我想把它传送到GDrive。应将其保存为GSheet。我正在尝试使用应用程序脚本自动化此过程

不幸的是,启动脚本时出错(API调用drive.files.insert失败,错误为:请求错误)这很奇怪,因为脚本运行了几次,文件可以毫无问题地转换。上周,当我将带有附件的邮件转发给某人时(不要问我上下文在哪里),它就起作用了。但现在,这一切都成了历史,我不知道如何修复错误

我是StackOverflow的新手,我真的很期待你的每一个回答。多谢各位

代码如下:

function importFunction() {

  var threads =  GmailApp.search('CC520_Report_Lukas_GAS_ABC');                             
  var messages = threads[0].getMessages();    
  var message = messages[0];                                                                
  var attachment = message.getAttachments()[0];                                            

  var resource = {
    title: 'NewFileLukas',                                
    mimeType: MimeType.GOOGLE_SHEETS,
    parents: [{id: 'xxxxx6BD1SIfI0Cz5bmGahzSlHUxxxxxx'}], 
};

var insert = Drive.Files.insert(resource, attachment);      // Here comes the error. 

我相信你的目标如下

  • 您想将
    .xlsm
    文件转换为Google电子表格
  • 附件
    是您使用的
    .xlsm
    文件
  • 在您的情况下,
    API调用drive.files.insert时出错,错误为:
    drive.files.insert(资源,附件)
    ,出现错误请求。
    • 您想删除此错误
对于这个问题,这个答案如何?我也经历过同样的问题

修改点:
  • .xlsm
    文件的mimeType是
    application/vnd.ms excel.sheet.macroenabled.12
    。在脚本中,当使用
    console.log(attachment.getContentType())
    时,如果
    attachment
    .xlsm
    文件,则返回此类mimeType
  • 当使用驱动API中的“About:get”方法确认导入格式时,
    application/vnd.ms excel.sheet.macroenabled.12
    似乎可以转换为
    application/vnd.google apps.spreadsheet
    。这可以在驱动器APIv2和v3上看到。
    • 但是,当
      应用程序/vnd.ms excel.sheet.macroenabled.12
      数据用于
      驱动.Files.insert(资源,附件)
      时,会发生错误。在本例中,我确认,即使在使用驱动器API v3测试时,也会出现相同的问题
    • 我认为这种转变可能还没有反映出来。我还相信,这将在未来的更新中进行修改
因此,作为当前的解决方法,我建议通过更改blob的mimeType,将
.xlsm
文件转换为Google电子表格

修改脚本: 修改脚本时,请按以下方式修改

发件人: 致: 或

注:
  • 在我的环境中,我可以确认通过将mimeType更改为
    mimeType.MICROSOFT_EXCEL
    ,可以将
    .xlsm
    文件转换为Google电子表格
  • 在这种情况下,
    .xlsm
    文件的宏不会转换为Google Apps脚本。而且,在将
    .xlsm
    文件转换为Google电子表格后,当Google电子表格转换为excel文件时,不包括宏。我想这就是规格。所以请小心这个
参考:

能否添加行
Logger.log(message.getSubject())
然后从Gmail用户界面识别主题为的电子邮件。查看附件,看看可以转换的附件和不能转换的附件之间有什么区别。
var attachment = message.getAttachments()[0];
var attachment = message.getAttachments()[0].setContentType(MimeType.MICROSOFT_EXCEL);
var attachment = message.getAttachments()[0];
if (attachment.getContentType() == "application/vnd.ms-excel.sheet.macroenabled.12") {
  attachment.setContentType(MimeType.MICROSOFT_EXCEL);
}