odftoolkit icon indicating copy to clipboard operation
odftoolkit copied to clipboard

Iterate cells

Open Asbjoedt opened this issue 2 years ago • 2 comments

Hello

I like your toolkit and the OpenDocument format is very easy to understand and save your data in.

I have encountered a problem, whne I try to iterate all cells using the below code:

import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.doc.table.OdfTableRow;
import org.odftoolkit.odfdom.doc.table.OdfTableCell;

        OdfSpreadsheetDocument spreadsheet =  OdfSpreadsheetDocument.loadDocument(filepath);
        List<OdfTable> tables = spreadsheet.getSpreadsheetTables();
        for (OdfTable table : tables) {
            List<OdfTableRow> rows = table.getRowList();
            for (OdfTableRow row : rows) {
                List<OdfTableCell> cells = row.getCellList(); // .getCellList() is not recognized
                for (OdfTableCell cell : cells) {
                    // Do something with cell
                }
            }
        }

This code is not recognized row.getCellList();.

Instead if I use this approach, the iteration works, but it does not iterate over an OdfTableCell data type but it iterates a Node data type, which is not what I want:

import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
import org.odftoolkit.odfdom.doc.table.OdfTable;
import org.odftoolkit.odfdom.doc.table.OdfTableRow;
import org.odftoolkit.odfdom.doc.table.OdfTableCell;

        OdfSpreadsheetDocument spreadsheet =  OdfSpreadsheetDocument.loadDocument(filepath);
        List<OdfTable> tables = spreadsheet.getSpreadsheetTables();
        for (OdfTable table : tables) {
            List<OdfTableRow> rows = table.getRowList();
            for (OdfTableRow row : rows) {
                NodeList cells = row.getOdfElement().getChildNodes();
                for (int i = 0; i < cells.getLength(); i++) {
                    Node cell = cells.item(i);
                    // Do something with cell
                }
            }
        }

So my question is, why is .getCellList() missing when iterating rows and can it be added to the toolkit?

Asbjoedt avatar May 08 '23 09:05 Asbjoedt

apparently class OdfTableRow offers only getCellByIndex() and getCellCount(), whereas class OdfTable has getRowByIndex(), getRowCount() and getRowList().

looks like the OdfTable methods were added in 2011 with 0170c73e0440c3db159be41af71d614bd09fcd03 while OdfTableRow methods are from 2009...

it's not obvious why a getCellList() is missing; it sounds useful, please add it and submit a pull request :)

mistmist avatar May 09 '23 15:05 mistmist

I second this, a pull request would be most welcome! Thanks in advance! :-)

svanteschubert avatar May 24 '23 12:05 svanteschubert