Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Retrieves all the rows in the active spreadsheet that contain data and logs the
  3.  * values for each row.
  4.  * For more information on using the Spreadsheet API, see
  5.  * https://developers.google.com/apps-script/service_spreadsheet
  6.  */
  7. function readRows() {
  8.   var sheet = SpreadsheetApp.getActiveSheet();
  9.   var rows = sheet.getDataRange();
  10.   var numRows = rows.getNumRows();
  11.   var values = rows.getValues();
  12.   var allDataArray = [];
  13.   for (var i = 0; i <= numRows - 1; i++) {
  14.     var row = values[i];
  15.     allDataArray.push(row);
  16.   }
  17.  
  18.   return allDataArray;
  19. };
  20.  
  21. /**
  22.  * Adds a custom menu to the active spreadsheet, containing a single menu item
  23.  * for invoking the readRows() function specified above.
  24.  * The onOpen() function, when defined, is automatically invoked whenever the
  25.  * spreadsheet is opened.
  26.  * For more information on using the Spreadsheet API, see
  27.  * https://developers.google.com/apps-script/service_spreadsheet
  28.  */
  29. function onOpen() {
  30.   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  31.   var entries = [{
  32.     name : "Read Data",
  33.     functionName : "readRows"
  34.   }];
  35.   spreadsheet.addMenu("Script Center Menu", entries);
  36. };
  37.  
  38.  
  39. /**
  40.  * Send an email at 9am and 9pm on the date, specifying the amount in the cell
  41.  *
  42.  */
  43.  
  44. function runDaily(){
  45.   var currentDate = getFormattedDate();
  46.  
  47.   // Stores data for row of paydays and amounts
  48.   var alldata = readRows();
  49.   var fran = alldata[2];
  50.   var kris = alldata[3];
  51.   var franData = tidyArray(fran);
  52.   var krisData = tidyArray(kris);
  53.  
  54.   var franAmount = checkGetAmount(currentDate,franData);
  55.   if (franAmount == "Not today"){
  56.     // do nothing
  57.   }else{
  58.     emailFran(franAmount);
  59.   }
  60.  
  61.   var krisAmount = checkGetAmount(currentDate,krisData);
  62.   if (krisAmount == "Not today"){
  63.     // do nothing
  64.   }else{
  65.     emailKris(krisAmount);
  66.   }
  67.  
  68.  
  69.  
  70. }
  71.  
  72. // This function removes data pre 2015 from the row, because the set up of the spreadsheet differs greatly in 2015 onwards. Removes cols A-K
  73. // It then formats the date to be Day Month DD YYYY (removes the time and GMT which is irrelevant)
  74. function tidyArray(data){
  75.   var removeData = data.splice(0,11);
  76.   var arrayLength = data.length;
  77.   for (var i = 0; i < arrayLength; i++) {
  78.     data[i] = data[i].toString();
  79.     data[i] = data[i].substring(0,15);
  80.   }
  81.   return data;
  82. }
  83.  
  84. // Emails Fran if the amount is returned
  85. function emailFran(amount){
  86.   var email = 'franchaselden@gmail.com';
  87.   var subject = "Happy Payday! Please pay £" + amount;
  88.   var message = "Hello there,\n\nThis is an automated message.\n\nPlease ensure that you pay £" + amount + " into the utilities account today.\n\nThanks!\n\n\nWhere does your money go? Click here to see: https://docs.google.com/spreadsheets/d/1J9S9JaVbVLfd6Jnz_f3286ZqM60rpNe8qQduWu4aIMc/edit?usp=sharing";
  89.   MailApp.sendEmail(email, subject, message);
  90. }
  91.  
  92. // Emails Kris if the amount is returned
  93. function emailKris(amount){
  94.   var email = 'lyons.kris@gmail.com';
  95.   var subject = "Happy Payday! Please pay £" + amount;
  96.   var message = "Hello there,\n\nThis is an automated message.\n\nPlease ensure that you pay £" + amount + " into the utilities account today.\n\nThanks!\n\n\nWhere does your money go? Click here to see: https://docs.google.com/spreadsheets/d/1J9S9JaVbVLfd6Jnz_f3286ZqM60rpNe8qQduWu4aIMc/edit?usp=sharing";
  97.   MailApp.sendEmail(email, subject, message);
  98. }
  99.  
  100.  
  101. // This function gets today's date and checks it against the data array. If it finds a matching date, it'll
  102. // give the amount of money associated with that date. If it finds nothing it will return false.
  103. function checkGetAmount(currentDate,data){
  104.   var arrayLength = data.length;
  105.   for (var i = 0; i < arrayLength; i++) {
  106.     // Compare currentDate and the value
  107.     if (data[i] == currentDate){
  108.       i++;
  109.       var amount = data[i];
  110.       return amount;
  111.     }else{
  112.       return 'Not today';}
  113.   }
  114. }
  115.  
  116.  
  117.  
  118. // This function formats today's date to Day DD Month YYYY (e.g Mon 15 Dec 2014)
  119. function getFormattedDate(){
  120.   var d = new Date();
  121.   // Day of the week
  122.   var weekday = new Array(7);
  123.   weekday[0]=  "Sunday";
  124.   weekday[1] = "Monday";
  125.   weekday[2] = "Tuesday";
  126.   weekday[3] = "Wednesday";
  127.   weekday[4] = "Thursday";
  128.   weekday[5] = "Friday";
  129.   weekday[6] = "Saturday";
  130.   var currentDay = weekday[d.getDay()];
  131.   var currentDay = currentDay.substring(0, 3);
  132.   // Date of the month
  133.   var currentDate = d.getDate();
  134.   // Month
  135.   var month = new Array();
  136.   month[0] = "January";
  137.   month[1] = "February";
  138.   month[2] = "March";
  139.   month[3] = "April";
  140.   month[4] = "May";
  141.   month[5] = "June";
  142.   month[6] = "July";
  143.   month[7] = "August";
  144.   month[8] = "September";
  145.   month[9] = "October";
  146.   month[10] = "November";
  147.   month[11] = "December";
  148.   var currentMonth = month[d.getMonth()];
  149.   var currentMonth = currentMonth.substring(0, 3);
  150.   // Year
  151.   var currentYear = d.getYear();
  152.   // Combine
  153.   var theDate = currentDay + " " + currentMonth + " " + currentDate + " " + currentYear;
  154.   return theDate;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement