I was looking for a solution to send automated emails from a Google Sheet connected to a Google Form, so I found Google Scripts. I never worked with it (or other scripts) before, so it's all new to me. I did find a lot of information on the internet, but I still have some questions.
Let's explain the situation. So I'm selling two different products in a Google Form, they both have a different price, so I was looking for a solution to send mails to the customers with the total price and some extra information. I have a Google Sheet where all the answers are put in automatically.
So I have 12 columns, as you can see in a copy of my sheet. https://docs.google.com/spreadsheets/d/1CrA9cNAwsUdLyaxzFiJrtXxu-0eiIMiA3IEaEaIYlZo/edit?usp=sharing
The script I use is added as a picture.
The problems:
- The script should prevent Google from sending duplicates, but that doesn't work
- I used a formula to fill the total amount in the sheet (column L) but I can't add it in the text of the script to send it in the mail
What I want:
- Automated mails, can I add the script as a add-on or how do I automate it in the sheet? Now I need to press the "play script" button every time
- I want the duplicate tracker to work, so no-one gets 2 mails
- I want the script to scan enough rows, so everyone gets a mail
- I want the total amount in the text of the mail
That's it I guess, thanks for helping!
// This constant is written in column C for rows for which an email
// has been sent successfully.
let EMAIL_SENT = 'EMAIL_SENT';
/**
* Sends non-duplicate emails with data from the current spreadsheet.
*/
function sendNonDuplicateEmails() {
try{
// Get the active sheet in spreadsheet
const sheet = SpreadsheetApp.getActiveSheet();
let startRow = 3; // First row of data to process
let numRows = 3; // Number of rows to process
// Fetch the range of cells A2:B3
const dataRange = sheet.getRange(startRow, 1, numRows, 300);
// Fetch values for each row in the Range.
const data = dataRange.getValues();
for (let i = 0; i < data.length; ++i) {
const row = data[i];
const emailAddress = row[1]; // Second column
const message = 'Beste ' + row[4] + '\n\nBedankt voor uw bestelling, we hebben deze succesvol ontvangen!' + '\n\nU bestelde ' + row[2] + ' pakjes droge worsten en ' + row[3] + ' pakjes pannenkoeken.' + '\nDit vormt dus ' + row[2] + ' x €6 en ' + row[3] + ' x €6,5.' + '\nIn totaal komt dit op ' + ' euro.' + '\n\nDe bestelling is pas officieel na overschrijving van bovenstaand bedrag op het rekeningnummer BE04 7755 8091 4631 met als mededeling: ' + row[2] + ' DW en ' + row[3] + ' PK.' + '\n\nBij vragen of opmerkingen kunt u ons altijd een mailtje sturen via [email protected].' + '\n\nChiro Zedelgem Dorp dankt u, laat het u smaken!';
const emailSent = row[11]; // Elfde kolom
if (emailSent !== EMAIL_SENT) { // Prevents sending duplicates
let subject = 'Worst- en pannenkoekenverkoop | Chiro Zedelgem Dorp';
// Send emails to emailAddresses which are presents in First column
MailApp.sendEmail(emailAddress, subject, message);
sheet.getRange(startRow + i, 11).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
catch(err){
Logger.log(err)
}
}
onFormSubmit
trigger. This fires with every submit so you do not have duplications. Look at this video