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

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

New Here ,
Apr 22, 2024 Apr 22, 2024

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

1.1K

Translate

Translate

Report

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
Community Expert ,
Apr 22, 2024 Apr 22, 2024

Copy link to clipboard

Copied

Mind posting your jsx so we can more effectively troubleshoot?

Votes

Translate

Translate

Report

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
New Here ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

@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.

 

Votes

Translate

Translate

Report

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
New Here ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

Sorry Robert I replied to your personal email by mistake.

regards

Steve

Votes

Translate

Translate

Report

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
New Here ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

Hi Robert 


Thank you for the reply. Your explanation is much appreciated but far above my level of competence I’m afraid! I have attached the Indesign tester doc and the jsx so you can see what I am trying to achieve. Basically I am trying to colour weekend and bank holiday tints on some calendar settings, but where the cell is split horizontally it doesn’t seem to see it as the same ROW.

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

@stevecole

 

I fully understand what you are trying to achieve, but I'm not JS guy and can't find a sample in JS to address specific Cell by row and col - I'm pretty sure @brian_p_dts will be able to help you - or @rob day or @Peter Kahrel.

 

Votes

Translate

Translate

Report

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
New Here ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

Thank you

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

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)

 

Screen Shot 1.png

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

@rob day 

 

So it's not possible to address Cell in the Table by its "address" - like in my VB example?

 

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

 

where CStr(column) & ":" & CStr(row) would be for example "4:5" - 4th column, 5th row.

 

Then, it would be just a case of iterating - something like this:

 

var row = cell.parentRow.index;
for (var c = 0; m < table.columns.length; c++) {
    table.cells[c + ":" + row].appliedCellStyle = weekendCellStyle;
}

 

 

I just don't know how "cells[c + ":" + row]" will be in JS.

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

quote

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)

 

Screen Shot 1.png


By @rob day

 

So addresses would be like that?

RobertTkaczyk_0-1713889420453.png

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

This numbers the first cell of each row and gets the number of cells in the row:

 

Screen Shot 3.png

 

function applyCellStylesToRows() {
    var doc = app.activeDocument;
    var rws = doc.stories.everyItem().tables.everyItem().rows.everyItem().getElements();
    for (var i = 0; i < rws.length; i++){
       rws[i].cells[0].fillColor = "Black";
       rws[i].cells[0].fillTint = 10;
       rws[i].cells[0].contents = "Row: " + (i+1).toString() + " (cell count "+ rws[i].cells.length + ")"
    };   
}

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

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

@rob day 

 

Then in VB it's much "nicer"...

 

But .name property should return "address" of the Cell - "col:row" - at least it works that way in VB.

 

Now it's only the case of how to use it in the same way to get a specific cell?

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

OK, this works:

 

function main() {
    var doc = app.activeDocument;
    app.select(doc.stories[0].tables[0].cells.itemByName("1:1"));
};

main();

 

This will select the same Cell:

 

RobertTkaczyk_0-1713892979828.png

 

 

@stevecole, this should work for you:

var row = cell.parentRow.index;
for (var c = 0; m < table.columns.length; c++) {
    table.cells.itemByName(c + ":" + row).appliedCellStyle = weekendCellStyle;
}

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

RobertTkaczyk_0-1713893564482.png

 

function CellAddress() {
    var doc = app.activeDocument;
    var table = doc.stories[0].tables[0];
    for (var c = 0; c < table.columns.length; c++){
       for (var r = 0; r < table.rows.length; r++){
           table.cells.itemByName(c + ":" + r).contents = table.cells.itemByName(c + ":" + r).contents + " " + c + ":" + r;
       };
    };   
}

CellAddress();

 

Votes

Translate

Translate

Report

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
New Here ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

I couldn’t get @Robert at ID-Tasker ’s suggestion to work, and don’t see a way to cover every possible variation Maybe someone else can help.

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

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

 

Screen Shot 4.png

 


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');

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

quote

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

 

Screen Shot 4.png

 

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.

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

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

 

Screen Shot 5.png

 

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

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 23, 2024 Apr 23, 2024

Copy link to clipboard

Copied

quote

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


By @rob day

 

Right.

 

quote

[...]

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


By @rob day

 

What error?

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

What error?

 

Screen Shot 9.png

 

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

quote

What error?

 

Screen Shot 9.png


By @rob day

 

Because it should be "c <" - not "m <". 

 

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

 

Because it should be "c <" - not "m <".

 

No error, but I get this:

 

Screen Shot 10.png

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

quote

 

Because it should be "c <" - not "m <".

 

No error, but I get this:

 

Screen Shot 10.png


By @rob day

 

I think that's correct? 

 

Unless, the goal is to style unmerged cells - in a row WITH merged cells - as well? 

 

As per @stevecole opening post:

 

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

 

Votes

Translate

Translate

Report

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