troldal / OpenXLSX

A C++ library for reading, writing, creating and modifying Microsoft Excel® (.xlsx) files.
BSD 3-Clause "New" or "Revised" License
1.35k stars 326 forks source link

OpenXLSX::XLDocument is not saving the same string into another workbook #283

Open NK01 opened 4 days ago

NK01 commented 4 days ago

The issue example:

OpenXLSX::XLDocument doc; // this is used to create and save the workbook
std::vector<std::vector<std::string>> data; // this contains the data to be saved in 2D matrix for easy insertion into excel

doc.create(workbook1, true);
OpenXLSX::XLWorksheet wks = doc.workbook().worksheet("Sheet1");
// Inserting data into the workbook
for (std::size_t i = 0; i < data.size(); ++i) 
{
    for (std::size_t j = 0; j < data[i].size(); ++j)
    {
        wks.cell(OpenXLSX::XLCellReference(static_cast<uint16_t>(i) + 1, static_cast<uint16_t>(j) + 1)).value() = data[i][j];
    }
}
doc.save()
doc.close()

doc.create(workbook2, true);
wks = doc.workbook().worksheet("Sheet1");
// Inserting data into the workbook
for (std::size_t i = 0; i < data.size(); ++i) 
{
    for (std::size_t j = 0; j < data[i].size(); ++j)
    {
        wks.cell(OpenXLSX::XLCellReference(static_cast<uint16_t>(i) + 1, static_cast<uint16_t>(j) + 1)).value() = data[i][j];
    }
}
doc.save()
doc.close()

In the above example the second workbook does not contain any data, but if I change some data in the std::vector<std::vector<std::string>> data matrix then the changed data is inserted into the second matrix but not anything else

Currently the workaround is to create a new OpenXLSX::XLDocument doc like this:

OpenXLSX::XLDocument doc; // this is used to create and save the workbook
std::vector<std::vector<std::string>> data; // this contains the data to be saved in 2D matrix for easy insertion into excel

doc.create(workbook1, true);
OpenXLSX::XLWorksheet wks = doc.workbook().worksheet("Sheet1");
// Inserting data into the workbook
for (std::size_t i = 0; i < data.size(); ++i) 
{
    for (std::size_t j = 0; j < data[i].size(); ++j)
    {
        wks.cell(OpenXLSX::XLCellReference(static_cast<uint16_t>(i) + 1, static_cast<uint16_t>(j) + 1)).value() = data[i][j];
    }
}
doc.save()
doc.close()

OpenXLSX::XLDocument doc2;
doc2.create(workbook2, true);
wks = doc2.workbook().worksheet("Sheet1");
// Inserting data into the workbook
for (std::size_t i = 0; i < data.size(); ++i) 
{
    for (std::size_t j = 0; j < data[i].size(); ++j)
    {
        wks.cell(OpenXLSX::XLCellReference(static_cast<uint16_t>(i) + 1, static_cast<uint16_t>(j) + 1)).value() = data[i][j];
    }
}
doc2.save()
doc2.close()

then everything works as expected. Is the first way not correct?

aral-matrix commented 3 days ago

Hi @NK01 & thank you for reporting this issue. It sounds like a bug: I see nothing wrong with re-using the XLDocument variable like you do. I will have a look to understand why the document seems to not properly "reset" on ::create, when I find some time - I am currently quite busy & I hope this isn't too urgent, since you have a workaround.

My apologies if this takes a few weeks for me to address.

NK01 commented 3 hours ago

Thank you for the response and yeah it is not urgent, as I have already put the workaround in the code. No worries for the delay, take your time