【问题标题】:How to send a Google Sheet as an email attachment?如何将 Google 表格作为电子邮件附件发送?
【发布时间】:2020-10-01 01:47:18
【问题描述】:

我目前正在尝试使用 Google Apps 脚本将 Google 表格作为电子邮件附件发送。当我执行代码(如下所示)时,它会自动将其作为 PDF 发送,即使我没有指定我想以这种格式发送。

有没有办法以 Google 表格格式发送附件?

function emailGoogleSheet()
{
    try
    {
      // Get file from Google Drive
      var googleSheet = DriveApp.getFileById("xxxxxxxxxxxxxxxx")
      
      // Send email with file attached
      MailApp.sendEmail("name@email.co.uk", "Report", "Please see the attached report.", {attachments: googleSheet})
    }
    catch (exception)
    {
        Logger.log(exception);
    }
}

【问题讨论】:

    标签: google-apps-script google-drive-api gmail-api


    【解决方案1】:

    这是一个普通的 Blob

    内容类型

    要转换为的 MIME 类型。对于大多数 blob,'application/pdf' 是 唯一有效的选项。

    你可以做两件事

    • 如果您想将 Google 电子表格作为电子表格发送 - 您需要在邮件正文中发送文件的链接,而不是将文件添加为附件
    • 如果您想以不同的文件格式(例如 xls)发送文件 - 您需要事先手动将其转换为这种格式 - 例如完成here

    【讨论】:

    • 感谢 ziganotschka 的回复。这是一个业务应用程序,业务依赖于谷歌,所以我需要使用选项一,它将链接电子邮件中的谷歌表。
    【解决方案2】:

    以下代码将完成工作

    const file = DriveApp.getFileById("xxxxxxxxxxxxxxxx");
    
    GmailApp.sendEmail(
        'email',
        'subject',
        new_emailbody,
        {
          htmlBody: new_emailbody,
          attachments: [file]
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多