I have a Google Spreadhsheet ("Trigger") in a Google Workbook with three columns:
- Column A: Email address
- Column B: Email text
- Column C: cell which either contains "yes" or "no"
The values for these columns start in row 2 and so far I need a solution for only this single row.
Whenever the value in cell C2
is "yes"
(the calculation here is based on a current stock price) I want to send an email to the email account in A2
with the text in B2
and the subject to be "Price has been reached? ==" & C2
What I have so far (I am very new to this but have some experience in VBA) is this:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 2)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var emailAddress = row[0]; // First column
var message = row[1]; // Second column
var subject = "Price has been reached? ==";
MailApp.sendEmail(emailAddress, subject, message);
}
}
This works in sending an email but does not take the value in C2
into account. How can I achieve this?
Also I am wondering how to code that this script should only be linked to spreadsheet "Trigger" and not to other sheets in this workbook?
Once this works I can define a proper trigger within the Script Editor and then think about how to make this work for more than one row.