Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var months = ['January','February','March','April','May','June','July','August','September','October', 'November','December']
function main() {
var currentDate = new Date();
var day = currentDate.getDate();
var month = months[currentDate.getMonth()]
var tasks = getTasksToBeNotified(day,month)
tasks = orderTasks(tasks)
createDocumentAndNotify(tasks,day,month)
}
var collTask = 1;
var collFrom = 2;
var collTill = 3;
var collDone = 4;
function getTasksToBeNotified(day, month){
var sheet = SpreadsheetApp.getActive().getSheetByName(month)
var tasks = [];
for (var row=3;row<=sheet.getLastRow();row++){
var from = sheet.getRange(row,collFrom).getValue();
var till = sheet.getRange(row,collTill).getValue();
var isCompleted = sheet.getRange(row,collDone).getValue();
if (isCompleted!="Si" && day>=from && day<=till){
var pendingDays= till-day // Pending days to be completed
var taskDescription = sheet.getRange(row,collTask).getValue()
tasks.push({name: taskDescription, pending:pendingDays, from:from, till:till})
}
}
return tasks
}
function compareTasks(task1,task2) {
if (task1.pending < task2.pending)
return -1;
if (task1.pending > task2.pending)
return 1;
return 0;
}
function orderTasks(tasks){
var orderedTasks = [];
tasks.sort(compareTasks).forEach(function(task){
orderedTasks.push(task);
});
return orderedTasks;
}
var emailTo = 'youremail@gmail.com'
function createDocumentAndNotify(tasks, day, month) {
Logger.log('Creating document')
var doc = DocumentApp.create(day.toString() +' of '+month);
doc.addHeader().setText("Tasks for day "+day.toString() +"th of "+month)
doc.getBody().appendParagraph('Tasks')
tasks.forEach(function(task){
var item ='Task "'+task.name + '"needs to be done before day '+task.till+'th'
Logger.log('Adding tassk '+item+' to document')
doc.getBody().appendListItem(item)
});
doc.saveAndClose()
Logger.log('Document with id '+doc.getId()+' is gonna be deleted')
sendEmail(doc)
DriveApp.getFileById(doc.getId()).setTrashed(true)
}
function sendEmail(doc){
var message = "Please find attached the document with the pending tasks.";
var subject = 'Tasks for tody ';
var pdf = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var attach = {fileName:'Tasks.pdf',content:pdf, mimeType:'application/pdf'};
Logger.log('Sending email with tasks to be done')
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment