I have a long standing form/spreadsheet/script - that send emails triggered on spreadsheet edit. The trigger is working but the email is not sending. I have dropped the script and recreated - which authorized again.
function sendNotification() {
//Get Active spreadsheet and worksheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sheet = spreadsheet.getSheetByName("Change Requests");
var sheet2 = spreadsheet.getSheetByName("Email Addresses");
//Set row for which to send notification
//get values
var lastRow = sheet.getRange(row, 1, 1, sheet.getLastColumn());
var values = lastRow.getValues();
//Other Variables
var body = "<HTML><BODY>";
var stuNumber = values[0][2];
var stuName = values[0][3].toString();
var stuHouse = values[0][5].toString();
var gradeLevel = values[0][6].toString();
var oldCourse = values[0][7].toString();
var newCourse = values[0][8].toString();
var subject = "New course change request submitted for " + stuName;
var academicAdviser;
var cc_recipients = sheet2.getRange(4, 2).getValue();
//Send notification email
try {
MailApp.sendEmail(academicAdviser, subject, body, {htmlBody: body, noReply: "True", cc: cc_recipients})
}
catch (f) {
Browser.msgBox(f.message);
}
}
In this line:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
spreadsheet is set to the activeSheet which is not a spreadsheet
In this line:
var lastRow = sheet.getRange(row, 1, 1, sheet.getLastColumn());
row is undefined.
In this Line:
MailApp.sendEmail(academicAdviser, subject, body, {htmlBody: body, noReply: "True", cc: cc_recipients})
academicAdviser is undefined and htmlBody is set to <HTML><BODY> which will produced no output for htmlBody parameter.
Instead of Using this:
Browser.msgBox(f.message);
You might consider using SpreadsheetApp.getUi().prompt() or alert()
And I think it's a good idea to add MailApp.getRemainingDailyQuota() somewhere to keep yourself from running over your quota.
After looking at your code I'd guess that it might be a good idea for you to read this section.
Related
I have the code below which creates a duplicate sheet, adds some data and then sends that new sheet as an email attachment. The issue is that the attachment is the sheet called sending ALL sheets and not solely the newly created sheet. I think what i need to do is set the new sheet as active setActiveSheet or Activate, but neither of these have worked for me.
var OrderSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("OTW Sheet");
var Row = OrderSheet.getLastRow();
var Values1 = OrderSheet.getRange(Row, 5).getValue();
var Values2 = OrderSheet.getRange(Row, 6).getValue();
var Values3 = OrderSheet.getRange(Row, 7).getValue();
var Values4 = OrderSheet.getRange(Row, 8).getValue();
var Values5 = OrderSheet.getRange(Row, 9).getValue();
var Values6 = OrderSheet.getRange(Row, 2).getValue();
var Invoice = SpreadsheetApp.openById("13JpYjMNg5CcGwnRX_ZMXJIw5jeGfUrVvAC47xCNzHdg");
Invoice.getSheetByName("BLANK").copyTo(Invoice).setName("OTW Invoice " + new Date).activate();
var Newsheet = Invoice.getActiveSheet();
Newsheet.getRange(17, 2).setValue(Values1);
Newsheet.getRange(18, 2).setValue(Values2);
Newsheet.getRange(19, 2).setValue(Values3);
Newsheet.getRange(20, 2).setValue(Values4);
Newsheet.getRange(21, 2).setValue(Values5);
Newsheet.getRange(6, 7).setValue(Values6);
var message = {
to: "wilson.aidan101#gmail.com",
subject: "Test Invoice",
body: "Hi,\n\nPlease find the latest invoice attached.\n\nThank you,\nAidan",
name: "Aidan",
attachments: [Invoice.getAs(MimeType.PDF).setName("Invoice")]
}
MailApp.sendEmail(message);
OrderSheet.getRange(Row, 1).setValue("COMPLETE");
`
As a guess, try to add:
Invoice.moveActiveSheet(1);
Before this line:
var Newsheet = Invoice.getActiveSheet();
Perhaps you see only first sheet in the PDF. So it makes sense to make the 'Newsheet' a first sheet.
Im looking for a google apps script that can send email based on form input.
I tried this but this is not working:
for(var i in mailCreator){
if(mailCreator[i][0].match("...#abc.nl")!=null){
MailApp.sendEmail({
to: "123#def.com",
subject: ADDED_TO_GROUP_SUBJECT,
htmlBody: emailBody,
})
}
}```
It would be great if i can search for a certain value in the form input instead of an exact match.
What is mailCreator? Is it an array where is the range with the mails? or Or try to capture a field from the form?
EDIT.
Hi Johan, I think this is better for what you need. I understand that your objetive is to send an email with the data collected from the form. I use this.
This takes the range of the form sheet and sends you an email with the data of the defined variables. We can return more data from the range obtained from the form sheet.
function SendMail() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var listamail = spreadSheet.getSheetByName("name sheet");
var dataRange = listamail.getDataRange();
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var text = 'Text for body.';
for (var i = 1; i < data.length; i++) {
(function(val) {
var row = data[i];
var emailAddress = row[1]; //position of email header — 1
var name = row[0]; // position of name header — 1
var message = text ;
var subject = 'Write subjet or ne';
MailApp.sendEmail(emailAddress, subject, message);
})(i);
}
}```
You can call this function whenever you submit the form. But u need edit this and only use last value of range.
var ADDED_TO_GROUP_DOC_URL = 'https://docs.google.com/location of your document';
function onFormSubmit(e) {
var mailCreator = formValues['E-mailadres'][0].trim();
var subject = 'Subject title';
var addedToGroupDocId = DocumentApp.openByUrl(ADDED_TO_GROUP_DOC_URL).getId();
var emailBody = docToHtml(addedToGroupDocId);
MailApp.sendEmail({
to: mailCreator,
subject: subject,
htmlBody: emailBody,
});
}```
I'm having trouble with the "Email _Sent" text. It is supposed to appear next to the recipient who received the email from my spreadsheet. However, when I select certain emails to send the message to, the text "Email _Sent" starts filling out the first empty rows in my spreadsheet. In this way, the text appears beside some recipients who didn't receive the message.
I'll post the script that I'm using:
function sendGeneralEmail() {
var Email = 4;
var Name = 3;
var emailSent = 6;
var subject = "Sample Analysis Service"
var html = HtmlService.createTemplateFromFile("SUM.1");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var data = ws.getRange("A5:G" + ws.getLastRow()).getValues();
data = data.filter(function(r){ return r[5] == true});
var Email_Sent = 'Email_Sent';
var sheet = SpreadsheetApp.getActiveSheet();
var notBlank = sheet.getRange("D5:G5");
var lastRow = notBlank.getLastRow();
data.forEach(function(row){
html.en = row[Name];
var htmlMessage = html.evaluate().getContent();
if(emailSent !=Email_Sent) {
GmailApp.sendEmail(
row[Email],
subject, "Your Email doesn't support HTML", {
name: "MASAR Team",
htmlBody: htmlMessage},
);
sheet.getRange(lastRow, 7, data.length, 1).setValue(Email_Sent);
SpreadsheetApp.flush();
}
});
}
Can someone help me to fix that?
The second issue is that, if I run the script and then stopped and came back and run it again on the same day, the text "Email_sent" stops updating to the spreadsheet. It only appears the first I run the script during the day.
Screenshot
Hope to have a solution to this
I tried to replicate your code and found some issues:
using setValue on range with multiple cells will copy the value to all cells within the range. In your example, all cells in getRange(lastRow, 7, data.length, 1) will have the value "Email_Sent". This causes the unwanted write of "Email_Sent" in your spreadsheet.
if(emailSent !=Email_Sent) will always true because '6' is not always equal to 'Email_Sent'
Based on your example, your goal is to send an email and write "Email_Sent" to the Email Status column of the recipient.
Here, I fixed some part of your code and added features.
Code:
function sendGeneralEmail() {
var subject = "Sample Analysis Service"
// var html = HtmlService.createTemplateFromFile("SUM.1");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
var data = ws.getRange("A5:G" + ws.getLastRow()).getValues();
var sheet = SpreadsheetApp.getActiveSheet();
for(var i=0; i<data.length; i++){
var selected = data[i][5];
var emailStatus = data[i][6];
if(selected && emailStatus != "Email_Sent"){
var email = data[i][4];
// html.en = row[Name];
// var htmlMessage = html.evaluate().getContent();
GmailApp.sendEmail(
email,
subject, "Your Email doesn't support HTML", {
name: "MASAR Team",
message: "ABCD",
// htmlBody: htmlMessage},
}
);
sheet.getRange(i+5, 7, 1, 1).setValue("Email_Sent");
}
}
}
Note: I commented the html parsing since I don't have the sample html and replaced the email message with a string.
Before running the script:
After running the script:
Reference:
setValue()
searched this and I need this code. The problem is I dont know how to run the code and I cannot also comment due to my low reputation.
I have a button on my google sheets where I will assign the code. My data range is
var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Front").getRange("C2:Q14")
function convSheetAndEmail(rng, email, subj)
{
var HTML = SheetConverter.convertRange2html(rng);
MailApp.sendEmail(emailAddress, subject, '',{htmlBody : HTML});
}
function doGet()
{
var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Front-User").getRange("C2:Q14")
var emailAddress = 'test#gmail.com'
var subject = 'Test'
convSheetAndEmail(dataRange, emailAddress, subject);
}
I'm trying to make a script to send an email every day , after checking in a spreadsheet, two conditions. If the first condition for True , the data to e- mail will be " subject1 ", otherwise, will be " subject2 ".
The script works correctly when I run manually , however, through the trigger, the value I would like to send via email, simply does not appear in the body.
I can not understand what could be wrong if manually , works perfectly .
The trigger is "time-driven/hour time/every 12 hours".
Can anyone help me?
This is the code:
function sendNotificationD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = ss.getActiveCell().getA1Notation();
var cellvalue = ss.getActiveCell().getValue().toString();
var column = ss.getRange('o13');
var cell = sheet.getRange('o13');
var value = column.getValue();
var emailSheet = ss.getSheetByName("totais");
var emails = emailSheet.getRange("q1:s1").getValues();
var range_bill_com = sheet.getRange('o13');
if (range_bill_com.getValue() !='CONGRATS'){
var recipients = emails;
var message = '';
var subject = 'subject1 here';
var body = 'IMPORTANT: ' + '\n\n' + 'your goal is' + ' ' + value;
MailApp.sendEmail(recipients, subject, body);
}
if (range_bill_com.getValue() =='CONGRATS'){
var recipients = emails;
var message = '';
var subject = 'subject2 here';
var body = 'you have reached your goal';
MailApp.sendEmail(recipients, subject, body);
}
};
At this line:
var sheet = ss.getActiveSheet();
The method getActiveSheet() gets the the active sheet that is being displayed in the spreadsheet UI when a user opens it, but when it runs from a time-driven trigger there is no active sheet, so you need to indicate the sheet, you can use getSheetByName() or getSheets() the line should look like this:
var sheet = ss.getSheetByName("Sheet1");
or
var sheet = ss.getSheets()[0]; // Index of the tab, 0 will return the first one
The function will still work if you fire it manually or from a trigger.