Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,16 +287,18 @@ public OdfTableCell getCellByIndex(int index) {
* @return the cell count
*/
public int getCellCount() {
OdfTable table = getTable();
Set<OdfTableCell> realCells = new HashSet<>();
List<CellCoverInfo> coverList =
table.getCellCoverInfos(0, 0, table.getColumnCount() - 1, table.getRowCount() - 1);
int rowIndex = getRowIndex();
for (int i = 0; i < table.getColumnCount(); i++) {
OdfTableCell cell = table.getOwnerCellByPosition(coverList, i, rowIndex);
realCells.add(cell);
// count cells by skipping covered-table-cell and taking into account number-columns-repeated attribute
int cellCount = 0;
for (Node node : new DomNodeList(maRowElement.getChildNodes())) {
if (node instanceof TableTableCellElement tableCell) {
if (tableCell.getTableNumberColumnsRepeatedAttribute() == null) {
cellCount++;
} else {
cellCount += tableCell.getTableNumberColumnsRepeatedAttribute();
}
}
}
return realCells.size();
return cellCount;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.odftoolkit.odfdom.doc.table;

import static org.junit.Assert.assertEquals;
import static org.odftoolkit.odfdom.utils.ResourceUtilities.getAbsoluteInputPath;

import junit.framework.AssertionFailedError;
import org.junit.Test;
import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;

public class TableCellCountTest {

// The number of columns that Excel always uses.
// For example Excel puts <table:table-cell table:number-columns-repeated="16384"/> if no cell values or covered cells
private static final int EXCEL_COLUMN_COUNT = 16384;

@Test
public void verifyCellCountForLibreOfficeGeneratedSpreadsheet() {
// Spreadsheet created by LibreOffice on Mac version 25.8.4.2 (AARCH64)
try (OdfSpreadsheetDocument spreadsheet = loadSpreadsheetDocument("TestLibreOfficeSpreadsheetTableCellCount.ods")) {
OdfTable sheet = spreadsheet.getSpreadsheetTables().get(0);
assertEquals(3, sheet.getColumnCount());

// 3 cells merged so 2 covered cells
assertEquals(3 - 2 /* covered cells */, sheet.getRowByIndex(0).getCellCount());

// 2 cells merged so 1 covered cell
assertEquals(3 - 1 /* covered cell */, sheet.getRowByIndex(1).getCellCount());

// no merged cells
assertEquals(3, sheet.getRowByIndex(2).getCellCount());

// 2 cells merged over 2 rows (simplified XML):
// <table:table-row>
// <table:table-cell table:number-columns-spanned="2" table:number-rows-spanned="2">
// <text:p>1</text:p>
// </table:table-cell>
// <table:covered-table-cell/>
// <table:table-cell>
// <text:p>2</text:p>
// </table:table-cell>
// </table:table-row>
// <table:table-row>
// <table:covered-table-cell table:number-columns-repeated="2"/>
// <table:table-cell>
// <text:p>2</text:p>
// </table:table-cell>
// </table:table-row>
// so 1 covered cell in first row
assertEquals(3 - 1 /* covered cell */, sheet.getRowByIndex(3).getCellCount());
// ... and 2 covered cells in second row
assertEquals(3 - 2 /* covered cells */, sheet.getRowByIndex(4).getCellCount());
}
}

@Test
public void verifyCellCountForExcelGeneratedSpreadsheet() {
// Spreadsheet created by Microsoft® Excel for Mac Version 16.106 (26020821)
try (OdfSpreadsheetDocument spreadsheet = loadSpreadsheetDocument("TestExcelSpreadsheetTableCellCount.ods")) {
OdfTable sheet = spreadsheet.getSpreadsheetTables().get(0);
assertEquals(EXCEL_COLUMN_COUNT, sheet.getColumnCount());

// 3 cells merged so 2 covered cells
assertEquals(EXCEL_COLUMN_COUNT - 2 /* covered cells */, sheet.getRowByIndex(0).getCellCount());

// 2 cells merged so 1 covered cell
assertEquals(EXCEL_COLUMN_COUNT - 1 /* covered cell */, sheet.getRowByIndex(1).getCellCount());

// no merged cells
assertEquals(EXCEL_COLUMN_COUNT, sheet.getRowByIndex(2).getCellCount());

// 2 cells merged over 2 rows so 1 covered cell in first row
assertEquals(EXCEL_COLUMN_COUNT - 1 /* covered cell */, sheet.getRowByIndex(3).getCellCount());
// ... and 2 covered cells in second row
assertEquals(EXCEL_COLUMN_COUNT - 2 /* covered cells */, sheet.getRowByIndex(4).getCellCount());

// Excel always adds
// <table:table-row table:number-rows-repeated="1048573" table:style-name="ro1">
// <table:table-cell table:number-columns-repeated="16384"/>
// </table:table-row>
assertEquals(EXCEL_COLUMN_COUNT, sheet.getRowByIndex(5).getCellCount());
}
}

private OdfSpreadsheetDocument loadSpreadsheetDocument(String filename) {
try {
return OdfSpreadsheetDocument.loadDocument(
getAbsoluteInputPath(filename));
} catch (Exception ex) {
throw new AssertionFailedError(ex.getMessage());
}
}
}
Binary file not shown.
Binary file not shown.