Google Apps Script - Send Google Sheet as Email Attachment

霸气de小男生 提交于 2021-02-11 05:51:24

问题


I am currently trying to send a google sheet as an email attachment using Google Apps Script. When I execute the code (shown below) it automatically sends it as a PDF even though I have not specified I want to send it in this format.

Is there a way to send the attachment in Google Sheet format?

{
    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);
    }
}



回答1:


  • The documentation for attachment specifies:

This is a regular Blob

  • And the documentation about the contentType for blobs says:

contentType

The MIME type to convert to. For most blobs, 'application/pdf' is the only valid option.

There are two things you can do

  • If you want to send your Google spreadsheet as a spreadsheet - you need to send the link to the file in the message body, rather than adding the file as attachment
  • If you want to send the file as a different file format (e.g. xls) - you will need to convert it to such beforehand, manually - e.g. as done here


来源:https://stackoverflow.com/questions/62324006/google-apps-script-send-google-sheet-as-email-attachment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!