我正在尝试在 Google Sheets 中编写一个脚本,该脚本从主工作表中提取数据,并发送包含填充数据的电子邮件,所有这些都基于单元格是否具有“是”或“否”
我将此脚本绑定到
onEdit()
触发器(我不知道我是否正确调用),并且它实际上发送了电子邮件,但某个地方出现错误,最终将我的整个函数转储到正文中电子邮件。
这是我的代码:
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,之前我真的只做过调试。这次我在调试自己的逻辑时遇到了麻烦:)
我已经在这个网站和谷歌上搜索了一个小时左右,但没有找到任何可以帮助我解决问题的东西。有人有什么建议吗?
在
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
。