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.
Related
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"});
}
}
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);
}
}
}
I'm trying to change the value of a cell in a loop. All variables are defined as they should be but the status itself won't change and returns an undefined or Function not found error.
My code is the following
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = 3;
var dataRange = sheet.getRange(startRow, 1, numRows, 8)
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var name = row[0];
var name2 = row[1];
var mother = row[2];
var father = row[3];
var sister = row[4];
var grandpa = row[5];
var mail = row[6];
var type = row[7];
var status = row[8];
var done = "DONE";
var not_done = "NOT DONE";
if (status = "TODO") {
//Do something...
status.setValue(done);
}
else {
//Do nothing...
}
}
Heading of the sheet are these:
Name Name2 Mother Father Sister Grandpa Mail Type Status
I tried adding a new loop
for (var i=0; i<numRows; i++) {
Utilities.sleep(500);
sheet.getRange(i+2, 9).setValue("DONE");
SpreadsheetApp.flush();
}
but this resulted in changing contents of other rows to and also an extra loop which shouldn't be necessary.
What (small) thing am I missing here?
That's because you are defining the numColumns to have 8 columns in total while the status field is 9th column (you started counting at 0). Hence it lies outside the range that you defined. In the 2nd one, it sorta worked because your range is 9.
The actual error is that status is a value not a cell object thus it has no "setValue". You should use the debugger which would have told you the exact line and problem. In the 2nd case you are using getRange correctly.also as another answer says you are using different column indexes (8 and 9 respectively)
The simplest way to replicate what you were trying to do is replace:
for (i in data) {
with:
for (var i=0; i<numRows; i++) {
and replace:
status.setValue(done);
with:
sheet.getRange(startRow + i, 9).setValue(done);
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');