Google-apps-script – Multi-var definition statement limit in Apps Script

google-apps-script

My personal javascript variable definition style is a single var statement followed by one or more definitions. It's an unimportant choice, but one that some find benefits in.

Carrying this over to Apps Script, I find it often ends up giving up early—cutting off its interpretation of those comma-separated definitions and so skipping some variables' creation and assignment.

For an example, the following is valid js syntax according to several checkers, but editBottom will not be defined (nor, indeed, get syntax highlighting to variables' blue):

function rangeToBounds() {
  var activeRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("R1C2:R3C4");

  var editTop = activeRange.getRow(),
      editLeft = activeRange.getColumn(),
      editRight = activeRange.getColumn()+activeRange.getNumColumns()-1,
      editBottom = activeRange.getRow()+activeRange.getNumRows()-1;
}

Whereas swapping in this equivalent set works as expected:

  var editTop = activeRange.getRow();
  var editLeft = activeRange.getColumn();
  var editRight = activeRange.getColumn()+activeRange.getNumColumns()-1;
  var editBottom = activeRange.getRow()+activeRange.getNumRows()-1;

Even this works:

  var editTop = activeRange.getRow(),
      editLeft = activeRange.getColumn(),
      editRight = activeRange.getColumn()+activeRange.getNumColumns()-1;
  var editBottom = activeRange.getRow()+activeRange.getNumRows()-1;

So I believe I'm hitting some length or nested-eval limit, not an Apps Script syntax rule.
What's actually going on?

Best Answer

I just tested the code. editBottom is defined, the only "problem" is that the font color for editBottom is the default color (black) instead of blue.

Here is my "mcve" version of the the OP code:

function rangeToBounds() {
  var activeRange = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("R1C2:R3C4");

  var editTop = activeRange.getRow(),
      editLeft = activeRange.getColumn(),
      editRight = activeRange.getColumn()+activeRange.getNumColumns()-1,
      editBottom = activeRange.getRow()+activeRange.getNumRows()-1;
  Logger.log(editBottom);
}

It's known from years (references pending) that Google Apps Script editor have this and other "soft problems".

It worth to note that now it's easier to develop Apps Script projects locally with other less buggy IDEs by using CLASP .

Related