Rename email attachment in Google Apps Script

153 views
Skip to first unread message

Trent Magis

unread,
Jun 21, 2022, 5:41:07 PM6/21/22
to Google Apps Script Community
The entire code is posted at the bottom of this email but the code in questions is as follows.  

function sendEmail_(pdf) {
  var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var coachName = " CoachName ";
  var pdfName = (coachName + month + " 2022 Mileage");
  var attachment = pdf.setName(pdfName);
  GmailApp.sendEmail('em...@email.com', 
                     coachName + month + " Mileage", 
                     coachName + " Mileage for " + month + " 2022" + '\n\n', { 
                       attachments: [attachment]
                       });
  }

I am wanting the pdf attachment to be named "CoachName month Mileage 2022".  When the script runs, the email sends properly, but the attachment is named "mail attachment".  Is there a simple way to get the attached file renamed when I send it?  Thanks



function onEditTrigger(e){
  const specificCell = ("G2");
  const specificValue = ("Yes");
  let cellCheck = (e.range.getA1Notation() == specificCell)
  let valueCheck = (SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('G2').getValue() == specificValue)
 
  if (!(cellCheck && valueCheck)) {
    return
  }
  else {
  exportRangeToPDf();
  }
}

 
function exportRangeToPDf(range) {
  var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var coachName = " CoachName ";
  var pdfName = (coachName + month + " 2022 Mileage");
  var blob,exportUrl,options,pdfFile,response,sheetTabNameToGet,sheetTabId,ss,ssID,url_base;

  range = range ? range : "A1:G50";//Set the default to whatever you want

  ss = SpreadsheetApp.getActiveSpreadsheet();
  ssID = ss.getId();
  sh = ss.getSheetByName(month);
  sheetTabId = sh.getSheetId();
  url_base = ss.getUrl().replace(/edit$/,'');

  exportUrl = url_base + 'export?exportFormat=pdf&format=pdf' +

    '&gid=' + sheetTabId + '&id=' + ssID +
    '&range=' + range +
    //'&range=NamedRange +
    '&size=A4' +     // paper size
    '&portrait=true' +   // orientation, false for landscape
    '&fitw=true' +       // fit to width, false for actual size
    '&sheetnames=true&printtitle=false&pagenumbers=true' + //hide optional headers and footers
    '&gridlines=false' + // hide gridlines
    '&fzr=false';       // do not repeat row headers (frozen rows) on each page

  options = {
    headers: {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken(),
    }
  }

  options.muteHttpExceptions = true;//Make sure this is always set

  response = UrlFetchApp.fetch(exportUrl, options);

  //Logger.log(response.getResponseCode())

  if (response.getResponseCode() !== 200) {
    console.log("Error exporting Sheet to PDF!  Response Code: " + response.getResponseCode());
    return;

  }
 
  blob = response.getBlob();
  blob.setName(pdfName)
  let pdf = DriveApp.getFolderById(''GoogleDriveFolderID')
  .createFile(blob)
  .setName(pdfName);

  sendEmail_(pdf);
  }
 
function sendEmail_(pdf) {
  var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var coachName = " CoachName ";
  var pdfName = (coachName + month + " 2022 Mileage");
  var attachment = pdf.setName(pdfName);
  GmailApp.sendEmail('em...@email.com', 
                     coachName + month + " Mileage",
                     coachName + " Mileage for " + month + " 2022" + '\n\n', {
                       attachments: [attachment]
                       });
  }

                       
                       

                       
                       
  

Clark Lind

unread,
Jun 23, 2022, 9:48:00 AM6/23/22
to Google Apps Script Community
Try removing the parentheses around the variable and just use:    var pdfName = coachName + month + " 2022 Mileage"
Reply all
Reply to author
Forward
0 new messages