无法使用 Google 脚本通过 Gmail 发送 excel 附件

Unable to send excel attachment by Gmail using Google Script

我正在使用 Google Apps 脚本 (*.gs) 通过 Gmail 发送邮件。邮件是工作文件,甚至使用 public URL 中可用图像的附件也工作正常。 我正在使用

var file = DriveApp.getFileById("Google file ID") 

获取文件并附加。我收到错误

The feature you are attempting to use has been disabled by your domain administrator.

我是管理员,想确切地了解在哪里启用此功能。有人可以指导我吗?

此外,我检查了我的 Google 脚本代码的 项目属性 ,发现脚本需要以下 4 个 OAuth 范围:这是范围,旁边是当我尝试在我的浏览器中访问它们时,它们是响应。

  1. https://mail.google.com/
    -- 能够访问邮件。
  2. https://www.googleapis.com/auth/drive
    -- 文本“drive”出现在浏览器顶部
  3. https://www.googleapis.com/auth/script.external_request
    -- 页面报错“Not Found. Error 404”
  4. https://www.googleapis.com/auth/spreadsheets.currentonly
    -- 页面报错“Not Found. Error 404”

以下是我尝试过的 3 种方案。第三个失败了。

  1. 发送不带任何附件的邮件:工作正常。
function SimpleMail(){
  GmailApp.sendEmail(<recipient email>, 'MAIL without any ATTACHMENT', 
 'Hi, \nPlease see mail without any attachment' )  
}
  1. 从 public URL 发送带附件的邮件:使用附加图片效果很好。
function sendAttachment(){
        var attachmentUlr = "<URL of an Image on a public facing website>" ;
        var fileBlob = UrlFetchApp
          .fetch(attachmentUlr)
          .getBlob()
          .setName("fileBlob"+'.jpg');  
        GmailApp.sendEmail(<recipient email>, 'MAIL with ATTACHMENT', 
 'Hi, \nPlease see mail with image from a website as attachment', {attachments:[fileBlob]})
}
  1. 带有来自我的 Google 驱动器 URL 的附件:失败,
function sendAttachment(){
        var attachmentUlr = DriveApp.getFileById('<Google File ID>');
        var fileBlob = UrlFetchApp
            .fetch(attachmentUlr)
            .getBlob()
            .setName("fileBlob"+'.xlsx');  
        GmailApp.sendEmail(<recipient email>, 'MAIL with ATTACHMENT', 
 'Hi, \nPlease see mail with file from Drive as attachment', {attachments:[fileBlob]})
}

这不是 return 文件 URL

DriveApp.getFileById('<Google File ID>')

这解释了第三个代码示例失败的原因

试试这个

function sendAttachment(){
   var file = DriveApp.getFileById('<Google File ID>');
   var fileBlob = file.getBlob();
   GmailApp.sendEmail(
      <recipient email>, 
      'MAIL with ATTACHMENT', 
      'Hi, \nPlease see mail with file from Drive as attachment', 
      {attachments:[fileBlob]}
   )
}

相关

  • Send email with picture attachment and body using google script
  • Sending Multiple attachments with Google Script from Google Drive

我以管理员身份启用 Drive SDK API 后,问题得到解决。 转到管理面板 > 应用程序 > G Suite > 驱动器和文档设置 > 功能和应用程序。 启用/选中复选框 - “允许用户使用 Drive SDK API 访问 Google Drive”。

启用此设置后,我的代码能够从 google 驱动器中选择文件作为附件。