Skip to content
Advertisement

Google Sheets Script Array Length Returning Null

See code below and log. I am working on a google sheets script that updates a google sheet when a linked google form is submitted. To do this I am using the array “event.namedValues”, which is generated automatically when a form is submitted. However while debugging some issues (and learning how to do this), I wanted to check the length of the array I was working with and it would return “null”. When I tried adding the .length property of the array to 0, the logger logged “NAN” (See log below). What am I doing wrong?

Code Sample:

// triggered by submitting google form
function onFormSubmit(event) {

  // sets formSubmit equal to "Named Values" array from the form submission event
  var formSubmit = event.namedValues;

  // creates new array
  var updateItems = new Array();
  
  // logs formSubmit array
  Logger.log(formSubmit);

  // creates variable to measure formSubmit array length
  var formSubmitLength = 0;
  formSubmitLength += formSubmit.length;

  // logs variable above
  Logger.log(formSubmitLength);

  // should log length of formSubmit array
  Logger.log(formSubmit.length);

  // runs through formSubmit array
  for (i = 0; i <= formSubmit.length; i++) {

    // checks if formSubmit[i] contains a value
    if (formSubmit[i] !== null)
    {
      // if formSubmit[i] does contain a value, pushes that value to updateItems array
      updateItems.push(formSubmit[i]);
    }

    // logs updateItems array
    Logger.log(updateItems);

  }

  }

Log Sample:

Jun 3, 2021, 6:13:19 PM Info {Perishable Items Used [Butter (Pack)]=[], Toiletries Used [Wipe totes]=[], Non-Perishable Items Used [Almond butter]=[], Perishable Items Used [Milks]=[], Toiletries Used [Conditioner]=[], Non-Perishable Items Used [Peanut butter]=[], Non-Perishable Items Used [Cereals]=[], Non-Perishable Items Used [Pasta sauce]=[], Non-Perishable Items Used [Artichokes]=[], Non-Perishable Items Used [Flour]=[], Perishable Items Used [Half & Half]=[], Cleaning Supplies Used [Hand soap]=[], Perishable Items Used [Grapes]=[], Non-Perishable Items Used [Coffee]=[], Non-Perishable Items Used [Peanuts]=[], Toiletries Used [Soap bars]=[], Non-Perishable Items Used [Evaporated milk]=[], Cleaning Supplies Used [Paper towels]=[], Non-Perishable Items Used [Pancake syrup]=[], Perishable Items Used [Yogurts]=[1], Non-Perishable Items Used [Snacks]=[], Toiletries Used [Toothpaste]=[], Non-Perishable Items Used [Salad dressing]=[], Perishable Items Used [Mozzarella]=[], Non-Perishable Items Used [Rice]=[], Perishable Items Used [Eggs (Dozen)]=[], Perishable Items Used [Bananas]=[], Toiletries Used [Toilet paper Pack]=[], Non-Perishable Items Used [Beans]=[], Cleaning Supplies Used [Garbage bags]=[], Non-Perishable Items Used [Pickles]=[], Perishable Items Used [Lunch meats]=[], Timestamp=[6/3/2021 18:13:18], Non-Perishable Items Used [Ketchup]=[], Non-Perishable Items Used [Jellies]=[], Non-Perishable Items Used [Pasta]=[], Non-Perishable Items Used [Soups]=[], Cleaning Supplies Used [Dish detergent]=[], Cleaning Supplies Used [Napkins]=[], Non-Perishable Items Used [Peppers]=[], Non-Perishable Items Used [Mustard]=[], Toiletries Used [Shampoo]=[], Non-Perishable Items Used [Sun dried tomatoes]=[], Perishable Items Used [Apples]=[]}

Jun 3, 2021, 6:13:19 PM Info null

Jun 3, 2021, 6:13:19 PM Info NaN

Jun 3, 2021, 6:13:19 PM Info []

Advertisement

Answer

Since e.namedValues is an object, it does not have a length property.

object

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement