Date not being found in data range

I'm trying to to send an email every week and include information from a sheet in the email content. However the date is not searching correctly even though I have the date and the logs show the text matching exactly.

This is for a weekly program that volunteers can sign up on the sheet. The purpose of this is to remind any who wish to sign up to do so and provide the information on how to do that.

function sendEmails() {
  var signupSheet = SpreadsheetApp.getActive().getSheetByName("volunteers");
  var upcomingSunday = new Date(Date.now() + 5*1000*60*60*24);
  upcomingSunday.setHours(0,0,0,0)
  Logger.log(upcomingSunday);
  var currentWeekRow = findRowByValue(signupSheet,upcomingSunday);
  Logger.log(currentWeekRow);
  if (signupSheet.getRange(currentWeekRow,2).getValues() = ""){
    var chairman = "Chairman";
    }
    else{
    var chairman = "";
  }
}
function findRowByValue(sheet,value){
  var dataRange = sheet.getRange("A1:A");
  var values = dataRange.getValues();
  Logger.log(values);
  Logger.log(value);
  for (var i = 0; i < values.length; i++) {
    for (var j = 0; j < values[i].length; j++) {     
      if (values[i][j] == value) {
        Logger.log(i);
        return i+1;
      }
    }    
   }
  return -1;
}

I expect the findRowByValue function to return the row with the date for upcomingSunday, but it keeps returning -1 which indicates it isn't finding it, however the log shows that it is there.

Answers:

Answer

Try this:

function sendEmails() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("volunteers");
  var rg=sh.getDataRange();//This gets the range that has all of the data on the sheet and column one is the date according to your code
  var vA=rg.getValues();//this gets the data 
  var chairman="";
  var nextSunday=getNextSunday();
  for(var i=0;i<vA.length;i++) {//vA[i][0] is column 1
    if(new Date(vA[i][0]).valueOf()==nextSunday.valueOf()) {//compare milliseconds to milliseconds
      if(vA[i][1]=="") {//this is column 2
        chairman="Chairman";
        //not sure what this has to do with send email
        //probably want to break here
        break;
      } 
    }
  }

}

function getNextSunday() {//This finds the next sunday based upon the current date
  var t=new Date();
  while(t.getDay()!=0) {
    t.setDate(t.getDate()+1);
  }
  t.setHours(0,0,0,0);//set time to midnight
  return t;
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.