OpenXLSX icon indicating copy to clipboard operation
OpenXLSX copied to clipboard

Failed to convert to std::vector<std::string]>

Open xrui94 opened this issue 2 years ago • 2 comments

124 lines of code:

auto rv = std::vector<std::string>(worksheet.row(2).values());

Reading the first row of data was successful, but an error was reported when reading the second row of data. I think all data should be able to be converted into string, but my program is not working. image

image

xrui94 avatar Apr 13 '23 04:04 xrui94

The .values() function from the XLRow class does not return a std::vector of strings, but XLCellValue objects. An XLCellValue object can be implicitly converted to a the type corresponding to the value type of the cell. That is the reason that your program can read the first row (all values are strings), but the following lines are mixed types.

I suggest that you use the following code instead:

auto rv = std::vector<XLCellValue>(worksheet.row(2).values());

For each element in the std::vector, you can then use the .get<>() function to get the underlying data. To check which data type is contained in the XLCellValue object, use the .type() member function.

troldal avatar Apr 13 '23 07:04 troldal

Yes, I got it. This is my code bellow, but I think it is not the best solution.


std::string GetValue(const OpenXLSX::XLCellValue& value)
{
    switch (value.type())
    {
    case OpenXLSX::XLValueType::Empty:
        return "";
    case OpenXLSX::XLValueType::Boolean:
       return std::to_string(value.get<bool>());
    case OpenXLSX::XLValueType::Integer:
        return std::to_string(value.get<int32_t>());
    case OpenXLSX::XLValueType::Float:
        return std::to_string(value.get<double>());
    case OpenXLSX::XLValueType::Error:
       return "";
    case OpenXLSX::XLValueType::String:
        return value.get<std::string>();
    default:
        return "";
    }
}

// ... (A function, but some code has been omitted here)

   uint32_t startRow = 2;
   uint32_t endRow = worksheet.rowCount();
   for (const OpenXLSX::XLRow &row : worksheet.rows(startRow, endRow))
   {
       DotField dotField;
       //auto rowValues = std::vector<std::string>(row.values());    // 对于小数,会报错
       auto rowValues = std::vector<OpenXLSX::XLCellValue>(row.values());

       for (const std::pair<std::string, uint16_t> &item : fieldLoc)
       {
           //
           auto value = GetValue(rowValues[item.second - 1]);
           if (item.first == "id")
           {
               if (value.size() == 0) break; // ID不能为空,因此,遇到空行,则跳过该行
               dotField.id = value;
           }
           else if (item.first == "x")
               dotField.x = std::stof(value);
           else if (item.first == "y")
               dotField.y = std::stof(value);
           else if (item.first == "height")
               dotField.height = std::stof(value);
           else
               std::cerr << "Failed read valur for \"" << item.first << "\" field." << std::endl;
       }

       // ID不能为空,因此,遇到空行,则跳过该行
       if (dotField.id.size() == 0) continue;

       // 保存当前读取的一行数据
       //data.insert({ dotField.id, dotField });
       data[dotField.id] = dotField;
   }

// ... (A function, but some code has been omitted here)

xrui94 avatar Apr 17 '23 01:04 xrui94

Dear @xrui94 , the XLCellValue since has a new getString() method, which can be used to simplify your code to this:

    std::vector<std::string> rvStrings; // declaration

    auto rv = std::vector<XLCellValue>(wks.row(1).values()); // get a vector of XLCellValue
    for( auto cv : rv ) rvStrings.push_back(cv.getString());   // iterate over rv & store strings in vector of strings

    // output contents of strings vector
    std::cout << "rvStrings is";
    for( auto sv : rvStrings ) std::cout << " \"" << sv << "\"";
    std::cout << std::endl;

I will mark this issue as resolved - please let me know if you are still an active user and have further questions.

aral-matrix avatar Jan 11 '25 01:01 aral-matrix