20bool shouldStop(
const export_ports::ExportRequest &request) {
21 if (request.waitIfPaused) {
22 request.waitIfPaused();
24 return request.shouldCancel && request.shouldCancel();
27export_ports::ExportResult canceledResult(
28 const export_ports::ExportRequest &request) {
29 export_ports::ExportResult result;
30 result.actualFormat = export_ports::ExportFormat::Xlsx;
31 result.resolvedOutputPath = request.outputPath;
32 result.status = export_ports::ExportStatus::Canceled;
33 result.message =
"Canceled";
37std::string columnRef(
int column) {
40 const int remainder = (column - 1) % 26;
41 ref.insert(ref.begin(),
static_cast<char>(
'A' + remainder));
42 column = (column - 1) / 26;
47std::vector<std::vector<std::string>>
48buildRows(
const internal::PropertyContractMatrix &matrix,
49 bool includeFormulas) {
50 std::vector<std::vector<std::string>> rows;
51 std::vector<std::string> header;
54 header.insert(header.end(), matrix.propertyNames.begin(),
55 matrix.propertyNames.end());
57 rows.push_back(std::move(header));
59 std::vector<double> columnSums(matrix.propertyNames.size(), 0.0);
61 for (
const auto &contractType : matrix.contractTypes) {
62 std::vector<std::string> row;
63 row.reserve(matrix.propertyNames.size() + 2);
64 row.push_back(contractType);
67 for (
size_t column = 0; column < matrix.propertyNames.size(); ++column) {
69 const auto propertyIt =
70 matrix.amountsByProperty.find(matrix.propertyNames[column]);
71 if (propertyIt != matrix.amountsByProperty.end()) {
72 const auto contractIt = propertyIt->second.find(contractType);
73 if (contractIt != propertyIt->second.end()) {
74 value = contractIt->second;
77 row.push_back(std::to_string(value));
78 columnSums[column] += value;
81 if (includeFormulas && !matrix.propertyNames.empty()) {
83 "=SUM(" + columnRef(2) + std::to_string(rowIndex) +
":" +
84 columnRef(
static_cast<int>(1 + matrix.propertyNames.size())) +
85 std::to_string(rowIndex) +
")");
87 row.push_back(std::to_string(rowSum));
89 rows.push_back(std::move(row));
93 if (!matrix.propertyNames.empty()) {
94 std::vector<std::string> totalRow;
95 totalRow.reserve(matrix.propertyNames.size() + 2);
99 double grandTotal = 0.0;
100 for (
double value : columnSums) {
101 totalRow.push_back(std::to_string(value));
104 if (includeFormulas && !matrix.contractTypes.empty()) {
107 columnRef(
static_cast<int>(2 + matrix.propertyNames.size())) +
108 "2:" + columnRef(
static_cast<int>(2 + matrix.propertyNames.size())) +
109 std::to_string(rowIndex - 1) +
")");
111 totalRow.push_back(std::to_string(grandTotal));
113 rows.push_back(std::move(totalRow));
122 std::shared_ptr<core::ports::xlsx_writer::IXlsxWriter> writer)
123 : writer_(std::move(writer)) {}
125export_ports::ExportResult
128 const export_ports::ExportRequest &request)
const {
129 export_ports::ExportResult result;
131 result.resolvedOutputPath = request.outputPath;
134 if (shouldStop(request)) {
135 return canceledResult(request);
137 if (request.outputPath.empty()) {
138 result.status = export_ports::ExportStatus::InvalidInput;
146 result.status = export_ports::ExportStatus::XlsxGenerationFailed;
147 result.errorCode = std::string(
149 result.message = std::string(
155 if (shouldStop(request)) {
156 return canceledResult(request);
158 const auto rows = buildRows(matrix, request.includeFormulas);
159 if (shouldStop(request)) {
160 return canceledResult(request);
163 if (!writer_->writeTable(request.outputPath, rows,
"Export")) {
164 result.status = export_ports::ExportStatus::XlsxGenerationFailed;
165 result.errorCode = std::string(
167 result.message = std::string(
172 result.status = export_ports::ExportStatus::Ok;
173 result.success =
true;
175 result.status = export_ports::ExportStatus::XlsxGenerationFailed;
178 result.message = std::string(
Defines private constants shared by core export implementations.
Declares the XLSX writing port used by the export layer.
Declares private helpers for building export matrix data from workspace state.
Declares XLSX export for the property/contract-type matrix.
XlsxExporter(std::shared_ptr< core::ports::xlsx_writer::IXlsxWriter > writer={})
core::ports::exporting::ExportResult exportData(const core::domain::catalog::WorkspaceCatalog &state, const core::ports::exporting::ExportRequest &request) const
Exports the property/contract matrix to an XLSX file.
constexpr std::string_view kOutputPathEmpty
constexpr std::string_view kXlsxGenerationFailed
constexpr std::string_view kTotal
constexpr std::string_view kPropertyHeader
constexpr std::string_view kOutputPathEmpty
constexpr std::string_view kXlsxGenerationFailed
PropertyContractMatrix buildPropertyContractMatrix(const core::domain::catalog::WorkspaceCatalog &state)
Builds the property/contract matrix from the current workspace snapshot.
ExportFormat actualFormat