I want to emails with attachment but I have an error
SyntaxError: Invalid or unexpected token (row 17, file "try.gs")
and I don't understand why?. The pdf named "i.pdf" is in my google drive
here is my code :
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process
// Fetch the range of cells A3:E3
// Column A = Name, Column B = Email, Column C = Message, Column D = Message1, Column E = Message2
var dataRange = sheet.getRange(startRow, 1, numRows, 4)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName(‘i.pdf’);
if (file.hasNext())
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(MimeType.PDF)],
name: ‘Simple mail’});
}
}
Can someone help me pls
The error is caused by the use of typographic / curly / single quotation characters = ‘’.
Replace them by straight single or double quotation characters '' o "".
Related
Notify via email when a cell is updated in google spreadsheet
Google AppScript syntax error for sendemail. Can't ID my problem
You have two typos when you declare var file and in the MailApp.sendEmail() function . Try this instead:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // Start at second row because the first row contains the data labels
var numRows = 3; // Put in here the number of rows you want to process
// Fetch the range of cells A3:E3
// Column A = Name, Column B = Email, Column C = Message, Column D = Message1, Column E = Message2
var dataRange = sheet.getRange(startRow, 1, numRows, 4)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[2]; // First column of selected data
var message = "Hey "; // Assemble the body text
var subject = "Sending emails from a Spreadsheet";
var file = DriveApp.getFilesByName("i.pdf");
if (file.hasNext())
MailApp.sendEmail(emailAddress, subject, message, {
attachments: [file.next().getAs(MimeType.PDF)],
name: "Simple mail"});
}
}
Related
So I have a script that would launch an e-mail, and I want it to be run when a specific cell (A5) contains a value over 10. Here's the script:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var rownum = 1; // #of rows
var rowstart = 2; // where to start row
// get range of cell
// column 1 = email, column 2 is first name
var dataRange = sheet.getRange(rowstart, 1, rownum, 2)
var data = dataRange.getValues()
for (i in data) {
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
var row = data[i];
var emailadr = row[0]; // data select
var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n Sent from my email"; // body
var sbjt = "You have mail!"; //subject
MailApp.sendEmail(emailadr, sbjt, msgcontent);}
}
}
Under Current project's triggers, I have the following:
Run: onEdit
Events: From spreadsheet On edit
Now, when I set cell "A5" to "21", the script doesn't run. What's the issue? I can't seem to figure it out. Thanks.
Using logger it shows that it does work. You can see in example below. You may want to try to use a time based trigger and set it to check every x amount of time and send email. Google may not allow the sendemail to run "onedit"
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var rownum = 1; // #of rows
var rowstart = 2; // where to start row
// get range of cell
// column 1 = email, column 2 is first name
var dataRange = sheet.getRange(rowstart, 1, rownum, 2)
var data = dataRange.getValues()
for (i in data) {
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
var row = data[i];
Logger.log("Yes it is larger than 10");
// var emailadr = row[0]; // data select
// var msgcontent = "Hey " + row[1] + ",\n Just:)chilling \n Sent from my email"; // body
// var sbjt = "You have mail!"; //subject
// MailApp.sendEmail(emailadr, sbjt, msgcontent);
}
}
}
Here is my code for the following, if I change the function getFilesByName() to getFileById() then it works perfectly fine, what is different and why is this not working as it should?
// This constant is written in column C for rows for which an email
// has been sent successfully.
var EMAIL_SENT = "EMAIL_SENT";
function sendEmails2() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 4; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 5)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[0];
var emailAddress = row[1]; // First column
var filename = DriveApp.getFilesByName(row[2]); // Second column
var message = row[3]; // Third column
var emailSent = row[4]; // Fourth column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "TEST" + name;
MailApp.sendEmail(emailAddress, subject, message, {
name: 'TEST',
attachments: [filename.getAs(MimeType.PDF)]
});
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
The error is actually pretty explanatory, you have a File Iterator class (documentation) and not a File, you must apply the method next() to get the most recently updated file with that name, if you have more files with that name, you can use a combination o hasNext() and next() in a loop.
I'm trying to create a script that checks if a cell is a certain color for instance #00ff00. If the cell color isn't 00ff00, I would like the script to take the email address in that cell and send an email. Everytime I run the following script "Missing ) after argument list. (line 22, file "Code")"
Can someone help me? Thanks.
var EMAIL_SENT = new Date() ;
function sendEmails2() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.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, 3)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Trying to get the color of a cell, if the color matches 00ff00, then I want the for loop to skip that cell and move to the next one.
var cell = sheet.getRange("A1").getBackgroundColor();
If(cell != results.getRange().getBackgroundColor(00ff00)); {
for
(var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var currentp = spreadsheet.getUrl(); //current spreadhseet page
var emailSent = row[2]; // Third column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
var subject = "Sending emails from a Spreadsheet";
MailApp.sendEmail(emailAddress, subject, message,currentp);
sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}}}}
There is an extra ; before the { on the below line:
If(cell != results.getRange().getBackgroundColor(00ff00)); {
My code seems to be working properly, except for line 22, which is writing an "emailSent" value to line 20 of the Google Spreadsheet that the script runs against, instead of line 2 of that spreadsheet, which is the only row that has data. Any ideas?
function sendEmails2() {
//trying to adopt code from this tutorial https://developers.google.com/apps-script/articles/sending_emails
//-believe there is an issue with where I am declaring getRange to work-
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var lastRow = 40; // Last row of data to process
// Fetch the range of cells A1:B10002
var dataRange = sheet.getRange(startRow, 1, lastRow, 10)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var i = data.length;
for (i in data) {
var row = data[i];
var emailAddress = row[7]; // raised an error at one point
var isEmailSent = row[9]; //
var partTimeApproved = row[0]
if (partTimeApproved != '') { // prevents sending emails to non-approved rows. for some reason = 'Y' doesn't work but !='' does //... or does it
if (isEmailSent == '') { // Prevents sending duplicates and sending emails to non-approved rows
var subject = "Email Subject Test";
var message = "Email body test"; // Standard string
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 10).setValue('emailSent'); //this didn't work: -replacing startRow + i with row-
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
}
I understand that while the code I am adopting from uses
getrange(startRow + i, 10).setValue('emailSent');
however, I do not understand how it could be finding row 20 and am not sure how to proceed in fixing it.
First
var i = data.length; <-- makes no sense
for (i in data) { <-- since you override i here
And I have no clue what data actually is. I am assuming it is an array of strings with numbers. AKA ['0','1']
So when you are expecting 2 and you are getting 20, it sounds like you are adding a string to a number
> 2 + "0"
"20"
convert it to a number.
> 2 + parseInt("0", 10)
2
So in the code:
getrange(startRow + parseInt(i, 10), 10).setValue('emailSent');
So I'm trying to set up a reminder email to automatically be sent based on the date in a cell. Kind of like this: Google Apps Script - Send Email based on date in cell
Here's my sample workbook: https://docs.google.com/spreadsheet/ccc?key=0AiHAV8ZZ5nexdDJqODhmamhldjN1ZTRKc09iZXNBZ3c#gid=0
This is the code that I have:
function sendEmail() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
//Logger.log(data)
for (i in data) {
var row = data[i];
var date = new Date();
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
//Logger.log(date);
var sheetDate = new Date(row[2]);
//Logger.log(sheetDate);
var Sdate = Utilities.formatDate(date,'GMT+0200','yyyy:MM:dd')
var SsheetDate = Utilities.formatDate(sheetDate,'GMT+0200', 'yyyy:MM:dd')
Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "It's time to practice!" +message;
MailApp.sendEmail(emailAddress, subject, message);
//Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}
But I'm not sure if it's working, and will it automatically send the email out? Obviously, I know very little script.
You only have 1 error in your existing code shared here, that is keeping it from working:
var sheetDate = new Date(row[2]);
You only have 2 indexes in your array, so this should be:
var sheetDate = new Date(row[1]);
Also, because you are using Utilities.formatDate to yyyy:MM:dd format, you do not need to set the hours minute and seconds, because Utilities.formatDate is returning a string with no time component. Furthermore, you do not need to create sheetDate or date, those can both be constructed as the first parameter in the Utilities.formatDate (see below). One other thing on this topic, because your date values are formatted as a date in your spreadsheet, they are being returned to your script as a date object, so really, it isn't necessary to call new Date(row[1]) .. but it doesn't hurt anything.
function sendEmail() {
try{
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow()-1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
//Logger.log(sheetDate);
var Sdate = Utilities.formatDate(new Date(),'GMT-0500','yyyy:MM:dd')
var SsheetDate = Utilities.formatDate(new Date(row[1]),'GMT+0200', 'yyyy:MM:dd')
Logger.log(Sdate+' =? '+SsheetDate)
if (Sdate == SsheetDate){
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "It's time to practice!" +message;
MailApp.sendEmail(emailAddress, subject, message);
//Logger.log('SENT :'+emailAddress+' '+subject+' '+message)
}
}
}catch(err){
Logger.log(err.lineNumber + ' - ' + err);
}
}