简体   繁体   中英

(GAS) Email Spreadsheet range based on date input in the cell

Below is a script that is SUPPOSED TO email me a range of not empty rows in the "Files Change Report" sheet on the date written in cell H2 of the same sheet. However, when testing the script, no matter what is in H2, the email is still sent. Could anybody advise what is wrong with the script? Thank you.

function email() {
    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var bulk = ss.getSheetByName("File Change Report");
    
      var lastrow = bulk.getLastRow();
    
      var email = bulk.getRange("E1").getValue();
      var emailFromName ="David Baker";
      var emailSubject = "UPDATES TO YOUR FILES";
    
      const WebLink = 'https://docs.google.com/forms/d/e/123...'
      var emailFooter = "To stop notifications<a href=" + WebLink + ">press here.</a>";
    
      var data1 = bulk.getRange(2, 5, lastrow).getValues(); // row no column E
      var data2 = bulk.getRange(2, 6, lastrow).getValues(); // date updated column F
      var data3 = bulk.getRange(2, 7, lastrow).getValues(); // file name column G
      var data4 = bulk.getRange(2, 4, lastrow).getValues(); // file url column D
      
      var SendDateRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("File Change Report").getRange("H2"); 
      var SendDate = SendDateRange.getValue();
      var curDateRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("File Change Report").getRange("H4"); 
      var curDate = curDateRange.getValue();
    
      for(var i in data1){
      if (SendDate.toString() == curDate.toString()){ table += "<tr><td width='7' style='padding-right:5px' align='right'>" + data1[i] + "</td><td style='padding:0px'>" + data2[i] + "</td><td style='padding: 0px 0px 0px 20px'><a href='" + data4[i] + "'>" + data3[i] + "</a></td></tr>"; //top right bottom left
      }
    
      var message = ""
      var table ="";
        
      message ="Files below have been updated:<br/><br/> <table style='margin-left:10px'><tr><th></th><th align='left'>Date</th><th align='left' style='padding: 0px 0px 0px 20px'>File Name</th></tr>" + table + "</table></li>"+ "<br><small> "+emailFooter+" </a></small>";
    
      MailApp.sendEmail(email,emailSubject, message, {name: emailFromName, htmlBody: message});
    
      var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Fetch all files");
        array = [];
        ss.getRange("A2:G") // Choose the range here.clear();
        .clearContent();
    }

From the question

However, when testing the script, no matter what is in H2, the email is still sent.

Change

if (SendDate.toString() == curDate.toString()){ table += "<tr><td width='7' style='padding-right:5px' align='right'>" + data1[i] + "</td><td style='padding:0px'>" + data2[i] + "</td><td style='padding: 0px 0px 0px 20px'><a href='" + data4[i] + "'>" + data3[i] + "</a></td></tr>"; //top right bottom left
      } // <--- this bracket should be moved 
    
      var message = ""
      var table ="";
        
      message ="Files below have been updated:<br/><br/> <table style='margin-left:10px'><tr><th></th><th align='left'>Date</th><th align='left' style='padding: 0px 0px 0px 20px'>File Name</th></tr>" + table + "</table></li>"+ "<br><small> "+emailFooter+" </a></small>";
    
      MailApp.sendEmail(email,emailSubject, message, {name: emailFromName, htmlBody: message});
    
      var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Fetch all files");
        array = [];
        ss.getRange("A2:G") // Choose the range here.clear();
        .clearContent();

to

if (SendDate.toString() == curDate.toString()){ table += "<tr><td width='7' style='padding-right:5px' align='right'>" + data1[i] + "</td><td style='padding:0px'>" + data2[i] + "</td><td style='padding: 0px 0px 0px 20px'><a href='" + data4[i] + "'>" + data3[i] + "</a></td></tr>"; //top right bottom left
       
    
      var message = ""
      var table ="";
        
      message ="Files below have been updated:<br/><br/> <table style='margin-left:10px'><tr><th></th><th align='left'>Date</th><th align='left' style='padding: 0px 0px 0px 20px'>File Name</th></tr>" + table + "</table></li>"+ "<br><small> "+emailFooter+" </a></small>";
    
      MailApp.sendEmail(email,emailSubject, message, {name: emailFromName, htmlBody: message});
    
      var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Fetch all files");
        array = [];
        ss.getRange("A2:G") // Choose the range here.clear();
        .clearContent();
} // <--- this is new position of the if bracket 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM