-1

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

This script can't generate qr code. QR code formula in header so it is automatically generate qr code in spreadsheet in column B with data from column D.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 = "myemailaddress@here.com";
  var qrCode = e.values[1].split("=")[3];//I want to try
  var empName = e.values[2];
  var empId = e.values[3];
  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 '+empName)
  .getId();
  // Open the temporary document
  var copyDoc = DocumentApp.openById(copyId);
  // Get the document’s body section
  var copyBody = copyDoc.getBody();

  replaceTextToImage(copyBody, 'qrcode', qrCode);//problem could not be generated
  copyBody.replaceText('name', empName);
  copyBody.replaceText('id', empId);
  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 ={"QR CODE";ARRAYFORMULA(IF(D2:D<>"";IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2:D);))} Name Id Photo
10/07/2021 8:35:24 QR CODE Robert 1234 https://drive.google.com/open?id=14SAL5EK8tqOESgZyAayScbTqhSEE89Wa
Joko
  • 3
  • 2
  • Do you see QR-codes or your Spreadsheet? Perhaps it makes sense to replaced it with simpler variant (for 'B2' cell): `=IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2)` – Yuri Khristich Jul 18 '21 at 09:12
  • Yes. I see QR code in my spreadsheet using array formula. When I used `=IMAGE("https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="&D2)` I had to drag it down. Event though, I need pdf file as attachment after someone submitting google form. – Joko Jul 18 '21 at 09:21
  • Probably the cause is here: `var blob = DriveApp.getFileById(fileId).getBlob();` You can insert this way a file. A photo from Drive, for instance. But I suspect that the `qrCode = e.values[3].split("=")[1];` doesn't return fileID of a file, since you nave no such file on Drive at all. I don't know what exactly it returns. URL?. Text? Probably you need to get the blob of the QR code another way, via `UrlFetchApp.fetch(url).getBlob()`. See my example. – Yuri Khristich Jul 18 '21 at 11:44
  • Can you show what exactly contains the variable `qrCode` when you send it into the function `replaceTextToImage()`? Is it a `fileID` of your qr code? Since the function takes file id. – Yuri Khristich Jul 19 '21 at 06:32

1 Answers1

0

I didn't manage to reproduce the problem. I repeated all the steps more or less and it works as intended. Here is the minimal reproducible example.

const doc_template_ID = 'template ID';
const ss_ID = 'spreadsheet ID';
const email_address = 'mail@gmail.com';

function make_doc_and_send_it_as_pdf() {

  // get data from spreadsheet
  const sheet = SpreadsheetApp.openById(ss_ID).getSheets()[0];
  const QR_text = sheet.getRange('B1').getValue();

  // make copy of the template and make changes in the doc
  const doc_file = DriveApp.getFileById(doc_template_ID).makeCopy('QR_code');
  const doc_ID = doc_file.getId()
  const doc = DocumentApp.openById(doc_ID);
  var body = doc.getBody();
  body = body.replaceText('{{text}}', QR_text);

  // insert a pic of the QR code
  const url   = "https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=" + QR_text; 
  const resp  = UrlFetchApp.fetch(url);
  const image = resp.getBlob();
  replaceTextToImage(body, '{{QR}}', image);

  doc.saveAndClose();
  
  // send the doc via email as pdf
  var pdf = doc_file.getAs("application/pdf");
  var subject = "QR pdf";
  var body = "sample text";
  MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

  doc_file.setTrashed(true);
}

// modified Tataike's function from here
// https://tanaikech.github.io/2018/08/20/replacing-text-to-image-for-google-document-using-google-apps-script/
// now it takes image (blob) instead of fileId

function replaceTextToImage(body, searchText, image) {
    var next = body.findText(searchText);
    if (!next) return;
    var r = next.getElement();
    r.asText().setText('');
    r.getParent().asParagraph().insertInlineImage(0, image);
    return next;
  };

My spreadsheet looks like this:

enter image description here

My doc template looks like this:

enter image description here

The PDF in email look like this:

enter image description here

It gets data from spreadsheet, makes a copy of doc template, changes text in the doc, inserts image of QR code in the doc (it replaces all the paragraph that contains '{{QR}}', I left it as it was, probably it can replace just a word in the paragraph)), and sends the doc via mail as PDF.


In your code, probably you need to change the function replaceTextToImage() this way:

var replaceTextToImage = function(body, searchText, blob) {
    var width = 300; // Please set this.
    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);
  }

It makes the function to take a blob instead of a fileId.

And to change these two lines:

var qrCode = e.values[3].split("=")[1];
var photo = e.values[4].split("=")[1];

with this:

var qrCode = UrlFetchApp.fetch(e.values[3].split("=")[1]).getBlob();
var photo = DriveApp.getFileById(e.values[4].split("=")[1]).getBlob();

I suppose that e.values[3].split("=")[1] contains a text of QR code, and e.values[4].split("=")[1] contains file ID. But I can be wrong.

Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
  • I'm sure you can add installabe trigger `onFormSubmit()` to my code this way https://stackoverflow.com/questions/26037556/form-answer-spreadsheet-onchange-trigger And it will work. It will send email with pdf (with qr code) after submit form. – Yuri Khristich Jul 19 '21 at 06:22