Trigger google sheet export and email when cell value changed to equal a value

124 views
Skip to first unread message

Trent Magis

unread,
Jun 17, 2022, 5:03:57 PM6/17/22
to Google Apps Script Community
I am trying to write a script that will automatically export the Google Sheet to a PDF and then email it when the user enters a specific value into a specific cell.  In this case, if the user enters "Yes" into cell G1, then I want the sheet to be exported as a PDF based on the name of the sheet (Jan, Feb, Mar, etc) into a Google Folder and also email the PDF.  

This is the script I have so far, but it is not exporting/emailing the Google Sheet - NOTE - I changed the actual Google Sheet and Folder ID to generic text to not share sensitive info, this is not the reason why it is not working.

Any help you can offer is greatly appreciated.

function onEdit(e){
    if(e.range.getA1Notation() == "G1") {
       return;
      var value = e.SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange('G1').getValue();  
     }
  if (e.value() == "Yes");
  exportAndSend ();
 
}
function exportAndSend () {
  var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var coachName = " CoachName ";
  var pdfName = coachName + month + " 2022 Mileage";
 
  let mileage = DriveApp.getFileById('GoogleSheet ID');
  let blob = mileage.getAs('application/pdf');
  let pdf = DriveApp.getFolderById('CoachFolder ID')
  .createFile(blob)
  .setName(pdfName);
 
  sendEmail_(pdf);
  }
 
function sendEmail_(pdf) {
  var month = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
  var coachName = " CoachName ";
  var pdfName = coachName + month + " 2022 Mileage";
  GmailApp.sendEmail('em...@gmail.com', coachName + month + " Mileage", "Mileage for " + month + "2022", {
                       attachments: [pdf]
                       });
  }

                       

Laurie J. Nason

unread,
Jun 19, 2022, 3:32:41 AM6/19/22
to google-apps-sc...@googlegroups.com
I think that your if statement in the onEdit function below should be:
if (e.value() == "Yes”){
  exportAndSend ();
}


------ Original Message ------
From "Trent Magis" <trent...@qicreative.com>
To "Google Apps Script Community" <google-apps-sc...@googlegroups.com>
Date 17/06/2022 22:03:57
Subject [Apps-Script] Trigger google sheet export and email when cell value changed to equal a value

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/ceb05d13-090e-4c17-8412-49d5219b4984n%40googlegroups.com.

Martin Molloy

unread,
Jun 19, 2022, 5:16:36 AM6/19/22
to google-apps-sc...@googlegroups.com
Line 3 of your onEdit function is a return. ie it stops at that point if the active cell is G1



--

Laurie J. Nason

unread,
Jun 19, 2022, 11:25:43 AM6/19/22
to google-apps-sc...@googlegroups.com
Oh - yeah - that won’t help either! :-)


The KAUST School – Operational Analyst

Information Technology Support

Landline: +966-12-808-6853

Mobile: +966-54-564-3698



------ Original Message ------
From "Martin Molloy" <martin...@mtmomk.co.uk>
Date 19/06/2022 10:16:19
Subject Re: [Apps-Script] Trigger google sheet export and email when cell value changed to equal a value

Витя Коледа

unread,
Jun 20, 2022, 8:33:43 AM6/20/22
to google-apps-sc...@googlegroups.com

вс, 19 июн. 2022 г., 18:25 Laurie J. Nason <laurie...@thekaustschool.org>:
1p4hegyq.png
Reply all
Reply to author
Forward
0 new messages