QR Image Formula

qrImageFormula(text, opts) URL-encodes text and returns a ready-to-write =IMAGE(...) formula that shows a QR code in the cell. {size} is the image size in pixels and defaults to 150 (allowed range 50–1000).

When to use

  • A sheet of public URLs or ticket IDs that should each have a scannable code
  • Filling a formula column from a script instead of typing =IMAGE by hand
  • A label-print tab where every row is one code

How to use this snippet

The image is loaded from QuickChart, a third-party URL that returns a PNG. The old chart.googleapis.com QR endpoint is gone (it now 404s). Sheets may ask you to allow external images the first time.

/** * Return an =IMAGE(...) formula for a QR code of text. * Encodes the payload so &, #, +, and spaces survive. opts.size defaults * to 150 and must be an integer from 50 to 1000. * * @param {string} text * @param {{size?: number}} [opts] * @return {string} */ function qrImageFormula(text, opts) { opts = opts || {}; var size = opts.size == null ? 150 : Number(opts.size); if (!(size >= 50) || Math.floor(size) !== size || size > 1000) { throw new Error('qrImageFormula: size must be an integer from 50 to 1000'); } var payload = encodeURIComponent(String(text == null ? '' : text)); return '=IMAGE("https://quickchart.io/qr?size=' + size + '&text=' + payload + '")'; }

Example

function fillQrColumn() { var sheet = SpreadsheetApp.getActive().getSheetByName('Labels'); var last = sheet.getLastRow(); if (last < 2) return; var values = sheet.getRange(2, 1, last - 1, 1).getDisplayValues(); // A = public URL or id var out = values.map(function (r) { var text = String(r[0] || '').trim(); return [text ? qrImageFormula(text, { size: 200 }) : '']; }); sheet.getRange(2, 2, out.length, 1).setFormulas(out); // B = QR }

Use setFormulas, not setValues, so Sheets treats the result as a formula.

Notes

  • Put only public URLs or opaque IDs in the code. The payload is also in the image URL, so a private name, email, or token would leak in the formula bar and in fetch logs. Don't encode anything you would not print on a sticker.
  • Widen the column and raise the row height so a phone can read the code. For print, IMAGE mode 4 with a matching pixel size is more predictable than shrinking to fit.
  • A sheet of hundreds of codes will issue hundreds of image requests; if some cells look broken, wait and let Sheets retry.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension