Trying to send the contents of a sheet1, A:R using the MailApp.sendEmail. I can't seem to figure out how to get the data to break after each row
I've tried to define the range based on last row and column
function sendEmails() {
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MissingInfo");
var Row = sheet.getLastRow()
var Col = sheet.getLastColumn();
var dataRange = sheet.getRange(1,1,Row,Col);
var data = dataRange.getValues();
var emailAddress = 'myemail#gmail.com';
var message = data
var subject = 'Sending emails from a Spreadsheet';
MailApp.sendEmail(emailAddress, subject, message);
}
This works, except the data, doesn't break at the end of each row. any ideas on how to have a line break?
data is a 2D array. You can join row arrays using newlines \n:
var message = data.join('\n');
Related
I found on this forum a very useful script for sending emails from Google Sheets. But, unfortunately, when I send the emails, the message is in Hebrew and is written from left to right (instead of right to left).
In CSS, there is an "rtl" option. But in Javascript, I didn't found anything similar.
The script and the screenshot of the email are below. I appreciate your help.
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = 'קוד מוצר לרישיון שרכשת';
MailApp.sendEmail(emailAddress, subject, message);
}
}
Hebrew text aligned left to right instead of right to left
You could try setting the body as htmlBody instead using the text-align CSS property:
function example() {
var htmlbody = '<html><head><style>.text{text-align: right;}</style></head><body><div class="text">body of your email</div></body></html>';
MailApp.sendEmail({to:emailAddress, subject:subject, htmlBody:htmlbody});
}
I found a G Script that works for sending an email to hundreds of addresses, based on an excel file with the name (which I can convert into Sheets if necessary).
I want to be able to add an attachment (which is currently an excel file, but can easily be converted into Sheets) to each email that is sent. The attachment is the same for each email.
The below script works for sending the emails, but I can't find the right way to add an attachment to each email (Note: line 17 is where I'm getting the error code).
I've tried a few different versions based on other similar strings on this site - but I can't get it to work with my situation, or maybe I don't understand how to structure the syntax accordingly.
/**
* Sends emails with data from the current spreadsheet.
*/
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B2
var dataRange = sheet.getRange(startRow, 1, numRows, 1);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = 'Sending emails from a Spreadsheet'
var file = Drive.Files.get('test123/xlsx');
MailApp.sendEmail(emailAddress, subject, message, {attachments:
[file]});
}
}
ReferenceError: "Drive" is not defined. (line 17, file "Code")
"Drive" is the Advanced Drive API.In order to use the Drive advanced api you need to enable that api. But you really don't need the advanced api here.
Is your file in drive?
If so try this instead:
var file = DriveApp.getFileById("XXXXXXXXXXX");
the "xxx" should be the drive id.
According to the reference documentation, it looks like the class you're looking for is DriveApp.
/**
* Sends emails with data from the current spreadsheet.
*/
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B2
var dataRange = sheet.getRange(startRow, 1, numRows, 1);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = 'Sending emails from a Spreadsheet';
var file = DriveApp.getFileById('test123/xlsx');
MailApp.sendEmail(emailAddress, subject, message, {attachments:
[file]});
}
}
I have created a function that will allow me to send emails to a list of contacts via Google Sheets. The email will include a subject line and a table containing the data from my spreadsheet. The problem i am facing is that the HTML table is only sending the headers and 1 row of data. I can't find the error i have made.
The table will be ever increasing so i would like to email function to include all cells that have data in it. Is this possible?
I have adapted this Script/HTML code from How to format email from spreadsheet data using arrays? but i am now stuck with how to amend it further for my needs.
function testMail(){
var emails = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Contacts");
var startRow = 2; // First row of data to process
var numRows = 2; // Number of rows to process
// Fetch the range of cells A1:B1
var dataRange = emails.getRange(startRow, 1, numRows, 1);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = "Current list of BGC 4 Acronyms";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var acronym = ss.getSheetByName("Sheet1");
var abbv = acronym.getRange("A5:A50").getValues();
var explan = acronym.getRange("B5:B50").getValues();
var message = composeMessage(abbv,explan);
var messageHTML = composeHtmlMsg(abbv,explan);
Logger.log(messageHTML);
MailApp.sendEmail(emailAddress, subject, message, {'htmlBody':messageHTML});
}
function composeMessage(abbv,explan){
var message = 'Here are the data you submitted :\n'
for(var c=0;c<explan[0].length;++c){
message+='\n'+abbv[0][c]+' : '+explan[0][c]
}
return message;
}
function composeHtmlMsg(abbv,explan){
var message = '<br><br><table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th>Acronym</th><th>Explanation</th><tr>'
for(var c=0;c<explan[0].length;++c){
message+='<tr><td>'+abbv[0][c]+'</td><td>'+explan[0][c]+'</td></tr>'
}
return message+'</table>';
}
The expected result is an email sent to each address in the list with a subject line and full table of data. What i am actually getting is this
Try changing this:
for(var c=0;c<explan[0].length;++c){
message+='\n'+abbv[0][c]+' : '+explan[0][c]
}
To this:
I assume that abbv and explan are columns.
for(var c=0;c<explan.length;c++){
message+='<br />' + abbv[c][0]+ ' : ' + explan[c][0];
}
Try this:
function composeHtmlMsg(abbv,explan){
var message = '<br><br><table style="background-color:lightblue;border-collapse:collapse;" border = 1 cellpadding = 5><th>Acronym</th><th>Explanation</th><tr>'
for(var c=0;c<explan.length;c++){
message+='<tr><td>'+abbv[c][0]+'</td><td>'+explan[c][0]+'</td></tr>'
}
return message+='</table>';
}
Based upon your spreadsheet sample I think this could work:
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet1');
var rg1=sh.getRange(5,1,sh.getLastRow()-4,1);
var rg2=sh.getRange(5,2,sh.getlastRow()-4,1);
var explan=rg1.getValues();
var abbv=rg2.getValues();
I am very new to coding and I can't figuer out what I am doing wrong. I need 1 email sent from the last entry in my form. So as the students submit their pre-test they get an email that contains the result of their pre-test.
This is what my code looks like:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 3; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 3, numRows, 1)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[1]; // Second column
var message = row[2]; // Third column
var subject = "Résultat de formatif";
// This logs the value in the very last cell of this sheet
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
Logger.log(lastCell.getValue());
MailApp.sendEmail(emailAddress, subject, message);
}
}
I do that by:
adding a separate column "STATUS" to the sheet
in your email sending function iterate over rows and act only on rows with "STATUS" being blank
after successfully sending email for a row, put something to the "STATUS" column
Please review the event object documentation.
You can install a "trigger" to automatically run your script on every form submission. Because this is triggered per submission, you don't need to worry about trying to find the last row or if the email had already been sent.
function sendEmails(formResponse) {
var answers = formResponse.values;
var emailAddress = answers[1]; // Second column
var message = answers[2]; // Third column
var subject = "Résultat de formatif";
// This logs the value in the very last cell of this sheet
var range = formResponse.range;
var row = range.getRow();
var column = range.getLastColumn();
try {
MailApp.sendEmail(emailAddress, subject, message);
} catch (e) {
console.error("Did not send email to " + emailAddress + ". See error: " + e);
range.getSheet().getRange(row, column+1).setValue("EMAIL NOT SENT");
return;
}
range.getSheet().getRange(row, column+1).setValue("EMAIL SENT");
}
I have been struggling for days to get this appScript to function within my Google Sheets. Currently it is getting "stuck" on MailApp.sendEmail line, particularly on emailAddress. It gives me this error message "Invalid email: undefined". I based the majority of the script on their Sending Emails from a Spreadsheet Tutorial. So I'm not too sure where it might be going wrong.
Any help is much appreciated!
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Candidates Ready for Offer");
var startRow = 2; // First row of data to process
var numRows = ss.getSheetByName("Candidates Ready for Offer").getLastRow()-1;
// Fetch recent additions
var dataRange = ss.getSheetByName("Candidates Ready for Offer").getRange(startRow, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[5];
var message = "row[1]" + "row [2]" + "at" + "row[4]" + "is ready for an offer!"
var subject = "Candidate is Ready for Offer";
MailApp.sendEmail(emailAddress, subject, message);
}
}
Thanks for sharing your sheet. There was actually a number of reasons why it wasn't working which I'll try and explain. Anyway, your script on the shared sheet will now function as you want it to. Here is what it looks like:
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = ss.getSheetByName("Candidates Ready for Offer");
var startRow = 4; // First row of data to process
var numRows = sh0.getLastRow()-startRow+1;
// Fetch recent additions
var dataRange = sh0.getRange(startRow,1,numRows,6);
// Fetch values for each row in the Range.
var data = dataRange.getValues();
Logger.log(data);
for (i in data) {
var row = data[i];
var emailAddress = row[3];
Logger.log(emailAddress);
var message = "row[1]" + "row [2]" + "at" + "row[4]" + "is ready for an offer!"
var subject = "Candidate is Ready for Offer";
MailApp.sendEmail(emailAddress, subject, message);
}
}
There's no need to keep calling the same sheet when you've already defined it as the variable sh0
The data range was including the first 3 rows. You didn't want this. Some simple maths in the var numRows allows the spreadsheet to only take the range containing data starting at row 4.
You were referencing the wrong column for email address. It was in column 4.
I hope this helps/makes sense.
Oli
The problem is your var dataRange. At the moment your code is only getting the range of one cell (startrow,3) instead of an array.
Replacing .getRange(startrow,3) with getDataRange() should fix your issue.