Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Retrieves all the rows in the active spreadsheet that contain data and logs the
- * values for each row.
- * For more information on using the Spreadsheet API, see
- * https://developers.google.com/apps-script/service_spreadsheet
- */
- function readRows() {
- var sheet = SpreadsheetApp.getActiveSheet();
- var rows = sheet.getDataRange();
- var numRows = rows.getNumRows();
- var values = rows.getValues();
- var allDataArray = [];
- for (var i = 0; i <= numRows - 1; i++) {
- var row = values[i];
- allDataArray.push(row);
- }
- return allDataArray;
- };
- /**
- * Adds a custom menu to the active spreadsheet, containing a single menu item
- * for invoking the readRows() function specified above.
- * The onOpen() function, when defined, is automatically invoked whenever the
- * spreadsheet is opened.
- * For more information on using the Spreadsheet API, see
- * https://developers.google.com/apps-script/service_spreadsheet
- */
- function onOpen() {
- var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
- var entries = [{
- name : "Read Data",
- functionName : "readRows"
- }];
- spreadsheet.addMenu("Script Center Menu", entries);
- };
- /**
- * Send an email at 9am and 9pm on the date, specifying the amount in the cell
- *
- */
- function runDaily(){
- var currentDate = getFormattedDate();
- // Stores data for row of paydays and amounts
- var alldata = readRows();
- var fran = alldata[2];
- var kris = alldata[3];
- var franData = tidyArray(fran);
- var krisData = tidyArray(kris);
- var franAmount = checkGetAmount(currentDate,franData);
- if (franAmount == "Not today"){
- // do nothing
- }else{
- emailFran(franAmount);
- }
- var krisAmount = checkGetAmount(currentDate,krisData);
- if (krisAmount == "Not today"){
- // do nothing
- }else{
- emailKris(krisAmount);
- }
- }
- // 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
- // It then formats the date to be Day Month DD YYYY (removes the time and GMT which is irrelevant)
- function tidyArray(data){
- var removeData = data.splice(0,11);
- var arrayLength = data.length;
- for (var i = 0; i < arrayLength; i++) {
- data[i] = data[i].toString();
- data[i] = data[i].substring(0,15);
- }
- return data;
- }
- // Emails Fran if the amount is returned
- function emailFran(amount){
- var email = 'franchaselden@gmail.com';
- var subject = "Happy Payday! Please pay £" + amount;
- 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";
- MailApp.sendEmail(email, subject, message);
- }
- // Emails Kris if the amount is returned
- function emailKris(amount){
- var email = 'lyons.kris@gmail.com';
- var subject = "Happy Payday! Please pay £" + amount;
- 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";
- MailApp.sendEmail(email, subject, message);
- }
- // This function gets today's date and checks it against the data array. If it finds a matching date, it'll
- // give the amount of money associated with that date. If it finds nothing it will return false.
- function checkGetAmount(currentDate,data){
- var arrayLength = data.length;
- for (var i = 0; i < arrayLength; i++) {
- // Compare currentDate and the value
- if (data[i] == currentDate){
- i++;
- var amount = data[i];
- return amount;
- }else{
- return 'Not today';}
- }
- }
- // This function formats today's date to Day DD Month YYYY (e.g Mon 15 Dec 2014)
- function getFormattedDate(){
- var d = new Date();
- // Day of the week
- var weekday = new Array(7);
- weekday[0]= "Sunday";
- weekday[1] = "Monday";
- weekday[2] = "Tuesday";
- weekday[3] = "Wednesday";
- weekday[4] = "Thursday";
- weekday[5] = "Friday";
- weekday[6] = "Saturday";
- var currentDay = weekday[d.getDay()];
- var currentDay = currentDay.substring(0, 3);
- // Date of the month
- var currentDate = d.getDate();
- // Month
- var month = new Array();
- month[0] = "January";
- month[1] = "February";
- month[2] = "March";
- month[3] = "April";
- month[4] = "May";
- month[5] = "June";
- month[6] = "July";
- month[7] = "August";
- month[8] = "September";
- month[9] = "October";
- month[10] = "November";
- month[11] = "December";
- var currentMonth = month[d.getMonth()];
- var currentMonth = currentMonth.substring(0, 3);
- // Year
- var currentYear = d.getYear();
- // Combine
- var theDate = currentDay + " " + currentMonth + " " + currentDate + " " + currentYear;
- return theDate;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement