0

I am working on a function that will construct a bar code from a few data points within my spreadsheet for items in an inventory.

My issue/question is two fold:

  1. When running through the for loop, I am getting this error message "TypeError: Cannot read property "length" from null."

  2. While it looks like I have successfully loaded the ranges I want into veritable, I am not entirely sure as to how to call them, could you please tell me if I am calling them correctly within the if statement?


function barCodeBuilder(stockName) {

  // Setting Ranges from prices table
  var sheet = SpreadsheetApp.getActive(); 
  var listNameRange = sheet.getRangeByName('MeterailsPricing!ITEM NAME');
  var listSkuRange = sheet.getRangeByName('MeterailsPricing!INVENTORY SKU');
  var listPriceRange = sheet.getRangeByName('MeterailsPricing!PRICE');
  var listSupRange = sheet.getRangeByName('MeterailsPricing!SUPPLIER CODE');

  //Errors begin here = TypeError: Cannot read property "length" from null
  for(listCounter = 0; listCounter < listNameRange.length; listCounter++){
    if(stockName == listNameRange[listCounter][0]){
      var barCode = {
        sku:listSkuRange[listCounter][0],
        supCode:listSupRange[listCounter][0],
        price:listPriceRange[listCounter][0]};
    }
  }

  //final bar code assembly
  var finalBarCode = barCode.sku + "-" + barCode.supCode + "-" + barCode.price;
  return finalBarCode;
}

And as always, thank you all for taking the time to help me out.

Talk to you soon. Greg R.

abielita
  • 13,147
  • 2
  • 17
  • 59

1 Answers1

0

Error "TypeError: Cannot read property "length" from null." occurred if there were no matches for the regular expression. Because of this, listNameRange will be null. You need to check for this before your loop.

Sample code snippet from this SO answer:

if (templateVars !== null) {
    for (var i = 0; i < templateVars.length; i++) {
        ...
    }
}

Here are some related SO posts which might help:

Community
  • 1
  • 1
adjuremods
  • 2,938
  • 2
  • 12
  • 17