Copy link to clipboard
Copied
Suppose, I've selected some fragment in my document, and I need to find and replace some text in it. Everything is ok, untill I select a part of a table with this text. Does anyone has idea, how to search through the selected cells in the table? Or, just how to iterate them?
Thanks!
Copy link to clipboard
Copied
Here is how to interate through table cells in the selected table:
var doc = app.ActiveDoc;
var tbl = doc.SelectedTbl;
var cell = tbl.FirstRowInTbl.FirstCellInRow;
while (cell.ObjectValid() === 1) {
// Do something with the cell object here.
cell = cell.NextCellInTbl;
}
- Rick
Copy link to clipboard
Copied
Thanks for the answer, but isn't it iterating through entire table?
Copy link to clipboard
Copied
var doc = app.ActiveDoc;
var tbl = doc.SelectedTbl;
// Navigate to the first selected row.
var row = tbl.FirstRowInTbl;
while (row.ObjectValid() === 1) {
if (row.id === tbl.TopRowSelection.id) {
break;
}
row = row.NextRowInTbl;
}
// Process the selected rows.
while (row.ObjectValid() ===1) {
cell = row.FirstCellInRow;
while (cell.ObjectValid() === 1) {
if ((cell.CellColNum >= tbl.LeftColNum) && (cell.CellColNum <= tbl.RightColNum)) {
// Do something here...
}
cell = cell.NextCellInRow;
}
if (row.id === tbl.BottomRowSelection.id) {
break;
}
row = row.NextRowInTbl;
}
This should do what you want.
Rick
Copy link to clipboard
Copied
Thanks, that seems way closer to the solution, but there still few things left.
1. Just for case, can we select row partly? And if yes, how should we act then?
2. What if there're more than 1 table in the selection?
Copy link to clipboard
Copied
1. The script already does this; it only processes the cells that are selected.
2. If there are more than one table in the selection, then you are basically back to interating through all of the cells in each table.
Here is how you can get a list of the tables that are selected in document:
var doc = app.ActiveDoc;
// Get all of the tables that are selected.
var textList = doc.GetTextForRange (doc.TextSelection, Constants.FTI_TblAnchor);
alert (textList.len);
I don't have time right now to give you any more details, but I may be able to later.
-Rick