排序中断后通过电子邮件发送脚本

时间:2019-06-20 21:29:35

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

我已经在此处发布了有关此内容的信息,因此希望可以就此代码的另一个问题进行询问。

使用表单发送新的提交时,我收到脚本电子邮件。它的运行效果很好,但后来我想将最新消息排在最前面,现在我没有收到任何电子邮件。我在这个脚本中错过了什么?

排序脚本

// Dirty but works
var SORT_COLUMN_INDEX = 1;
var ASCENDING = false;

var NUMBER_OF_HEADER_ROWS = 1;


var activeSheet;

function autoSort(sheet) {
  var range = sheet.getDataRange();

  if (NUMBER_OF_HEADER_ROWS > 0) {

    range = range.offset(NUMBER_OF_HEADER_ROWS, 0);
  }

  range.sort( {
    column: SORT_COLUMN_INDEX,
    ascending: ASCENDING
  } );
}


function onEdit(event) {
  var editedCell;

  activeSheet = SpreadsheetApp.getActiveSheet();

  editedCell = activeSheet.getActiveCell();

  if (editedCell.getColumn() == SORT_COLUMN_INDEX) {
    autoSort(activeSheet);
  }
}


function onOpen(event) {
  activeSheet = SpreadsheetApp.getActiveSheet();
  autoSort(activeSheet);
}

function onInstall(event) {
  onOpen(event);
}


电子邮件脚本

function SendEmail() {
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 2;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,19);
 var AllValues = WholeRange.getValues();

 var message = "";
 for (i in AllValues) {
 var CurrentRow = AllValues[i];

 var EmailSent = CurrentRow[19];

 if (EmailSent == "Sent") 
     continue;

  message =
      "<p><b>Email?: </b>" + CurrentRow[1] + "</p>" +
      "<p><b>District: </b>" + CurrentRow[2] + "</p>" +
      "<p><b>Vendor Cost: </b>" + CurrentRow[3] + "</p>" +
      "<p><b>Vendor Cost: </b>" + CurrentRow[4] + "</p>" +
      "<p><b>Default Price: </b>" + CurrentRow[5] + "</p>" +
      "<p><b>Vendor: </b>" + CurrentRow[6] + "</p>" +
      "<p><b>Vendor SKU: </b>" + CurrentRow[7] + "</p>" +
      "<p><b>Manufacturer: </b>" + CurrentRow[8] + "</p>" +
      "<p><b>Manufacturer SKU: </b>" + CurrentRow[9] + "</p>" +
      "<p><b>Barcode: </b>" + CurrentRow[10] + "</p>"+
      "<p><b>Category: </b>" + CurrentRow[11] + "</p>" +
      "<p><b>Device: </b>" + CurrentRow[12] + "</p>" +
      "<p><b>Model: </b>" + CurrentRow[13] + "</p>"+
      "<p><b>Color: </b>" + CurrentRow[14] + "</p>";


  var setRow = parseInt(i) + StartRow;


  ActiveSheet.getRange(setRow, 19).setValue("Sent");
}


 var SendTo = "@vzwpix.com";
 var SendToEmail = "davidm";


  var Subject = "New Sku:" +" "+ CurrentRow[13] +" " +"From:"+" "+CurrentRow[1];

  MailApp.sendEmail({
      to: SendTo,
      cc: "",
      subject: Subject,
      htmlBody: message,
});

   MailApp.sendEmail({
      to: SendToEmail,
      cc: "",
      subject: Subject,
      htmlBody: message,
});


}

备用工作脚本

我调整了脚本以搜索空列并发送邮件 这是脚本:

function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 3; // Start at second row because the first row contains the data labels
  var lastRow = sheet.getLastRow()
  var lastCol = sheet.getLastColumn()

  // Fetch the range of cells, in this case A2:T
  // Column B, row[2] = Email Address, Column C, row[1] = Name
  var dataRange = sheet.getRange(startRow, 1, lastRow, lastCol)

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

  for (var i = 0; i < data.length-1; ++i) {
    var row = data[i];
   var emailAddress = row[1]; // 
    var message = 
      "<p><b>Email?: </b>" + row[1] + "</p>" +
      "<p><b>District: </b>" + row[2] + "</p>" +
      "<p><b>Vendor Cost: </b>" + row[3] + "</p>" +
      "<p><b>Vendor Cost: </b>" + row[4] + "</p>" +
      "<p><b>Default Price: </b>" + row[5] + "</p>" +
      "<p><b>Vendor: </b>" + row[6] + "</p>" +
      "<p><b>Vendor SKU: </b>" + row[7] + "</p>" +
      "<p><b>Manufacturer: </b>" + row[8] + "</p>" +
      "<p><b>Manufacturer SKU: </b>" + row[9] + "</p>" +
      "<p><b>Barcode: </b>" + row[10] + "</p>"+
      "<p><b>Category: </b>" + row[11] + "</p>" +
      "<p><b>Device: </b>" + row[12] + "</p>" +
      "<p><b>Model: </b>" + row[13] + "</p>"+
      "<p><b>Color: </b>" + row[14] + "</p>"



    ; // Assemble the body text
        var cell = row[19]; // Last column
    if (cell == "") { 
   var subject = "Sending emails from a Spreadsheet";
  MailApp.sendEmail({
      to: "PHONE@vzwpix.com",
      cc: "",
      subject: subject,
      htmlBody: message,
});
  }
}
}

0 个答案:

没有答案