0

Hi I'm using this script to create and email a sheet from google sheet that works great but i would also like to have the script attach all the file that are in a specific folder from google drive and then email my google sheet and the files from the folder in one email.

'''

function Test() {


  var source = SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheet = spreadsheetId ? SpreadsheetApp.openById(spreadsheetId) 
  :SpreadsheetApp.getActiveSpreadsheet();
  var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
  var body = 'test'
  var email = Session.getActiveUser().getEmail();
  var token = ScriptApp.getOAuthToken();
  var ssID = spreadsheetId;
  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                          "?format=pdf&"+
                          '&portrait=true' + //orientation, false for landscape
                          '&scale=4' +
                          '&top_margin=0.25' +              //All four margins must be set!
                          '&bottom_margin=0.25' +           //All four margins must be set!
                          '&left_margin=0.05' +             //All four margins must be set!
                          '&right_margin=0.05' +            //All four margins must be set!
                          '&horizontal_alignment=CENTER';   //LEFT/CENTER/RIGHT



  var response = UrlFetchApp.fetch(url, {
        headers: {
            'Authorization': 'Bearer ' + token
        }
    });


   var att = response.getBlob().setName( 'Test.PDF')
   var files = DriveApp.getFolderById('1GW6_ofWqztakjKJ9WWtxo7dhnPX0js').getFiles();
   var attachements = [];
   while (files.hasNext()) {
   var file = files.next();
   attachements.push(file.getAs(MimeType.PDF));

 }

  GmailApp.sendEmail(email, " Subject " , body, {
  attachments: attachements


   });



}

''' when I use " attachments: attachements " I get all the files from the folder as attachment and when I use " attachments: att " I get my google sheet as attachment but i cant seem to figure out how to get my google sheet plus all the files from the folder on the same email

Thanks for you help

Yvan L
  • 233
  • 3
  • 12

1 Answers1

0
  • You want to send an email including the attachment files by including all blobs.

If my understanding is correct, how about the following modification?

From:

var attachements = [];

To:

var attachements = [att];
  • In this case, at first, att is put to the array of attachements. And all files in the folder are added to attachements.

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hi Tanaike if you wouldn't mine me asking one more question would there be a way to save all the files from the folder and the attach sheet to one pdf in the same folder – Yvan L Jan 23 '20 at 01:29
  • I've tried like this but it's not working var pdf = attachements; folder.createFile(pdf) I get this error Cannot find method createFile(DriveApp.FileIterator) – Yvan L Jan 23 '20 at 01:30
  • @Yvan L Thank you for replying. I'm glad your issue was resolved. About your new question, I would like to support you. But I have to apologize for my poor English skill. About your new question, unfortunately, I'm not sure whether I could correctly understand about it. You want to merge several PDF files as one PDF file. If my understanding is correct, is this thread useful for your situation? https://stackoverflow.com/q/15414077/7108653 If I misunderstood your new question and this was not useful for your situation, I apologize. – Tanaike Jan 23 '20 at 01:42
  • Thank for the link Tanaike it looks more complex then i first thought – Yvan L Jan 23 '20 at 02:14
  • @Yvan L If you want to merge several PDF file as one PDF file, unfortunately, in the current stage, there are no built-in methods for directly achieving it in the methods of Google Apps Script. So it is required to use the workaround. By this, I think that several answers have been posted. When you could find the answer for resolving your issue, I'm glad. – Tanaike Jan 23 '20 at 02:21