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

Extendscript to change font size in table

Explorer ,
Sep 12, 2019 Sep 12, 2019

Copy link to clipboard

Copied

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);
}

Views

959

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 ,
Sep 13, 2019 Sep 13, 2019

Copy link to clipboard

Copied

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

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
Explorer ,
Sep 14, 2019 Sep 14, 2019

Copy link to clipboard

Copied

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

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
Explorer ,
Sep 14, 2019 Sep 14, 2019

Copy link to clipboard

Copied

Also i dont understand the need of GetFontFamilyIndex and GetIndexFromName function

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 ,
Sep 14, 2019 Sep 14, 2019

Copy link to clipboard

Copied

  • This GetFontFamilyIndex is necessary, because the fonts are maintained by the OS (Windows), not by FM. And this is not a linked list at all. As you can see from the comment in the function the order of the font names is also not alphabetical.
  • Function GetIndexFromName is a standard function of me, because (according to many scripters) it is good practice to break down a task to re-useable functions.
  • Wait until tomorrow for an extension of the script to handle all paragraphs (of same ¶ style name CellBody) in a cell. This requires 'just' another inner loop.

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
Explorer ,
Sep 14, 2019 Sep 14, 2019

Copy link to clipboard

Copied

thank you so much once again. i tried adding a simple step adding " oPgf = oCell.NextPgf; " in the code: while (oCell.ObjectValid()) { oPgf = oCell.FirstPgf; ApplyPgfFmt (oDoc, oPgf, sPgfFmt) oPgf = oCell.NextPgf; oCell = oCell.NextCellInRow; it didnt work

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
Explorer ,
Sep 14, 2019 Sep 14, 2019

Copy link to clipboard

Copied

can you also please help me with set the table minimum row height to 0. i used this code

: //Set Ror height for(var i=0; i<textItems.len; i +=1) { oTbl = textItems[i].obj; oTbl.RowMinHeight = 0 * PT; }

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
Mentor ,
Sep 17, 2019 Sep 17, 2019

Copy link to clipboard

Copied

Hi, I think you need: oPgf = oPgf.NextPgfInFlow to ge

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
Mentor ,
Sep 17, 2019 Sep 17, 2019

Copy link to clipboard

Copied

Hi I think you need oPgf = oPgf.NextPgfInFlow to get the next paragraph. NextPgf does not exist for a cell.

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
Explorer ,
Sep 17, 2019 Sep 17, 2019

Copy link to clipboard

Copied

Thnaks for sharing the code

But is my placement is correct?

 

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)
oPgf = oPgf.NextPgfInFlow;
oCell = oCell.NextCellInRow;
}
oRow = oRow.NextRowInTbl;
}
} //--- end TblCurrBodyRowNum

 

it doesn't seem to work

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 ,
Sep 18, 2019 Sep 18, 2019

Copy link to clipboard

Copied

LATEST

Dear Suryanarayanan_21

You need to set up an additional loop inside the cell:

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;
      while (oPgf.ObjectValid()) {                // to be done within the cell
        ApplyPgfFmt (oDoc, oPgf, sPgfFmt) 
        oPgf = oPgf.NextPgfInFlow;
      }
      oCell = oCell.NextCellInRow;
    }
    oRow = oRow.NextRowInTbl;  
  }
} //--- end TblCurrBodyRowNum

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