How to Write an App Script to Create a Label with a Name, Weight, and a QR Code that Submits a Form to Remove an Item from Inventory?


BarcodeBC > Articles > How to Write an App Script to Create a Label with a Name, Weight, and a QR Code that Submits a Form to Remove an Item from Inventory?


Write an App Script to Create a Label with a Name, Weight, and a QR Code that Submits a Form to Remove an Item from Inventory

To create an app script that generates a label with a name, weight, and QR Code and submits a form to remove an item from inventory, you can follow these steps.

1. Create a Google Form to remove an item from inventory. The form should have fields for the item name, weight, and any other relevant information.

2. Use a library like Google Charts to generate a QR Code based on the item information entered in the form. You can use the following code to generate a QR Code.


var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" + encodeURI(item_name + " " + item_weight);

3. Create a function that runs when the form is submitted. This function should generate the label with the item name, weight, and QR Code, and then submit the form to remove the item from inventory. Here's an example of how this function could look.


function onSubmit(e) {
  var item_name = e.response.getItemResponses()[0].getResponse();
  var item_weight = e.response.getItemResponses()[1].getResponse();
  
  var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" + encodeURI(item_name + " " + item_weight);
  
  var label = "<h1>" + item_name + "</h1><p>Weight: " + item_weight + "</p><img src='" + url + "'>";
  
  // Use the following code to print the label on a label printer or display it on a screen
  // ...
  
  // Submit the form to remove the item from inventory
  var form_url = "https://docs.google.com/forms/d/[FORM_ID]/formResponse";
  var form_data = {
    "entry.[ENTRY_ID_FOR_ITEM_NAME]": item_name,
    "entry.[ENTRY_ID_FOR_ITEM_WEIGHT]": item_weight
  };
  UrlFetchApp.fetch(form_url, {method: "post", payload: form_data});
}

Note that you'll need to replace [FORM_ID], [ENTRY_ID_FOR_ITEM_NAME], and [ENTRY_ID_FOR_ITEM_WEIGHT] with the appropriate values for your form. You'll also need to adjust the code to print the label on a label printer or display it on a screen.


I only need one label printed or as many as ten, or so. But I would never use the whole sheet. I would like to tell it where to start.

To print only one or a specific number of labels from a sheet of labels, and to specify where to start printing, you can modify the code to use the Google Docs API and the Google Cloud Print API. Here's an updated version of the code.


function onSubmit(e) {
  var item_name = e.response.getItemResponses()[0].getResponse();
  var item_weight = e.response.getItemResponses()[1].getResponse();
  
  var url = "https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=" + encodeURI(item_name + " " + item_weight);
  
  var label = "<h1>" + item_name + "</h1><p>Weight: " + item_weight + "</p><img src='" + url + "'>";
  
  var doc = DocumentApp.create('Label for ' + item_name); // create a new document
  var body = doc.getBody();
  body.insertHtml(0, label); // insert the label into the document
  
  var start_index = 0; // the index of the first label to print
  var num_labels = 1; // the number of labels to print
  
  var printer_name = 'My Printer'; // replace with the name of your printer
  
  var document_id = doc.getId();
  var ticket = {
    "version": "1.0",
    "print": {
      "color": {
        "type": "STANDARD_COLOR",
        "vendor_id": "Color"
      },
      "duplex": {
        "type": "NO_DUPLEX"
      },
      "page_orientation": {
        "type": "PORTRAIT"
      },
      "copies": {
        "copies": num_labels
      },
      "page_range": {
        "interval": [
          {
            "start": start_index,
            "end": start_index + num_labels - 1
          }
        ]
      }
    }
  };
  var content_type = 'application/pdf';
  
  var printers = CloudPrintService.getPrinters(); // get all printers associated with your Google account
  var printer_id = null;
  for (var i = 0; i < printers.length; i++) {
    if (printers[i].name == printer_name) {
      printer_id = printers[i].id;
      break;
    }
  }
  if (printer_id) {
    var response = CloudPrintService.submitJob(printer_id, document_id, content_type, doc.getAs(content_type), ticket); // submit the print job
    doc.saveAndClose();
  }
  else {
    Logger.log('Printer not found: ' + printer_name);
  }
  
  // Submit the form to remove the item from inventory
  var form_url = "https://docs.google.com/forms/d/[FORM_ID]/formResponse";
  var form_data = {
    "entry.[ENTRY_ID_FOR_ITEM_NAME]": item_name,
    "entry.[ENTRY_ID_FOR_ITEM_WEIGHT]": item_weight
  };
  UrlFetchApp.fetch(form_url, {method: "post", payload: form_data});
}

This code creates a new Google Docs document for each label, inserts the label into the document, and then prints the specified number of labels starting at the specified index on the sheet of labels. It also submits the form to remove the item from inventory after printing the label(s).

To use this code, you'll need to replace [FORM_ID], [ENTRY_ID_FOR_ITEM_NAME], and [ENTRY_ID_FOR_ITEM_WEIGHT] with the appropriate values for your form, and replace 'My Printer' with the name of your printer. You'll also need to enable the Google Docs API and the Google Cloud Print API in your Google Cloud Console.



Create a Barcode Inventory in Google Sheets

To create a barcode inventory in Google Sheets, you can follow these steps.

1. Install a barcode font: To display barcodes in Google Sheets, you need to install a barcode font. There are several free barcode fonts available online that you can download and install on your computer. Once you've installed the font, you can use it in Google Sheets to display barcodes.

2. Create a new Google Sheets document: Open Google Sheets and create a new document.

3. Set up your inventory columns: Set up your inventory columns with the necessary information, such as item name, item number, item description, and item quantity.

4. Add a barcode column: Add a new column to your inventory spreadsheet to hold the barcode values. You can label this column "Barcode" or something similar.

5. Format the barcode column: In the barcode column, select all of the cells where you want to add barcodes. Then, change the font to the barcode font that you installed in step 1.

6. Generate barcodes: To generate barcodes, you can use a formula that concatenates the necessary information for each item into a single string, and then use the "barcode" font to display the barcode. For example, if you're using the free "Code 128" barcode font, you can use the following formula in the first cell of the barcode column, assuming that the item number is in column B.


=CODE128("*" & B2 & "*")

This formula will generate a barcode for the item number in cell B2.

7. Copy and paste the formula: Once you've created the formula for the first item, you can copy and paste it into all of the cells in the barcode column. The formula will automatically update to use the correct item number for each row.

With these steps, you can create a barcode inventory in Google Sheets that allows you to track your inventory using barcodes.