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
  • 2173 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

stevecole
stevecoleAuthor
Participating Frequently
April 24, 2024

Hi @stevecole , Is the content always in the first column? This isn’t as easy as it seems because while your example looks like it has 5 rows, it really has 8 (column 2 has 8 rows)

 


Hi Rob

The answer is no. Sometimes the table has up to 20 columns and 31 rows. The table represents a calendar date setting but the x's and the Y's maybe in the first or last column. Depending on the design. What I am looking for is a script to colour EVERY cell in the ROW. as you later post says address 1:1 2:1 and 2:2.

 

Steve