Google Script:无法找到从电子表格中获取数据的范围

时间:2016-03-07 19:43:42

标签: google-apps-script

我正试图通过谷歌应用程序脚本收到电子邮件。我正在做的是,如果今天有个人生日,我会收到提醒电子邮件。我有一张谷歌表已经有这些数据。但我在一些事情上遇到了问题。首先,下面是我正在运行的代码

enter image description here

var activeSheet = SpreadsheetApp.getActiveSpreadsheet();
var birthdaysSheet = activeSheet.getSheetByName("Data");
var settingsSheet = activeSheet.getSheetByName("Settings");
var sendEmailTo = settingsSheet.getRange("B4").getValue();

function emailAlert() {


  if (turnOnEmailNotice.toLowerCase() == "no")
  { 
    Logger.log("The Email Notification is NOT turned ON. System will Exit.");
    exit 
  } 

  //Get the total number of filled row in the sheet.
  var currentRowAT = 2;
  var currentCellValueAT = "start";
  while (currentCellValueAT != ""){
    if (currentCellValueAT = birthdaysSheet.getRange("A" + currentRowAT).getValue() != ""){
    currentRowAT = currentRowAT +1;
    }
  }
  var birthdaysSheetLastRow = currentRowAT - 1;

  // Get today's Date (with Month, Date and Year)
  var today = new Date();
  var todayMth = today.getMonth()+1;
  var todayDate = today.getDate();
  var todayYear = today.getFullYear();



  for (k=2; k < birthdaysSheetLastRow + 1; k++)
  {
     var targetBday = new Date();
     targetBday = birthdaysSheet.getRange("P" + k).getValue();

     // If Birthday is not speicified, continue with the next row
     if (targetBday == ""){continue};

     var unadjTargetBday = new Date();
     var unadjTargetBdayMth = targetBday.getMonth()+1;
     var unadjTargetBdayDate = targetBday.getDate();
     var unadjTargetBdayYear = targetBday.getFullYear();
     var unadjTargetBday = targetBday;

     targetBday.setDate(targetBday.getDate()-daysInAdvance); // Calculating how many days in advance you want to trigger the notification. This is set in Settings Tab.
     var targetBdayMth = targetBday.getMonth()+1;
     var targetBdayDate = targetBday.getDate();

     if (targetBdayMth + " " + targetBdayDate == todayMth + " " + todayDate)
     {
       var targetBirthDateYearsOld = (today.getYear() - unadjTargetBday.getYear())-1900;
       prepareAndSendEmail(k, targetBirthDateYearsOld);       
     }
   } 
}

/*
*   This method actually prepares the HTML of the email body and send it out.
*/

function prepareAndSendEmail(row, targetBirthDateYearsOld)
{

  var firstName= birthdaysSheet.getRange("A" + row).getValue();
  var lastName = birthdaysSheet.getRange("B" + row).getValue();
  var emailAddress = birthdaysSheet.getRange("D" + row).getValue();
  var Birthday = birthdaysSheet.getRange("C" + row).getValue();

var message = "";

  message = message + "Hi all," + NEW_LINE + NEW_LINE;
  message = message + "Today is <B style=\"color:tomato\">" + firstName +  " " + lastName +  "</B>'s Birthday." + NEW_LINE + NEW_LINE;


 var tableHeader = "";
  tableHeader = tableHeader + "<table width=\"40%\" style=\"background-color:#FF8040;align:center;font-family:calibri,arial\" border=\"0\" cellpadding=\"2\" cellspacing=\"0\" >";
  tableHeader = tableHeader + "<tr style=\"text-align:center;font-weight: bold;height:5px;\"> <td> </td> <td></td></tr>";
  message = message + tableHeader;

  var color = "GhostWhite";
  message = message + "<TR style=\"background-color:" + color + "\"> <TD style=\"text-align:left;font-weight: bold;\"> Birthday: </TD> <TD>" +  formatDate(targetCandidateBirthday) + "</TD>";
  message = message + "<TR style=\"background-color:" + color + "\"> <TD style=\"text-align:left;font-weight: bold;\"> Age: </TD> <TD>" +     targetBirthDateYearsOld + "</TD>";
message = message + "<TR style=\"background-color:" + color + "\"> <TD style=\"text-align:left;font-weight: bold;\"> Email: </TD> <TD>" +  targetCandidateEmailAddress + "</TD>";
}

问题:

  1. 我犯了错误,因为我在var firstName= birthdaysSheet.getRange("A" + row).getValue();

    时遇到错误

    - 错误:未找到范围

  2. 我认为我的年龄代码似乎也错了。

  3. 如果我只使用第一行,则年龄将为undefined

2 个答案:

答案 0 :(得分:0)

我想知道如果结构如下,你的第一个循环是否会更好:

//Get the total number of filled row in the sheet.
var currentRowAT = 2;
var currentCellValueAT = "start";
var thisRow, theBirthday = "";

var birthdayData = birthdaysSheet.getRange(1, 1, birthdaysSheet.getLastRow()); //Get all of column A data

for (var i=0;i<birthdayData.length;i+=1) {
  thisRow = birthdayData[i];
  theBirthday = thisRow[0];
  if (theBirthday === "") {
    var birthdaysSheetLastRow = currentRowAT - 1;
    break;  //Stop looping through the data
  };
};

答案 1 :(得分:0)

问题解决了。我只是在excel表中运行脚本。有效。没有改变任何事情。

谢谢你们的帮助。