Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsglobal-variablesvar

Is it ok to have variables without "var = "?


This script seems to function perfectly fine. This question is more directed at how it was written (by a beginner).

I have been working on a script and after breaking it up into multiple functions, I was having difficulty passing variables from the first function to the next. After reading around, I discovered that I don't necessarily need to include "var = ", although I'm not 100% certain as to what the difference is. I managed to get the "variables" (are they still considered variables?) to pass down to the following functions, but I just wanted to make sure what I've done is efficient/acceptable.

function onEdit(e){
  /*  I switched these from "var = " because they weren't passing
  down to the following functions.
  */
  activess = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  activeCell = activess.getActiveCell();
  valueToFind = activeCell.getValue();
  //  Column Values
  foundItemValues = [];
  foundSubItemValues = [];
  foundCatValues = [];
  foundSubCatValues = [];

  //  These never change regardless of active sheet
  catss = SpreadsheetApp.getActive().getSheetByName('Categories-Concat');
  catData = catss.getRange(1,2,catss.getLastRow(),catss.getLastColumn()).getValues();
  catIndex = catData[0].indexOf(activeCell.getValue()) + 2;
  subCatIndex = catData[0].indexOf(activeCell.getValue()) + 2;

  itemss = SpreadsheetApp.getActive().getSheetByName('Items');
  itemdata = itemss.getRange(2,1,itemss.getLastRow(),4).getValues();

  if(e.range.getSheet().getName() == 'projectSelections'){  
    activess = e.range.getSheet().getName();
    colCss = SpreadsheetApp.getActive().getSheetByName('Categories-Concat');
    colCdata = colCss.getRange(1,2,1,colCss.getLastColumn()).getValues();
    colCIndex = colCdata[0].indexOf(activeCell.getValue()) + 2;

    if(activeCell.getColumn() == 2 && activeCell.getRow() > 1){
      this.subCategoryDV(e);
    }
  }
}


function subCategoryDV(e){
  //  Populate SUB-CATEGORY data validations
  activeCell.offset(0, 1).clearDataValidations();
  for (var q = 1; q < catData.length; q++){
    for(var i=0;i<catData.length;i++){ 
      if(valueToFind==catData[0][i]){
        foundSubCatValues.push(catData[q][i]);
      }
    }
  }
  var subCatValidationRange = foundSubCatValues;
  var subCatValidationRule = SpreadsheetApp.newDataValidation().requireValueInList(subCatValidationRange).build();
  if(activeCell.getValue() != ""){ 
    activeCell.offset(0, 1).setDataValidation(subCatValidationRule);
  }
}      

Solution

  • The keyword var ensures that the variables remain in local scope (roughly, only the function the variable is in can see it.) See here: What is the purpose of the var keyword and when should I use it (or omit it)?

    In general, it's good practice to try to keep things local - there's a lot of issues with global variables and a quick google search of why global variables or evil (or something similar) will tell you all about it.

    If you're trying to use a second function, you will need to pass every variable referenced - activeCell, catData, valueToFind, and foundSubCatValues, along side 'e'.

    The other thing you could do is define the function subCategoryDV inside the function onEdit.