Home > Software design >  Is it possible to scan a QR code with google app script?
Is it possible to scan a QR code with google app script?

Time:07-27

I want to create a web app using Google Apps Script (because that is the only platform I have web app experience with) that scans and returns data from a QR code.

I have done some digging, but I can't find anything about using a GAS web app to access and read from the camera. Does anyone know if there is a way to do this?

The goal is to have a web app that can scan a QR code and search a Google Sheet for the item linked to that QR code. I can do everything but get the camera to open/scan (if even possible).

TIA

CodePudding user response:

You can integrate minhaz solution as follows

gs

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('QRCode')
      .addItem('Open QRCode reader', 'showDialog')
      .addToUi();
}

function showDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Page')
      .setWidth(600)
      .setHeight(480);
  SpreadsheetApp.getUi().showModalDialog(html, 'QRCode reader');
}

function getQRCode(txt){
  SpreadsheetApp.getActiveRange().setValue(txt)
}

Page.html

<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/html5-qrcode"></script>
</head>
<body>
  <div id="qr-reader" style="width:500px"></div>
  <script>
    var resultContainer = document.getElementById('qr-reader-results');
    function onScanSuccess(decodedText, decodedResult) {
      google.script.run.getQRCode(decodedText); 
      google.script.host.close();
    }
    var html5QrcodeScanner = new Html5QrcodeScanner("qr-reader", { fps: 10, qrbox: 250 });
    html5QrcodeScanner.render(onScanSuccess);
  </script>
</body>
</html>

Then adapt getQRCode function in gs code to your needs.

CodePudding user response:

The short answer is it is possible, however the process would be complicated. There's a work around but there's no direct library from Apps script so you would need to make use of third party APIs. Google Apps Script alone does not have built in functions/methods/services for this.

Suggestion

I have found this reference link from @Tanaike.

Reading QR code using GAS: Decoding QR code on Google Slides using Google Apps Script

From here, you can make use of the script to get the data from the QR. Here's a workaround that you could try:

1.) From the Web App here's a reference link to access camera using Google Script website: Access Camera from Google Script Website. You can research further on how to connect this script to capture the image.

2.) From there, save the image to your Docs/sheets.

3.) Use Tanaike's script for both the captured image and the image from your sheet to decode and compare the data.

Please take note that this is only a general idea but there may be other ways/solutions to address this in a more optimal way using advanced JavaScript.

  • Related