簡體   English   中英

如何將 Google 表格作為 email 附件發送?

[英]How to send a Google Sheet as an email attachment?

我目前正在嘗試使用 Google Apps 腳本將 Google 表格作為 email 附件發送。 當我執行代碼(如下所示)時,它會自動將其作為 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);
    }
}

這是一個普通的 Blob

內容類型

要轉換為的 MIME 類型。 對於大多數 blob,“application/pdf”是唯一有效的選項。

你可以做兩件事

  • 如果您想將 Google 電子表格作為電子表格發送 - 您需要將鏈接發送到郵件正文中的文件,而不是將文件添加為附件
  • 如果您想以不同的文件格式(例如 xls)發送文件 - 您需要事先手動將其轉換為這種格式 - 例如在這里完成

以下代碼將完成工作

const file = DriveApp.getFileById("xxxxxxxxxxxxxxxx");

GmailApp.sendEmail(
    'email',
    'subject',
    new_emailbody,
    {
      htmlBody: new_emailbody,
      attachments: [file]
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM