Applying a cell style to a table seems hacky -- what am I doing wrong?

I’m trying to add a cell style called smsCodeCellStyle to the entire header row of two tables. This cell style contains a fill color for the cell and a paragraph style with a text fill color.

Here’s a part of one of the generated tables, so you can see the desired effect:


This is my current approach, which works, but seems janky. This approach also prevents other parts of the cell styles from being applied:

  for (let table = 0; table < createdTables.length; table ++) { // for each table...
    createdTables[table].headerRowCount++; // add the header row
    for (let cell = 0; cell < createdTables[table].rows.firstItem().cells.length; cell++) { // for each cell in the header row...
      const currentCell = createdTables[table].rows.firstItem().cells.item(cell); // get the cell
      currentCell.fillColor = (smsCodeCellStyle.fillColor as Color).name; // do some magic to get the colors
      currentCell.texts.firstItem().fillColor = (
        (smsCodeCellStyle.appliedParagraphStyle as ParagraphStyle)
          .fillColor as Color
      ).name;
    }
  }

But ideally I’d just have this, which seems like it would work, but crashes the application:

  for (let table = 0; table < createdTables.length; table ++) {
    createdTables[table].headerRowCount++;
    for (let cell = 0; cell < createdTables[table].rows.firstItem().cells.length; cell++) {
      const currentCell = createdTables[table].rows.firstItem().cells.item(cell);
      currentCell.appliedCellStyle = smsCodeCellStyle;
    }
  }

Any ideas why this crashes? Maybe there’s an issue trying to get the paragraph style from the cell style? Maybe there’s a better approach?

I have tried and succeeded with your ideal code. Could not find the cause of the crash.

  • macOS 12.6.7
  • InDesign 2023(18.5.1.79), 2024(19.1.0.43)

It seemed to simplify the code a bit more, so I did.

const { app } = require('indesign') ;

const doc = app.activeDocument ;
const smsCodeCellStyle = doc.cellStyles.itemByName('smsCodeCellStyle') ;
const createdTables = doc.pages.item(0).textFrames.item(0).tables.everyItem().getElements() ;

for (const currentTable of createdTables) {
  currentTable.headerRowCount++ ;
  currentTable.rows.firstItem().cells.everyItem().appliedCellStyle = smsCodeCellStyle ;
}
1 Like

Hey thanks for giving it a shot. But something is still fishy. I’ve copied your code exactly, commented out all my other code, and I’m still getting the crash. I also tried it with a different cell style… :thinking:

Did your tables have any content in them when you ran the script?

I’ve also tried adding the style on table creation, no luck.

I’m still able to set the cell contents, but not the style.

I have tried adding app.clearOverridesWhenApplyingStyle = true; at the top of my code and currentCell.texts.firstItem().clearOverrides(OverrideType.ALL); before I change the style. I’ve also tried my original approach through the dev console on a manually-created table.

I was able to get around this by specifying the header row cell style in the table style options:

1 Like

If you can accomplish this without writing code, that would be best.

1 Like

In that original code, smsCodeCellStyle was a CellStyle object. However I think I solved the actual problem, and it involved adding .name to the end of the object. So

currentCell.appliedCellStyle = smsCodeCellStyle.name

seems to be what I was looking for. Thanks again for the help.

In case anyone is running into this crash, I found that you can query application styles through the document, so instead of:

app.cellStyles.itemByName('yourCellStyle');

do this:

app.activeDocument.cellStyles.itemByName('yourCellStyle');

And it works with no pesky crash!