Apply Style to Table Cell Text using Extendscript CC

New Here ,
May 18, 2016

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?

TOPICS
Scripting

Views

395

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

1 Correct Answer

Mentor , May 19, 2016
Russ Ward Mentor , May 19, 2016
Hi Green,I don't have time to fully debug this script, but I do see some things that seem unusual.    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.e...

Likes

Translate

Translate
Jump to answer Jump to answer
Mentor ,
May 19, 2016

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.

  1.     var result = pgf.GetText(Constants.FTI_TblAnchor); 
  2.     var all_text = ''
  3.     for (var i = 0; i < result.length; i += 1) { 
  4.         var t_object = result.obj; 
  5.         var t_cell = t_object.FirstRowInTbl.FirstCellInRow; 
  6.         while(t_cell.ObjectValid()){ 
  7.             var cell_items = t_cell.GetText(Constants.FTI_String); 
  8.             var tr = new TextRange(); 
  9.              tr.beg.obj = tr.end.obj = pgf; 

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:

  1. var t_cell = t_object.FirstRowInTbl.FirstCellInRow; 
  2. while(t_cell.ObjectValid()){ 
  3.    var cellPgf = t_cell.FirstPgf;
  4.    var cell_items = cellPgf.GetText(Constants.FTI_String); 
  5.    var tr = new TextRange();
  6.    tr.beg.obj = tr.end.obj = cellPgf;

Hope this helps some.

Russ

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Green_Chili AUTHOR LATEST
New Here ,
May 19, 2016

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!

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more