2
votes

I have a Google Sheet with many cells, one of which is a QR code (QR code contains name and Employee ID). A Google Docs file is generated based on the values from the worksheet, by replacing placeholder text. This Google Doc will be used as a digital pass.

How can I place the QR code image in the Google Docs, generated from the Google Sheets data?

Ex. In the sheet, I stored Employee ID, Name, and QR Code in columns A, B, and D.
I use replaceText to replace placeholder text in the Document:

      copyBody.replaceText('EmployeeID', row[i][0]);
      copyBody.replaceText('firstName', row[i][1]);
      copyBody.replaceText('qrCode', row[i][3]); etc.

The QR Code is stored in the sheet as a formula result:
=IMAGE("https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" & C2)

When I run my script, the EmployeeID and Name fields are replaced in the template document with the desired values, but nothing in the qrCode fields.

4

4 Answers

1
votes

I don't think that the QR code that is generated on your Spreadsheet cell is considering as text, so the function will replace 'qrCode' by nothing. You should use the code above:

var doc = DocumentApp.openById('DocId')
var ss = SpreadsheetApp.openById('SsId');
var qrCode = ss.getRange('qrCode').getValue(); // Get the QR code value from te Spreadsheet
var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" + qrCode 
var resp = UrlFetchApp.fetch(url); // Get the image of QR code
var barcode = doc.getChild(0).asParagraph().appendInlineImage(resp.getBlob()); // Value of child depend of where you want your QR code. 

For more information about appendInlineImage(blob) method and getChild(name) method

1
votes

You can generate a QR image inside google sheets with the following formula:

=image("https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl="&A2)

Set this formula in cell B2. Put your URL or anything else in cell A2

The QR code will automatic appear in cell B2 You might to need resize the cell size

enter image description here

1
votes

IMAGE =IMAGE(URL), which is =IMAGE("http://....") but in your formula:

=IMAGE("https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl="&C2)

which "&C2" is placed outside of the " "

You have to join the image URL together before use the image function. I think you need to join the image URL first:

 =JOIN("","https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=&",C2)

`

Or, you can combine both functions:

=IMAGE(JOIN("","https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=&",C2))
1
votes

An update for those still looking for how to do this (Aug 2018)...

Google has deprecated the QR chart API. I was able to find this API-based QR generator: http://goqr.me/api/

It has worked well for me using the following syntax:

=image("https://api.qrserver.com/v1/create-qr-code/?size=160x160"&[YOUR_DATA],3)

[YOUR DATA] will need to be "URL encoded" in order to work if it's more than a single number or word. This can be accomplished with the Google Sheets ENCODEURL function. Here's the whole formula I use to display a QR code that represents two concatenated cells with a hyphen between them:

=IMAGE("https://api.qrserver.com/v1/create-qr-code/?size=160x160&data="&ENCODEURL(G1&" - "&G2),3)