这是一个谜题。我有一个谷歌电子表格。这意味着允许在线评估讲座。居民登录相关表格并填写。
伴随它的是一个“唠叨”功能 - 第二张表检查他们的名字,跟踪他们是否已经提交,如果没有,它会在一定时间(3天)过去后发送一封唠叨电子邮件。
该脚本运行良好,我手动执行。另外,如果我使用分钟计时器(即每 10 分钟运行一次)。
但是,当我为我想要的内容设置脚本时 - 每天检查一次,它没有运行,并且我收到以下错误报告:
无效的电子邮件:#N/A
这特别奇怪,因为我为其他电子表格运行了其他类似的功能,而且它们一直有效。
我能想到的唯一不同的是我有几个类似的脚本正在运行(每周一张)。我尝试将函数名称更改为独特的名称,还将电子表格调用更改为 openById,并将工作表变量更改为 getSheetByName,以防多个类似的脚本以某种方式相互混淆。没有差异。
电子表格副本的链接在这里https://docs.google.com/a/brown.edu/spreadsheet/ccc?key=0AkP6szc0nsS2dEItLXEwdjVsWUFpOTdjZUdtTlo4cGc#gid=0
我在下面附上了脚本(你会注意到我添加了错误报告,但它并没有增加太多)。数据变量从教程中借用,该教程片段对我来说一直很好用。
任何见解将不胜感激。
代码:
var SEMINAR_FORM_URL = "http://med.brown.edu/DPHB/training/psychiatry_general/secure/seminarevals/seminar_evals.html";
var YES = "YES";
var DISTRIBUTE = "robert_boland_1@brown.edu,robertboland11@gmail.com";
//This function sends out a nag to do the seminar evals including a link to the seminar form
//This is allowed when sheet 2 says "YES" to "Nag?"
//Should not check more than once daily or will keep sending out and should be triggered AFTER the checkCoverageSubmit.
function sendNag() {
try {
var ss = SpreadsheetApp.openById("0AkP6szc0nsS2dDkwQTE0OVlPeWM5ZlJIenVKLU1pVVE");
var sheet = ss.getSheetByName("Sheet2");
var startRow = 2
// getRowsData was reused from Reading Spreadsheet Data using JavaScript Objects tutorial
var data = getRowsData(sheet);
// For every Seminar report row
for (var i = 0; i < data.length; ++i) {
var row = data[i];
row.rowNumber = i + 2;
if (row.nag==YES) { //sends the nag message below
var message = "<HTML><BODY>"
+ "<P> THIS IS A REMINDER MESSAGE:"
+ "<P> For " + row.name
+ "<P> You still need to complete your seminar evaluations for"
+ "<P> the seminars done on "+ row.dateOfSeminar
+ "<P>"
+ "<P> It has been "+ row.daysElapsed + " days since the seminar."
+ "<P>"
+ '<P><b> Please fill out the evaluations. You can find links to the evalutions on <A HREF="' + SEMINAR_FORM_URL + '"><b>HERE.</b></A> Do this now!</b>.'
+ "<P>"
+ "<P>"
+ "<P>"
+ "<P>--------------------------------------------------------"
+ "<P> <i>You are receiving this message because our records report that you have yet to complete a required seminar evaluations. </i>"
+ "<P> If you think this remind was sent in error, please <a href='mailto:robert_boland_1@brown.edu'><b>contact me</b></a> to prevent further nagging!"
+ "<P>"
+ "<P>_______________________________________________________________________________"
+ "</HTML></BODY>";
if(row.name){
MailApp.sendEmail(row.name, "Reminder: Please complete your seminar evals!", "", {cc:DISTRIBUTE,htmlBody: message});
} SpreadsheetApp.flush();
}
}
} catch (e) {
MailApp.sendEmail("robert_boland_1@brown.edu", "Error report", e.message);
}
}
/////////////////////////////////////////////////////////////////////////////////
// Code reused from Reading Spreadsheet Data using JavaScript Objects tutorial //
/////////////////////////////////////////////////////////////////////////////////
// getRowsData iterates row by row in the input range and returns an array of objects.
// Each object contains all the data for a given row, indexed by its normalized column name.
// Arguments:
// - sheet: the sheet object that contains the data to be processed
// - range: the exact range of cells where the data is stored
// This argument is optional and it defaults to all the cells except those in the first row
// or all the cells below columnHeadersRowIndex (if defined).
// - columnHeadersRowIndex: specifies the row number where the column names are stored.
// This argument is optional and it defaults to the row immediately above range;
// Returns an Array of objects.
function getRowsData(sheet, range, columnHeadersRowIndex) {
var headersIndex = columnHeadersRowIndex || range ? range.getRowIndex() - 1 : 1;
var dataRange = range ||
sheet.getRange(headersIndex + 1, 1, sheet.getMaxRows() - headersIndex, sheet.getMaxColumns());
var numColumns = dataRange.getEndColumn() - dataRange.getColumn() + 1;
var headersRange = sheet.getRange(headersIndex, dataRange.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(dataRange.getValues(), normalizeHeaders(headers));
}
// For every row of data in data, generates an object that contains the data. Names of
// object fields are defined in keys.
// Arguments:
// - data: JavaScript 2d array
// - keys: Array of Strings that define the property names for the objects to create
function getObjects(data, keys) {
var objects = [];
for (var i = 0; i < data.length; ++i) {
var object = {};
var hasData = false;
for (var j = 0; j < data[i].length; ++j) {
var cellData = data[i][j];
if (isCellEmpty(cellData)) {
continue;
}
object[keys[j]] = cellData;
hasData = true;
}
if (hasData) {
objects.push(object);
}
}
return objects;
}
// Returns an Array of normalized Strings.
// Empty Strings are returned for all Strings that could not be successfully normalized.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
keys.push(normalizeHeader(headers[i]));
}
return keys;
}
// Normalizes a string, by removing all alphanumeric characters and using mixed case
// to separate words. The output will always start with a lower case letter.
// This function is designed to produce JavaScript object property names.
// Arguments:
// - header: string to normalize
// Examples:
// "First Name" -> "firstName"
// "Market Cap (millions) -> "marketCapMillions
// "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored"
function normalizeHeader(header) {
var key = "";
var upperCase = false;
for (var i = 0; i < header.length; ++i) {
var letter = header[i];
if (letter == " " && key.length > 0) {
upperCase = true;
continue;
}
if (!isAlnum(letter)) {
continue;
}
if (key.length == 0 && isDigit(letter)) {
continue; // first character must be a letter
}
if (upperCase) {
upperCase = false;
key += letter.toUpperCase();
} else {
key += letter.toLowerCase();
}
}
return key;
}
// Returns true if the cell where cellData was read from is empty.
// Arguments:
// - cellData: string
function isCellEmpty(cellData) {
return typeof(cellData) == "string" && cellData == "";
}
// Returns true if the character char is alphabetical, false otherwise.
function isAlnum(char) {
return char >= 'A' && char <= 'Z' ||
char >= 'a' && char <= 'z' ||
isDigit(char);
}
// Returns true if the character char is a digit, false otherwise.
function isDigit(char) {
return char >= '0' && char <= '9';
}