0

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:

  1. The script should prevent Google from sending duplicates, but that doesn't work
  2. 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:

  1. 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
  2. I want the duplicate tracker to work, so no-one gets 2 mails
  3. I want the script to scan enough rows, so everyone gets a mail
  4. I want the total amount in the text of the mail

That's it I guess, thanks for helping!

enter image description here

    // 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)
  }
}
6
  • 1
    Welcome to Stack Overflow. Code should be added as text no as image.
    – Wicket
    Commented Jan 11, 2022 at 19:50
  • You want to research using a trigger or an event. You can have these fire when a form is submitted, when a a spreadsheet changes, every 5 minutes. Commented Jan 11, 2022 at 19:51
  • Like @pgSystemTester is suggesting. You want to use the onFormSubmit trigger. This fires with every submit so you do not have duplications. Look at this video
    – RemcoE33
    Commented Jan 11, 2022 at 20:12
  • Okey, thanks everyone. I just watched the video, but it looks like I can't do all the other stuff with the calculation of the money etc.? Commented Jan 11, 2022 at 20:26
  • 2
    @WardVerduyn, I can see you're trying to get there and that's great that you tracked down events, but I don't understand specifically what's going on. I suggest you circle back on your project as well as really read how to ask a question and really read it (many people including me just brush past such documentation). If you isolate where/what your issue is, list off 2 or 3 google searches that you've tried, I suspect you'll figure it out on your own (youv'e already got a lot going). Also working with the debuger might be a huge help. Commented Jan 11, 2022 at 21:54

0