循环范围

时间:2015-04-22 10:49:07

标签: javascript google-apps-script google-sheets

我借了一些脚本来发送提醒电子邮件。这工作正常,但发送后,我希望它将一个单元格的值设置为状态'是'所以下次它循环它就不会动作那条线。

我已经尝试了一些事情并确实让它在某一点工作,因为它将值设置在错误的行上。有问题的行是下面代码中的最后一行

function sendEmail() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Meeting Room Booking Requests');
  var startRow = 2;
  var numRows = sheet.getLastRow()-1;   // Number of rows to process

  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  var range = dataRange.getValues();

    // get the spreadsheet object
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Meeting Room Booking Requests');
  var lastRow = sheet.getLastRow();
  var startRow = 2;

  // grab column 17 (the 'difference' column)
  var range = sheet.getRange(2,17,lastRow-startRow+1,1);
  var numRows = range.getNumRows();
  var time_left_values = range.getValues();

  // grab column 18 (the 'reminder sent' column)
  var range = sheet.getRange(2,18,lastRow-startRow+1,1);
  var numRows = range.getNumRows();
  var remindersent = range.getValues();  

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();

  //Logger.log(data)

  for (i in data) {
    var row = data[i];
    var time_left = time_left_values[i][0];
    var reminder_sent = remindersent[i][0];

    if(reminder_sent == 'No')
      {
       if(time_left >= 0) 
          {
            var emailAddress = "test@test.co.uk";//row[6];  // First column
            var message = row[1];       // Second column
            var subject = "Sending emails from a Spreadsheet";
            MailApp.sendEmail(emailAddress, subject, message);
            sheet.getRange(data,17).setValue('Yes');   
    };    
  };
}
}

1 个答案:

答案 0 :(得分:0)

全部排序.....最终代码在

之下
function sendEmail() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Meeting Room Booking Requests');
  var startRow = 2;
  var numRows = sheet.getLastRow()-1;   // Number of rows to process

  var linkText = 'test@test.co.uk';

  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  var range = dataRange.getValues();

    // get the spreadsheet object
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Meeting Room Booking Requests');
  var lastRow = sheet.getLastRow();
  var startRow = 2;

  // grab column 17 (the 'difference' column)
  var range = sheet.getRange(2,17,lastRow-startRow+1,1);
  var numRows = range.getNumRows();
  var time_left_values = range.getValues();

  // grab column 18 (the 'reminder sent' column)
  var range = sheet.getRange(2,18,lastRow-startRow+1,1);
  var numRows = range.getNumRows();
  var remindersent = range.getValues();

  // grab column 15 (the 'reminder time' column)
  var range = sheet.getRange(2,18,lastRow-startRow+1,1);
  var numRows = range.getNumRows();
  var remindertime = range.getValues();    

  var emailaddressTo = ("test@test.co.uk");
  var emailaddressCC = ("test@test.co.uk, test@test.co.uk");
  var testemail = ("test@test.co.uk");

  // Fetch values for each row in the Range.
  var data = dataRange.getValues();

  //Logger.log(data)

  for (i in data) 
  {
    var row = data[i];
    var time_left = time_left_values[i][0];
    var reminder_sent = remindersent[i][0];
    var reminder_time = remindertime[i][0];

      if(reminder_sent == 'No')
        {
         if(time_left >= 0) 
          {
            var emailAddress = "test@test.co.uk";//row[6];  // First column
            var room = row[0];       // First column
            var start = row[2];       // Third column
            var end = row[3];       // Fourth column
            var name = row[4];       // Fifth column
            var email = row[6];       // Seventh column
            var mailsubject = "Booking Reminder";

            var body = "Hi " + name + "," + "<br />" +
                       "<br />" + "You have the below room booked in the London office:"  + "<br />" +
                       "<br />" + "<b>Room:</b>" + " " + room + 
                       "<br />" + "<b>Required From:</b>" + " " + start + 
                       "<br />" + "<b>Required To:</b>" + " " + end + "<br />" + 
                       "<br />" + "If for any reason you do not require this room please contact: " + linkText.link(linkText); 

            MailApp.sendEmail(
                               {

                                to:       email,
                                //noReply:  true,  
                                replyTo:  testemail,//emailaddressCC,
                                cc:       testemail,//emailaddressCC,
                                subject:  mailsubject,
                                htmlBody: body,
                                name:     'oom Booking'
                                }
                              );
            data[i][17] = "Yes";  
          };    
        };
  };
  dataRange.setValues(data);
}