Skip to main content
K.Daube
Community Expert
Community Expert
October 31, 2016
Answered

Get row and col nr of current cell

  • October 31, 2016
  • 1 reply
  • 612 views

Dear all,

For absolute addressing a table cell out of the current cell I need to know where I am.

I have not found a proporty like CurrentRow or CellRowNum. CellRow is an object Row.

// CellNumbering.jsx
// have cursor in a table cell
#target framemaker
  oDoc  = app.ActiveDoc;
  oTbl  = oDoc.SelectedTbl;
  oPgf  = oDoc.TextSelection.beg.obj; 
  oCell = oPgf.InTextObj;
 
//jRow  = oCell.???;              // ???
  jCol  = oCell.CellColNum;       // 0 ... n
$.writeln (oTbl.TblNumCols);      // 4
$.writeln (jCol);                 // 2

Is it necessary to loop through all rows and cells to find the currentselection => current location found?

This topic has been closed for replies.
Correct answer frameexpert

Hi Klaus,

Rows don't have a number property, so you would have to start at the top of the table and count down to the current row. You could use something like this:

#target framemaker

var doc = app.ActiveDoc;

var tbl = doc.SelectedTbl;

var pgf = doc.TextSelection.beg.obj;

var cell = pgf.InTextObj;

var row = cell.CellRow;

// Get the row number (first row is 0).

var rowNum = getRowNum (tbl, row);

alert (rowNum);

function getRowNum (tbl, currRow) {

   

    var row = tbl.FirstRowInTbl;

    var counter = 0;

   

    while (row.ObjectValid () === 1) {

        if (row.id === currRow.id) {

            return counter;

        }

        counter += 1;

        row = row.NextRowInTbl;

    }

}

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
October 31, 2016

Hi Klaus,

Rows don't have a number property, so you would have to start at the top of the table and count down to the current row. You could use something like this:

#target framemaker

var doc = app.ActiveDoc;

var tbl = doc.SelectedTbl;

var pgf = doc.TextSelection.beg.obj;

var cell = pgf.InTextObj;

var row = cell.CellRow;

// Get the row number (first row is 0).

var rowNum = getRowNum (tbl, row);

alert (rowNum);

function getRowNum (tbl, currRow) {

   

    var row = tbl.FirstRowInTbl;

    var counter = 0;

   

    while (row.ObjectValid () === 1) {

        if (row.id === currRow.id) {

            return counter;

        }

        counter += 1;

        row = row.NextRowInTbl;

    }

}

www.frameexpert.com
4everJang
Legend
October 31, 2016

function getRowNum( currRow )

{

     var i = 0;

     while( currRow.PrevRowInTbl.ObjectValid( ) )

     {

          currRow = currRow.PrevRowInTbl;

          i++;

     }

     return i;

}

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
November 1, 2016

Many thanks both to Ric and Jang!

I could mark only 1 as Correct Answer

I will make a CurrentCellNum function to have the indexing the same for both row and column.

  jRow  = GetRowNum (oRow);               // 1 ... n
  jCol  = oCell.CellColNum;               // 0 ... m

This would be prone to erraneous use.