Support to add any number of sheets to excel

This commit is contained in:
Ricardo Montañana Gómez 2023-09-20 00:58:01 +02:00
parent 925f71166c
commit 20a6ebab7c
Signed by: rmontanana
GPG Key ID: 46064262FD9A7ADE
4 changed files with 38 additions and 8 deletions

View File

@ -13,7 +13,8 @@ namespace platform {
string do_grouping() const { return "\03"; } 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 normalSize = 14; //font size for report body
colorTitle = 0xB1A0C7; colorTitle = 0xB1A0C7;
@ -23,6 +24,11 @@ namespace platform {
createFile(); createFile();
} }
lxw_workbook* ReportExcel::getWorkbook()
{
return workbook;
}
lxw_format* ReportExcel::efectiveStyle(const string& style) lxw_format* ReportExcel::efectiveStyle(const string& style)
{ {
lxw_format* efectiveStyle; lxw_format* efectiveStyle;
@ -169,9 +175,25 @@ namespace platform {
void ReportExcel::createFile() 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<string>(); const string name = data["model"].get<string>();
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(); setProperties();
createFormats(); createFormats();
formatColumns(); formatColumns();

View File

@ -21,8 +21,8 @@ namespace platform {
}; };
class ReportExcel : public ReportBase { class ReportExcel : public ReportBase {
public: public:
explicit ReportExcel(json data_); explicit ReportExcel(json data_, lxw_workbook* workbook);
virtual ~ReportExcel() { closeFile(); }; lxw_workbook* getWorkbook();
private: private:
void writeString(int row, int col, const string& text, const string& style = ""); void writeString(int row, int col, const string& text, const string& style = "");
void writeInt(int row, int col, const int number, const string& style = ""); void writeInt(int row, int col, const int number, const string& style = "");

View File

@ -104,13 +104,15 @@ namespace platform {
cout << "Invalid index" << endl; cout << "Invalid index" << endl;
return -1; 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; cout << Colors::YELLOW() << "Reporting " << files.at(index).getFilename() << endl;
auto data = files.at(index).load(); auto data = files.at(index).load();
if (excelReport) { if (excelReport) {
ReportExcel reporter(data); ReportExcel reporter(data, workbook);
reporter.show(); reporter.show();
openExcel = true;
workbook = reporter.getWorkbook();
} else { } else {
ReportConsole reporter(data); ReportConsole reporter(data);
reporter.show(); reporter.show();
@ -281,6 +283,9 @@ namespace platform {
sortDate(); sortDate();
show(); show();
menu(); menu();
if (openExcel) {
workbook_close(workbook);
}
cout << "Done!" << endl; cout << "Done!" << endl;
} }

View File

@ -1,5 +1,6 @@
#ifndef RESULTS_H #ifndef RESULTS_H
#define RESULTS_H #define RESULTS_H
#include "xlsxwriter.h"
#include <map> #include <map>
#include <vector> #include <vector>
#include <string> #include <string>
@ -44,10 +45,12 @@ namespace platform {
bool complete; bool complete;
bool partial; bool partial;
bool indexList = true; bool indexList = true;
bool openExcel = false;
lxw_workbook* workbook = NULL;
vector<Result> files; vector<Result> files;
void load(); // Loads the list of results void load(); // Loads the list of results
void show() const; 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; void showIndex(const int index, const int idx) const;
int getIndex(const string& intent) const; int getIndex(const string& intent) const;
void menu(); void menu();