Search code examples
javascriptarraysloopsxlsxextendscript

For loop in ExtendScript - keys().forEach not a function


I am working in ExtendScript with bridge to attach labels to documents from an excel document. I am parsing through the excel document using the js-xlsx library. I am running a for loop to parse through the individual cells and get the values, which works perfectly when I run it from the command line. However, when I run it in ExtendScript I am getting an exception that keys().forEach is not a function. Does ExtendScript read for loops differently for some reason? I am having trouble figuring this out. Here is the line that is giving me trouble:

var range = {s:{c:0, r:0}, e:{c:1,r:30}};
for (var R = range.s.r; R <= range.e.r; ++R) {
  for (var C = range.s.c; C <= range.e.c; ++C) {
      var cell_address = xls.utils.encode_cell({c:C, r:R});

Solution

  • Extendscript, as a language, is stuck at ECMAScript 3 – so it doesn't natively know about Object.keys(), nor Array.forEach(). Not even JSON.

    Official mention of this can be found in the After Effects scripting guide pag.3, where it says:

    After Effects scripts use the Adobe ExtendScript language, which is an extended form of JavaScript used by several Adobe applications, including Photoshop, Illustrator, and InDesign. ExtendScript implements the JavaScript language according to the ECMA-262 specification. The After Effects scripting engine supports the 3rd Edition of the ECMA-262 Standard, including its notational and lexical conventions, types, objects, expressions, and statements. ExtendScript also implements the E4X ECMA-357 specification, which defines access to data in XML format.

    The above is not found in the PS scripting guide, nor reference.

    If you want to use ES5 features, either include shims, or give extendscriptr a try. Extendscriptr is a scripting community project (no Adobe involvement) which should let you to write ES5/ES6 code that is then converted to ES3.