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
Copy link to clipboard
Copied
Mind posting your jsx so we can more effectively troubleshoot?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Sorry Robert I replied to your personal email by mistake.
regards
Steve
Copy link to clipboard
Copied
Hi Robert
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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.
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)
By @rob day
So addresses would be like that?
Copy link to clipboard
Copied
This numbers the first cell of each row and gets the number of cells in the row:
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');
Copy link to clipboard
Copied
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?
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:
@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;
}
Copy link to clipboard
Copied
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();
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
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.
Copy link to clipboard
Copied
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');
Copy link to clipboard
Copied
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.
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
Your approach might be better, but the snippet you posted is throwing an error when I try it with @stevecole’s code.
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
By @rob day
Right.
[...]
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?
Copy link to clipboard
Copied
What error?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Because it should be "c <" - not "m <".
No error, but I get this:
Copy link to clipboard
Copied
Because it should be "c <" - not "m <".
No error, but I get this:
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.