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"; }
};
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<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();
createFormats();
formatColumns();

View File

@ -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 = "");

View File

@ -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;
}

View File

@ -1,5 +1,6 @@
#ifndef RESULTS_H
#define RESULTS_H
#include "xlsxwriter.h"
#include <map>
#include <vector>
#include <string>
@ -44,10 +45,12 @@ namespace platform {
bool complete;
bool partial;
bool indexList = true;
bool openExcel = false;
lxw_workbook* workbook = NULL;
vector<Result> 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();