From c2398150b44b54a1f08e7af4b6a64f0c170046f0 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 27 十月 2026 14:35:48 +0800
Subject: [PATCH] fix: 支持计算值和最终值按批注格式化
---
src/utils/inspectionValueFormat.js | 9 +++-
src/utils/excelFountion.js | 25 +++++-------
src/workers/InspectionWorker.worker.js | 40 ++++++++-----------
3 files changed, 34 insertions(+), 40 deletions(-)
diff --git a/src/utils/excelFountion.js b/src/utils/excelFountion.js
index a61500b..0013a84 100644
--- a/src/utils/excelFountion.js
+++ b/src/utils/excelFountion.js
@@ -61,21 +61,18 @@
*/
function AVERAGE(...val) {
try {
- let num = null;
- let arr = [];
- if (val && val.length > 0) {
- arr = val.filter((item) => item != null && item !== "");
- arr.forEach((item) => {
- num += item;
- });
- if (arr.length > 0) {
- return num / arr.length;
- } else {
- return null;
+ // 鍗曞厓鏍煎�肩粡甯镐互瀛楃涓插舰寮忎紶鍏ワ紱鍏堝睍寮�鍙傛暟骞惰浆鎴愭暟瀛楋紝閬垮厤
+ // `AVERAGE(C3:D3)` 鍦ㄥ睍寮�鍚庢敹鍒板瓧绗︿覆鑰屽緱鍒� NaN銆�
+ const values = [];
+ const flatten = (items) => items.forEach((item) => {
+ if (Array.isArray(item)) return flatten(item);
+ if (item !== null && item !== undefined && item !== "" && item !== " ") {
+ const number = Number(item);
+ if (Number.isFinite(number)) values.push(number);
}
- } else {
- return null;
- }
+ });
+ flatten(val);
+ return values.length ? values.reduce((sum, item) => sum + item, 0) / values.length : null;
} catch (error) {}
}
/**
diff --git a/src/utils/inspectionValueFormat.js b/src/utils/inspectionValueFormat.js
index f9b6b16..4a5858e 100644
--- a/src/utils/inspectionValueFormat.js
+++ b/src/utils/inspectionValueFormat.js
@@ -1,9 +1,10 @@
/**
* 瑙f瀽鍘熷璁板綍妯℃澘涓殑妫�楠屽�兼壒娉ㄣ��
- * 鍏煎鏃ф牸寮忊�滄楠屽�尖�濓紝骞舵敮鎸侊細妫�楠屽�硷紝2銆佹楠屽�硷紝2锛岀瀛﹁鏁版硶銆�
+ * 鏀寔妫�楠屽�笺�佽绠楀�笺�佹渶缁堝�肩殑鎵规敞锛屼緥濡傦細妫�楠屽�硷紝2銆佽绠楀�硷紝2锛屾渶缁堝�硷紝2锛岀瀛﹁鏁版硶銆�
*/
export function parseInspectionValueAnnotation(annotation) {
- if (typeof annotation !== 'string' || !annotation.includes('妫�楠屽��')) return null;
+ const targetNames = ['妫�楠屽��', '璁$畻鍊�', '鏈�缁堝��'];
+ if (typeof annotation !== 'string') return null;
// 鎵规敞鍏佽涓枃閫楀彿銆佽嫳鏂囬�楀彿浠ュ強鈥滃拰鈥濊繛鎺ユ牸寮忓弬鏁般��
const parts = annotation
.replace(/锛�/g, ',')
@@ -12,6 +13,7 @@
.split(',')
.map(item => item.trim())
.filter(Boolean);
+ if (!targetNames.includes(parts[0])) return null;
const decimalIndex = parts.findIndex((item, index) => index > 0 && /^\d+$/.test(item));
const decimalPlaces = decimalIndex < 0 ? null : Number(parts[decimalIndex]);
if (decimalPlaces != null && (!Number.isInteger(decimalPlaces) || decimalPlaces < 0 || decimalPlaces > 15)) {
@@ -31,9 +33,10 @@
item && index + 1 !== decimalIndex && item !== '绉戝璁℃暟娉�' && !roundingNames[item]
);
if (unsupported.length || parts.length > 4) {
- return { error: '妫�楠屽�兼壒娉ㄦ牸寮忓簲涓猴細妫�楠屽�硷紝2锛屽洓鑸嶄簲鍏� 鎴� 妫�楠屽�硷紝2锛岀瀛﹁鏁版硶锛屽洓鑸嶄簲鍏�' };
+ return { error: '鎵规敞鏍煎紡搴斾负锛氭楠屽�硷紝2锛屽洓鑸嶄簲鍏ワ紱璁$畻鍊硷紝2锛涙渶缁堝�硷紝2锛岀瀛﹁鏁版硶' };
}
return {
+ target: parts[0],
decimalPlaces,
displayMode,
// 鍙傛暟椤哄簭鍐冲畾澶勭悊椤哄簭锛氱瀛﹁鏁版硶鍦ㄥ皬鏁颁綅鍓嶈〃绀哄厛杞瀛﹁鏁版硶銆�
diff --git a/src/workers/InspectionWorker.worker.js b/src/workers/InspectionWorker.worker.js
index 119d3ce..3334b84 100644
--- a/src/workers/InspectionWorker.worker.js
+++ b/src/workers/InspectionWorker.worker.js
@@ -1,5 +1,6 @@
// 澶氱嚎绋嬮噷闈㈤渶瑕佷繚瀛樼殑鏁版嵁
import Big from "big.js";
+import { parseInspectionValueAnnotation, formatInspectionValue } from "../utils/inspectionValueFormat";
let code = "";
// 琛ㄦ牸鏁版嵁锛堟覆鏌擄級
@@ -405,7 +406,12 @@
// 鍒ゆ柇褰撳墠琛屾槸鍚︿负褰撳墠妫�楠岄」鎵�鍦ㄨ锛屽鏋滀负褰撳墠琛岋紝鍒欑粰琛ㄦ牸鏁版嵁璧嬪��
for (var b in a) {
if (a[b].c == item.c) {
- a[b].v.v = comResult;
+ // 璁$畻鍊笺�佹渶缁堝�间篃鎸夊崟鍏冩牸鎵规敞鐨勫皬鏁颁綅鏁版牸寮忓寲锛岄伩鍏嶈绠楃粨鏋滅粫杩囧墠绔緭鍏ユ牎楠屻��
+ const annotation = a[b].v && a[b].v.ps && a[b].v.ps.value;
+ const formatConfig = parseInspectionValueAnnotation(annotation);
+ a[b].v.v = formatConfig && !formatConfig.error
+ ? formatInspectionValue(comResult, formatConfig)
+ : comResult;
hasComputedWrite = true;
break;
}
@@ -704,29 +710,17 @@
*/
function AVERAGE(...val) {
try {
- let num = null;
- let arr = [];
- if (
- val != null &&
- val != undefined &&
- val != "undefined" &&
- val != "NaN" &&
- val.length > 0
- ) {
- arr = val.filter(
- (item) => item !== null && item !== "" && item != undefined
- );
- arr.forEach((item) => {
- num += item;
- });
- if (arr.length > 0) {
- return num / arr.length;
- } else {
- return null;
+ // 鍗曞厓鏍煎�煎彲鑳芥槸瀛楃涓诧紱缁熶竴杞暟瀛楀悗鍐嶈绠楋紝鍏煎鍖洪棿鍙傛暟銆�
+ const values = [];
+ const flatten = (items) => items.forEach((item) => {
+ if (Array.isArray(item)) return flatten(item);
+ if (item !== null && item !== undefined && item !== "" && item !== " ") {
+ const number = Number(item);
+ if (Number.isFinite(number)) values.push(number);
}
- } else {
- return null;
- }
+ });
+ flatten(val || []);
+ return values.length ? values.reduce((sum, item) => sum + item, 0) / values.length : null;
} catch (error) {
console.log("error---", error);
}
--
Gitblit v1.9.3