Copy link to clipboard
Copied
Hello,
I have the following code. I'm attempting to parse a document and search for specific keywords based on regex. This part is working. I then get the text range for those keywords and highlight the text. This works. What doesn't work is when I try to do the same thing in text that is in a Table Cell.
#target framemaker
var doc = app.ActiveDoc;
var totalPgfs = 0;
var pgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
function do_table_work(pgf){
var result = pgf.GetText(Constants.FTI_TblAnchor);
var all_text = '';
for (var i = 0; i < result.length; i += 1) {
var t_object = result.obj;
var t_cell = t_object.FirstRowInTbl.FirstCellInRow;
while(t_cell.ObjectValid()){
var cell_items = t_cell.GetText(Constants.FTI_String);
var tr = new TextRange();
tr.beg.obj = tr.end.obj = pgf;
tr.beg.offset = 0; //this is built dynamically
tr.end.offset = 7; //this is built dynamically
apply_char_format(text_range)
t_cell = t_cell.NextCellInTbl;
}
}
}
function apply_char_format(text_range, name) {
var color = doc.GetNamedColor(name);
if (color.ObjectValid()) {
var set_val = new TypedVal();
set_val.valType = Constants.FT_Integer;
set_val.ival = true;
doc.SetTextVal(text_range, Constants.FP_UseBkColor, set_val);
set_val.valType = Constants.FT_Id;
set_val.obj = color;
doc.SetTextVal(text_range, Constants.FP_BkColor, set_val);
} else {
alert('Color does not exist');
}
}
while (pgf.ObjectValid()) {
do_table_work(pgf);
pgf = pgf.NextPgfInFlow;
}
The problem is it's not highlighting the text in the table cell like I want it to. If I process the document, and use the following:
var result = pgf.GetText(Constants.FTI_String);
It will highlight the text (with additional code to loop through those results).
Any thoughts on what I'm missing?
Hi Green,
I don't have time to fully debug this script, but I do see some things that seem unusual.
Copy link to clipboard
Copied
Hi Green,
I don't have time to fully debug this script, but I do see some things that seem unusual.
While GetText() is valid for a table cell, I'm not sure if that is what you want. I think I would drill down to individual paragraphs. I'm not completely clear on what this code wants to do exactly, but it seems that this would make it much easier to set up text ranges. GetText() will cover all paragraphs in a cell, if there are multiple.
Added to this, your line "tr.beg.obj = tr.end.obj = pgf;" doesn't make any sense to me, because you are going back to the paragraph that contained the table anchor. In other words, you just left the table entirely.
So, this is just some guessing, but it seems more likely that you would want something along these lines:
Hope this helps some.
Russ
Copy link to clipboard
Copied
Hey Russ,
You actually helped me find the problem. The line:
tr.beg.obj = tr.end.obj = pgf;
Should be:
tr.beg.obj = tr.end.obj = cellPgf;
This resolved my issue. Thank you for the assistance!