lastDataColumn for merged cells

Hi,

It is possible to return value of getMaxDataColumn/MaxDataRow methods, includes a merged range of cell, that are max?

For example :

I have a book (attachments) and i want to receive a:
1) Max Data Column = 7
2) Max Data Row = 4

@Test
public void maxColumnTets() throws Exception {
Workbook wb = new Workbook("D://lastColumn.xlsx");

Cells cells = wb.getWorksheets().get(0).getCells();

System.out.println(cells.getMaxDataColumn());
System.out.println(cells.getMaxDataRow());
}

I do not needed to receive a last styled cell, or merged. I needed only data cells.

Best regards. Alexey

Hi,


Thanks for your template file, sample code and details.

Well, I am afraid, if we do include merged cells’ range for getMaxDataColumn()/getMaxDataRow() method(s), it won’t help you much. The reason is the merged cell will be holding the first cell’s data only. As per the definition of MaxDataRow and MaxDataColumn attributes, these attributes will consider the cells with data only to obtain row and column indices, so any cell with formattings (e.g borders, back/fore color etc.) won’t be included. When you merge a range of cells, only the data in the first cell (upper left most cell) would be used to make it a single cell (merged) and other cells’ data (in the range) would be lost, you may confirm this behavior in MS Excel too. So, if we do include the other cells (except for the main (left-most) cell) for the attributes, it won’t help much to your cause as the data in other cells have been lost already.

Thank you.

Hi. Thanks for answer, but i don’t needed to receive data of another cell. I needed only count of column.


Basicaly, i render a HTML table and if i will render only one column, then i will have no enough place for text. I needed 6 columns for colspan attribute set.

So, is very complicated to add API function something getLastDataColumn/Row, that will returns data column/row + span (or add optional parameter to getaMaxDataColumn/Row, that will change behavior)?

Best regards. Alexey

Hi,


I am afraid, there is no better way to cope with it and you have to write your own code to accomplish your custom oriented task. For example, I have written a simple code snippet that will check if the last cell is merged. And, if the cell is merged, we can calculate the columns’ range to be added to the MaxDataColumn attribute to get the final/updated value accordingly.
e.g
Sample code:

String filePath = “lastColumn.xlsx”;

Workbook wb = new Workbook(filePath);
Worksheet ws = wb.getWorksheets().get(0);

//Get max data column
int maxdCol = ws.getCells().getMaxDataColumn();
int drow = 1;

//Get the merged cell
Cell mCell = ws.getCells().get(maxdCol, drow);

//If last cell is merged then calculate its merged range accordingly.
if (mCell.isMerged())
{
Range rng = mCell.getMergedRange();

int idxCol = mCell.getColumn() + rng.getColumnCount();
//int idxRow = mCell.getRow() + rng.getRowCount();//if required.
int newmaxdCol = idxCol-1;
System.out.println(newmaxdCol); //7 - Ok
}

Hope, this helps a bit.

Thank you.