Google表格 - 根据条件

时间:2017-01-04 00:57:50

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

我试图在Google表格中编写一个脚本,从主表格中提取数据,并发送包含填充数据的电子邮件,所有这些都取决于单元格是否有"是"或"否"

我将此脚本绑定到onEdit()触发器(我不知道我是否正确调用)并且我实际上发送了电子邮件,但在某处发生了错误最终将我的整个功能转储到电子邮件正文中。

Here's a copy of the sheet in question

这是我的代码:

function sendEmails() {
  var trackOriginSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(e);
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Master");
  var startRow = 2;  // First row of data to process
  var numRows = 506;   // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, numRows, 26);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  var refDesc = function(trackOriginSheet) {      
      var mirandaSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Miranda");
      var piperSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Piper");
      var lowesSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Lowes");
      var goldenSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Golden");
      var refDescContent = trackOriginSheet;
      if (trackOriginSheet === mirandaSheet) {
        var getMirSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Miranda");
        var startMirRow = 2;  // First row of data to process
        var numRowsMir = 506;   // Number of rows to process
        // Fetch the range of cells A2:B3
        var dataRangeMir = sheet.getRange(startRow, 1, numRows, 26);
        // Fetch values for each row in the Range.
        var dataMir = dataRange.getValues();
        for (var j in dataMir) {
          var secondRowMir = dataMir[i];
          var intRefDescMir = secondRowMir[3];
          return intRefDescMir;
        }
      } else if (trackOriginSheet === piperSheet) {
        var getPipSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Piper");
        var startPipRow = 2;  // First row of data to process
        var numRowsPip = 506;   // Number of rows to process
        // Fetch the range of cells A2:B3
        var dataRangePip = sheet.getRange(startRow, 1, numRows, 26);
        // Fetch values for each row in the Range.
        var dataPip = dataRange.getValues();
        for (var k in dataPip) {
          var secondRowPip = dataPip[k];
          var intRefDescPip = secondRowPip[3];
          return intRefDescPip;
        }
      } else if (trackOriginSheet === lowesSheet) {
        var getLowSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Lowes");
        var startLowRow = 2;  // First row of data to process
        var numRowsLow = 506;   // Number of rows to process
        // Fetch the range of cells A2:B3
        var dataRangeLow = sheet.getRange(startRow, 1, numRows, 26);
        // Fetch values for each row in the Range.
        var dataLow = dataRange.getValues();
        for (var l in dataLow) {
          var secondRowLow = dataLow[l];
          var intRefDescLow = secondRowLow[3];
          return intRefDescLow;
        }
      } else if (trackOriginSheet === goldenSheet) {
        var getGoldSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Golden");
        var startGoldRow = 2;  // First row of data to process
        var numRowsGold = 506;   // Number of rows to process
        // Fetch the range of cells A2:B3
        var dataRangeGold = sheet.getRange(startRow, 1, numRows, 26);
        // Fetch values for each row in the Range.
        var dataGold = dataRange.getValues();
        for (var m in dataGold) {
          var secondRowGold = dataGold[m];
          var intRefDescGold = secondRowGold[3];
          return intRefDescGold;
        }
      }
  };
  for (var i in data) {
    var row = data[i];
    var teacherEmail = row[1];  // Teacher Email Column
    var iepEmail = row[16];  // IEP Manager Email Column
    var counselorEmail = row[19];  // Assigned Counselor Email Column
    var referallEmails = [teacherEmail, iepEmail, counselorEmail];   //All admin emails
    var studName = row[4] + " " + row[3];  //Stores student name in selected sheet
    var adminActions = row[3];
    var datesAppl = row[4];
    var message = "The referral submitted for " + studName + "has been completed. The following action(s) were taken:" + refDesc + " " + adminActions + " " + datesAppl + " " + "Please email the member of the discipline team who handled the referral if you have any questions or concerns, at: "; 
    var subject = "Referral: " + studName;
    MailApp.sendEmail(teacherEmail, subject, message);
  }
}
Logger.log(sendEmails);

“守则”存在问题,可能会超出需要的范围。这是我的第一个"实际应用"在JavaScript中,我之前只做过调试。这次我无法调试自己的逻辑:)

我已经在这个网站和谷歌上挖了一个小时左右,但是没有找到任何帮助我解决问题的方法。有人有任何提示吗?

1 个答案:

答案 0 :(得分:1)

var message = "The referral submitted for " + studName + "has been completed. The following action(s) were taken:" + refDesc + " " + adminActions + " " + datesAppl + " " + "Please email the member of the discipline team who handled the referral if you have any questions or concerns, at: ";

我怀疑你的意思是refDesc()而不是refDesc