Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Goes through the budgets listed and checks if the current spend is over the percentage limit, if so, the tracker will send an email to the respective account managers
- function trackBudget() {
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GDS Version");
- var values = sheet.getDataRange().getValues();
- var headers = values.shift(); //plucks the value from the first row
- var count = 0; //ensures the loop only sends one email
- var quota = MailApp.getRemainingDailyQuota();
- var percentageLimit = values[0][16]; //Q2
- var channel;
- var budget;
- var spendToDate;
- var checker = values[1][16];//Q3 //ensures the function only sends one email per day
- if(checker == "ON") {
- for (var i=0; i<values.length; i++) {
- channel = values[i][0];
- budget = values[i][1]; //B column
- spendToDate = values[i][2]; //C column
- //var ui = SpreadsheetApp.getUi();
- // Check totals sales
- if (budget < spendToDate){
- if(count==0){
- sendEmail(channel,percentageLimit);
- //sendEmail(budget,spendToDate);
- //ui.alert('budget > spendToDate'+ spendToDate + ' '+ channel+ ' '+stdDev);
- count= 1;
- sheet.getRange("Q3").setValue("OFF");
- }
- }
- }
- }
- }
- // Useless for now
- function getSheet(name) {
- return SpreadsheetApp.getActiveSpreadsheet().getSheetByName(name);
- }
- // Resets trigger at midnight.
- function resetEmailTrigger() {
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GDS Version");
- var values = sheet.getDataRange().getValues();
- var headers = values.shift(); //plucks the value from the first row
- var checker = values[1][16];
- if (checker == "OFF") {
- sheet.getRange("Q3").setValue("ON");
- }
- }
- //Sends the email if the conditions are met
- function sendEmail(channel, percentageLimit){
- // Fetch the email address
- var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("B2:B4");
- var nameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("A2");
- var unfiltered = emailRange.getValues();
- var emailAddress = unfiltered.filter(String);
- //clears the nulls from the array
- var link = SpreadsheetApp.getActiveSpreadsheet().getUrl();
- for(var i=0; i<emailAddress.length; i++) {
- var message = "Hey, \n\n" + channel +" is nearing the limit of the budget. It's "+ percentageLimit + "% away from the budget. \n\n"+ "Click the link to be redirected to the tracker to review. You can also change the limit in the tracker. " + link;
- var subject = "Account Budget Nearing The Limit";
- MailApp.sendEmail(emailAddress[i], subject, message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement