From 20a6ebab7c78902548fceb733030676654f1caf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana?= Date: Wed, 20 Sep 2023 00:58:01 +0200 Subject: [PATCH] Support to add any number of sheets to excel --- src/Platform/ReportExcel.cc | 28 +++++++++++++++++++++++++--- src/Platform/ReportExcel.h | 4 ++-- src/Platform/Results.cc | 9 +++++++-- src/Platform/Results.h | 5 ++++- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Platform/ReportExcel.cc b/src/Platform/ReportExcel.cc index 50293bf..ab0b0a2 100644 --- a/src/Platform/ReportExcel.cc +++ b/src/Platform/ReportExcel.cc @@ -13,7 +13,8 @@ namespace platform { string do_grouping() const { return "\03"; } }; - ReportExcel::ReportExcel(json data_) : ReportBase(data_), row(0) + + ReportExcel::ReportExcel(json data_, lxw_workbook* workbook) : ReportBase(data_), row(0), workbook(workbook) { normalSize = 14; //font size for report body colorTitle = 0xB1A0C7; @@ -23,6 +24,11 @@ namespace platform { createFile(); } + lxw_workbook* ReportExcel::getWorkbook() + { + return workbook; + } + lxw_format* ReportExcel::efectiveStyle(const string& style) { lxw_format* efectiveStyle; @@ -169,9 +175,25 @@ namespace platform { void ReportExcel::createFile() { - workbook = workbook_new((Paths::excel() + fileName).c_str()); + if (workbook == NULL) { + workbook = workbook_new((Paths::excel() + fileName).c_str()); + } const string name = data["model"].get(); - worksheet = workbook_add_worksheet(workbook, name.c_str()); + string suffix = ""; + int num = 1; + // Create a sheet with the name of the model + while (true) { + string efectiveName = name + suffix; + worksheet = workbook_add_worksheet(workbook, efectiveName.c_str()); + if (worksheet == NULL) { + suffix = to_string(++num); + } else { + break; + } + if (num > 100) { + throw invalid_argument("Couldn't create sheet " + efectiveName); + } + } setProperties(); createFormats(); formatColumns(); diff --git a/src/Platform/ReportExcel.h b/src/Platform/ReportExcel.h index 8c86788..6e0f6eb 100644 --- a/src/Platform/ReportExcel.h +++ b/src/Platform/ReportExcel.h @@ -21,8 +21,8 @@ namespace platform { }; class ReportExcel : public ReportBase { public: - explicit ReportExcel(json data_); - virtual ~ReportExcel() { closeFile(); }; + explicit ReportExcel(json data_, lxw_workbook* workbook); + lxw_workbook* getWorkbook(); private: void writeString(int row, int col, const string& text, const string& style = ""); void writeInt(int row, int col, const int number, const string& style = ""); diff --git a/src/Platform/Results.cc b/src/Platform/Results.cc index 3566ab7..e136a62 100644 --- a/src/Platform/Results.cc +++ b/src/Platform/Results.cc @@ -104,13 +104,15 @@ namespace platform { cout << "Invalid index" << endl; return -1; } - void Results::report(const int index, const bool excelReport) const + void Results::report(const int index, const bool excelReport) { cout << Colors::YELLOW() << "Reporting " << files.at(index).getFilename() << endl; auto data = files.at(index).load(); if (excelReport) { - ReportExcel reporter(data); + ReportExcel reporter(data, workbook); reporter.show(); + openExcel = true; + workbook = reporter.getWorkbook(); } else { ReportConsole reporter(data); reporter.show(); @@ -281,6 +283,9 @@ namespace platform { sortDate(); show(); menu(); + if (openExcel) { + workbook_close(workbook); + } cout << "Done!" << endl; } diff --git a/src/Platform/Results.h b/src/Platform/Results.h index 3f5655a..c418135 100644 --- a/src/Platform/Results.h +++ b/src/Platform/Results.h @@ -1,5 +1,6 @@ #ifndef RESULTS_H #define RESULTS_H +#include "xlsxwriter.h" #include #include #include @@ -44,10 +45,12 @@ namespace platform { bool complete; bool partial; bool indexList = true; + bool openExcel = false; + lxw_workbook* workbook = NULL; vector files; void load(); // Loads the list of results void show() const; - void report(const int index, const bool excelReport) const; + void report(const int index, const bool excelReport); void showIndex(const int index, const int idx) const; int getIndex(const string& intent) const; void menu();