Tuesday, June 21, 2016

A program to help my marriage

Like many wives, mine likes complements. Unfortunately, I am usually pretty bad at remembering to give them. A couple years ago I decided I would send her an e-mail every weekday with three nice things. For a while I was pretty consistent and she really appreciated it. Eventually, though, I became inconsistent.

Programming to the rescue! I have used JavaScript and Google App Scripts to make the best program ever. First, I made a Google Form that only has one question. When I fill in the form with something I love about my wife and press submit, it adds it to a Google Sheet called "3 Things - Specific". 

I use that form to add things I love about my wife that are specifically related to something that happened in the last day. I also have another sheet that I manually add to called "3 Things - General". In this sheet I have over 250 things I love about my wife (many have similarities) that are always applicable. 

My program first checks to see if there are any new entries in "3 Things - Specific". It uses those first. However, if there are less than three entries, the program will pull entries from "3 Things - General". Once it gets to the end of the general list, it goes back to the beginning. The program will send an e-mail to my wife (from me) with those items. It is setup to run automatically run every weekday at the same time. The best part is that my wife gets an e-mail every day with three things I like about her and I don't have to remember to send it every day. Also, I can easily add things I like about her as soon as I think of them.

Here is the code from my program:

function threeThings() {
  var emailAddress = "tea......@gmail.com";
  var lastRowG = 257;
  var subject = "3 things";
  var thing1 = "";
  var thing2 = "";
  var thing3 = "";
  var cellS;
  var cellG;

  
  var ssG = SpreadsheetApp.openById("1vS9TBuT.......");
  var ssS = SpreadsheetApp.openById("1QdukK9t.......");
  var sheetG = ssG.getSheetByName('Sheet1'); 
  var sheetS = ssS.getSheetByName('Form Responses 1'); 
  var rangeG = sheetG.getRange(1,2); 
  var rangeS = sheetS.getRange(1,3);
  
  var startRowS = rangeS.getValue();
  var startRowG = rangeG.getValue();
  
  if (startRowG >= lastRowG-3) startRowG = 1; 
  
  cellS = sheetS.getRange(startRowS, 2).getValue();
  if (cellS != "") {
     thing1 = cellS;
     cellS = sheetS.getRange(++startRowS, 2).getValue();
     if (cellS != "") {
       thing2 = cellS;
       cellS = sheetS.getRange(++startRowS, 2).getValue();
       if (cellS != "") {
         thing3 = cellS;
         startRowS++;
       } else {
         thing3 = sheetG.getRange(startRowG++, 1).getValue();
       }
       } else {
       thing2 = sheetG.getRange(startRowG, 1).getValue();
       thing3 = sheetG.getRange(++startRowG, 1).getValue();
       startRowG++;
     }
  } else {
       thing1 = sheetG.getRange(startRowG, 1).getValue();
       thing2 = sheetG.getRange(++startRowG, 1).getValue();
       thing3 = sheetG.getRange(++startRowG, 1).getValue();
       startRowG++;
  }
     

  var message = "1. "+ thing1 +"\n2. "+ thing2 +"\n3. "+ thing3;
  
  MailApp.sendEmail(emailAddress, subject, message);
  sheetG.getRange(1, 2).setValue(startRowG);
  sheetS.getRange(1, 3).setValue(startRowS);
  SpreadsheetApp.flush();
}  
  

No comments:

Post a Comment