Skip to main content
Inspiring
September 12, 2019
Question

Extendscript to change font size in table

  • September 12, 2019
  • 1 reply
  • 1587 views

using this code to change the table column width and apply font size.

width works but font size not changing.

 

var doc = app.ActiveDoc;
var flow = doc.MainFlowInDoc;
var tbl = 0;
var textItems = flow.GetText(Constants.FTI_TblAnchor);
for (var i = 0; i < textItems.len; i += 1)
{ tbl = textItems[i].obj;

var tblColWidths = new Metrics (5 * 15 * 65536, 5 * 15 * 65536, 5*25*65536, 5*35*65536, 5*15*65536); tbl.TblColWidths = tblColWidths;
var FontSize = (8);
}
var doc = app.ActiveDoc;
var flow = doc.MainFlowInDoc;
{
var textDocument = textProp.value;

textDocument.font = "Arial";
textDocument.FontSize = (8);

doc.setValue(textDocument);
}

    This topic has been closed for replies.

    1 reply

    K.Daube
    Community Expert
    Community Expert
    September 13, 2019

    Well, Suryanarayanan_21, the font business is not that transparent.

    Modiying the ¶ format just does this in the catalogue. Hence afterward you need to run through the table and assign the modified format to the cell paragraphs:

     

     

    // table_font_change.jsx
    // Document must be active !
    // Only the first table in document is handled - and in this only the first ¶ in cells
    #target framemaker
    main ();                                          // ease debugging
    
    function main () {
    var oDoc, oFlow, oTbl, textItems, tblColWidths, textDocument, PT, FontSize, oPgfFmt;
      PT = 65536;                                     // use a named constant
      oDoc = app.ActiveDoc;
      oFlow = oDoc.MainFlowInDoc;
      textItems = oFlow.GetText(Constants.FTI_TblAnchor);
    // Set the column widths
      for (var i = 0; i < textItems.len; i += 1) {
        oTbl = textItems[i].obj;
        tblColWidths = new Metrics (5 * 15 * PT, 5 * 15 * PT, 5*25*PT, 5*35*PT, 5*15*PT);
        oTbl.TblColWidths = tblColWidths;
      }
    // Change font for the paragraph format CellBody (this changes the catalogue only)
      oPgfFmt = oDoc.GetNamedPgfFmt("CellBody");  
      if (oPgfFmt.ObjectValid()) {  
        oPgfFmt.FontFamily = GetFontFamilyIndex ("Arial"); 
        oPgfFmt.FontSize   = 8 * PT;
      }
    // Apply the correct(ed) ¶ format to all cells
      SetTblCellParaFmt (oDoc, oTbl, "CellBody" );
    }
    
    function SetTblCellParaFmt (oDoc, oTbl, sPgfFmt ) { // == Apply ¶ to all cells in table ===========
    // Arguments  oDoc    Current document
    //            oTbl    Current table
    //            sPgfFmt Paragraph format name to apply
    // Returns   -
    var oRow, oCell, oPgf;
      oRow = oTbl.FirstRowInTbl;
      while (oRow.ObjectValid()) {
        if (oRow.RowType === Constants.FV_ROW_HEADING || oRow.RowType === Constants.FV_ROW_FOOTING) {
          oRow = oRow.NextRowInTbl;  
          continue;
        }
        oCell = oRow.FirstCellInRow;
        while (oCell.ObjectValid()) {
          oPgf = oCell.FirstPgf;
          ApplyPgfFmt (oDoc, oPgf, sPgfFmt) 
          oCell = oCell.NextCellInRow;
        }
        oRow = oRow.NextRowInTbl;  
      }
    } //--- end TblCurrBodyRowNum
    
    function ApplyPgfFmt (oDoc, oPgf, sPgfFmt) { // === Apply paragraph format ========================
    // Arguments  oDoc     Current document
    //            oPgf     ¶ to get the new format
    //            sPgfFmt  Name of pgf format to be applied
    var oPgfFmt = 0, oProps;
    
      oPgfFmt = oDoc.GetNamedPgfFmt(sPgfFmt);  
      if (oPgfFmt.ObjectValid()) {  
        oProps = oPgfFmt.GetProps();  
        oPgf.SetProps(oProps);  
      } else {  
        oPgf.Name = sPgfFmt;                          
      }
    } //--- end ApplyPgfFmt
    
    function GetFontFamilyIndex (sName) { // === Get the index in FontFamilyNames =====================
    // Arguments  sName    name of fontweight to be found (case indepently)
    // Returns    Index of font families available in the current session; or null if not found
    // Comment    The list of font families depends on your installation: On my it starts with
    //            <Reserved>, Modern, Hobo Std, OCR A Std, Adobe Arabic, Adobe Devanagari, 
    //            Adobe Hebrew, TeamViewer13, Barbedor FS, Barbedor FS Caps, Barbedor FS Med, 
    //            Marlett, Arial, Arabic Transparent, ...
    var j, aNames = app.FontFamilyNames;              // array
    //for (j = 0; j < aNames.length; j++) {
    //  $.writeln (aNames[j]);
    //}
      j = GetIndexFromName (aNames, sName); 
      return j;
    } //--- end Fgr_GetFontFamilyIndex  
    
    function GetIndexFromName (aArray, sSearch) { //=== Get Index of of particular contents ===========
    // Arguments  aArray   array of ini entries to be searched
    //            sSearch  may be part of what to find - only first occurence in array element is found
    // Returns    Index of found item - or null
    // Comment    Character case and blanks are ignored in both source item and search item
    //            aArray may contain placeholder null items which are ignored.
    var j, jFound, saLen, sItem, sSearch;
      saLen     = aArray.length;
      sSearch   = sSearch.replace (" ", "");
      sSearch   = sSearch.toUpperCase();
      for (j = 0; j < saLen; j++) {
        if (aArray[j] != null) {
          sItem = aArray[j].toUpperCase();
          sItem   = sItem.replace (" ", "");
          jFound = sItem.indexOf(sSearch);
          if (sItem.indexOf(sSearch) == 0) { // found at the beginning
            return j;
          }
        }
      }
      return null;
    } //--- end GetIndexFromName

     

     

    BTW: I have never seen an assignment like this:

     

     

    var FontSize = (8);

     

     

    HTH Klaus Daube

    Inspiring
    September 14, 2019

    Thank you so much!!! that works perfectly..

    is there a chance for apply this to all ¶ in the cell?

     

     

    and in this only the first ¶ in cells