Google Apps Script - Send Email based on date in cell

后端 未结 3 1719
庸人自扰
庸人自扰 2020-12-13 12:10

I\'ve looked around and have bits and pieces but can\'t put the puzzle together. I\'m attempting to create a script that will run on a trigger configured to run daily. The

3条回答
  •  暖寄归人
    2020-12-13 13:09

    I would do it this way ....

    Setup the spreadsheet like this: https://docs.google.com/spreadsheet/ccc?key=0AkGlO9jJLGO8dDJad3VNTkhJcHR3UXlJSVRNTFJreWc

    Change the code to:

    function sendEmails() {
      var spreadsheet = SpreadsheetApp.openById('Type spreadsheet key here from spreadsheet URL');       
      /// e.g.  var spreadsheet = SpreadsheetApp.openById('0AkGlO9jJLGO8dDJad3VNTkhJcHR3UXlJSVRNTFJreWc');     
    
      var sheet = spreadsheet.getSheets()[0]; // gets the first sheet, i.e. sheet 0
    
      var range = sheet.getRange("B1"); 
      var dateString = new Date().toString();
      range.setValue(dateString);   // this makes all formulas recalculate
    
      var startRow = 4;  // First row of data to process
      var numRows = 50;   // Number of rows to process
      // Fetch the range of cells
      var dataRange = sheet.getRange(startRow, 1, numRows, 4)
      // Fetch values for each row in the Range.
      var data = dataRange.getValues();   
    
      for (i in data) {
        var row = data[i];
        if( row[3] == true) {
          var emailAddress = row[0];  // First column
          var message = row[1];       // Second column
          var subject = "Task Item Due";
          try {
              MailApp.sendEmail(emailAddress, subject, message);
          } catch(errorDetails) {
            // MailApp.sendEmail("eddyparkinson@someaddress.com", "sendEmail script error", errorDetails.message);
          }
    
        }
      }
    }
    

    The trigger:

    Because of the trigger, you will need to open the spreadsheet using openById. To do this, in the code replace 'Type spreadsheet key here from spreadsheet URL'. To find the key, open the google docs spreadsheet, the link has "key=A0a0df..." paste the code. See the example.

    JavaScript: if you want to learn more about using Java Script, I recommend http://www.w3schools.com/js/default.asp

提交回复
热议问题