admin管理员组

文章数量:1391929

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 mand 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});

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 mand 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});
Share Improve this question asked Jul 21, 2016 at 15:22 Kevin PasquarellaKevin Pasquarella 893 silver badges14 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

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 munity project (no Adobe involvement) which should let you to write ES5/ES6 code that is then converted to ES3.

Here's how to iterate through dictionary in ExtendScript (works in After Effects at least)

var my_dictionary = {};
my_dictionary['fruit'] = "apple";

iterate_through_dict(my_dictionary)

function iterate_through_dict(my_dictionary) {
    for (var i in my_dictionary) {
        alert(my_dictionary[i]);
    }
}

I figured this out. The error was further down in the file. ExtendScript, or the version I have, does not support forEach loops, so I had to write out the file for the forEach function and import it.

(Though it should be better to post this as a ment to Davide's answer,)

There is an alternative ES5 shim which seems to be more suitable to ExtendScript: https://github./ExtendScript/extendscript-es5-shim/

You can download the all-in-one shim onto a same folder:

curl -o extendscript-es5-shim.js https://raw.githubusercontent./ExtendScript/extendscript-es5-shim/master/index.js

and use it as follows:

#include "extendscript-es5-shim.js";

// Have a fun with forEach() or any ES5 functions.

本文标签: javascriptFor loop in ExtendScriptkeys()forEach not a functionStack Overflow