Skip to main content
Fightergator
Inspiring
July 19, 2022
Answered

Using ExtendSript to Apply Background Color to Table

  • July 19, 2022
  • 2 replies
  • 612 views

I trying to apply the "None" background (Character Designer) to tables in a document.  Some searches I run leave text within the tables with background highlighting. While I can select the entire document and apply the "None" background to the body text, it doesn't change the highlighting in the Tables.

 

I have a script that steps to each table in the document and selects it, but can't figure out how to apply the desired background color after it's selected.  Any suggestion?  

This topic has been closed for replies.
Correct answer Fightergator

Ok, I see. I would suggest that you create a Character Format that just sets the background color to what you want. Then you can use this function to apply it to each paragraph in the table.

function applyCharFmt (textRange, name, doc) {

    var charFmt, prop;
    
    charFmt = doc.GetNamedCharFmt (name);
    if (charFmt.ObjectValid () === 1) {
        doc.SetTextProps (textRange, charFmt.GetProps ());
    }
    else {
        prop = new PropVal ();
        prop.propIdent.num = Constants.FP_CharTag;
        prop.propVal.valType = Constants.FT_String;
        prop.propVal.sval = name;
        doc.SetTextPropVal (textRange, prop);
    }
}

But you still have to navigate each cell in the table and each paragraph in the cell. Something like this:

var cell, pgf;
// Assuming tbl is the table object.
cell = tbl.FirstRowInTbl.FirstCellInRow;
while (cell.ObjectValid () === 1) {
    if (cell.CellIsStraddled === 0) {
        pgf = cell.FirstPgf;
        while (pgf.ObjectValid () === 1) {
            // Make a text range and pass it to the applyCharFmt function.
            // ...
            pgf = pgf.NextPgfInFlow;
        }
    }
    cell = cell.NextCellInTbl;
}
            

To get the TextRange to pass to the applyCharFmt function for each paragraph, use this:

var textRange;

textRange = new TextRange (new TextLoc (pgf, 0),
    new TextLoc (pgf, Constants.OBJ_END_OFFSET - 1));
applyCharFmt (textRange, "whatever-you-named-your-Character-Format", doc);

I don't have time to test the code, but it should be pretty clean.


Thanks Rick...I really appreciate it.  I'll load your code tomorrow and check it out.  Hopefully won't have to bother you with that issue again.  r/Curt

2 replies

frameexpert
Community Expert
Community Expert
July 20, 2022

Look in the Table Designer to see how you apply shading to the entire table in the FrameMaker interface. You do it by shading rows or columns, not the entire table at once. To shade the entire table, you need something like this:

You do the same thing programmatically using these properties:

Instead of processing each Cell object, you are processing properties of the entire Tbl object.

Fightergator
Inspiring
July 20, 2022

Rick...If |I manually select the entire table (see below) then go to the Character Designer and apply a background color, it background colors all text in the table in one action without stepping through each column or row. I may not have explained it right (I wasn't look trying to shade each cell).  I'm to background color all the text in the table...Character Designer --> Background --> Apply.

frameexpert
Community Expert
Community Expert
July 20, 2022

Ok, I see. I would suggest that you create a Character Format that just sets the background color to what you want. Then you can use this function to apply it to each paragraph in the table.

function applyCharFmt (textRange, name, doc) {

    var charFmt, prop;
    
    charFmt = doc.GetNamedCharFmt (name);
    if (charFmt.ObjectValid () === 1) {
        doc.SetTextProps (textRange, charFmt.GetProps ());
    }
    else {
        prop = new PropVal ();
        prop.propIdent.num = Constants.FP_CharTag;
        prop.propVal.valType = Constants.FT_String;
        prop.propVal.sval = name;
        doc.SetTextPropVal (textRange, prop);
    }
}

But you still have to navigate each cell in the table and each paragraph in the cell. Something like this:

var cell, pgf;
// Assuming tbl is the table object.
cell = tbl.FirstRowInTbl.FirstCellInRow;
while (cell.ObjectValid () === 1) {
    if (cell.CellIsStraddled === 0) {
        pgf = cell.FirstPgf;
        while (pgf.ObjectValid () === 1) {
            // Make a text range and pass it to the applyCharFmt function.
            // ...
            pgf = pgf.NextPgfInFlow;
        }
    }
    cell = cell.NextCellInTbl;
}
            

To get the TextRange to pass to the applyCharFmt function for each paragraph, use this:

var textRange;

textRange = new TextRange (new TextLoc (pgf, 0),
    new TextLoc (pgf, Constants.OBJ_END_OFFSET - 1));
applyCharFmt (textRange, "whatever-you-named-your-Character-Format", doc);

I don't have time to test the code, but it should be pretty clean.

Inspiring
July 19, 2022
Fightergator
Inspiring
July 20, 2022

Thanks for the point out, but doesn't work in my situation.  I want to apply the background color to the entire table, and not to individual cells.  I supposed I could step through each cell, but that seems to be rather time consuming.  Thanks Again...