完成工具

This commit is contained in:
lnk
2026-07-13 15:12:55 +08:00
parent b61388443f
commit 9181e2d4e9
77 changed files with 1847801 additions and 20269 deletions

View File

@@ -7,9 +7,11 @@
#include <fstream>
#include <iomanip>
#include <functional>
#include <limits>
#include <iostream>
#include <map>
#include <regex>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
@@ -593,23 +595,30 @@ static const ValueMeta* findMetaForKey(
if (exactCandidates.empty()) collectByName(metas, secondary, wiring, filter, exactCandidates);
return bestMeta(exactCandidates, pk);
}
// 把 XML 中序列 DA如 phs*Har[%-2]$mag$f改显示成 phs*Har[0-48]$mag$f
static bool parseDaValueOffset(const std::string& daPath, int& offset) {
std::smatch m;
std::regex re(R"(%(-?\d+))");
if (!std::regex_search(daPath, m, re)) return false;
offset = std::stoi(m[1].str());
return true;
}
// 把 XML 中序列 DA如 phs*Har[%-2]$mag$f按“数组基准 + 取值偏移量”改显示成 phs*Har[0-48]$mag$f
static std::string rewriteDaRangeForDisplay(const std::string& daPath,
const ValueMeta& vm,
int keyStart,
int keyEnd) {
if (!vm.isSequence) return daPath;
// 规则:
// XML 模板里是 [%-2]
// key 是 [2-50]
// 显示要变成 [0-48]
// 即keyStart - vm.seqStart, keyEnd - vm.seqStart
int dispStart = keyStart - vm.seqStart;
int dispEnd = keyEnd - vm.seqStart;
(void)keyStart;
(void)keyEnd;
int valueOffset = 0;
parseDaValueOffset(daPath, valueOffset);
int dispStart = vm.seqStart + valueOffset;
int dispEnd = vm.seqEnd + valueOffset;
std::smatch m;
std::regex re(R"(\[%-(\d+)\])");
std::regex re(R"(\[%(-?\d+)\])");
if (std::regex_search(daPath, m, re)) {
std::ostringstream oss;
oss << "[" << dispStart << "-" << dispEnd << "]";
@@ -693,6 +702,13 @@ struct SheetSpec {
std::string frontType;
};
struct SheetStats {
std::string sheetName;
int normal = 0;
int abnormal = 0;
int missing = 0;
};
struct OriginRecord {
std::string path;
std::string value;
@@ -700,8 +716,16 @@ struct OriginRecord {
int sourceOrder = 0;
};
struct OriginHeader {
std::string title;
std::string fileName;
int sourceOrder = 0;
std::vector<HeaderItem> fields;
};
struct OriginData {
std::vector<OriginRecord> records;
std::vector<OriginHeader> headers;
std::unordered_map<std::string, std::vector<size_t>> pathToRecords;
std::unordered_set<size_t> used;
};
@@ -710,6 +734,15 @@ struct OriginMatch {
std::string path;
std::string value;
std::string reportName;
int sourceOrder = -1;
int rangeStart = std::numeric_limits<int>::min();
int rangeEnd = std::numeric_limits<int>::min();
std::vector<int> valueIndexes;
};
struct ComparePlan {
int originPickStart = std::numeric_limits<int>::min();
int originPickEnd = std::numeric_limits<int>::min();
};
struct JsonDataBucket {
@@ -741,6 +774,51 @@ static std::string reportNameFromRoot(const json& root, const fs::path& originPa
return originPath.filename().u8string();
}
static const std::vector<std::string>& originHeaderKeys() {
static const std::vector<std::string> keys = {
"rpt_no",
"data_time",
"mp_id",
"rpt_id",
"voltage_level",
"v_wiring_type"
};
return keys;
}
static const std::vector<std::string>& originHeaderDisplayKeys() {
static const std::vector<std::string> keys = {
"rpt_no",
"data_time",
"mp_id",
"voltage_level",
"v_wiring_type"
};
return keys;
}
static std::string rootHeaderValue(const json& root, const std::string& key) {
if (!root.is_object()) return "";
auto it = root.find(key);
if (it == root.end()) return "";
return jsonScalarForOutput(*it);
}
static void appendOriginHeader(OriginData& data,
const fs::path& originPath,
int sourceOrder,
const json& root,
const std::string& reportName) {
OriginHeader header;
header.title = reportName.empty() ? originPath.filename().u8string() : reportName;
header.fileName = originPath.filename().u8string();
header.sourceOrder = sourceOrder;
for (const auto& key : originHeaderKeys()) {
header.fields.push_back({key, rootHeaderValue(root, key)});
}
data.headers.push_back(std::move(header));
}
static std::vector<json> parseJsonRoots(const std::string& text) {
std::vector<json> roots;
size_t pos = 0;
@@ -787,6 +865,7 @@ static std::vector<json> parseJsonRoots(const std::string& text) {
static void appendOriginRoot(OriginData& data, const fs::path& originPath, int sourceOrder, const json& root) {
std::string reportName = reportNameFromRoot(root, originPath, sourceOrder);
appendOriginHeader(data, originPath, sourceOrder, root, reportName);
const json* mms = &root;
if (root.is_object() && root.contains("mms_json") && root["mms_json"].is_object()) {
@@ -908,7 +987,9 @@ static std::vector<std::string> makeOriginPathCandidates(const std::string& doPa
std::string primary = makeOriginPath(doPath, daPath, stat, phase, wiring);
if (!primary.empty()) paths.push_back(primary);
return paths;
}static bool parseOriginRangePath(const std::string& path,
}
static bool parseOriginRangePath(const std::string& path,
std::string& before,
int& start,
int& end,
@@ -923,12 +1004,38 @@ static std::vector<std::string> makeOriginPathCandidates(const std::string& doPa
return start <= end;
}
static bool parseOriginIndexedPath(const std::string& path,
const std::string& before,
const std::string& after,
int& index) {
if (path.size() < before.size() + after.size() + 3) return false;
if (path.compare(0, before.size(), before) != 0) return false;
if (!after.empty() &&
path.compare(path.size() - after.size(), after.size(), after) != 0) {
return false;
}
size_t midStart = before.size();
size_t midEnd = after.empty() ? path.size() : path.size() - after.size();
if (midEnd <= midStart + 2 || path[midStart] != '[' || path[midEnd - 1] != ']') return false;
std::string number = path.substr(midStart + 1, midEnd - midStart - 2);
if (number.empty() || !std::all_of(number.begin(), number.end(), [](unsigned char ch) {
return std::isdigit(ch) != 0;
})) {
return false;
}
index = std::stoi(number);
return true;
}
static OriginMatch matchOrigin(OriginData* origin,
const std::string& doPath,
const std::string& daPath,
const std::string& stat,
const std::string& phase,
const std::string& wiring) {
const std::string& wiring,
int pickStartOverride = std::numeric_limits<int>::min(),
int pickEndOverride = std::numeric_limits<int>::min()) {
OriginMatch match;
if (!origin) return match;
@@ -938,25 +1045,75 @@ static OriginMatch matchOrigin(OriginData* origin,
std::string before, after;
int start = 0, end = 0;
if (parseOriginRangePath(path, before, start, end, after)) {
std::ostringstream values;
bool any = false;
std::string reportName;
for (int i = start; i <= end; ++i) {
if (i != start) values << ",";
std::string onePath = before + "[" + std::to_string(i) + "]" + after;
const OriginRecord* rec = firstUnusedRecord(origin, onePath);
if (rec) {
any = true;
values << rec->value;
if (reportName.empty()) reportName = rec->reportName;
markRecordUsed(origin, rec);
int pickStart = pickStartOverride == std::numeric_limits<int>::min() ? start : pickStartOverride;
int pickEnd = pickEndOverride == std::numeric_limits<int>::min() ? end : pickEndOverride;
struct IndexedRecord {
int index = 0;
size_t recordIndex = 0;
};
std::map<int, std::vector<IndexedRecord>> bySource;
for (size_t recIndex = 0; recIndex < origin->records.size(); ++recIndex) {
int indexedPath = 0;
if (!parseOriginIndexedPath(origin->records[recIndex].path, before, after, indexedPath)) continue;
bySource[origin->records[recIndex].sourceOrder].push_back({indexedPath, recIndex});
}
int bestSource = -1;
int bestIntersection = 0;
bool bestHasPickStart = false;
for (const auto& kv : bySource) {
int intersection = 0;
bool hasPickStart = false;
for (const auto& rec : kv.second) {
if (rec.index >= pickStart && rec.index <= pickEnd) ++intersection;
if (rec.index == pickStart) hasPickStart = true;
}
if (intersection <= 0) continue;
if (bestSource < 0 ||
(hasPickStart && !bestHasPickStart) ||
(hasPickStart == bestHasPickStart && intersection > bestIntersection)) {
bestSource = kv.first;
bestIntersection = intersection;
bestHasPickStart = hasPickStart;
}
}
if (any) {
match.path = path;
match.value = values.str();
match.reportName = reportName;
return match;
if (bestSource >= 0) {
std::map<int, size_t> indexToRecord;
int displayStart = std::numeric_limits<int>::max();
int displayEnd = std::numeric_limits<int>::min();
for (const auto& rec : bySource[bestSource]) {
if (indexToRecord.count(rec.index)) continue;
indexToRecord[rec.index] = rec.recordIndex;
displayStart = std::min(displayStart, rec.index);
displayEnd = std::max(displayEnd, rec.index);
}
std::ostringstream values;
bool any = false;
std::string reportName;
for (int i = displayStart; i <= displayEnd; ++i) {
if (i != displayStart) values << ",";
match.valueIndexes.push_back(i);
auto it = indexToRecord.find(i);
if (it == indexToRecord.end()) continue;
const OriginRecord& rec = origin->records[it->second];
any = true;
values << rec.value;
if (reportName.empty()) reportName = rec.reportName;
markRecordUsed(origin, &rec);
}
if (any) {
match.path = before + "[" + std::to_string(displayStart) + "-" + std::to_string(displayEnd) + "]" + after;
match.value = values.str();
match.reportName = reportName;
match.sourceOrder = bestSource;
match.rangeStart = displayStart;
match.rangeEnd = displayEnd;
return match;
}
}
continue;
}
@@ -966,13 +1123,16 @@ static OriginMatch matchOrigin(OriginData* origin,
match.path = path;
match.value = rec->value;
match.reportName = rec->reportName;
match.sourceOrder = rec->sourceOrder;
markRecordUsed(origin, rec);
return match;
}
}
return match;
}static size_t utf8CharLen(unsigned char c) {
}
static size_t utf8CharLen(unsigned char c) {
if (c < 0x80) return 1;
if ((c & 0xE0) == 0xC0) return 2;
if ((c & 0xF0) == 0xE0) return 3;
@@ -1367,20 +1527,87 @@ static bool hasNullValue(const std::vector<std::string>& values) {
}
static bool valuesEqualForCompare(const std::vector<std::string>& finalValues,
const std::vector<std::string>& originValues) {
if (finalValues.size() != originValues.size()) return false;
const std::vector<std::string>& originValues,
int originStartIndex = 0) {
if (originStartIndex < 0) return false;
if (originValues.size() < static_cast<size_t>(originStartIndex) + finalValues.size()) return false;
for (size_t i = 0; i < finalValues.size(); ++i) {
if (compareValueKey(finalValues[i]) != compareValueKey(originValues[i])) return false;
if (compareValueKey(finalValues[i]) != compareValueKey(originValues[originStartIndex + i])) return false;
}
return true;
}
static std::string matchStyleForValues(bool hasOrigin,
static int displayIndexForOriginIndex(const std::vector<int>& originIndexes, int originIndex) {
auto it = std::find(originIndexes.begin(), originIndexes.end(), originIndex);
if (it == originIndexes.end()) return -1;
return static_cast<int>(std::distance(originIndexes.begin(), it));
}
static bool valuesEqualForCompareByOriginIndex(const std::vector<std::string>& finalValues,
const std::vector<std::string>& originValues,
const std::vector<int>& originIndexes,
int originPickStart) {
if (originPickStart == std::numeric_limits<int>::min()) {
return valuesEqualForCompare(finalValues, originValues, 0);
}
if (originIndexes.empty()) {
int originStartIndex = originPickStart < 0 ? -1 : originPickStart;
return valuesEqualForCompare(finalValues, originValues, originStartIndex);
}
for (size_t i = 0; i < finalValues.size(); ++i) {
int displayIndex = displayIndexForOriginIndex(originIndexes, originPickStart + static_cast<int>(i));
if (displayIndex < 0 || displayIndex >= static_cast<int>(originValues.size())) return false;
if (compareValueKey(finalValues[i]) != compareValueKey(originValues[displayIndex])) return false;
}
return true;
}
static bool parseArrayBaseRange(const std::string& arrayBase, int& start, int& end) {
std::smatch m;
static const std::regex re(R"(^\s*(-?\d+)\s*-->\s*(-?\d+)\s*$)");
if (!std::regex_match(arrayBase, m, re)) return false;
start = std::stoi(m[1].str());
end = std::stoi(m[2].str());
return start <= end;
}
static int parseOffsetText(const std::string& text, int fallback = 0) {
try {
if (trimValue(text).empty()) return fallback;
return std::stoi(trimValue(text));
} catch (...) {
return fallback;
}
}
static ComparePlan comparePlanForItem(const std::string& finalKey, const OutItem& item) {
ComparePlan plan;
(void)finalKey;
int baseStart = 0;
int baseEnd = 0;
if (!parseArrayBaseRange(item.arrayBase, baseStart, baseEnd)) return plan;
int valueOffset = parseOffsetText(item.valueOffset, 0);
plan.originPickStart = baseStart + valueOffset;
plan.originPickEnd = baseEnd + valueOffset;
return plan;
}
static std::string matchStyleForValues(bool hasOriginContext,
bool hasOriginMatch,
const std::vector<std::string>& finalValues,
const std::vector<std::string>& originDisplayValues) {
const std::vector<std::string>& originDisplayValues,
int originStartIndex = 0) {
if (hasNullValue(finalValues)) return "MatchRed";
if (!hasOrigin) return "";
return valuesEqualForCompare(finalValues, originDisplayValues) ? "MatchGreen" : "MatchOrange";
if (!hasOriginContext) return "";
if (!hasOriginMatch) return "MatchRed";
return valuesEqualForCompare(finalValues, originDisplayValues, originStartIndex) ? "MatchGreen" : "MatchOrange";
}
static void addRowStats(SheetStats* stats, const std::string& rowStyle) {
if (!stats) return;
if (rowStyle == "MatchGreen") ++stats->normal;
else if (rowStyle == "MatchOrange") ++stats->abnormal;
else if (rowStyle == "MatchRed") ++stats->missing;
}
static std::string cellAttrs(const std::string& styleId, const std::string& extra = "") {
@@ -1422,6 +1649,16 @@ static std::string originSeriesKey(const std::string& finalKey, const OutItem& i
return replaceKeyRange(finalKey, start, end);
}
static std::string originSeriesKeyForMatch(const std::string& finalKey,
const OutItem& item,
const OriginMatch& match) {
if (match.rangeStart != std::numeric_limits<int>::min() &&
match.rangeEnd != std::numeric_limits<int>::min()) {
return replaceKeyRange(finalKey, match.rangeStart, match.rangeEnd);
}
return originSeriesKey(finalKey, item);
}
static void writeSsCell(std::ofstream& ofs, const std::string& value, const std::string& attrs = "") {
ofs << "<Cell" << attrs << "><Data ss:Type=\"String\">" << htmlEscape(value) << "</Data></Cell>";
}
@@ -1438,6 +1675,33 @@ static std::string displayLabelForSheet(const OutItem& item) {
return item.label;
}
static std::string originHeaderFieldValue(const OriginHeader& header, const std::string& key) {
for (const auto& field : header.fields) {
if (field.key == key) return field.val;
}
return "";
}
static std::vector<OriginHeader> visibleOriginHeadersForSheet(OriginData* origin,
const std::vector<OutItem>& items,
const std::string& wiring) {
std::vector<OriginHeader> visible;
if (!origin) return visible;
OriginData probe = *origin;
std::set<int> usedOrders;
for (const auto& item : items) {
if (item.wiring != wiring) continue;
OriginMatch om = matchOrigin(&probe, item.doPath, item.daPath, item.stat, item.phase, item.wiring);
if (!om.value.empty() && om.sourceOrder >= 0) usedOrders.insert(om.sourceOrder);
}
for (const auto& header : origin->headers) {
if (usedOrders.count(header.sourceOrder)) visible.push_back(header);
}
return visible;
}
static void writeDataWorksheet(std::ofstream& ofs,
const std::string& sheetName,
const std::string& wiring,
@@ -1445,9 +1709,11 @@ static void writeDataWorksheet(std::ofstream& ofs,
const std::vector<std::string>& blocks,
const std::vector<OutItem>& items,
OriginData* origin,
bool showStatBlocks = true) {
const int maxValues = 50;
const int totalCols = 9 + maxValues;
bool showStatBlocks,
SheetStats* stats) {
const int maxValues = 64;
std::vector<OriginHeader> visibleOriginHeaders = visibleOriginHeadersForSheet(origin, items, wiring);
const int totalCols = std::max(9 + maxValues, 3 + static_cast<int>(visibleOriginHeaders.size()));
ofs << "<Worksheet ss:Name=\"" << htmlEscape(sheetName) << "\"><Table>\n";
ofs << "<Column ss:Width=\"210\"/><Column ss:Width=\"75\"/><Column ss:Width=\"85\"/><Column ss:Width=\"85\"/>";
@@ -1460,10 +1726,34 @@ static void writeDataWorksheet(std::ofstream& ofs,
writeSsCell(ofs, "头部信息", " ss:StyleID=\"Section\" ss:MergeAcross=\"" + std::to_string(totalCols - 1) + "\"");
ofs << "</Row>\n";
for (const auto& h : headers) {
size_t originHeaderRows = visibleOriginHeaders.empty() ? 0 : originHeaderDisplayKeys().size() + 1;
size_t headerRows = std::max(headers.size(), originHeaderRows);
for (size_t row = 0; row < headerRows; ++row) {
writeSsRowStart(ofs);
writeSsCell(ofs, h.key, " ss:StyleID=\"Default\"");
writeSsCell(ofs, h.val, " ss:StyleID=\"Default\" ss:MergeAcross=\"" + std::to_string(totalCols - 2) + "\"");
if (row < headers.size()) {
writeSsCell(ofs, headers[row].key, " ss:StyleID=\"Default\"");
writeSsCell(ofs, headers[row].val, " ss:StyleID=\"Default\"");
} else {
writeSsCell(ofs, "", " ss:StyleID=\"Default\"");
writeSsCell(ofs, "", " ss:StyleID=\"Default\"");
}
if (!visibleOriginHeaders.empty()) {
if (row == 0) {
writeSsCell(ofs, "原始报告", " ss:StyleID=\"Section\"");
for (const auto& oh : visibleOriginHeaders) {
writeSsCell(ofs, oh.fileName.empty() ? oh.title : oh.fileName, " ss:StyleID=\"Section\"");
}
} else {
size_t fieldIndex = row - 1;
const auto& displayKeys = originHeaderDisplayKeys();
std::string key = fieldIndex < displayKeys.size() ? displayKeys[fieldIndex] : "";
writeSsCell(ofs, key, " ss:StyleID=\"Default\"");
for (const auto& oh : visibleOriginHeaders) {
writeSsCell(ofs, key.empty() ? "" : originHeaderFieldValue(oh, key), " ss:StyleID=\"Default\"");
}
}
}
ofs << "</Row>\n";
}
@@ -1499,16 +1789,34 @@ static void writeDataWorksheet(std::ofstream& ofs,
ofs << "</Row>\n";
}
OriginMatch om = matchOrigin(origin, item.doPath, item.daPath, item.stat, item.phase, item.wiring);
std::string finalKey, finalValue;
splitFinalText(item.finalText, finalKey, finalValue);
finalKey = offsetFinalKey(finalKey, item);
ComparePlan comparePlan = comparePlanForItem(finalKey, item);
OriginMatch om = matchOrigin(origin, item.doPath, item.daPath, item.stat, item.phase, item.wiring,
comparePlan.originPickStart, comparePlan.originPickEnd);
std::vector<std::string> finalValues = splitCommaValues(finalValue);
std::vector<std::string> originValuesRaw = splitCommaValues(om.value);
std::vector<std::string> originValues = roundOriginValuesForDisplay(originValuesRaw);
bool hasOriginMatch = origin && !om.value.empty();
std::string rowStyle = matchStyleForValues(hasOriginMatch, finalValues, originValues);
std::string originKey = originSeriesKey(finalKey, item);
int finalRangeStart = 0;
int finalRangeEnd = 0;
bool isSequenceRow = parseKeyRange(finalKey, finalRangeStart, finalRangeEnd);
int originStartIndex = -1;
if (comparePlan.originPickStart != std::numeric_limits<int>::min() &&
om.rangeStart != std::numeric_limits<int>::min()) {
originStartIndex = displayIndexForOriginIndex(om.valueIndexes, comparePlan.originPickStart);
}
std::string rowStyle;
if (hasNullValue(finalValues)) rowStyle = "MatchRed";
else if (!origin) rowStyle = "";
else if (!hasOriginMatch) rowStyle = "MatchRed";
else rowStyle = valuesEqualForCompareByOriginIndex(finalValues, originValues, om.valueIndexes,
comparePlan.originPickStart)
? "MatchGreen"
: "MatchOrange";
addRowStats(stats, rowStyle);
std::string originKey = originSeriesKeyForMatch(finalKey, item, om);
writeSsRowStart(ofs);
writeSsCell(ofs, displayLabelForSheet(item), cellAttrs(rowStyle, " ss:MergeDown=\"1\""));
@@ -1526,7 +1834,12 @@ static void writeDataWorksheet(std::ofstream& ofs,
writeSsRowStart(ofs);
writeSsCell(ofs, "原始值", cellAttrs(rowStyle, " ss:Index=\"8\""));
writeSsCell(ofs, om.value.empty() ? "" : originKey, cellAttrs(rowStyle));
for (int i = 0; i < maxValues; ++i) writeSsCell(ofs, i < (int)originValues.size() ? originValues[i] : "", cellAttrs(rowStyle));
for (int i = 0; i < maxValues; ++i) {
std::string attrs = (isSequenceRow && hasOriginMatch && i == originStartIndex)
? " ss:StyleID=\"OriginPickBlue\""
: cellAttrs(rowStyle);
writeSsCell(ofs, i < (int)originValues.size() ? originValues[i] : "", attrs);
}
ofs << "</Row>\n";
lastCategory = item.category;
@@ -1555,7 +1868,7 @@ static void writeOriginRecordWorksheet(std::ofstream& ofs,
bool roundValues) {
const int maxValues = 50;
ofs << "<Worksheet ss:Name=\"" << htmlEscape(sheetName) << "\"><Table>\n";
ofs << "<Column ss:Width=\"320\"/><Column ss:Width=\"120\"/><Column ss:Width=\"160\"/>";
ofs << "<Column ss:Width=\"320\"/><Column ss:Width=\"120\"/><Column ss:Width=\"160\"/><Column ss:Width=\"220\"/>";
for (int i = 0; i < maxValues; ++i) ofs << "<Column ss:Width=\"90\"/>";
ofs << "\n";
@@ -1563,6 +1876,7 @@ static void writeOriginRecordWorksheet(std::ofstream& ofs,
writeSsCell(ofs, "原始路径", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "报告名", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "icd中代表的字段", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "原始值", " ss:StyleID=\"Head\"");
for (int i = 1; i <= maxValues; ++i) writeSsCell(ofs, "" + std::to_string(i), " ss:StyleID=\"Head\"");
ofs << "</Row>\n";
@@ -1576,6 +1890,7 @@ static void writeOriginRecordWorksheet(std::ofstream& ofs,
writeSsCell(ofs, rec.path);
writeSsCell(ofs, rec.reportName);
writeSsCell(ofs, "");
writeSsCell(ofs, rec.value);
for (int j = 0; j < maxValues; ++j) writeSsCell(ofs, j < (int)originValues.size() ? originValues[j] : "");
ofs << "</Row>\n";
}
@@ -1583,13 +1898,49 @@ static void writeOriginRecordWorksheet(std::ofstream& ofs,
}
static void writeMatchedWorksheet(std::ofstream& ofs, const OriginData& origin) {
writeOriginRecordWorksheet(ofs, origin, "已匹配的原始数据", true, false);
writeOriginRecordWorksheet(ofs, origin, "已匹配的原始数据", true, true);
}
static void writeUnmatchedWorksheet(std::ofstream& ofs, const OriginData& origin) {
writeOriginRecordWorksheet(ofs, origin, "未匹配的原始数据", false, true);
}
static void writeSummaryWorksheet(std::ofstream& ofs, const std::vector<SheetStats>& stats) {
ofs << "<Worksheet ss:Name=\"统计信息\"><Table>\n";
ofs << "<Column ss:Width=\"180\"/><Column ss:Width=\"110\"/><Column ss:Width=\"110\"/><Column ss:Width=\"110\"/><Column ss:Width=\"90\"/>\n";
writeSsRowStart(ofs);
writeSsCell(ofs, "工作表", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "数据正常个数", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "数据异常个数", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "数据缺失个数", " ss:StyleID=\"Head\"");
writeSsCell(ofs, "总数", " ss:StyleID=\"Head\"");
ofs << "</Row>\n";
int totalNormal = 0;
int totalAbnormal = 0;
int totalMissing = 0;
for (const auto& item : stats) {
int total = item.normal + item.abnormal + item.missing;
totalNormal += item.normal;
totalAbnormal += item.abnormal;
totalMissing += item.missing;
writeSsRowStart(ofs);
writeSsCell(ofs, item.sheetName);
writeSsCell(ofs, std::to_string(item.normal));
writeSsCell(ofs, std::to_string(item.abnormal));
writeSsCell(ofs, std::to_string(item.missing));
writeSsCell(ofs, std::to_string(total));
ofs << "</Row>\n";
}
writeSsRowStart(ofs);
writeSsCell(ofs, "合计", " ss:StyleID=\"Section\"");
writeSsCell(ofs, std::to_string(totalNormal), " ss:StyleID=\"Section\"");
writeSsCell(ofs, std::to_string(totalAbnormal), " ss:StyleID=\"Section\"");
writeSsCell(ofs, std::to_string(totalMissing), " ss:StyleID=\"Section\"");
writeSsCell(ofs, std::to_string(totalNormal + totalAbnormal + totalMissing), " ss:StyleID=\"Section\"");
ofs << "</Row>\n";
ofs << "</Table></Worksheet>\n";
}
static std::vector<HeaderItem> headersForSheet(const std::vector<HeaderItem>& headers, const std::string& dataType) {
std::vector<HeaderItem> out = headers;
std::string displayDataType = dataType;
@@ -1660,6 +2011,7 @@ static void writeExcelWorkbook(const fs::path& outPath,
ofs << "<Style ss:ID=\"MatchGreen\" ss:Parent=\"Default\"><Interior ss:Color=\"#D9EAD3\" ss:Pattern=\"Solid\"/></Style>\n";
ofs << "<Style ss:ID=\"MatchOrange\" ss:Parent=\"Default\"><Interior ss:Color=\"#FCE4D6\" ss:Pattern=\"Solid\"/></Style>\n";
ofs << "<Style ss:ID=\"MatchRed\" ss:Parent=\"Default\"><Interior ss:Color=\"#F4CCCC\" ss:Pattern=\"Solid\"/></Style>\n";
ofs << "<Style ss:ID=\"OriginPickBlue\" ss:Parent=\"Default\"><Interior ss:Color=\"#CFE2F3\" ss:Pattern=\"Solid\"/></Style>\n";
ofs << "</Styles>\n";
std::vector<SheetSpec> sheets = {
@@ -1679,6 +2031,7 @@ static void writeExcelWorkbook(const fs::path& outPath,
const std::vector<HeaderItem> emptyHeaders;
const std::unordered_map<std::string, std::string> emptyValues;
std::vector<SheetStats> summaryStats;
for (const auto& spec : sheets) {
if (!specAllowsFrontType(spec, frontType)) continue;
auto bucketIt = dataByType.find(spec.dataType);
@@ -1687,12 +2040,16 @@ static void writeExcelWorkbook(const fs::path& outPath,
bucketIt == dataByType.end() ? emptyValues : bucketIt->second.valueByKey;
std::vector<OutItem> sheetItems = buildItemsForSheet(valueMetas, spec, valueByKey);
sortOutItems(sheetItems);
writeDataWorksheet(ofs, spec.name, spec.wiring, headersForSheet(headers, spec.dataType), blocks, sheetItems, origin, spec.showStatBlocks);
}
if (origin) {
writeMatchedWorksheet(ofs, *origin);
writeUnmatchedWorksheet(ofs, *origin);
SheetStats stats;
stats.sheetName = spec.name;
writeDataWorksheet(ofs, spec.name, spec.wiring, headersForSheet(headers, spec.dataType), blocks, sheetItems, origin, spec.showStatBlocks, &stats);
summaryStats.push_back(stats);
}
writeSummaryWorksheet(ofs, summaryStats);
OriginData emptyOrigin;
const OriginData& originForRawSheets = origin ? *origin : emptyOrigin;
writeMatchedWorksheet(ofs, originForRawSheets);
writeUnmatchedWorksheet(ofs, originForRawSheets);
ofs << "</Workbook>\n";
}