[Copy & Paste] Generate QR Codes in Google Apps Script→ Oct 2024 Update: Google Charts API was deprecated, so a new solution has been added.

* If you need help with the content of this article for work or development, individual support is available.

Automation Doctor

Today I’ll show you how to generate QR codes using Google Apps Script (GAS).

No API registration required — you can use it immediately!


Generating QR Codes with Google Chart Tool

Since GAS can’t generate QR codes on its own, we call an external API.
The simplest option is the Google Chart Tool:
https://developers.google.com/chart


Basic: Get a QR Code Image in GAS

Here’s the minimal code required to generate a QR code via GET request:

// Function to generate a QR code
function generateQR(data) {

  // Send GET request to Google Chart Tool
  let url = 'https://chart.googleapis.com/chart?chs=100x100&cht=qr&chl=' + data;
  let option = {
    method: "get",
    muteHttpExceptions: true
  };
  let result = UrlFetchApp.fetch(url, option);

  // Return the image blob
  return result;
}

Advanced: Save the QR Image to a Specific Folder

To save the generated QR code as a file in Google Drive:

// Function to generate a QR code
function generateQR(data) {

  let url = 'https://chart.googleapis.com/chart?chs=100x100&cht=qr&chl=' + data;
  let option = {
    method: "get",
    muteHttpExceptions: true
  };
  let result = UrlFetchApp.fetch(url, option);

  return result;
}

// Function to save the file
function generateImageFile(url) {

  // Generate the QR code and store it as "blob"
  let blob = generateQR(url);

  // Specify your folder ID (replace ****** with your own)
  let folder = DriveApp.getFolderById("******");

  // Save the file
  folder.createFile(blob).setName("*****.png");
}

Automation Doctor:
“Surprisingly easy, isn’t it?
If you have questions, drop them in the comments!”


Reference

Himanahito — “Google Apps ScriptでQRコードを生成してみる”
https://note.com/himajin_no_asobi/n/n51de21bf73e5


Update

Google Charts API became unavailable, so here’s the new working code using the API from goqr.me.

function generateQRCodeFromSheet(data) {
  const baseUrl = 'https://api.qrserver.com/v1/create-qr-code/';
  const qrCodeUrl = `${baseUrl}?data=${encodeURIComponent(data)}`;
  
  // Insert the QR code image URL into the active sheet
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const cell = sheet.getActiveCell(); // Puts the QR code in the currently selected cell
  cell.setFormula(`=IMAGE("${qrCodeUrl}")`);
}

This works almost the same as before.

If you want margin (the code above has none), use this version:

function generateQRCodeFromSheet(data) {
  const baseUrl = 'https://api.qrserver.com/v1/create-qr-code/';
  const qrCodeUrl = `${baseUrl}?data=${encodeURIComponent(data)}&margin=10`;
  
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const cell = sheet.getActiveCell();
  cell.setFormula(`=IMAGE("${qrCodeUrl}")`);
}

API reference:
https://goqr.me/api

That’s all — happy automating!

ZIDOOKA!

Need help with the content of this article?

I provide individual technical support related to the issues described in this article, as a freelance developer. If the problem is blocking your work or internal tasks, feel free to reach out.

Support starts from $30 USD (Estimate provided in advance)
Thank you for reading

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Policy on AI Usage

Some articles on this site are written with the assistance of AI. However, we do not rely entirely on AI for writing; it is used strictly as a support tool.