skip to Main Content

I have a site with a form that is connected to Google Sheets. Whenever I send the form, it will fill the Google sheet nicely, but the Google script won’t automatically send out the notification e-mail to the user and admin.

If I hit the run on sendEmailNotificationFromSheet it will send the e-mail.

I just started messing around with webpages and scripts any help would be appreciated.

function doPost(e) {
  try {
    var sheet = getSheet();
    var params = e.parameter;
    Logger.log('Parameters received: ' + JSON.stringify(params));

    var name = params.name || 'N/A';
    var email = params.email || 'N/A';
    var phone = params.phone || 'N/A';
    var service = params.service || 'N/A';
    var extraServices = params.selectedServices || 'N/A';
    var date = params.date || 'N/A';

    // Append row to the sheet
    sheet.appendRow([name, email, phone, service, extraServices, date]);
    Logger.log('Booking appended to sheet: ' + name);

    // Send email notifications
    sendEmailNotificationFromSheet();

    return ContentService.createTextOutput(JSON.stringify({
      "result": "success",
      "data": params
    })).setMimeType(ContentService.MimeType.JSON);
  } catch (error) {
    Logger.log('Hiba: ' + error.toString());
    return ContentService.createTextOutput(JSON.stringify({
      "result": "error",
      "error": error.toString()
    })).setMimeType(ContentService.MimeType.JSON);
  }
}

function sendEmailNotificationFromSheet() {
  try {
    var sheet = getSheet();
    var lastRow = sheet.getLastRow();
    var lastRowData = sheet.getRange(lastRow, 1, 1, sheet.getLastColumn()).getValues()[0];

    var name = lastRowData[0];
    var email = lastRowData[1];
    var phone = lastRowData[2];
    var service = lastRowData[3];
    var extraServices = lastRowData[4];
    var date = lastRowData[5];

    Logger.log('Data from the latest row - Név: ' + name + ', Email: ' + email + ', Telefon: ' + phone + ', Szolgáltatás: ' + service + ', Egyéb Szolgáltatások: ' + extraServices + ', Dátum: ' + date);

    // Admin email cím
    var adminEmail = "[email protected]"; // Helyettesítse a saját admin email címével

    // Email tárgya és törzse az admin számára
    var subjectAdmin = "Új foglalás érkezett";
    var bodyAdmin = "Új foglalás érkezett:nn" +
      "Név: " + name + "n" +
      "Email: " + email + "n" +
      "Telefon: " + phone + "n" +
      "Szolgáltatás: " + service + "n" +
      "Egyéb Szolgáltatások: " + extraServices + "n" +
      "Dátum: " + date;

    // Email küldése az adminnak
    MailApp.sendEmail(adminEmail, subjectAdmin, bodyAdmin);
    Logger.log('Admin email elküldve: ' + adminEmail);

    // Email tárgya és törzse az ügyfél számára
    var subjectUser = "Foglalás visszaigazolás";
    var bodyUser = "Kedves " + name + ",nn" +
      "Köszönjük, hogy foglaltál az xxxxxxx. Az alábbiakban találhatók a foglalás részletei:nn" +
      "Szolgáltatás: " + service + "n" +
      "Egyéb Szolgáltatások: " + extraServices + "n" +
      "Dátum: " + date + "nn" +
      "Várjuk a megjelenésed!nn" +
      "Üdvözlettel,n" +
      "xxxxxxx";

    // Visszaigazoló email küldése az ügyfélnek
    MailApp.sendEmail(email, subjectUser, bodyUser);
    Logger.log('Ügyfél email elküldve: ' + email);
  } catch (error) {
    Logger.log('Hiba az email értesítés küldése során: ' + error.toString());
  }
}

function getSheet() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = spreadsheet.getActiveSheet();
  return sheet;
}

2

Answers


  1. Chosen as BEST ANSWER

    I found the issue and was able to fix it. For some reason google script wasn't authorized to send e-mails even though I authorized it the first time. So I had to reauthorize it and now it works.


  2. Google Apps Script might not update the spreadsheet being modified immediately; because of this, if you need to read data from a spreadsheet after it was altered by the same script, you might include SpreadsheetApp.flush() for the Apps Script engine to apply the changes made by the previous statements. However, this might be a suboptimal solution from a performance perspective.

    If the code already has all the data required to be included in the email, avoid making unnecessary calls to the Spreadsheet Service, as this increases the required time to complete the task.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search