Spreadsheet: Add Python API for getUsedCells

Also adds access to getNonEmptyCells, and unit tests for both. Designed
to fix #7587.
This commit is contained in:
Chris Hennes
2022-11-03 22:50:41 -05:00
parent b444ae94d5
commit 11dd7fc5f9
3 changed files with 76 additions and 0 deletions

View File

@@ -180,5 +180,26 @@ following dependency order.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getUsedCells">
<Documentation>
<UserDocu>
getUsedCells()
Get a list of the names of all cells that are marked as used. These cells may
or may not have a non-empty string content.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="getNonEmptyCells">
<Documentation>
<UserDocu>
getNonEmptyCells()
Get a list of the names of all cells with data in them.
</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@@ -980,6 +980,27 @@ PyObject *SheetPy::recomputeCells(PyObject *args) {
}PY_CATCH;
}
PyObject *SheetPy::getUsedCells(PyObject *args)
{
auto usedCells = getSheetPtr()->getCells()->getUsedCells();
Py::List pyCellList;
for (const auto &cell : usedCells) {
pyCellList.append(Py::String(cell.toString()));
}
return Py::new_reference_to(pyCellList);
}
PyObject *SheetPy::getNonEmptyCells(PyObject *args)
{
auto usedCells = getSheetPtr()->getCells()->getNonEmptyCells();
Py::List pyCellList;
for (const auto &cell : usedCells) {
pyCellList.append(Py::String(cell.toString()));
}
return Py::new_reference_to(pyCellList);
}
// +++ custom attributes implementer ++++++++++++++++++++++++++++++++++++++++
PyObject *SheetPy::getCustomAttributes(const char*) const

View File

@@ -1345,6 +1345,40 @@ class SpreadsheetCases(unittest.TestCase):
with self.assertRaises(AttributeError):
self.assertEqual(ss1.B1, "fail")
def testGetUsedCells(self):
sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
test_cells = ['B13','C14','D15']
for i,cell in enumerate(test_cells):
sheet.set(cell,str(i))
used_cells = sheet.getUsedCells()
self.assertEqual(len(used_cells), len(test_cells))
for cell in test_cells:
self.assertTrue(cell in used_cells)
for cell in test_cells:
sheet.set(cell,"")
sheet.setAlignment(cell,"center")
non_empty_cells = sheet.getUsedCells()
self.assertEqual(len(non_empty_cells), len(test_cells)) # Alignment counts as "used"
def testGetNonEmptyCells(self):
sheet = self.doc.addObject('Spreadsheet::Sheet','Spreadsheet')
test_cells = ['B13','C14','D15']
for i,cell in enumerate(test_cells):
sheet.set(cell,str(i))
non_empty_cells = sheet.getNonEmptyCells()
self.assertEqual(len(non_empty_cells), len(test_cells))
for cell in test_cells:
self.assertTrue(cell in non_empty_cells)
for cell in test_cells:
sheet.set(cell,"")
sheet.setAlignment(cell,"center")
non_empty_cells = sheet.getNonEmptyCells()
self.assertEqual(len(non_empty_cells), 0) # Alignment does not count as "non-empty"
def tearDown(self):
#closing doc
FreeCAD.closeDocument(self.doc.Name)