'Detecting last column and last row with Xlnt library in a xlsx file

I am trying to use the Xlnt library to create and manipulate some xlsx files.
Are there functions in this library that let you know which one it is the last not empty column and the last not empty row?
Here is a code example:

#include <xlnt/xlnt.hpp>
#include <string>
#include <iostream>

int main ()
{
     xlnt::workbook wb;
     xlnt::worksheet ws = wb.active_sheet();
     ws.cell("AB1").value(5);
     ws.cell("B12").value(10);
     ws.cell("C3").formula("=AB1+B12");
     wb.save("example.xlsx");
     std::string lastColumn = ws.last_column();     // exists similar function?
     size_t lastRow = ws.last_row();                // exists similar function?
     std::cout << lastColumn << ' ' << lastRow << std::endl;

     return 0;
}

Reading the code it is clear that the last column is "AB" and the last row is 12, but obviously the program does not know which cells have contents of any type.



Solution 1:[1]

From the docs there is a end iterator returning one past the last row.

https://github.com/tfussell/xlnt/blob/master/include/xlnt/worksheet/worksheet.hpp#L630

So something like this should probably work:

 xlnt::worksheet ws = wb.active_sheet();
 const auto last_row = *std::prev(ws.end());

If there is no row inside the sheet this will probably be undefined behaviour, so please add error checking accordingly.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1