Search code examples
javascriptgoogle-sheetsgoogle-apps-scriptcode-generationgoogle-forms

JavaScript function in Google spreadsheets to send QR codes to emails


We are planning for an event, to register you have to fill out a Google form, after filling it there is this function in the "Apps Script" of the spreadsheet that will generate and send a QR code to your email, I also did the trigger but the function didn't work, any thoughts on what went wrong?

The function:

function sendQrCodeEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var qrCodeUrlPrefix = ''https://chart.googleapis.com/chart?cht=qr&chs=150x150&chl=';

  for (var i = 1; i < data.length; i++) {
    var email = data[i][2]; // third column is the email
    var name = data[i][1]; // second column is the name
    var qrCodeUrl = qrCodeUrlPrefix + encodeURIComponent(email);

    // Send Email with QR Code
    MailApp.sendEmail({
      to: email,
      subject: 'Your Event QR Code',
      htmlBody: 'Hello ' + name + ',<br><br>Thank you for registering. Please find your 
      QR code below.<br><img src="' + qrCodeUrl + '"><br>Best Regards,<br>Event Team'
    });
  }
}

Thank you for your time.


Solution

  • I think you have syntax error and some issue in the body tag.

     function sendQrCodeEmails() {
          var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
          var data = sheet.getDataRange().getValues();
          var qrCodeUrlPrefix = 'https://chart.googleapis.com/chart?cht=qr&chs=150x150&chl=';
        
          for (var i = 1; i < data.length; i++) {
            var email = data[i][2]; // third column is the email
            var name = data[i][1]; // second column is the name
            var qrCodeUrl = qrCodeUrlPrefix + encodeURIComponent(email);
        
            // Send Email with QR Code
            MailApp.sendEmail({
              to: email,
              subject: 'Your Event QR Code',
              htmlBody: 'Hello ' + name + ',<br><br>Thank you for registering. Please find your QR code below.<br><img src="' + qrCodeUrl + '"><br>Best Regards,<br>Event Team'
            });
          }
        }