• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Using ExtendSript to Apply Background Color to Table

Contributor ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

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?  

TOPICS
Scripting

Views

299

Translate

Translate

Report

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

correct answers 2 Correct answers

Community Expert , Jul 20, 2022 Jul 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_Ch
...

Votes

Translate

Translate
Contributor , Jul 20, 2022 Jul 20, 2022

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

Votes

Translate

Translate
Explorer ,
Jul 19, 2022 Jul 19, 2022

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

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:

frameexpert_0-1658339697049.png

You do the same thing programmatically using these properties:

frameexpert_1-1658339805728.png

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

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.

Table.jpg

Panel.jpg

Result.jpg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 20, 2022 Jul 20, 2022

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

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