Skip to content

Commit

Permalink
Cleanup of demo programs
Browse files Browse the repository at this point in the history
  • Loading branch information
troldal committed Sep 18, 2021
1 parent 9e4c08c commit e39aa65
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 583 deletions.
5 changes: 0 additions & 5 deletions Examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,4 @@ target_link_libraries(Demo6 PRIVATE OpenXLSX::OpenXLSX)
add_executable(Demo7 Demo7.cpp)
target_link_libraries(Demo7 PRIVATE OpenXLSX::OpenXLSX)

#=======================================================================================================================
# Define Demo8 target
#=======================================================================================================================
add_executable(Demo8 Demo8.cpp)
target_link_libraries(Demo8 PRIVATE OpenXLSX::OpenXLSX)

34 changes: 29 additions & 5 deletions Examples/Demo7.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,53 @@ using namespace OpenXLSX;
int main()
{
cout << "********************************************************************************\n";
cout << "DEMO PROGRAM #07: Row Handling - Implicit Conversion\n";
cout << "DEMO PROGRAM #07: Row Data - Implicit Conversion\n";
cout << "********************************************************************************\n";


// The previous example showed, among other things, how a std::vector of XLCellValue objects
// could be assigned to to an XLRow object, to populate the data to the cells in that row.
// In fact, this can be done with any container supporting bi-directional iterators, holding
// any data type that is convertible to a valid cell value. This is illustrated in this example.

// First, create a new document and access the sheet named 'Sheet1'.
cout << "\nGenerating spreadsheet ..." << endl;
XLDocument doc;
doc.create("./Demo07.xlsx");
auto wks = doc.workbook().worksheet("Sheet1");

// A std::vector holding values that are convertible to a cell value can be assigned to an XLRow
// object, using the 'values()' method. For example ints, doubles, bools and std::strings.
wks.row(1).values() = std::vector<int> { 1, 2, 3, 4, 5, 6, 7, 8 };
wks.row(2).values() = std::vector<double> { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8 };
wks.row(3).values() = std::vector<bool> { true, false, true, false, true, false, true, false };
wks.row(4).values() = std::vector<std::string> { "A", "B", "C", "D", "E", "F", "G", "H" };

// Save the sheet...
cout << "Saving spreadsheet ..." << endl;
doc.save();
doc.close();

// ...and reopen it (just to make sure that it is a valid .xlsx file)
cout << "Re-opening spreadsheet ..." << endl << endl;
doc.open("./Demo07.xlsx");
wks = doc.workbook().worksheet("Sheet1");

// The '.values()' method returns a proxy object that can be converted to any container supporting
// bi-directional iterators. The following three blocks shows how the row data can be converted to
// std::vector, std::deque, and std::list. In principle, this will also work with non-stl containers,
// e.g. containers in the Qt framework. This has not been tested, though.

// Conversion to std::vector<XLCellValue>
cout << "Conversion to std::vector<XLCellValue> ..." << endl;
for (auto& row : wks.rows()) {
// for (auto& value : std::vector<XLCellValue>(row.values())) {
for (auto& value : row.values<std::vector<XLCellValue>>()) {
for (auto& value : std::vector<XLCellValue>(row.values())) {
cout << value << " ";
}
cout << endl;
}

// Conversion to std::deque<XLCellValue>
cout << endl << "Conversion to std::deque<XLCellValue> ..." << endl;
for (auto& row : wks.rows()) {
for (auto& value : std::deque<XLCellValue>(row.values())) {
Expand All @@ -48,6 +65,7 @@ int main()
cout << endl;
}

// Conversion to std::list<XLCellValue>
cout << endl << "Conversion to std::list<XLCellValue> ..." << endl;
for (auto& row : wks.rows()) {
for (auto& value : std::list<XLCellValue>(row.values())) {
Expand All @@ -56,6 +74,11 @@ int main()
cout << endl;
}

// In addition to supporting any bi-directional container types, the cell values can also be converted to
// compatible plain data types instead of XLCellValue objects, e.g. ints, doubles, bools and std::string.
// This is illustrated in the following three blocks:

// Conversion to std::vector<[int, double, bool, std::string]>
cout << endl << "Conversion to std::vector<[int, double, bool, std::string]> ..." << endl;
for (auto& value : std::vector<int>(wks.row(1).values())) cout << value << " ";
cout << endl;
Expand All @@ -66,9 +89,9 @@ int main()
for (auto& value : std::vector<std::string>(wks.row(4).values())) cout << value << " ";
cout << endl;

// Conversion to std::deque<[int, double, bool, std::string]>
cout << endl << "Conversion to std::deque<[int, double, bool, std::string]> ..." << endl;
// for (auto& value : std::deque<int>(wks.row(1).values())) cout << value << " ";
for (auto& value : wks.row(1).values<std::deque<int>>()) cout << value << " ";
for (auto& value : std::deque<int>(wks.row(1).values())) cout << value << " ";
cout << endl;
for (auto& value : std::deque<double>(wks.row(2).values())) cout << value << " ";
cout << endl;
Expand All @@ -77,6 +100,7 @@ int main()
for (auto& value : std::deque<std::string>(wks.row(4).values())) cout << value << " ";
cout << endl;

// Conversion to std::list<[int, double, bool, std::string]>
cout << endl << "Conversion to std::list<[int, double, bool, std::string]> ..." << endl;
for (auto& value : std::list<int>(wks.row(1).values())) cout << value << " ";
cout << endl;
Expand Down
21 changes: 0 additions & 21 deletions Examples/Demo8.cpp

This file was deleted.

Loading

0 comments on commit e39aa65

Please sign in to comment.