PLEASE HELP!! I am getting this error on my code upon RUNNING
enter image description here
Hello everyone please help me remove the error code I am getting
function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1 = ss.getSheetByName('Recipients');
var sheet2 = ss.getSheetByName('Content');
var subject = sheet2.getRange(2,1).getValue();
var n = sheet1.getLastRow();
for (var i = 2; i < n+1 ; i++ ) {
var email = sheet1.getRange(i,2).getValue();
var name=sheet1.getRange(i,1).getValue();
var MerchantAcquired=sheet1.getRange(i,3).getValue();
var message = sheet2.getRange(2,2).getValue();
var options = {
cc: sheet1.getRange(i,4).getValue(),
}
message = message.replace("<name>",name).replace("<merchant>",MerchantAcquired);
MailApp.sendEmail (email, subject, message, options);
SpreadsheetApp.getUi().alert('Successfully Sent');
}
}
I tried to lessen the {} but it doesn't work.
Related
i want to send email using google script but contains multiple hyperlink and text
function jira() {
var ss =SpreadsheetApp.getActiveSpreadsheet();
var jira = ss.getSheetByName('Mailer');
var currentEmail = "emailhere#gmail.com"
var status = jira.getRange("C1").getValue();
var total_ticket = jira.getRange("C2").getValue();
var jira_ticket =jira.getRange(3,3,jira.getLastRow()-2,1).getValues();
var subject = "[AUTOMATION MAILER] Pending JIRA Ticket - Change Phone Number"
var body = "Hi Leaders\n\n" +
"There is "+total_ticket+" pending tickets on jira. Kindly solve this ticket_id : <a href='" + jira_ticket + "'>agenda</a>"+
"\n\n\nBest Regards"
Logger.log(body)
if(status=='Yes'){
MailApp.sendEmail({
to: currentEmail,
subject: subject,
htmlBody:body
});
MailApp.sendEmail(currentEmail,subject,body,)
}
}
here is the sheet looks like. i want all the jira ticket clickable in email
This will create drafts with hyperlinks. From that you should be able to create emails.
function creatDataList() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet0');
sh.clearContents();
let uA = [];
for (var i = 0; i < 5; i++) {
let url = `http://domain${i}.com`;
let add = `name${i}#domain${i}.com`;
let sub = `Subject${i}`
uA.push([url, add, sub]);
}
sh.getRange(1, 1, uA.length, uA[0].length).setValues(uA);
}
function createDrafts() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const vs = sh.getDataRange().getValues();
vs.forEach((r, i) => {
GmailApp.createDraft(r[1], r[2], '', { htmlBody: `This is a fake link: ${r[0]}` });
});
}
Been playing around with this and i am new to this unfortunately. So my goal is if a row in column h is true, in the email message it would get the value of the same row as column h and display what would be column a as the message.
ALL HELP IS THOROUGHLY APPRECIATED
function CheckSales() {
// Fetch the repair scba
var monthSalesRange =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("H1:H20");
var monthSales = monthSalesRange.getValue();
var ui = SpreadsheetApp.getUi();
// Check totals sales
if (monthSales = true) {
sendEmail();
}
}
function sendEmail() {
// Fetch the email address
var emailRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Email").getRange("B2");
var emailAddress = emailRange.getValue();
// Send Alert Email.
var message = 'A bottle is in need of repair!'; // Second column
var subject = 'SCBA Repair Email';
MailApp.sendEmail(emailAddress, subject, message);
}
It sounds like your spreadsheet looks like:
A
B
...
H
Message to send
Target email address
...
True/False
For rows where column H is true, you want to email the message in column A to the user in column B.
If that understanding is correct, this should do the trick:
function generateEmails() {
var dataRange = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1:H20");
var data = dataRange.getValues();
var subject = 'SCBA Repair Email';
for (var i = 0; i < data.length; i++) {
var row = data[i];
if (row[7]) {
var recipient = row[1];
var body = row[0];
MailApp.sendEmail(recipient, subject, body);
}
}
}
I have a Google Sheet where the Email & Name columns are auto-fill based on user submitted form. Also, I have 2 more columns where one column contain the list of giveaway code and another column is to act as an approval to send the email.
So for now I manage to create a button where when click it will send all emails with the giveaway code.
Now,
How can I only send the email to the user that only has a value "y" in the Status column?
Also, after sending the emails, the value "y" will changed to "d"? So that later, if I use the function again, it will only sending to the new users.
This is the sheet example https://docs.google.com/spreadsheets/d/1KkShktBnJoW9TmIzNsAuAJb6XslBrMwJGEJu7xeF1fk/edit?usp=sharing
This is the code
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Test1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:E1000");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[1];
var timeStamp = rowData[0];
var recipient = rowData[2];
var code = rowData[3];
var status = rowData[4];
var message = 'Dear ' + recipient + ',\n\n' + 'The giveaway code is ' + code;
var subject = 'Here is your giveaway code!';
MailApp.sendEmail(emailAddress, subject, message);
}
}
/**
* The event handler triggered when opening the spreadsheet.
* #param {Event} e The onOpen event.
*/
function onOpen(e) {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'Send Emails', functionName: 'sendArticleCountEmails'}
];
spreadsheet.addMenu('Send Emails', menuItems);
}
Just add an if statement like this:
function sendArticleCountEmails() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName("Test1"));
var sheet = SpreadsheetApp.getActiveSheet();
var dataRange = sheet.getRange("A2:E1000");
var data = dataRange.getValues();
for (i in data) {
var rowData = data[i];
var emailAddress = rowData[1];
var timeStamp = rowData[0];
var recipient = rowData[2];
var code = rowData[3];
var status = rowData[4];
var message = 'Dear ' + recipient + ',\n\n' + 'The giveaway code is ' + code;
var subject = 'Here is your giveaway code!';
if (status == "y") {
MailApp.sendEmail(emailAddress, subject, message);
var actualRow = parseInt(i)+2;
sheet.getRange(actualRow,5).setValue("d");
}
}
}
For months I have used this little script to auto reply to clients but on may 2 it started returning empty values. Did google change something or did I mess something up?
The body and the subject are still returned as normal. However, reply to email-ID is blank.
Below is the relevant code:
var message = GmailApp.getMessagesForThread(label[i]);
var body = message[0].getPlainBody();
// Logger.log(body);
var email = message[0].getReplyTo();
Logger.log(email);
Logger.log(sub);
Thanks for the help guys.
try this
function getMail(){
var myspreadsheet = SpreadsheetApp.openById('*************');
var mysheet = myspreadsheet.getSheets()[0];
//0~99まで
var threads = GmailApp.getInboxThreads(0 , 500);
var messages = GmailApp.getMessagesForThreads(threads);
var froms = [];
var row = mysheet.getLastRow()+1
messages.get
for(var i = 0; i < threads.length; i++)
{
froms.push([messages[i][0].getFrom(),messages[i][0].getSubject()]);
}
mysheet.getRange(mysheet.getLastRow()+1,1,threads.length,2).setValues(froms);
}
Hi i need to sort out messages that are not read in Gmail. But i can't pull out the body, sender and attachments properties when im adding the 'isUnread'.
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var unread = firstThread.isUnread();
var message = unread.getMessages()[0];
If i do it like this it works, but i do not get the unread messages.
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
Any tips?
Maybe is just a matter of order, you can try
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var messages = firstThread.getMessages();
var unread = [];
for (var i=0; i<messages.length; i++){
if (messages[i].isUnread()) unread.push(messages[i]);
}
sailens approach works as expected, search is also an easy way to get what you want.
Below 2 small functions to demonstrate:
function getUnreadMessages() {
var unreadThreads = GmailApp.getInboxThreads(0,100);
if(unreadThreads.length==0){Logger.log('no unread messages');return};
var unreads = [];unreads.push('\n\n');
for(var n in unreadThreads){
if(unreadThreads[n].isUnread()){unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessageCount()+' message(s)\n')};
}
Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}
function searchUnreads() {
var unreadThreads = GmailApp.search('label:unread');
if(unreadThreads.length==0){Logger.log('no unread messages');return};
var unreads = [];unreads.push('\n\n');
for(var n in unreadThreads){
unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessageCount()+' message(s)\n');
}
Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}
UPDATE :
a version that gets some details from every message in the threads, build in another function for ease of use
function getUnreadMessages() {
var unreadThreads = GmailApp.getInboxThreads(0,100);
if(unreadThreads.length==0){Logger.log('no unread messages');return};
var unreads = [];unreads.push('\n\n');
for(var n in unreadThreads){
if(unreadThreads[n].isUnread()){unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessages()[n].getDate()
.getMessageCount()+' message(s)\n');getDetails(unreadThreads[n])};
}
Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}
function searchUnreads() {
var unreadThreads = GmailApp.search('label:unread');
if(unreadThreads.length==0){Logger.log('no unread messages');return};
var unreads = [];unreads.push('\n\n');
for(var n in unreadThreads){
unreads.push(unreadThreads[n].getFirstMessageSubject()+' has '+unreadThreads[n].getMessageCount()+' message(s)\n');
}
Logger.log(unreads);//shows the unread threads with their subjects and number of messages
}
function getDetails(thread){
for(var n=0;n<thread.getMessageCount();n++){
Logger.log(thread[n].getMessages()[n].getBody()+' on '+thread[n].getMessages()[n].getDate())
Logger.log(thread[n].getMessages()[n].getAttachments());
}
}
I got it to work and simple as it could be:
function myFunction() {
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var message = firstThread.getMessages()[0];
var unread = message.isUnread();
var sender = message.getFrom();
var body = "Detta mail är ursprungligen skickat från" + " " + sender + '\n' + message.getBody();
var subject = message.getSubject();
var attachment = message.getAttachments();
if (unread == true){
GmailApp.sendEmail("user.name#compay.com", subject, "", {htmlBody: body, attachments: attachment});
}