As Rubén pointed out, the format of sendEmail
method is different: the last parameter is an object of options such as {attachments: ...}
.
Also, images inserted in a spreadsheet via "insert image" are not associated with any cell in a sheet, they are just floating over the sheet. This makes them unsuitable for your purpose of matching images to recipients listed in the sheet.
Images via a URL
A way to associate images with cells is to use =image(url)
formula, which requires the image to be accessible somewhere. I suggest the following approach: put the URL itself in the column for attachment, say D, and =image(D2)
command elsewhere, just to have a preview of the image. Then you can obtain the image as a blob with
var image = UrlFetchApp.fetch(row[3]).getBlob()
and send it via
MailApp.sendEmail(emailAddress, subject, message, {attachments: [image]});
The full script is as follows:
function emailImage(){
var EMAIL_SENT = "EMAIL_SENT";
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 20; // Number of rows to process
var data = sheet.getRange(startRow, 1, numRows, 20).getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = row[1]; // Second column
var message = row[2]; // Third column
var image = UrlFetchApp.fetch(row[3]).getBlob(); // Fourth column
var emailSent = row[4]; // Fifth column
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message, {attachments: [image]});
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Images on Google Drive
If the images are stored in Google Drive, they can be accessed by file Id (the long alphanumerical part of their URL). Put the Ids in column D and replace the line with var image =
by
var image = DriveApp.getFileById(row[3]).getBlob();
if (value >= "B2") ...
That's just comparing value to the string "B2". You wanted to compare to the content of the cell B2. That is,
if (value >= sheet.getRange("B2").getValue()) ...
Best Answer
To do this you should use a Google Sheets add-on or Google Apps Script. If you go for using Google Apps Script, you could use an on edit installable trigger,the MailApp or GmailApp services and one of the send methods of those services and JavaScript control statements to handle the rules.
Example (untested):
References