Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Named range and Table support #203

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
752563d
creating definedName
akira215 Dec 24, 2022
e64a644
Changing Class name XLNamedRange
akira215 Dec 24, 2022
725d8da
Adding sanitary check adding named range
akira215 Dec 24, 2022
184b90c
adding deleteNamedRange
akira215 Dec 24, 2022
3a58ce6
Work in progress in XLDocument::open
akira215 Dec 26, 2022
b4b1e74
work in progress XLTable
akira215 Dec 27, 2022
b073a59
Working on XLTable
akira215 Dec 27, 2022
222ab17
XLtable working with iterators
akira215 Dec 28, 2022
35b24ce
Working on createTable in XLDocument
akira215 Dec 29, 2022
b226afe
Creating table almost finished check todo
akira215 Dec 30, 2022
1ee6bc7
Turning XLSharedStrings as singleton to be cleared
akira215 Dec 30, 2022
541858a
Correcting singleton XLSharedString
akira215 Dec 30, 2022
7bbaa5e
Removing bug from addNamedRange
akira215 Dec 31, 2022
7dee4de
Cleaning up the code
akira215 Dec 31, 2022
e635f0f
Update readme
akira215 Dec 31, 2022
962ffe6
Correction on readme
akira215 Dec 31, 2022
162d36e
Correcting XLTableRowIterator End
akira215 Dec 31, 2022
ea5863f
Adding table total feature
akira215 Dec 31, 2022
406ce1d
Fixing table totla formula clearing
akira215 Jan 2, 2023
151a045
all functionality of tablestyle checked
akira215 Jan 2, 2023
cdd4978
working on proxy tableformulas
akira215 Jan 3, 2023
1b5a2f2
implementation of proxy for tablecolumn formulas
akira215 Jan 3, 2023
0e5bfcb
finishing table formulas
akira215 Jan 3, 2023
2a1dd03
update the demo10 & add columns table method
akira215 Jan 3, 2023
960a187
correcting behavior of table total visible
akira215 Jan 4, 2023
0886174
Inserting and appending table column
akira215 Jan 4, 2023
7b290dc
deleting table column Ok
akira215 Jan 6, 2023
566d12e
Inserting and deleting row in tables
akira215 Jan 6, 2023
4add800
add table autofilter syncro
akira215 Jan 6, 2023
5e7ca4e
adding helper getAs... in XLCellValue
akira215 Jan 7, 2023
d835ea0
adding const to getter in workbook
akira215 Jan 8, 2023
175b24f
Adding table utilities
akira215 Jan 17, 2023
989fbf6
correcting sheetID
akira215 Jan 17, 2023
5767b2f
Changing setTabColor Names
akira215 Jan 17, 2023
89971b4
updating example
akira215 Jan 17, 2023
30010ef
Fixed this pointer check in XLCell
akira215 Jan 28, 2023
3fb704f
Enforce formula node order and properly convert cell type to number
akira215 Jan 28, 2023
a08ae26
Fix changing values of cells of type "inlineStr"
akira215 Jan 28, 2023
13688bc
Fix loading of xlsx files without xl/workbook.xml
akira215 Jan 28, 2023
7aac01f
Update XLSheet.cpp with correct type
akira215 Jan 28, 2023
eba7fb2
Use a constexpr variable for the template array
akira215 Jan 28, 2023
a296593
Fix crash when using XLCellProxyValue()::set() with an empty string
akira215 Jan 28, 2023
c677a0a
Working on style
akira215 Jan 28, 2023
f6ea8aa
Fix Error in XLDateTime on 30th
akira215 Jan 28, 2023
c12e694
adding include stdint.h
akira215 Oct 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 44 additions & 27 deletions Examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,59 +11,76 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_subdirectory(external/nowide EXCLUDE_FROM_ALL)

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

#=======================================================================================================================
# Define Demo1A target
# Define Demo01A target
#=======================================================================================================================
if(${OPENXLSX_ENABLE_LIBZIP})
find_package(LIBZIP MODULE REQUIRED)
add_executable(Demo1A Demo1A.cpp)
target_link_libraries(Demo1A PRIVATE OpenXLSX::OpenXLSX libzip::zip)
add_executable(Demo01A Demo01A.cpp)
target_link_libraries(Demo01A PRIVATE OpenXLSX::OpenXLSX libzip::zip)
endif()

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

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

#=======================================================================================================================
# Define Demo4 target
# Define Demo04 target
#=======================================================================================================================
add_executable(Demo4 Demo4.cpp)
target_link_libraries(Demo4 PRIVATE OpenXLSX::OpenXLSX nowide::nowide)
add_executable(Demo04 Demo04.cpp)
target_link_libraries(Demo04 PRIVATE OpenXLSX::OpenXLSX nowide::nowide)

#=======================================================================================================================
# Define Demo9 target
# Define Demo09 target
#=======================================================================================================================
add_executable(Demo5 Demo5.cpp)
target_link_libraries(Demo5 PRIVATE OpenXLSX::OpenXLSX)
add_executable(Demo05 Demo05.cpp)
target_link_libraries(Demo05 PRIVATE OpenXLSX::OpenXLSX)

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

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

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

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

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

#=======================================================================================================================
# Define Demo11 target
#=======================================================================================================================
add_executable(Demo11 Demo11.cpp)
target_link_libraries(Demo11 PRIVATE OpenXLSX::OpenXLSX)
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions Examples/Demo3.cpp → Examples/Demo03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ int main()

// The color of each sheet tab can be set using the 'setColor' method for a
// sheet, and passing an XLColor object as an argument.
wbk.sheet("Sheet2").setColor(XLColor(0, 0, 0));
wbk.sheet("Sheet3").setColor(XLColor(255, 0, 0));
wbk.sheet("Sheet4").setColor(XLColor(0, 255, 0));
wbk.sheet("Sheet5").setColor(XLColor(0, 0, 255));
wbk.sheet("Sheet2").setTabColor(XLColor(0, 0, 0));
wbk.sheet("Sheet3").setTabColor(XLColor(255, 0, 0));
wbk.sheet("Sheet4").setTabColor(XLColor(0, 255, 0));
wbk.sheet("Sheet5").setTabColor(XLColor(0, 0, 255));

doc.save();

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
65 changes: 65 additions & 0 deletions Examples/Demo09.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <OpenXLSX.hpp>

using namespace std;
using namespace OpenXLSX;

int main() {
cout << "********************************************************************************\n";
cout << "DEMO PROGRAM #09: Named Range\n";
cout << "********************************************************************************\n";

// This example illustrate the use of the named range object. This could be particulary
// usefull when programming without GUI, to set up name in the Excel file, so that
// data could be safetly retrived and written by the code in dedicated location in the Excel file
// Unlike other implementation, named range is accessible within workbook object. Sheet limited
// maed range are as well accessed via the wrokbook.

// First, create a new document and access the 1st sheet.
cout << "\nGenerating spreadsheet ..." << endl;
XLDocument doc;
doc.create("./Demo09.xlsx");
auto wks = doc.workbook().sheet(1).get<XLWorksheet>();
string sheetName = wks.name();

// Define a namedrange "New name"
// At this stage the range shall be continuous
// a third argument could be provided the index of the spreasheet where
// the named range will be defined. Absolute marks ($) will be added.
string fullRef = sheetName + "!A5:E10";
XLNamedRange myRange = doc.workbook().addNamedRange("MyNamedRange",fullRef /*, localSheetId*/);

// 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("./Demo09.xlsx");
wks = doc.workbook().worksheet(sheetName);

// The NamedRange object is a range, it can be iterated
cout << "Filling the named range object ..." << endl;
XLNamedRange rng = doc.workbook().namedRange("MyNamedRange");
int i = 0;
for (auto& cell : rng){
cell.value() = i;
i++;
}

// It can also be accessed via []
cout << "Value of the 10th cell of the named range:" << endl;
cout << rng[10].value() << endl;

// for convenience, in particular in case of mono cell named range,
// a fonction is provided: firstCell()
cout << "first Cell Value: " << rng.firstCell().value()<< endl;

// Finally named range could be removed
//doc.workbook().deleteNamedRange("MyNamedRange");

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

return 0;
}
159 changes: 159 additions & 0 deletions Examples/Demo10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#include <OpenXLSX.hpp>

#include <vector>

using namespace std;
using namespace OpenXLSX;

int main() {
cout << "********************************************************************************\n";
cout << "DEMO PROGRAM #10: Tables first part\n";
cout << "********************************************************************************\n";

// This example illustrate the use of the named range object. This could be particulary
// usefull when programming without GUI, to set up name in the Excel file, so that
// data could be safetly retrived and written by the code in dedicated location in the Excel file
// Unlike other implementation, named range is accessible within workbook object. Sheet limited
// maed range are as well accessed via the wrokbook.

// First, create a new document and access the 1st sheet.
cout << "\nGenerating spreadsheet ..." << endl;
XLDocument doc;
doc.create("./Demo10.xlsx");
auto wks = doc.workbook().sheet(1).get<XLWorksheet>();
string sheetName = wks.name();

// Fill the column header and create the table
// If the table header is not set, default name will be setup
// Also columns name shall be unique, a increment marks is added if required
cout << "Creating table ..." << endl;
wks.cell("B2").value() = "#";
wks.cell("C2").value() = "One";
wks.cell("D2").value() = "Col";
wks.cell("E2").value() = "With";
wks.cell("F2").value() = "Table";

XLTable myTable = doc.workbook().addTable(sheetName,"MyTable","B2:F18");
//XLTable myTable = doc.workbook().addTable(sheetName,"MyTable","B2:F4");

// 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("./Demo10.xlsx");
wks = doc.workbook().worksheet(sheetName);

// We can retrieve the headers name:
XLTable tbl = doc.workbook().table("MyTable");
vector<std::string> headers = tbl.columnNames();

cout << "Table "<< tbl.name() << " header names:" << endl;
int i = 0;
for(const auto& h : headers){
cout << i++ << " - " << h << endl;
}

// We can also access different items
auto ncol = tbl.columnIndex("Col"); // Retrieve the colum index by the name
auto sht = tbl.getWorksheet(); // Retrieve and object worksheet
auto rang = tbl.tableRange(); // whole table range including total & headers
auto bodyRange = tbl.dataBodyRange(); // Only data body table range (excl. total & headers)
auto nRow = tbl.rowsCount(); // Only data body table range (excl. total & headers)
// And iterate throught the table rows
auto nCol = tbl.columnsCount(); // Only data body table range (excl. total & headers)

int j = 0;
for(const auto& row : tbl.tableRows()){
row[tbl.columnIndex("#")].value() = j;
row[1].value() = "Data" + to_string(j);
row[2].value() = "Col" + to_string(j);
row[3].value() = 4 * j;
row[tbl.columnIndex("Table")].value() = (float)j * 2.0f / 3.0f;
j++;
}

// loop could also be done on columns
for(auto& col : tbl.columns())
for (auto& cell : col.bodyRange())
cout << cell.value() << " - ";

cout << endl;

// Also show the total with selected function
tbl.autofilter().hideArrows();
//tbl.setHeaderVisible(false);

// Total formulas
tbl.setTotalVisible(true);
//tbl.column("Table").setTotalsRowFunction("");
//tbl.column("Table").setTotalsRowFunction("count");
tbl.column("Table").totalsRowFormula() ="sum";
string totaFormula = tbl.column("Table").totalsRowFormula();
cout << "total Formula in the table column : " << totaFormula << endl;

tbl.column("#").totalsRowLabel() ="Demo Total";
string totaLabel = tbl.column("#").totalsRowLabel();
cout << "total Label in the table column : " << totaLabel << endl;

// To clear, a empty string could be sent, or the method
// clearTotalsRowFormula could be called
tbl.column("Table").totalsRowFormula() ="";
tbl.setTotalVisible(false);

// Columns formulas could be setup, check and cleared, using either
// empty string or calling clearColumnFormula
tbl.column("With").columnFormula() = "MyTable[[#This Row],['#]]*2";
string columFormula = tbl.column("With").columnFormula();
cout << "Column Formula : " << columFormula << endl;


//Inserting columns
auto& newCol = tbl.insertColumn("newCol",2);
newCol.columnFormula() = "MyTable[[#This Row],['#]]+2";
cout << "Inserted Column : " << newCol.name() << endl;

auto& appendCol = tbl.appendColumn("newCol"); // test the auto increment
appendCol.columnFormula() = "MyTable[[#This Row],[One]]&amp;\"akira\"&amp;MyTable[[#This Row],[newCol]]";
cout << "Append Column : " << newCol.name() << endl;

// Deleting colum
tbl.deleteColumn("newCol");

//Inserting and deleting rows
auto newRow = tbl.insertRow(2);
tbl.deleteRow(3);

tbl.setHeaderVisible(false);
tbl.setHeaderVisible(true);



/*
// Table style basics
cout << "Table Style : " << tbl.tableStyle().style() << endl;
tbl.tableStyle().setStyle("TableStyleDark7");

cout << "Column stripes : " << tbl.tableStyle().columnStripes() << endl;
cout << "row stripes : " << tbl.tableStyle().rowStripes() << endl;

tbl.tableStyle().showRowStripes(false);
tbl.tableStyle().showColumnStripes(true);

cout << "1st Column highlighted : " << tbl.tableStyle().firstColumnHighlighted() << endl;
cout << "last Column highlighted : " << tbl.tableStyle().lastColumnHighlighted() << endl;

tbl.tableStyle().showFirstColumnHighlighted(true);
tbl.tableStyle().showLastColumnHighlighted(true);
*/

auto tablelist = doc.workbook().tableNames();
doc.workbook().deleteTable("MyTable");

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

return 0;
}
Loading