Skip to content

Testing if a cell is empty with empty() #333

@r10kindsofpeople

Description

@r10kindsofpeople

Not sure if this qualifies as a bug, or just user error. After finding the definition of class XLCell in XLCell.hpp and:

       /**
         * @brief test if cell object has no (valid) content
         * @return
         */
        bool empty() const;

I assumed this was the way to test if a cell is empty. In practice, I have a worksheet, created by this library, where the same cell returns false for
wks.cell(row, col).empty() but returns true for (wks.cell(row, col).value().type() == OpenXLSX::XLValueType::Empty)

Posting in case it helps someone else, if there's an example of how to test for an empty cell, I missed it.

Minimalist code to recreate:

int main(int argc, char **argv)
{
    // First, create a new document and access the sheet named 'Sheet1'.
    // New documents contain a single worksheet named 'Sheet1'
    XLDocument doc;
    doc.create("./Demo01.xlsx", XLForceOverwrite);
    auto wks = doc.workbook().worksheet("Sheet1");

    wks.cell(1,1).value() = "Hello";
    wks.cell(1,2).value() = "World";
	//NOTE THAT WE DO NOT SET cell(2,1) HERE
    wks.cell(2,2).value() = "Cruel World";

    doc.save();
    doc.close();

    doc.open("./Demo01.xlsx");
    wks = doc.workbook().worksheet("Sheet1");

    if (wks.cell(2,1).empty())
    	printf("Cell 2,1 is empty.\n");
    else
    	printf("Cell 2,1 is NOT empty.\n");
    if (wks.cell(2,1).value().type() == OpenXLSX::XLValueType::Empty)
    	printf("Cell 2,1 is empty.\n");
    else
    	printf("Cell 2,1 is NOT empty.\n");

    doc.close();
}

Result:
Cell 2,1 is NOT empty.
Cell 2,1 is empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestresolvedThis issue has been resolved.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions