1

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 the Google Sheets format?

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

2 Answers2

1

This is a regular Blob

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
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • 1
    Thanks for the response ziganotschka. This is for a business application and the business is google dependant so I will need to use option one which will link the google sheet in the email. – AGB_DT_54302290 Jun 15 '20 at 09:49
0

the following code will do the work

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

GmailApp.sendEmail(
    'email',
    'subject',
    new_emailbody,
    {
      htmlBody: new_emailbody,
      attachments: [file]
    });
liquidkat
  • 566
  • 4
  • 12