0
votes

I have google form with response as in table (spreadsheet) below.

This script can't generate qr code. I don't know how to solve it.

var docTemplate = "doc ID"; 
var docName = "Vehicle check with images";

function onFormSubmit(e) {
  var replaceTextToImage = function(body, searchText, fileId) {
    var width = 300; // Please set this.
    var blob = DriveApp.getFileById(fileId).getBlob();
    var r = body.findText(searchText).getElement();
    r.asText().setText("");
    var img = r.getParent().asParagraph().insertInlineImage(0, blob);
    var w = img.getWidth();
    var h = img.getHeight();
    img.setWidth(width);
    img.setHeight(width * h / w);
  }

  //Get information from form and set as variables
  var email_address = "[email protected]";
  var empName = e.values[1];
  var empId = e.values[2];
  var qrCode = e.values[3].split("=")[1];//I want to try
  var photo = e.values[4].split("=")[1];

  // Get document template, copy it as a new temp doc, and save the Doc’s id
  var copyId = DriveApp.getFileById(docTemplate)
  .makeCopy(docName+' for '+name)
  .getId();
  // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);
  // Get the document’s body section
  var copyBody = copyDoc.getBody();

  copyBody.replaceText('name', empName);
  copyBody.replaceText('id', empId);
  replaceTextToImage(copyBody, 'qrcode', qrCode);//problem could not be generated
  replaceTextToImage(copyBody, 'photo', photo);

  copyDoc.saveAndClose();
  var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
  var subject = "sample attachment file";
  var body = "sample text: " + empName + "";
  MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

  DriveApp.getFileById(copyId).setTrashed(true);
}
Timestamp QRCode Name Id Photo
10/07/2021 8:35:24 Formula =ARRAYFORMULA(IF(B2:B<>"";IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2:D);)) Robert 1234 https://drive.google.com/open?id=14SAL5EK8tqOESgZyAayScbTqhSEE89Wa