Update cell values on Google sheet during mail merge apps script - google-apps-script

I'm using the Google simple mail merge apps script and I want to update the "status" of each person in the spreadsheet to "confirmed" once the email has been sent to them.
The scripts uses the getRowsData function to give you all of the data from a row ...
// Create one JavaScript object per row of data.
var objects = getRowsData(dataSheet, dataRange);
I'm not sure how to use this to set the value of the "status" column, which is the 7th column on my sheet. On their Sending Emails From A Spreadsheet tutorial code they use a startRow variable and getRange ...
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
...
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
... but I can't see how to adapt that to use the with the "objects" variable in the Mail Merge example's code.
It doesn't make sense to use the "startRow" variable when I've already got the all the row info in the "objects" variable.
I tried this ...
dataSheet.getRange(rowData + i, 7).setValue(EMAIL_SENT);
... but just got the error "Cannot convert [object Object]0 to (class)." during debugging.
I know I'm doing something really stupid / missing something obvious. My Javascript is pretty rusty!
If someone can point me in the right direction, that would be great! :)
PS - Here's the standard simple mail merge script ....
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 6);
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A1").getValue();
var emailSubject = templateSheet.getRange("A2").getValue();
var emailFrom = templateSheet.getRange("A3").getValue();
var emailReplyTo = templateSheet.getRange("A4").getValue();
var imageb64 = templateSheet.getRange("A5").getValue();
var imageb64h = templateSheet.getRange("A6").getValue();
// Create one JavaScript object per row of data.
var objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
if (emailFrom == null || emailFrom == ""){
MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText);
}else{
var inlineImages = {};
var imgblob;
var imgType;
if (imageb64 != null && imageb64 != ""){
imageType = imageb64.substring(5, imageb64.indexOf(";"))
imageb64 = imageb64.substring(imageb64.indexOf(",") + 1)
imgblob = Utilities.newBlob(Utilities.base64Decode(imageb64), imageType, "signature"); // decode and blob
inlineImages["signature"] = imgblob
}
if (imageb64h != null && imageb64h != ""){
imageType = imageb64h.substring(5, imageb64h.indexOf(";"))
imageb64h = imageb64h.substring(imageb64h.indexOf(",") + 1)
imgblob = Utilities.newBlob(Utilities.base64Decode(imageb64h), imageType, "header"); // decode and blob
inlineImages["header"] = imgblob
}
MailApp.sendEmail(rowData.emailAddress, emailSubject, "", {cc: rowData.copy, name: emailFrom, replyTo: emailReplyTo, htmlBody: emailText, inlineImages: inlineImages});
}
}
}
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Send Emails",
functionName : "sendEmails"
}
];
spreadsheet.addMenu("Mail Actions", entries);
};
// Replaces markers in a template string with values define in a JavaScript data object.
// Arguments:
// - template: string containing markers, for instance ${"Column name"}
// - data: JavaScript object with values to that will replace markers. For instance
// data.columnName will replace marker ${"Column name"}
// Returns a string without markers. If no data is found to replace a marker, it is
// simply removed.
function fillInTemplateFromObject(template, data) {
var email = template;
// Search for all the variables to be replaced, for instance ${"Column name"}
var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);
// Replace variables from the template with the actual values from the data object.
// If no value is available, replace with the empty string.
for (var i = 0; i < templateVars.length; ++i) {
// normalizeHeader ignores ${"} so we can call it directly here.
var variableData = data[normalizeHeader(templateVars[i])];
email = email.replace(templateVars[i], variableData || "");
}
return email;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the '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
// - 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) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.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.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
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';
}

It doesn't make sense to use the "startRow" variable when I've already got the all the row info in the "objects" variable.
Since the object only contains the data in the rows and not the row itself, using the startRow variable is not such a bad idea.
Now the reason why it failed is only cause in your line
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
should be
sheet.getRange(startRow + i, 3).setValue("EMAIL_SENT");

Related

Type error: cannot read property length from null

We have had a sheet that gets data for a quality of work form. This has run flawlessly for over a year now, nothing at all with the sheets, form or script has ever changed since then.
But for some reason, I am now getting an error whenever we run the SendEmail function:
TypeError: Cannot read property "length" from null. (line 83, file "SendEmail")
This is where the error is now happening:
for (var i = 0; i < templateVars.length; ++i) {
The first column (Item) should have sequential numbers entered automatically whenever the script is run. Nut this now all stays blank and the email template never gets emailed.
function SendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 19);
var lastRow = dataSheet.getMaxRows();
for (var x = 0; x < dataSheet.getMaxRows() - 2; ++x) {
if(dataSheet.getRange(x+2, 3).getValue()!=""){
if(dataSheet.getRange(x+2, 15).getValue()==""){
var EMInum = dataSheet.getRange((x + 1), 1).getValue() + 1;
dataSheet.getRange(x+2, 1).setValue(EMInum);
dataSheet.getRange(x+2, 2).setFormula("=\"QoW-\"&text(A" + (x+2) + ",\"000\")");
dataSheet.getRange(x+2, 15).setFormula("=vlookup(I" + (x+2) + ",ContactDetails,2,0)");
dataSheet.getRange(x+2, 16).setFormula("=vlookup(I" + (x+2) + ",ContactDetails,3,0)");
dataSheet.getRange(x+2, 17).setFormula("=match(J" + (x+2) + ",CompanyName,1)");
dataSheet.getRange(x+2, 18).setFormula("=vlookup(I" + (x+2) + ",ContactDetails,Q" + (x+2) + ",0)");
dataSheet.getRange(x+2, 19).setFormula("=vlookup(I" + (x+2) + ",ContactDetails,Q" + (x+2) + "+1,0)");
dataSheet.getRange(x+2, 27).setFormula("=I" + (x+2)+ "&if(Z" + (x+2)+ "=\"\" ,\" OPEN\",\" CLOSED\")");
dataSheet.getRange(x+2, 28).setFormula("=if(right(AA" + (x+2) + ",4)=\"OPEN\",now()-datevalue(I" + (x+2)+ "),0)");
dataSheet.getRange(x+2, 29).setFormula("=if(AB" + (x+2) + "=0,\"e) Closed\",if(AB" + (x+2) + "<90,\"a) 0 - 90 days\",if(AB" + (x+2) + "<180,\"b) 90 - 180 days\",if(AB" + (x+2) + "<360,\"c) 180 - 360 days\",\"d) Over 360 days\"))))");
//Browser.msgBox("");
}
}
}
//
//
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A1").getValue();
var defaultCCAddress = templateSheet.getRange("B17").getValue(); // Added by zumzum as per projectid:a0CD000000xFxWQ
// Create one JavaScript object per row of data.
objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "NO REPLY: EM&I Quality of Work Issue Submission " + dataSheet.getRange(i + 2, 2).getValue();
var emailCheck = dataSheet.getRange(i + 2, 13).getValue();
//Browser.msgBox(emailCheck);
var emailAddress = dataSheet.getRange(i + 2, 18).getValue();
var ccemailAddress = dataSheet.getRange(i + 2, 15).getValue();
if(emailCheck=="")
{
/* original code commented by zumzum as per projectid:a0CD000000xFxWQ
MailApp.sendEmail(emailAddress, emailSubject, emailText,{cc:ccemailAddress+",david.mortlock#emialliance.com,peter.gresty#emialliance.com",bcc:"richard.priddes#emialliance.com"});
dataSheet.getRange(i + 2, 13).setValue("sent - " + Date());
*/
// Start od new code added by zumzum as per projectid:a0CD000000xFxWQ
MailApp.sendEmail(emailAddress, emailSubject, emailText,{cc:ccemailAddress+","+defaultCCAddress});
dataSheet.getRange(i + 2, 13).setValue("sent - " + Date());
// End of new code added by zumzum as per projectid:a0CD000000xFxWQ
}
}
}
// Replaces markers in a template string with values define in a JavaScript data object.
// Arguments:
// - template: string containing markers, for instance ${"Column name"}
// - data: JavaScript object with values to that will replace markers. For instance
// data.columnName will replace marker ${"Column name"}
// Returns a string without markers. If no data is found to replace a marker, it is
// simply removed.
function fillInTemplateFromObject(template, data) {
var email = template;
// Search for all the variables to be replaced, for instance ${"Column name"}
var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);
// Replace variables from the template with the actual values from the data object.
// If no value is available, replace with the empty string.
for (var i = 1; i < templateVars.length; i++) {
// normalizeHeader ignores ${"} so we can call it directly here.
var variableData = data[normalizeHeader(templateVars[i])];
email = email.replace(templateVars[i], variableData || "");
}
return email;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the '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
// - 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) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.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.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
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';
}```
The error you are getting can be reproduced by running fillInTemplateFromObject(emailTemplate, rowData) with any string for emailTemplate that does not contain a placeholder in the form ${"foo"} (the literal double quotes are required per the regex /\$\{\"[^\"]+\"\}/g).
In SendEmail, the value for emailTemplate is coming from the first sheet, cell A1. Check what value you have in that cell. Does it contain templates in the correct ${"foo"} form?
Since this error can be caused by a user manipulating the spreadsheet, consider improving fillInTemplateFromObject by checking for a successful match before trying to use the expected array.
function fillInTemplateFromObject(template, data) {
var email = template;
// Search for all the variables to be replaced, for instance ${"Column name"}
var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);
if (! templateVars) {
throw new Error("No templates found in your template sheet. Aborting.")
}
// Replace variables from the template with the actual values from the data object.
// If no value is available, replace with the empty string.
for (var i = 0; i < templateVars.length; i++) {
// normalizeHeader ignores ${"} so we can call it directly here.
var variableData = data[normalizeHeader(templateVars[i])];
email = email.replace(templateVars[i], variableData || "");
}
return email;
}
You can catch that error in SendEmail and provide even more useful information:
// inside SendEmail
try {
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
} catch (error) {
throw new Error("In spreadsheet " + ss.getUrl() + " sheet " + templateSheet.getSheetName() + " row " + (i+1) + " there were no templates found.")
}
If you still need help debugging the error, use the apps script debugger or use the various logging options built into apps script to output the state of your data at run time.
Why are you using ++i rather than i++? My guess is this is the problem, if templateVars is an array, you are getting to the end of the array and trying to run it once on a row that doesn't exist. Maybe before, this was an empty row, but now the last row is the end of the sheet.
Knowing nothing else about your code I'd change:
for (var i = 0; i < templateVars.length; ++i) {
to
for (var i = 1; i < templateVars.length; i++) {

Apps script getRowsData function deprecated?

I'm working through a tut about a basic mail merge using google sheets https://developers.google.com/apps-script/articles/mail_merge#section-4-more-powerful-templates .
The code sample contains the following (which I have modified):
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainSheet = ss.getSheetByName("Main");
var templateSheet = ss.getSheetByName("Campaigns");
var emailTemplate = templateSheet.getRange("A1").getValue();
// Create one JavaScript object per row of data.
objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSubject = "Tutorial: Simple Mail Merge";
MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText);
}
}
I am unable to find a reference to 'getRowsData' in https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet.
Has this been deprecated?
You just needed to scroll down a bit further.
function getRowsData(sheet, range, columnHeadersRowIndex) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.getValues(), normalizeHeaders(headers));
}
You may have missed all of this as well.
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the '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
// - 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) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.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.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
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';
}
You can use the copy icon in the upper right corner as shown in the image below:
Cooper,
The sample code on the tutorial page (https://developers.google.com/apps-script/articles/mail_merge) labeled 'Full Code' does not contain the getRowsData declaration that you have posted. So, using the copy icon will not work as the copied code is missing the getRowsData function declaration. To access the getRowsData function portion of the code you need to copy the template spreadsheet and open the script editor to view the code.

Google apps script spreadsheet:error

I work for a public school district and have written a script to take data from a Google form response sheet and filter it into separate sheets based on which school the information relates to. Using the tutorial located at https://developers.google.com/apps-script/guides/sheets . The modified script is 1300 lines long (including some explinations) and has been running smoothly up until recently.
The script now returns numerous errors. Most commonly I see errors such as:
"Service timed out: Spreadsheets (line 40, file "Transfer/SortID/Copy/Sort")"
"Service error: Spreadsheets (line 40, file "Transfer/SortID/Copy/Sort")"
The line that is flagged in the error is
"headersRange.setValues([columnNames]);"
I have tried everything that I can think of including creating a new target spreadsheet.
I have included the beginning section of the code below. Again, this script was running great up until recently. (Last time it ran successfully was 5/6)
Thanks in advance for your help!
// This is where the data used in this example will be retrieved from:
// https://docs.google.com/a/psdschools.org/spreadsheet/ccc?key=0AswYUpTPhetrdFYxUEJZSkxHeVdfSk5pajh3UjYxaUE&usp=drive_web#gid=0
function TransferAndSort() {{
var DATA_SPREADSHEET_ID = "0AswYUpTPhetrdFYxUEJZSkxHeVdfSk5pajh3UjYxaUE"
//update data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var dataSs = SpreadsheetApp.openById(DATA_SPREADSHEET_ID);
var dataSheet = dataSs.getSheets()[0];
// Fetch all the data
var data = getRowsData(dataSheet);
// This is the data we want to display
var columnNames = ["Last Name","First Name", "ID Number", "School", "Date Written", "Purpose", "Requested Route #", "Requested Stop Location", "Type of Student", "Contact Made"];
// Index data by School name
var dataBySchool = {};
var schools = [];
for (var i = 0; i < data.length; ++i) {
var rowData = data[i];
if (!dataBySchool[rowData.school]) {
dataBySchool[rowData.school] = [];
schools.push(rowData.school);
}
dataBySchool[rowData.school].push(rowData);
}
schools.sort();
var headerBackgroundColor = dataSheet.getRange(1, 1).getBackgroundColor();
for (var i = 0; i < schools.length; ++i) {
var sheet = ss.getSheetByName(schools[i]) ||
ss.insertSheet(schools[i], ss.getSheets().length);
sheet.clear();
var headersRange = sheet.getRange(1, 1, 1, columnNames.length);
headersRange.setValues([columnNames]);
headersRange.setBackgroundColor(headerBackgroundColor);
setRowsData(sheet, dataBySchool[schools[i]]);
}
}
// setRowsData fills in one row of data per object defined in the objects Array.
// For every Column, it checks if data objects define a value for it.
// Arguments:
// - sheet: the Sheet Object where the data will be written
// - objects: an Array of Objects, each of which contains data for a row
// - optHeadersRange: a Range of cells where the column headers are defined. This
// defaults to the entire first row in sheet.
// - optFirstDataRowIndex: index of the first row where data should be written. This
// defaults to the row immediately below the headers.
function setRowsData(sheet, objects, optHeadersRange, optFirstDataRowIndex) {
var headersRange = optHeadersRange || sheet.getRange(1, 1, 1, sheet.getMaxColumns());
var firstDataRowIndex = optFirstDataRowIndex || headersRange.getRowIndex() + 1;
var headers = normalizeHeaders(headersRange.getValues()[0]);
var data = [];
for (var i = 0; i < objects.length; ++i) {
var values = []
for (j = 0; j < headers.length; ++j) {
var header = headers[j];
// If the header is non-empty and the object value is 0...
if ((header.length > 0) && (objects[i][header] == 0)) {
values.push(0);
}
// If the header is non-empty or the object value is empty...
else if ((!(header.length > 0)) || (objects[i][header]=='')) {
values.push('');
}
else {
values.push(objects[i][header]);
}
}
data.push(values);
}
var destinationRange = sheet.getRange(firstDataRowIndex, headersRange.getColumnIndex(),
objects.length, headers.length);
destinationRange.setValues(data);
}
// 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';
}
I don't have a great answer for you, but this might help or at least offer a little bit of insight. I'm experiencing similar problems with an add-on that I'm developing. The issue seems to be related to calling sheet.clear(). After a sheet.clear(), certain operations take a long time the first time they are called. In our case, we're seeing sheet.getMaxColumns() take upwards of 20s after calling sheet.clear(), whereas it takes only milliseconds if we remove the sheet.clear() call. If I call it twice in a row, the first call takes a long time, but the second call is very fast again. I have confirmed that the same behavior is true even if switching to sheet.clearContents().
I'm hoping this is some sort of bug recently introduced by Google that will be addressed quickly, as we don't really have a workaround to calling sheet.clear() at this point in time.
Hope this helps, and good luck!

Open Google Docs Spreadsheet by name

I have a situation where a script is taking input data and sending it to a spreadsheet. After a while, this spreadsheet becomes too big.
Right now we have to manually move the items from the the primary spreadsheet to a new one. The reason is that not everyone is familiar with the code and are willing to change the ID in the code.
I would like to know if there is a way to open the spreadsheet by name. If not, is there a better way of achieving what we need (described above)
The DocsList service used in one of the answers no longer functions as it has been depreciated. I updated my scripts to look more like the following.
// Open the file
var FileIterator = DriveApp.getFilesByName(FileNameString);
while (FileIterator.hasNext())
{
var file = FileIterator.next();
if (file.getName() == FileNameString)
{
var Sheet = SpreadsheetApp.open(file);
var fileID = file.getId();
}
}
The replacement for DocsList is DriveApp https://developers.google.com/apps-script/reference/drive/drive-app
Update: DocsList has now been sunset. Use DriveApp.getFilesByName(name) instead.
David has provided some good code for shuffling your data around. If all you really did need was just to open a spreadsheet by name then this will do the trick:
function getSpreadsheetByName(filename) {
var files = DocsList.find(filename);
for(var i in files)
{
if(files[i].getName() == filename)
{
// open - undocumented function
return SpreadsheetApp.open(files[i]);
// openById - documented but more verbose
// return SpreadsheetApp.openById(files[i].getId());
}
}
return null;
}
The way that I do something similar to what you are asking for is to first have a script make a copy of my spreadsheet (which I will call a frozen backup) that is getting too big. Once I safely have the copy, I then have the same script delete all those rows from the too big spreadsheet that are no longer required. (I believe that having multiple frozen backups does not cost the google account anything, so this is viable)
Note that I delete rows one by one; this takes time. I do this since I don't delete all the rows below a point, but only certain rows which match a condition.
In my case I do have another small procedure in addition to the above, which is to have the script copy all the rows that I am about to delete into a third spreadsheet (in addition to the frozen backup), but this seems to be more that you have asked for.
This is my code (note that the main spreadsheet, in the sheet we are going to remove rows from, named 'Original', has column A as Time Stamp for each row; the cell A1 is called Time Stamp):
function ssCopy() {
var id = "0ArVhODIsJ2.... spreadsheet key of the master spreadsheet";
var smsSS = SpreadsheetApp.openById(id);
var recordingSS = SpreadsheetApp.openById("0AvhOXv5OGF.... spreadsheet key of archive spreadsheet");// you probably wont be using this
var recordingSMSCopiesSheet = recordingSS.getSheets()[0];
var outgoingSMSsheet = smsSS.getSheetByName("Original");
outgoingSMSsheet.getRange("A1").setValue("Time Stamp");
var startRow = 2;
var numRows = outgoingSMSsheet.getDataRange().getLastRow();
var numCols = 13;
var dataRange = outgoingSMSsheet.getRange(startRow, 1, numRows, numCols);
var objects = getRowsData(outgoingSMSsheet, dataRange); // Create one JavaScript object per row of data.
var rowDataNumberArray = [];
var rowToDeleteIndex = [];
for (var i = 0; i < objects.length; ++i) { // Get a row object
var rowData = objects[i];
if( Date.parse(rowData.timeStamp) > Date.parse(ScriptProperties.getProperty('lastDate')) && (rowData.done == 1) ){ //these are not used if these same 2 lines are inserted instead of here, downbelow
var rowStuff = []
rowToDeleteIndex.push(i);
for(n in objects[i]){
rowStuff.push( objects[i][n] )}
rowDataNumberArray.push( rowStuff )};
Logger.log("rowData.number1 = " + rowStuff);
}
Logger.log(" rowDataNumberArray ~ " + rowDataNumberArray);
if(rowDataNumberArray.length > 0)
{
for(row in rowDataNumberArray)
{recordingSMSCopiesSheet.appendRow(rowDataNumberArray[row]);}
}
spreadsheetFrozenBackup(id)
for( var i = rowToDeleteIndex.length-1; i >= 0; i-- ) //backwards on purpose
outgoingSMSsheet.deleteRow(rowToDeleteIndex[i]+ 0 + startRow); //so we don't have to re-calculate our row indexes (thanks to H. Abreu)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function spreadsheetFrozenBackup(id) {
// Get current spreadsheet.
var ss = SpreadsheetApp.openById(id);
// Name the backup spreadsheet with date.
var bssName = " Frozen Spreadsheet at: " + Utilities.formatDate(new Date(), "GMT+1:00", "yyyy-MM-dd'T'HH:mm:ss") + " : " + ss.getName() ;
var bs = SpreadsheetApp.openById((DocsList.copy(DocsList.getFileById(ss.getId()), bssName)).getId());
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the '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
// - 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) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.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.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
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';
}
// setRowsData fills in one row of data per object defined in the objects Array. // https://developers.google.com/apps-script/storing_data_spreadsheets
// For every Column, it checks if data objects define a value for it.
// Arguments:
// - sheet: the Sheet Object where the data will be written
// - objects: an Array of Objects, each of which contains data for a row
// - optHeadersRange: a Range of cells where the column headers are defined. This
// defaults to the entire first row in sheet.
// - optFirstDataRowIndex: index of the first row where data should be written. This
// defaults to the row immediately below the headers.
function setRowsData(sheet, objects, optHeadersRange, optFirstDataRowIndex) {
var headersRange = optHeadersRange || sheet.getRange(1, 1, 1, sheet.getMaxColumns());
var firstDataRowIndex = optFirstDataRowIndex || headersRange.getRowIndex() + 1;
var headers = normalizeHeaders(headersRange.getValues()[0]);
var data = [];
for (var i = 0; i < objects.length; ++i) {
var values = []
for (j = 0; j < headers.length; ++j) {
var header = headers[j];
values.push(header.length > 0 && objects[i][header] ? objects[i][header] : "");
}
data.push(values);
}
var destinationRange = sheet.getRange(firstDataRowIndex, headersRange.getColumnIndex(),
objects.length, headers.length);
destinationRange.setValues(data);
}

Need a script to send an email without resending to previously recorded senders

I am using the script below which I modified from an existing sendEmail script. I am collecting information from a Google Form and wish to send the email with each "Submit". The problem I am having is that the script sends to every row of the spreadsheet even though that information was already sent. I need the script to somehow mark the row sent so that the information cannot be resent. My "newbie" way around this was to have the email address changed to my email address after sending. I filtered my inbox to automatically delete these emails. That is my crude solution, but I would like it to work correctly. Any suggestions, please?
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = ss.getSheets()[0];
var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 17);
var templateSheet = ss.getSheets()[1];
var emailTemplate = templateSheet.getRange("A1").getValue();
// Create one JavaScript object per row of data.
objects = getRowsData(dataSheet, dataRange);
// For every row object, create a personalized email from a template and send
// it to the appropriate person.
for (var i = 0; i < objects.length; ++i) {
// Get a row object
var rowData = objects[i];
// Generate a personalized email.
// Given a template string, replace markers (for instance ${"First Name"}) with
// the corresponding value in a row object (for instance rowData.firstName).
var emailText = fillInTemplateFromObject(emailTemplate, rowData);
var emailSent = rowData[4];
if (emailSent != EMAIL_SENT) {
var emailSubject = "CCSS Walkthrough";
MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText);
var startRow = 2;
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(startRow + i, 2).setValue("myemailaddress");
SpreadsheetApp.flush ();
}
}
}
// Replaces markers in a template string with values define in a JavaScript data object.
// Arguments:
// - template: string containing markers, for instance ${"Column name"}
// - data: JavaScript object with values to that will replace markers. For instance
// data.columnName will replace marker ${"Column name"}
// Returns a string without markers. If no data is found to replace a marker, it is
// simply removed.
function fillInTemplateFromObject(template, data) {
var email = template;
// Search for all the variables to be replaced, for instance ${"Column name"}
var templateVars = template.match(/\${\"[^\"]+\"\}/g);
// Replace variables from the template with the actual values from the data object.
// If no value is available, replace with the empty string.
for (var i = 0; i < templateVars.length; ++i) {
// normalizeHeader ignores ${"} so we can call it directly here.
var variableData = data[normalizeHeader(templateVars[i])];
email = email.replace(templateVars[i], variableData || "");
}
return email;
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// The code below is reused from the '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
// - 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) {
columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1;
var numColumns = range.getEndColumn() - range.getColumn() + 1;
var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1, numColumns);
var headers = headersRange.getValues()[0];
return getObjects(range.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.
// Arguments:
// - headers: Array of Strings to normalize
function normalizeHeaders(headers) {
var keys = [];
for (var i = 0; i < headers.length; ++i) {
var key = normalizeHeader(headers[i]);
if (key.length > 0) {
keys.push(key);
}
}
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';
}
The best method is to add a column and mark it email_sent and then filter those out when sending.
Section 2 of this tutorial does exactly what you are trying to accomplish.
This method allows you to retain the email address of the person that submitted the form.
Since what you want to do is: " I am collecting information from a Google Form and wish to send the email with each "Submit"." You don't really even have to access the spreadsheet to send the information you want to send. The spreadsheet will actually store the information that came with the Form submit but you don't have to go back to the spreadsheet to send the data to an email on submit. You can instead write a function that is triggered at Form Submit, which will take in the form submit as an event and grab the relevant data from there and send it to the desired email address. I am including a demo code here, check the comments for more detials.
function onFormSubmit(event) { //On form submission send an email to the approver.
var sheet = SpreadsheetApp.getActiveSheet();
//Get values that have to be put on the email.
//event.values[index] is indexed in the same order that form values are ordered on the spreadhseet
var formVal1 = event.values[0];
var formVal2 = event.values[1];
.
.
.
.
.
var formValN = event.values[N];
//Setting the message that goes on the email sent to the approver.
var message = 'This message has the following information from the form submit: '+ formVal1+'blah blah' + formVal2+...........+formValN;
//Title for the mail sent.
var title = 'New Submission';
//Email address the email is sent to.
var mailAdd = 'example#receiveemail.com' //this could be an array for multiple recipients.
//Sending Email.
MailApp.sendEmail(mailAdd, title, message);
}
After this script is written up you have to go to Resources>Current script's triggers. In that let your function be triggered from the spreadsheet on Form Submit.