function handleMultipleEmails() {
// Get the active spreadsheet
const ss = SpreadsheetApp.getActiveSpreadsheet();
// Get the Marks Master sheet and from that sheet, get the list of students
const sheet = ss.getSheetByName("Marks Master");
const students = sheet.getRange("AO2:AO41").getValues();
// For each student, call the emailReportK function passing in the student's name
// The student's name will be set to the C8 cell
const emailInfo = getPDFUrls(students);
const pdfs = convertToPDF(emailInfo);
sendEmails(pdfs, emailInfo);
}
function getPDFUrls(students) {
let emailInfo = []
const ss = SpreadsheetApp.getActiveSpreadsheet();
const studentTerm = ss.getSheetByName("Student Report");
const sheetId = studentTerm.getSheetId();
let url = ss.getUrl();
url += '#gid=';
url += sheetId;
ss.setActiveSheet(studentTerm);
for(let i = 0; i < students.length; i++) {
//Set the student's name
ss.getRange('C8').setValue(students[i]);
const words = ss.getRange('C8');
const title = words.getValues()[0];
const term = ss.getRange("D10");
const terms = term.getValues()[0];
const email = ss.getRange('g6').getValues()[0];
const school = ss.getRange('C1').getValues()[0];
const subject = school + ": " + title + "_Term_"+ terms +" Report";
const motto = ss.getRange('C2').getValues()[0];
const body = "Good day Parent/Guardian, \n\nPlease find attached, school term report for " + title + "."+ "\n\nRegards, \n" + school + "\n" + motto + "\n";
const exportOptions ='export?exportFormat=pdf&format=pdf' //export as pdf
// Print either the entire Spreadsheet or the specified sheet if optSheetId is provided
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=letter' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=false' // fit to width, false for actual size
+ '&sheetnames=false&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
emailInfo.push({url: url_base+exportOptions, email: email, subject: subject, body: body})
}
return emailInfo
}
function convertToPDF(emailInfo) {
const urls = []
for(let i = 0; i < emailInfo.length; i++) {
urls.push(emailInfo[i].url)
}
var pdfs = UrlFetchApp.fetchAll(urls);
return pdfs
}
function sendEmails(pdfs, emailInfo) {
for(let i = 0; i < pdfs.length; i++) {
var blob = pdfs[i].getBlob().setName(emailInfo[i].subject + '.pdf');
const mailOptions = {
attachments:blob
}
GmailApp.sendEmail(emailInfo[i].email, emailInfo[i].subject, emailInfo[i].body, mailOptions);
}
}
function emailReportK(student) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const studentTerm = ss.getSheetByName("Student Report");
const sheetId = studentTerm.getSheetId();
let url = ss.getUrl();
url += '#gid=';
url += sheetId;
Logger.log("Url: ", url);
ss.setActiveSheet(studentTerm);
// const sheet = ss.getRange(1, 1, 46, 16);
// Subject of the email message
//Set the student's name
ss.getRange('C8').setValue(student);
const words = ss.getRange('C8');
const title = words.getValues()[0];
const term = ss.getRange("D10");
const terms = term.getValues()[0];
const email = ss.getRange('g6').getValues()[0];
const school = ss.getRange('C1').getValues()[0];
const subject = school + ": " + title + "_Term_"+ terms +" Report";
const motto = ss.getRange('C2').getValues()[0];
const body = "Good day Parent/Guardian, \n\nPlease find attached, school term report for " + title + "."+ "\n\nRegards, \n" + school + "\n" + motto + "\n";
//"Good day Parent/Guardian," & vbLf & vbLf _
// & "Please find attached, " & Title & "'s School Term Report." & vbLf & vbLf _
// & "Regards," & vbLf _
//& School & vbLf _
// & Motto & vbLf
// let sheet = ss.getSheetName("Student-Term");
// const unformattedUrl = studentTerm.getUrl();
// Logger.log("SpreadSheet Url " + unformattedUrl);
// let formattedUrl = unformattedUrl.split("/");
// formattedUrl = formattedUrl.slice(0, formattedUrl.length - 1);
// formattedUrl = formattedUrl.join("/");
// formattedUrl = formattedUrl + "/export?";
const exportOptions ='export?exportFormat=pdf&format=pdf' //export as pdf
// Print either the entire Spreadsheet or the specified sheet if optSheetId is provided
+ (sheetId ? ('&gid=' + sheetId) : ('&id=' + spreadsheetId))
// following parameters are optional...
+ '&size=letter' // paper size
+ '&portrait=true' // orientation, false for landscape
+ '&fitw=false' // fit to width, false for actual size
+ '&sheetnames=false&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
var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url_base+exportOptions, params);
var blob = response.getBlob().setName(subject + '.pdf')
var mailOptions = {
attachments:blob
}
//var pdfFile = ss.getBlob().getAs('application/pdf').setName("Pdf1");
//// Send the PDF file as an attachement
GmailApp.sendEmail(email, subject, body, mailOptions);
}