Creating Script that will look at checkbox, if checked include CC'd on email

I have a pretty simple script that looks every minute to see if a checkbox is checked, if it is, then it creates a PDF of my sheet and emails it to a number of different people listed. After it’s done, it sets that checkbox to false so it doesn’t send again.

What I’d like to do is have it look at another cell in my sheet and if that cell is checked, it would CC the email in ccEmail2. If that box is not checked, it would not include them in the email. Have tried to search around and couldn’t find anything. Feel like a well placed If statement would do it, but I don’t know much about scripting…

My Sheet:

My script:

function emailCameraPackage() {

var ss = SpreadsheetApp.getActiveSpreadsheet();

var projectinfo = ss.getSheetByName("Project Info");

var camerapackage = ss.getSheetByName("Camera Package Builder"); // Enter the name of the sheet here

var subject = "Equipment List for " + projectinfo.getRange(2,1).getValue();

var body = projectinfo.getRange(2,14).getValue();

var email = projectinfo.getRange(2,13).getValue();

var ccEmail = projectinfo.getRange(2,9).getValue();

var ccEmail2 = projectinfo.getRange(2,11).getValue();

// Base URL
var url = "".replace("SS_ID", ss.getId());

/* Specify PDF export parameters

var url_ext = 'exportFormat=pdf&format=pdf' // export as pdf / csv / xls / xlsx
     + '&size=letter' // paper size legal / letter / A4
     + '&portrait=true' // orientation, false for landscape
     + '&fitw=true&source=labnol' // fit to page width, false for actual size
     + '&sheetnames=false&printtitle=false' // hide optional headers and footers
     + '&pagenumbers=false&gridlines=false' // hide page numbers and gridlines
     + '&fzr=false' // do not repeat row headers (frozen rows) on each page
     + '&gid='; // the sheet's Id

var token = ScriptApp.getOAuthToken();

var response = UrlFetchApp.fetch(url + url_ext + camerapackage.getSheetId(), {
        headers : {
            'Authorization' : 'Bearer ' + token
    }).getBlob().setName(projectinfo.getRange(2,1).getValue() + ".pdf");

// Uncomment the line below to save the PDF to the root of your drive. 
//  var newFile = DriveApp.createFile(response).setName(sheet.getName() + ".pdf")

if(projectinfo.getRange(2,15).isChecked()==true) {
if (MailApp.getRemainingDailyQuota() > 0)
GmailApp.sendEmail(email, subject, body, {
htmlBody : body,
attachments : [response],
cc : ccEmail+","+ccEmail2


Found some additional info with a quick Google search, but it looks like you have that much figured out.

To answer your question, you could set up another IF statement that would check the CC checkbox and call GmailApp.SendEmail() as you have it set up now, then add an ELSE to call GmailApp.SendEmail() without the cc parameter if the checkbox is not checked.

I ended up figuring this out, I ended up pretty much doing what you had suggested, I set up an if statement that setup the CC emails. It looked at the check box, if it was true, it set cc emails as one thing, if it was false, it set it as something else, then the next if statement I included the variable I had just created.

1 Like