Background
This is a function that is part of a library written in GAS. A function inside a library I've written is supposed to send out an email to another user or users with accompanying details (based on parameters) from the email account of whoever runs the function. Basically if I run the program from my google account, and your email address is one of the parameters, then you get an email from me. I realize that this is wrapped inside of Google's sendEmail
function anyway, but I've added some of my own parameters.
What the function does specifically: when it is run, an email is sent to the specified email address/addresses with the accompanying subject and email body. Additionally, another email address can be added as a CC. Finally there is another option where, if the container the app script is attached to is a spreadsheet, you can specify which sheet on the spreadsheet you want converted into a PDF (specifying its PDF name), and have that sent with the email as well. Inside the function there can be six possible parameters: emailAddress
, emailSubject
, emailBody
, emailCC
, sheetName
, and pdfName
. I believe all of those are fairly self-explanatory besides the last two. As I previously mentioned, sheetName
is the name of the sheet on the spreadsheet that you want converted into a PDF, and pdfName
is the name you set your newly created PDF to.
My Request
I'm still somewhat new to GAS/JavaScript, and I wanted to challenge myself by making a library that contains a lot of the functions that my company uses on a regular basis so that I could learn more. Unfortunately, I'm very limited on who I can ask for guidance on how to improve my library. One of the methods that I was told about that was easier was to make separate functions for each of the combos of parameters within a top level object, but I have not been able to figure out how to properly implement that, so any assistance is appreciated.
My Code
function sendEmail(){
var function1 = function(emailAddress, emailSubject, emailBody){
MailApp.sendEmail(emailAddress, emailSubject, emailBody)
}
var function2 = function(emailAddress, emailSubject, emailBody, emailCC){
MailApp.sendEmail(emailAddress, emailSubject, emailBody, {cc: emailCC})
}
var function3 = function(emailAddress, emailSubject, emailBody, sheetName, pdfName){
MailApp.sendEmail(emailAddress, emailSubject, emailBody, {attachments: [pdfConversionPortrait(sheetName).setName(pdfName)]})
}
var function4 = function(emailAddress, emailSubject, emailBody, emailCC, sheetName, pdfName){
MailApp.sendEmail(emailAddress, emailSubject, emailBody, {
cc: emailCC,
attachments: [pdfConversionPortrait(sheetName).setName(pdfName)]})
}
try{
if(arguments.length === 3){
function1(arguments[0].toString(), arguments[1], arguments[2])
} else if(arguments.length === 4){
function2(arguments[0].toString(), arguments[1], arguments[2], arguments[3].toString())
} else if(arguments.length === 5){
function3(arguments[0].toString(), arguments[1], arguments[2], arguments[3], arguments[4])
} else if(arguments.length === 6){
function4(arguments[0].toString, arguments[1], arguments[2], arguments[3].toString, arguments[4], arguments[5])
}
} catch(e){
e instanceof TypeError ? Logger.log('Error: Specified sheet does not exist') : Logger.log(e)
}
}
Other Relevant Code
function pdfConversionPortrait(sheetName){
let ss = SpreadsheetApp.getActiveSpreadsheet()
let ss_id = ss.getId()
let sheet_id = ss.getSheetByName(sheetName).getSheetId()
let response = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/d/" + ss_id + "/export?format=pdf&gid=" + sheet_id, {
muteHttpExceptions: true,
headers: {
Authorization: 'Bearer ' + ScriptApp.getOAuthToken(),
},
}).getBlob()
return response
}