Auto Email Function for Google Sheets Mobile - email

I am trying to run an amazing script shared by Lauren Script for Google sheets (auto email function) and have an issue. I'm fairly new on stackoverflow so let me know if i need to ask this in another way if I'm breaking any norms? I have setup and successfully edited this script to do what i need it to do. I have a team that will be using this on mobile devices. I tried it and it did not work! can you help or point me in the right direction?
This is for a task list, when someone then selects a checkbox it sends any added notes and details to team leaders (and as per the script then unchecks the box). The checkbox is great as there is one per row. But open to any suggestions
Sorry for the lack of clarity, and thanks #TheMaster for helping out here. Code is below, but sounds like i am trying to do the impossible? Thought i would be able to do this with google sheets but it seems it is not possible on sheets android ap. I am not very advanced and might need to abandon the project! Any suggestions maybe even to do this in a different method?
function EmailNotification(e) {
var ui = SpreadsheetApp.getUi();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Deliveries'); //source sheet
var columnb = sheet.getRange('J:J'); //Column with check boxes
var columnbvalue = columnb.getValues();
var notifysheet = ss.getSheetByName('Responses'); //destination sheet
var data = [];
var rownum =[];
//Condition check in B:B (or check box column); If true copy the same row to data array
for (i=0; i<columnbvalue.length;i++) {
if (columnbvalue[i] == 'true') {
var columnb2 = sheet.getRange('J2:J');
columnb2.setValue('false');
// What you want to say in the pop up alert
var response = ui.alert('Hold Up!\n Are you sure you want to send an email to team leaders for this delivery?', ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
data.push.apply(data,sheet.getRange(i+1,1,1,20).getValues());
//Copy matched ROW numbers to rownum
rownum.push(i);
//Copy data array to destination sheet
notifysheet.getRange(notifysheet.getLastRow()+1,1,data.length,data[0].length).setValues(data);
var activeRow = notifysheet.getLastRow();
var suppliername = notifysheet.getRange(activeRow, 1).getDisplayValue(); // The number is the column number in the destination "responses" sheet that you want to include in the email
var owner = notifysheet.getRange(activeRow, 3).getDisplayValue();
var recievedby = notifysheet.getRange(activeRow, 8).getDisplayValue();
var instructions = notifysheet.getRange(activeRow, 5).getDisplayValue();
var status = notifysheet.getRange(activeRow, 7).getDisplayValue();
var notes = notifysheet.getRange(activeRow, 9).getDisplayValue();
var email = notifysheet.getRange(activeRow, 4).getDisplayValue();
var subject = 'Delivery completed!'
//Body of the email message, using HTML and the variables from above
var message =
'<br><br><div style="margin-left:40px;">Hello '+ owner +'</div>'
+'<br><br><div style="margin-left:40px;">A delivery you requested has been fulfilled</div>'
+'<br><br><div style="margin-left:40px;"><h3 style="text-decoration: underline;">Recieved Name: '+ recievedby +'</h3></div>'
+'<div style="margin-left:40px;">Supplier: '+ suppliername +'</div>'
+'<div style="margin-left:40px;">Special instructions you added: '+ instructions +'</div>'
+'<div style="margin-left:40px;">Status: '+ status +'</div>'
+'<div style="margin-left:40px;">Notes: '+ notes +'</div>'
+ '<br><br><div style="margin-left:40px;">ヽ(⌐■_■)ノI Did It ♪♬</div>';
MailApp.sendEmail(
email,
subject,
"",
{
htmlBody: message,
name: 'Shop Alerts', //The name you want to email coming from
});
}
}
}
}

Mobile apps don't support ui or alerts. You just need to remove them.
function EmailNotification(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Deliveries'); //source sheet
var columnb = sheet.getRange('J:J'); //Column with check boxes
var columnbvalue = columnb.getValues();
var notifysheet = ss.getSheetByName('Responses'); //destination sheet
var data = [];
var rownum = [];
//Condition check in B:B (or check box column); If true copy the same row to data array
for (let i = 0; i < columnbvalue.length; i++) {
if (columnbvalue[i][0] === true) {
//modified to strict equality
var columnb2 = sheet.getRange('J2:J');
columnb2.setValue('false');
data.push.apply(data, sheet.getRange(i + 1, 1, 1, 20).getValues());
//Copy matched ROW numbers to rownum
rownum.push(i);
//Copy data array to destination sheet
notifysheet
.getRange(notifysheet.getLastRow() + 1, 1, data.length, data[0].length)
.setValues(data);
var activeRow = notifysheet.getLastRow();
var suppliername = notifysheet.getRange(activeRow, 1).getDisplayValue(); // The number is the column number in the destination "responses" sheet that you want to include in the email
var owner = notifysheet.getRange(activeRow, 3).getDisplayValue();
var recievedby = notifysheet.getRange(activeRow, 8).getDisplayValue();
var instructions = notifysheet.getRange(activeRow, 5).getDisplayValue();
var status = notifysheet.getRange(activeRow, 7).getDisplayValue();
var notes = notifysheet.getRange(activeRow, 9).getDisplayValue();
var email = notifysheet.getRange(activeRow, 4).getDisplayValue();
var subject = 'Delivery completed!';
//Body of the email message, using HTML and the variables from above
var message =
'<br><br><div style="margin-left:40px;">Hello ' +
owner +
'</div>' +
'<br><br><div style="margin-left:40px;">A delivery you requested has been fulfilled</div>' +
'<br><br><div style="margin-left:40px;"><h3 style="text-decoration: underline;">Recieved Name: ' +
recievedby +
'</h3></div>' +
'<div style="margin-left:40px;">Supplier: ' +
suppliername +
'</div>' +
'<div style="margin-left:40px;">Special instructions you added: ' +
instructions +
'</div>' +
'<div style="margin-left:40px;">Status: ' +
status +
'</div>' +
'<div style="margin-left:40px;">Notes: ' +
notes +
'</div>' +
'<br><br><div style="margin-left:40px;">ヽ(⌐■_■)ノI Did It ♪♬</div>';
MailApp.sendEmail(email, subject, '', {
htmlBody: message,
name: 'Shop Alerts', //The name you want to email coming from
});
}
}
}

Related

Cannot read property 'getRange' of null error. I am positive the sheet exists and is named correctly in the script

My fellow teachers and I like to send students little google drawings to let them know they've been doing well lately. We have a spreadsheet that we use and I am trying to automate the process of sending them an email when their drawing is ready to be viewed. I keep getting the 'Cannot read property 'getRange' of null error' even though I am 100% sure that a sheet exists with the name PR and that it is spelled right. I am new to Google Script so I lack the skills to troubleshoot any more than the googling I've done already, which basically just says to make sure you've named the sheet correctly. Any help would be very appreciated!
var studentFirstNameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("A2:A");
var studentFirstname = studentFirstNameRange.getValues();
var studentEmailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("D2:D");
var studentEmail = studentEmailRange.getValues();
var emailSendRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("L2:L");
var emailSend = emailSendRange.getValues();
if (emailSend){
// Send Alert Email.
var message = 'Hi ' + studentFirstname + '! Your teachers noticed you have been doing a great job this year, so we made this for you! Keep up the great work!' ; // Second column
var subject = 'Positive Recognition';
MailApp.sendEmail(studentEmail, subject, message);
}
}```
According to the title and the error you related, I did'nt get any error.
However, I don't understand your condiiton if (emailSend) neither the way you send emails. If you send emails to all the population at once, you can try
function myFunction() {
var lastRow = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getLastRow()
var studentFirstNameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("A2:A"+lastRow);
var studentFirstname = studentFirstNameRange.getValues();
var studentEmailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("D2:D"+lastRow);
var studentEmail = studentEmailRange.getValues();
var emailSendRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("L2:L"+lastRow);
var emailSend = emailSendRange.getValues();
if (emailSend){
// Send Alert Email.
var message = 'Hi ' + studentFirstname + '! Your teachers noticed you have been doing a great job this year, so we made this for you! Keep up the great work!' ; // Second column
var subject = 'Positive Recognition';
Logger.log(studentEmail.join())
MailApp.sendEmail(studentEmail.join(), subject, message);
}
console.log('there is no errors!')
}
if you want to send individually
function myFunction() {
var lastRow = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getLastRow()
var studentFirstNameRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("A2:A" + lastRow);
var studentFirstname = studentFirstNameRange.getValues();
var studentEmailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("D2:D" + lastRow);
var studentEmail = studentEmailRange.getValues();
var emailSendRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PR').getRange("L2:L" + lastRow);
var emailSend = emailSendRange.getValues();
for (var i = 0; i < studentFirstname.length; i++) {
if (emailSend[i][0]) {
// Send Alert Email.
var message = 'Hi ' + studentFirstname[i][0] + '! Your teachers noticed you have been doing a great job this year, so we made this for you! Keep up the great work!'; // Second column
var subject = 'Positive Recognition';
MailApp.sendEmail(studentEmail[i][0], subject, message);
}
}
}

MailApp.sendEmail function runs without errors but no emails are sent

The below script runs without any visible error, but no emails are sent. Any ideas as to why?
function sendEmailLoop() {
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
sheets.forEach(function(sheet) {
var range = sheet.getDataRange();
if (sheet.getName() == "Summary") //Disregard tab named 'Summary'
{
}
else {
var range = sheet.getDataRange(); //to set the range as array
var values = range.getDisplayValues(); //to get the value in the array
var lastRow = range.getLastRow();
var ss = SpreadsheetApp.getActiveSpreadsheet(); //declare the spreadsheet
var sheet = ss.getSheetByName("Sheet1");
var Title = values[0][0]; //[Title] cell A1
var URL = values[0][1]; //[URL] cell B1
var i;
var logContent = '';
for (i = 3; i < lastRow; i++) {
var Name = values[i][0]; //[Name] cell A++
var Email = values[i][1]; // [Email] cell B++
Logger.log('to: ' + Email);
Logger.log('subject: ' + Name + Title + 'Test');
Logger.log('message: ' + 'This is a test message for the training that can be found at ' + URL);
/*
MailApp.sendEmail({
to: Email,
subject: Name + Title + 'Test',
message: 'This is a test message for the training that can be found at ' + URL});
*/
}; //end for loop - email tab data
}; // end 'else'
}); // end function(sheet)
} // end SendEmailLoop()
Here is the Stackdriver log of a successful execution (success meaning no errors, but still no emails are sent):
The structure of the spreadsheet associated with the script:
Note - an earlier version of this script didn't include the sheets.forEach() method call (ie, to loop through each tab of the spreadsheet) and the emails were sent fine.
Could the lack of emails being sent or received be related to the fact that I have many sheets and this function is looping through them?
function sendEmailLoop() {
var exclA=['Summary'];
var ss=SpreadsheetApp.getActive();
var sheets=ss.getSheets();
sheets.forEach(function(sheet) {
if (exclA.indexOf(sheet.getName())==-1) {
var range=sheet.getDataRange();
var values=range.getDisplayValues();
var lastRow=range.getLastRow();
var Title=values[0][0];
var URL=values[0][1];
var logContent='';
for (var i=3; i <values.length; i++) {
var Name=values[i][0];
var Email=values[i][1];
Logger.log('to: %s\nsubject: %s %s Test\nmessage: %s This is a test message for the training that can be found at %s',Email,Name,Title,URL);
/*
MailApp.sendEmail({
to: Email,
subject: Name + Title + 'Test',
message: 'This is a test message for the training that can be found at ' + URL});
*/
}
}
});
}
Answer:
You need to uncomment your MailApp code.
Code changes:
I tested your code and it seems to run without issue for me, including the receipt of the emails, only that the code comments around your MailApp call need to be removed (the /* and the */).
I would also suggest adding a conditional line before you send the email in the event .getDataRange() obtains seemingly empty rows:
if (Email == "") {
continue;
};
MailApp.sendEmail({
to: Email,
subject: Name + Title + 'Test',
message: 'This is a test message for the training that can be found at ' + URL});
References:
JavaScript Comments

Unable to add inline image to email in google apps script

I'm new to Google Apps script and am trying to add an image inline to an automated response email.
The auto reply works perfectly, the main text of the email formats well in plain text and html.
the problem i'm facing is that the image does not appear.
my code:
// This constant is written in column Y for rows for which an email
// has been sent successfully.
var EMAIL_SENT = 'EMAIL_SENT';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Data'))
var startRow = 2; // First row of data to process
// Fetch the range
var dataRange = sheet.getRange("L2:L1000")
var dataRange2 = sheet.getRange("K2:K1000")
var dataRange3 = sheet.getRange("O2:O1000")
var dataRange4 = sheet.getRange("Y2:Y1000")
var dataRange5 = sheet.getRange("B2:B1000")
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var data2 = dataRange2.getValues();
var data3 = dataRange3.getValues();
var data4 = dataRange4.getValues();
var data5 = dataRange5.getValues();
for (var i = 0; i < data.length; ++i) {
var yesno = data2[i]
if(yesno == "Yes"){
var TFlogoUrl = "https://drive.google.com/openid=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";
var TFlogoBlob = UrlFetchApp
.fetch(TFlogoUrl)
.getBlob()
.setName("TFlogoBlob");
var emailAddress = data[i];
var ShipID = data3[i];
var cmdrID = data5[i];
var TFmsg = "Hi " + cmdrID + ",/n /nThank you for signing up to The Fatherhoods Lost Souls Expedition./n /nYour unique Ship ID is: " + ShipID + "/n /nWe look forward to seeing you on the expedition CMDR!/n /nFly Safe,/nThe Lost Souls Expedition team.";
var htmlTFmsg = "Hi " + cmdrID + ",<br> <br>Thank you for signing up to The Fatherhoods Lost Souls Expedition.<br> <br>Your unique Ship ID is: " + ShipID + "<br> <br>We look forward to seeing you on the expedition CMDR!<br> <br>Fly Safe,<br>The Lost Souls Expedition team.<br><img src='cid:TFlogo'>";
emailSent = data4[i]; // email sent (column Y)
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Lost Souls Expedition Sign up confirmation";
MailApp.sendEmail(emailAddress,subject,TFmsg,{
htmlBody: htmlTFmsg,
inlineImage:
{
TFlogo:TFlogoBlob
}
});
sheet.getRange("Y" + (startRow + i)).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
}
How about this modification?
Modification points:
You cannot retrieve the file blob from this URL var TFlogoUrl = "https://drive.google.com/openid=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";. If you want to retrieve the file blob from URL, please use var TFlogoUrl = "http://drive.google.com/uc?export=view&id=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";. 1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287 is the file ID.
As an another method, from the file ID, it is found that the values of getSharingAccess() and getSharingPermission() are ANYONE_WITH_LINK and VIEW, respectively. So you can also retrieve the blob using var TFlogoBlob = DriveApp.getFileById("1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287").getBlob().setName("TFlogoBlob");. I recommend this.
When you want to use the inline image to email, please modify from inlineImage to inlineImages.
The script which reflected above points is as follows.
Modified script:
Please modify your script as follows.
From:
var TFlogoUrl = "https://drive.google.com/openid=1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";
var TFlogoBlob = UrlFetchApp.fetch(TFlogoUrl).getBlob().setName("TFlogoBlob");
To:
var id = "1nzmvP_zzOms1HiBoFCsVLFjDM6ZzM287";
var TFlogoBlob = DriveApp.getFileById(id).getBlob().setName("TFlogoBlob");
And
From:
inlineImage: {TFlogo:TFlogoBlob}
To:
inlineImages: {TFlogo:TFlogoBlob}
References:
sendEmail(recipient, subject, body, options)
If I misunderstand your question, please tell me. I would like to modify it.

google sheets script regarding sending emails

I'd presume this would be an easy solve but I've been unable to find a solution anywhere online.
I'm trying to send automated emails when I run a script. The problem is it will send one email for every row (multiple emails to the same person) instead of sending one email with every row/column included in that one email.
The second issue is I want to send to more than one recipient. In my sheet, I want to send the data in B2:F3 to the first email address and then I want to send B4:F5 to the second email provided.
Thank you for your help. - Jared
Here's my script:
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("sheet1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:m8");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[0];
var city = rowData[11];
var dimension = rowData[1];
var product = rowData[2];
var tally = rowData[3];
var price = rowData[5];
var ship = rowData[5];
var message = city + '\n\n' + dimension + ' ' + product + ' ' + tally + ' ' + price + ' ' + ship;
var subject = 'PFP Lumber';
MailApp.sendEmail("emailaddress1#blank.com",
"PFP Lumber",
message);
}
}
From your shared sample sheet and script, there are some questions.
It seems that price and ship are rowData[5].
price and ship are rowData[4] and rowData[5], respectively?
It seems that rowData[11] is Phone.
Is this rowData[6]?
If price, ship and Phone are rowData[4], rowData[5] and rowData[6], respectively, you want to send the following emails.
Mail 1 :
mailaddress: emailaddress1#blank.com
message: Gulfport desc1 1 1 1 1, Gulfport desc2 2 2 2 2
Mail 2 :
mailaddress: emailaddress2#blank.com
message: Gulfport desc3 3 3 3 3, Gulfport desc4 4 4 4 4
If my understanding is correct, how about this modification? I think that there are several answers for your situation, so please think of this as one of them.
Modification points :
Retrieve email address and messages.
When new email address was NOT found at column A, the message is added.
When new email address was found at column A, the email is separated.
Modified script :
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("sheet1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:m8");
var data = dataRange.getValues();
// Modified
var temp = [];
var emailAddress, dimension, product, tally, price, ship, city;
for (var i in data) {
[emailAddress, dimension, product, tally, price, ship, city] = data[i];
var message = city + '\n\n' + dimension + ' ' + product + ' ' + tally + ' ' + price + ' ' + ship;
if (emailAddress || !dimension) {
if (temp.length > 0) {
MailApp.sendEmail(temp[0], "PFP Lumber", temp.slice(1,temp.length).join("\n"));
var temp = [];
}
temp.push(emailAddress, message);
if (!dimension) break;
} else {
temp.push(message);
}
}
}
Note :
Several messages for one mail address is separated by \n.
Because I'm not sure the variables for each columns that you want to use, please modify them if the variables of my modified script is difference from you want.
In this modified script, after the rows that Dimension of column B is empty, I recognized that there are no emails you want to send.
If I misunderstand your question, I'm sorry.
Edit :
When you want to add a signature, how about this script?
function sendEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("sheet1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:m8");
var data = dataRange.getValues();
var temp = [];
var emailAddress, dimension, product, tally, price, ship, city;
var signature = "\n---\nsample signature";
for (var i in data) {
[emailAddress, dimension, product, tally, price, ship, city] = data[i];
var message = city + '\n\n' + dimension + ' ' + product + ' ' + tally + ' ' + price + ' ' + ship;
if (emailAddress || !dimension) {
if (temp.length > 0) {
MailApp.sendEmail(temp[0], "PFP Lumber", temp.slice(1,temp.length).join("\n") + signature);
var temp = [];
}
temp.push(emailAddress, message);
if (!dimension) break;
} else {
temp.push(message);
}
}
}

Google script: email with answers given by a single user to a form compiled in google drive

I've created a form using google drive.
In order to provide to answerers a summary of what they wrote I would like to send an email to each of them, using the email they insert in the form.
I think it is possible to do this installing a script in the spreadsheet of the form result that send an email every time a new row is recorded.
I've found (on this forum) the following script that is, unfortunately, an email alert with a modify in a certain spreadsheet.
function sendNotification() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var row = sheet.getActiveRange().getRow();
var cellvalue = ss.getActiveCell().getValue().toString();
var recipients = "me#gmail.com";
var message = '';
if(cell.indexOf('G')!=-1){
message = sheet.getRange('D'+ sheet.getActiveCell().getRowIndex()).getValue()
}
var subject = 'Update to '+sheet.getName();
var body = sheet.getName() + ' has been updated. Visit ' + ss.getUrl() + ' to view the changes on row: «' + row + '». New comment: «' + cellvalue + '». For message: «' + message + '»';
MailApp.sendEmail(recipients, subject, body);
};
And here is the help I need. The script sends an email to a single predefined address. Is it possible to let the script take the recipient email from a certain cell in the relevant row (for example the email is recorded in the col C of the spreadsheet) and send him a message that contains some text taken by other cells of the same row ( for example col D and E)?
Using the variables you defined in your code (sheet, row):
//returns email address in column C
var recipient = sheet.getRange(row, 3).getValue();
//returns values in column D and E
var info1 = sheet.getRange(row, 4).getValue();
var info2 = sheet.getRange(row, 5).getValue();
Please be aware that this is an 'expensive' way to get the values and you would better served getting all values as an array in one call and then referencing them by index, as below.
//get all required values in one call
var values = sheet.getRange(row, 3, 1, 3).getValues();
//define variables using array index
var recipients = values[0];
var info1 = values[1];
var info2 = values[2];