【问题标题】:Get a Google Sheet script to skip empty rows?获取 Google Sheet 脚本以跳过空行?
【发布时间】:2015-03-09 20:09:58
【问题描述】:

我有一个从谷歌表格发送电子邮件的脚本。工作表中的数据由谷歌表单填充。工作表中的一些字段使用数组公式向工作表添加其他数据示例:工作表中的数组公式填充字段:

=ArrayFormula(IF(ROW(A:A)=1,"link",VLOOKUP($C:C,array!$A$2:$C$9000,3,FALSE)))

这会导致“#N/A”显示在没有用户提交数据的行中。我可以接受,但这会导致脚本需要很长时间才能运行。当用户提交数据时,时间戳会放在第一列。如果为空,我可以使用它来让脚本跳过以停止吗?现在,我让脚本检查 1000 行,但理想情况下它会检查行的时间戳,如果没有时间戳,则停止。

/**
 * Add a menu item that calls this script
 */
function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var menu = [{
    name: "Send Email",
    functionName: "uiSendEmail"
  }];

  ss.addMenu("SCCCounseling", menu);

}
var EMAIL_SENT = "EMAIL_SENT";
/**
 * Send the e-mail to everyone listed in the spreadsheet
 */

function uiSendEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1000;
  var dataRange = sheet.getRange(startRow, 1, numRows, 1000)
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i = 0; i < data.length; ++i) {

    var row = data[i];

    var timestamp = row[0];

    var username = row[1];

    var counselor = row[2];

    var date = row[3];

    var sName = row[4];

    var wNum = row[5];

    var toemail = row[6];

    var sLName= row[7];

    var pin = row[8];

    var link = row[9];

    var cMail = row[10];

    var emailSent = row[11]; 

    var subject = "Online Counseling Appointment Confirmation - " + sName + " " + sLName + " " + wNum + " ";

    var emailBody = "Online Counseling Appointment " + " - " + sName + " " + sLName + " " + wNum + 

    "\n\nHello " + sName +

    "\n\n\nYou are scheduled for an online counseling appointment for Sacramento City College: " +

    "\n\n\nDate: " + date +

    "\n\nPlease follow the instructions below at least one day before your appointment to make " +

    "\nsure that your computer is ready. " +

    "\n\nIf your computer is not ready, you will not be able to " +

    "\nparticipate in online counseling. " +

    "\n\nTo cancel your appointment, please email: " +

    "\n\nCounselor: " + counselor + " at " + cMail +

    "\n\nYou will be required to show photo identification at the start of the appointment to verify " +

    "\nyour identity. " +

    "\n\n\nONE DAY BEFORE YOUR APPOINTMENT " +

    "\n\nGo to the following link to test your computer readiness one day before your " +

    "\nscheduled online counseling appointment: " +

    "\n\nhttp://www.cccconfer.org/support/supportReadiness.aspx " +

    "\n\n\nFIVE MINUTES BEFORE YOUR APPOINTMENT: " +

    "\n\nGo to the following link to connect to the online counseling appointment and the " +

    "\ncounselor will join you: " + link +

    "\n\n\nIf you want to use the phone for audio: " +

    "\n\nTelephone Conference Line: 719-785-4469, Toll-free: 1-888-450-4821 " +

    "\n\nStudent PIN: " + pin +

    "\n\n\nFOR ASSISTANCE CONNECTING" + 

    "\n\nContact CCC Confer Client Services, Monday - Friday 8am - 4pm  " +

    "\n\nPhone: 1-760-744-1150 ext 1537 or 1554 " +

    "\n\nEmail: clientservices@cccconfer.org " +

    "\n\n\nThank you " + sName;





    var htmlBody = "<b>Online Counseling Appointment Confirmation" + " -</b> " + " - " + sName +  " " + sLName + " " + wNum +
      "<br/><br/><br/>" + "Hello " + sName + ", <br/><br/>" + "You are scheduled for an online counseling appointment for Sacramento City College: <i>" + 
      "<br/><br/><br/>" + "<b>Date: " + date + "<br/><br/>"  + "</b><br/>" + "</i><br/>Please follow the instructions below <b>at least one day before</b> your appointment to make sure that your computer is ready." +
      "<br/><br/><i>If your computer is not ready, you will not be able to participate in online counseling.</i> " + "<br/><br/> To cancel your appointment, please email: " + 
      "<br/><br/><font color=\"red\">Counselor:</font> " + counselor + " at " + cMail + "<br/><br/><br/>You will be required to show photo identification at the start of the appointment to verify your identity. " +
      "<br/><br/><br/><b>ONE DAY BEFORE YOUR APPOINTMENT:</b> " + "<br/><br/>➢  Go to the following link to test your computer readiness one day before your scheduled online counseling appointment: " +
      "<br/><br/>http://www.cccconfer.org/support/supportReadiness.aspx " +
      "<br/><br/><br/><b>FIVE MINUTES BEFORE YOUR APPOINTMENT:</b>" +
      "<br/><br/>➢  Go to the following link to connect to the online counseling appointment and the counselor will join you (link to counselor online room). " +
      "<br/><br/>  " + link +
      "<br/><br/>➢  If you want to use the phone for audio: " +
      "<br/><ul><li>Telephone Conference Line: 719-785-4469, Toll-free: 1-888-450-4821</li><li>Student PIN: " + pin + "</li></ul>" +
      "<br/><br/><br/><b>FOR ASSISTANCE CONNECTING</b>" + 
      "<br/><br/>Contact CCC Confer Client Services, Monday - Friday 8am - 4pm  " + 
      "<br/><br/>Phone: 1-760-744-1150 ext 1537 or 1554 " +
      "<br/><br/>Email: clientservices@cccconfer.org " +
      "<br/><br/><br/>Thank You! " + sName;

      var optAdvancedArgs = {name: "SCC Counseling", htmlBody: htmlBody};


    if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
      var subject = "Online Counseling Appointment Confirmation - " + sName + " " + wNum + " ";
    MailApp.sendEmail(toemail, subject, emailBody, optAdvancedArgs);
  sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
      // Make sure the cell is updated right away in case the script is interrupted
  SpreadsheetApp.flush();
  }
}
}

【问题讨论】:

  • 将其包装在 if 中并检查时间戳是否为空。
  • 您的 getRange(startRow, 1, numRows, 1000) 正在获得 1,000 列。这真的是你想要的吗?

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


【解决方案1】:

创建一个简单的函数来测试#NA!

function checkForNa() {
  var theVal = SpreadsheetApp.getActiveSpreadsheet().getRange("A1").getValue();

  if (theVal === "#NA!") {
    Logger.log('it's NA');
    return;
  };

  Logger.log('There is data. The code continued');
};

故意造成#NA!错误,然后运行代码。

如果代码有效,请将条件检查合并到您的代码中。 return; 语句将停止您的代码。

如果您不想让代码停止,而是要跳过这一行,可以使用break; 语句,但break; 只能在循环内使用。因此,您需要一个嵌套循环来检查 #NA! 值,以跳过该行。

function checkForNa() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var startRow = 2;  // First row of data to process
  var numRows = 1000;
  var dataRange = sheet.getRange(startRow, 2, numRows, 1000)
  var data = dataRange.getValues();

  for (var i = 0; i < data.length; ++i) {
    var theVal = data[i][0]; //Get first value out of this row

    for (var j = 0;j<1;j++) { //Only loops once
      if (theVal === "#NA!") {
        Logger.log('skipped this row: ' + i);
        break;
      };
      Logger.log('loop number: ' + i);
    };
  };
};

这是示例代码。您需要根据您的代码对其进行调整。

【讨论】:

    【解决方案2】:

    只需创建一个简单的 IF 语句来检查时间戳。它会工作得很好。您可能必须修复右括号,因为我不确定它们应该在哪里。

    /**
     * Add a menu item that calls this script
     */
    function onOpen() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    
      var menu = [{
        name: "Send Email",
        functionName: "uiSendEmail"
      }];
    
      ss.addMenu("SCCCounseling", menu);
    
    }
    var EMAIL_SENT = "EMAIL_SENT";
    /**
     * Send the e-mail to everyone listed in the spreadsheet
     */
    
    function uiSendEmail() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var startRow = 2;  // First row of data to process
      var numRows = 1000;
      var dataRange = sheet.getRange(startRow, 1, numRows, 1000)
      // Fetch values for each row in the Range.
      var data = dataRange.getValues();
      for (var i = 0; i < data.length; ++i) {
        if (data[i][INDEX_OF_TIMESTAMP] != "") {
    
          var row = data[i];
    
          var timestamp = row[0];
    
          var username = row[1];
    
          var counselor = row[2];
    
          var date = row[3];
    
          var sName = row[4];
    
          var wNum = row[5];
    
          var toemail = row[6];
    
          var sLName= row[7];
    
          var pin = row[8];
    
          var link = row[9];
    
          var cMail = row[10];
    
          var emailSent = row[11]; 
    
          var subject = "Online Counseling Appointment Confirmation - " + sName + " " + sLName + " " + wNum + " ";
    
          var emailBody = "Online Counseling Appointment " + " - " + sName + " " + sLName + " " + wNum + 
    
          "\n\nHello " + sName +
    
          "\n\n\nYou are scheduled for an online counseling appointment for Sacramento City College: " +
    
          "\n\n\nDate: " + date +
    
          "\n\nPlease follow the instructions below at least one day before your appointment to make " +
    
          "\nsure that your computer is ready. " +
    
          "\n\nIf your computer is not ready, you will not be able to " +
    
          "\nparticipate in online counseling. " +
    
          "\n\nTo cancel your appointment, please email: " +
    
          "\n\nCounselor: " + counselor + " at " + cMail +
    
          "\n\nYou will be required to show photo identification at the start of the appointment to verify " +
    
          "\nyour identity. " +
    
          "\n\n\nONE DAY BEFORE YOUR APPOINTMENT " +
    
          "\n\nGo to the following link to test your computer readiness one day before your " +
    
          "\nscheduled online counseling appointment: " +
    
          "\n\nhttp://www.cccconfer.org/support/supportReadiness.aspx " +
    
          "\n\n\nFIVE MINUTES BEFORE YOUR APPOINTMENT: " +
    
          "\n\nGo to the following link to connect to the online counseling appointment and the " +
    
          "\ncounselor will join you: " + link +
    
          "\n\n\nIf you want to use the phone for audio: " +
    
          "\n\nTelephone Conference Line: 719-785-4469, Toll-free: 1-888-450-4821 " +
    
          "\n\nStudent PIN: " + pin +
    
          "\n\n\nFOR ASSISTANCE CONNECTING" + 
    
          "\n\nContact CCC Confer Client Services, Monday - Friday 8am - 4pm  " +
    
          "\n\nPhone: 1-760-744-1150 ext 1537 or 1554 " +
    
          "\n\nEmail: clientservices@cccconfer.org " +
    
          "\n\n\nThank you " + sName;
    
    
    
    
    
          var htmlBody = "<b>Online Counseling Appointment Confirmation" + " -</b> " + " - " + sName +  " " + sLName + " " + wNum +
          "<br/><br/><br/>" + "Hello " + sName + ", <br/><br/>" + "You are scheduled for an online counseling appointment for Sacramento City College: <i>" + 
          "<br/><br/><br/>" + "<b>Date: " + date + "<br/><br/>"  + "</b><br/>" + "</i><br/>Please follow the instructions below <b>at least one day before</b> your appointment to make sure that your computer is ready." +
          "<br/><br/><i>If your computer is not ready, you will not be able to participate in online counseling.</i> " + "<br/><br/> To cancel your appointment, please email: " + 
          "<br/><br/><font color=\"red\">Counselor:</font> " + counselor + " at " + cMail + "<br/><br/><br/>You will be required to show photo identification at the start of the appointment to verify your identity. " +
          "<br/><br/><br/><b>ONE DAY BEFORE YOUR APPOINTMENT:</b> " + "<br/><br/>➢  Go to the following link to test your computer readiness one day before your scheduled online counseling appointment: " +
          "<br/><br/>http://www.cccconfer.org/support/supportReadiness.aspx " +
          "<br/><br/><br/><b>FIVE MINUTES BEFORE YOUR APPOINTMENT:</b>" +
          "<br/><br/>➢  Go to the following link to connect to the online counseling appointment and the counselor will join you (link to counselor online room). " +
          "<br/><br/>  " + link +
          "<br/><br/>➢  If you want to use the phone for audio: " +
          "<br/><ul><li>Telephone Conference Line: 719-785-4469, Toll-free: 1-888-450-4821</li><li>Student PIN: " + pin + "</li></ul>" +
          "<br/><br/><br/><b>FOR ASSISTANCE CONNECTING</b>" + 
          "<br/><br/>Contact CCC Confer Client Services, Monday - Friday 8am - 4pm  " + 
          "<br/><br/>Phone: 1-760-744-1150 ext 1537 or 1554 " +
          "<br/><br/>Email: clientservices@cccconfer.org " +
          "<br/><br/><br/>Thank You! " + sName;
    
          var optAdvancedArgs = {name: "SCC Counseling", htmlBody: htmlBody};
    
    
          if (emailSent != EMAIL_SENT) {  // Prevents sending duplicates
            var subject = "Online Counseling Appointment Confirmation - " + sName + " " + wNum + " ";
          MailApp.sendEmail(toemail, subject, emailBody, optAdvancedArgs);
          sheet.getRange(startRow + i, 12).setValue(EMAIL_SENT);
          // Make sure the cell is updated right away in case the script is interrupted
          SpreadsheetApp.flush();
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多