Skip to main content
stevecole
Participating Frequently
April 22, 2024
Question

Find content in cell and apply cell style across the entire ROW

  • April 22, 2024
  • 2 replies
  • 2203 views

Any help?

I have succesfully created a jsx which finds content "x" within cells and applies a cell style across the entire ROW.

The problem is some cells are split horizontally and the lower part of the ROW on that cell does get the cell style.

 

Steve

This topic has been closed for replies.

2 replies

rob day
Community Expert
Community Expert
April 23, 2024

This also seems to work at least on the provided sample:

 

 


function applyCellStylesToRows() {
    var doc = app.activeDocument;
    var weekendCellStyle = doc.cellStyles.itemByName("Weekend");
    var bankHolidayCellStyle = doc.cellStyles.itemByName("BankHoliday");
    var c = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
    var s;
    for (var i = 0; i < c.length; i++){
       if (c[i].contents.match(/\b\d{1,2}x\b/i)) {
            s =  weekendCellStyle   
       }else if (c[i].contents.match(/\b\d{1,2}Y\b/i)) {
            s = bankHolidayCellStyle
       }
       c[i].appliedCellStyle = s
    };   
}

app.doScript(applyCellStylesToRows, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Apply Cell Styles');

 

Robert at ID-Tasker
Legend
April 23, 2024
quote

This also seems to work at least on the provided sample:

 

 

function applyCellStylesToRows() {
    var doc = app.activeDocument;
    var weekendCellStyle = doc.cellStyles.itemByName("Weekend");
    var bankHolidayCellStyle = doc.cellStyles.itemByName("BankHoliday");
    var c = doc.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
    var s;
    for (var i = 0; i < c.length; i++){
       if (c[i].contents.match(/\b\d{1,2}x\b/i)) {
            s =  weekendCellStyle   
       }else if (c[i].contents.match(/\b\d{1,2}Y\b/i)) {
            s = bankHolidayCellStyle
       }
       c[i].appliedCellStyle = s
    };   
}

app.doScript(applyCellStylesToRows, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Apply Cell Styles');

 


By @rob day

 

But @stevecole wants to style the entire ROW - not just a single Cell.

 

rob day
Community Expert
Community Expert
April 23, 2024

I understand. The cells read left to right top to bottom, so my version is simply changing the cell style when the column 1 cell contains content—it works when the content is in the first column. Here’s X,Y,Y,X,Y

 

 

Your approach might be better, but the snippet you posted is  throwing an error when I try it with @stevecole’s code.

 

brian_p_dts
Community Expert
Community Expert
April 22, 2024

Mind posting your jsx so we can more effectively troubleshoot?

stevecole
stevecoleAuthor
Participating Frequently
April 23, 2024

Hi 

I have added an InDesign file as well which shows lower part of horizontal split cell being coloured up. There are two cell styles which are called Weekend and  BankHoliday.

Thank you

Steve

Robert at ID-Tasker
Legend
April 23, 2024

@stevecole 

 

You should rather get row number and iterate by Cell address.

 

VB exmple of how to address specific Cell:

 

myTable.Cells.Item(CStr(column) & ":" & CStr(row))

 

 

Or you can check if Cells are merged / span across rows:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Cell.html

 

And "rowSpan" property.