Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
1

Force collection rebuild for getElements()

Explorer ,
Jun 06, 2023 Jun 06, 2023

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/880...

 

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;

 

TOPICS
How to , Scripting , UXP Scripting
219
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jun 06, 2023 Jun 06, 2023
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines