Skip to main content
Phil 815
Inspiring
June 6, 2023
Question

Force collection rebuild for getElements()

  • June 6, 2023
  • 1 reply
  • 229 views

Hi all, I have a question regarding getElements() and it's behavior. I've found that when I apply styles to the returned collection from getElements() the styles do not update on the UI. However, when I apply styles to the returned collection from everyItem() it correctly updates the UI.

 

I read this post explaining that getElements() returns a static array which limits the interaction with InDesign to improve performance. https://community.adobe.com/t5/indesign-discussions/quot-everyitem-getelements-quot-what-use/m-p/8801639#M36581

 

Here's my issue: I would like to selectively apply styles to cells. EveryItem() does not support js lambda expressions like .filter() so my only option is to use getElements(). Is there a way to force an interaction between the returned array from getElements() and InDesign so the style is reflected properly?

 

Style updated/rendered correctly:

var myCells = table.cells.everyItem();

myCells.appliedCellStyle = stylez;

 

Style not updated/rendered correctly:

var myCells = table.cells.everyItem().getElements().filter(cell => cell.extractLabel("rowType") === "0");

myCells.appliedCellStyle = stylez;

 

This topic is closed to new replies. Start a new post to keep the conversation going.

1 reply

Marc Autret
Legend
June 6, 2023

Hi @Phil 815,

 

Instead of

 

var myCells = table.cells.everyItem().getElements().filter
(
cell => cell.extractLabel("rowType") === "0"
);

// Cannot work since Array doesn't support `appliedCellStyle`!
myCells.appliedCellStyle = stylez;

 

it looks like you simply need forEach:

 

table.cells.everyItem().getElements().forEach
(
c => c.extractLabel("rowType")==="0" && (c.appliedCellStyle=stylez)
);

 

Note: I assumed you're programming in UXP syntax (idjs). In ExtendScript, just loop in myCells (unfiltered) and do the same:

 

// ExtendScript version

// . . .
var myCells = table.cells.everyItem().getElements();
var c,i;
for
( i=myCells.length ; i-- ;
 (c=myCells[i]).extractLabel("rowType")==="0" && (c.appliedCellStyle=stylez)
);

 

Best,

Marc