cloud-web-corejs 1.0.54-dev.424 → 1.0.54-dev.425
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -125,6 +125,7 @@ export function calculateFormula(VFR, DSV, formulaJs, formulaFieldRef, changedFi
|
|
|
125
125
|
return formulaValue
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
+
// ... existing code ...
|
|
128
129
|
export function baseCalculateFormula(that,formulaStr, row /* VFR, DSV, formulaJs, formulaFieldRef, changedFieldRef */) {
|
|
129
130
|
if(!formulaStr)return
|
|
130
131
|
let formula = formulaStr;
|
|
@@ -132,15 +133,62 @@ export function baseCalculateFormula(that,formulaStr, row /* VFR, DSV, formulaJs
|
|
|
132
133
|
let DSV = that.getGlobalDsv()
|
|
133
134
|
let formulaJs = formulajs
|
|
134
135
|
let formulaFieldRef = that;
|
|
136
|
+
let changedFieldRef = arguments[5];
|
|
137
|
+
|
|
138
|
+
// 增强子表单字段变化时的计算范围控制
|
|
135
139
|
if (formulaFieldRef.tableParam) {
|
|
136
|
-
} else if (!!formulaFieldRef.subFormItemFlag && !!changedFieldRef
|
|
137
|
-
/*
|
|
138
|
-
|
|
140
|
+
} else if (!!formulaFieldRef.subFormItemFlag && !!changedFieldRef?.subFormItemFlag) {
|
|
141
|
+
/* 子表单字段变化,只能触发子表单同一行字段的计算公式重新计算 */
|
|
142
|
+
if (changedFieldRef.subFormRowId !== formulaFieldRef.subFormRowId) {
|
|
139
143
|
return
|
|
140
|
-
}
|
|
144
|
+
}
|
|
141
145
|
}
|
|
142
146
|
|
|
143
|
-
|
|
147
|
+
// 预处理:识别并标记聚合函数,记录它们的参数字段所属子表单
|
|
148
|
+
const aggregationFunctions = ['SUM', 'AVERAGE', 'MAX', 'MIN', 'SUMIF', 'SUMIFS'];
|
|
149
|
+
let functionCalls = [];
|
|
150
|
+
|
|
151
|
+
// 解析公式中的函数调用
|
|
152
|
+
// 方案1:先替换占位符,再解析函数调用
|
|
153
|
+
// 临时处理公式中的占位符,以便正确识别函数调用
|
|
154
|
+
let tempFormula = formulaStr;
|
|
155
|
+
const placeholders = tempFormula.match(/\{\{([^}]*)\}\}/g) || [];
|
|
156
|
+
placeholders.forEach(placeholder => {
|
|
157
|
+
// 处理函数占位符
|
|
158
|
+
if (placeholder.includes('.func')) {
|
|
159
|
+
const parts = placeholder.split('.');
|
|
160
|
+
if (parts.length >= 2) {
|
|
161
|
+
const fieldId = parts[0].substring(2); // 去掉开头的 {{
|
|
162
|
+
const funcName = parts[1];
|
|
163
|
+
// 临时替换函数占位符为函数名,便于后续解析
|
|
164
|
+
tempFormula = tempFormula.replace(placeholder, funcName);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// 解析公式中的函数调用 - 修改正则表达式,支持大小写字母
|
|
170
|
+
let functionCallMatch;
|
|
171
|
+
const functionCallRegex = /([A-Za-z]+)\s*\(([^)]*)\)/g;
|
|
172
|
+
while ((functionCallMatch = functionCallRegex.exec(tempFormula)) !== null) {
|
|
173
|
+
const funcName = functionCallMatch[1].toUpperCase();
|
|
174
|
+
const params = functionCallMatch[2];
|
|
175
|
+
|
|
176
|
+
if (aggregationFunctions.includes(funcName)) {
|
|
177
|
+
// 找出原始公式中对应的位置
|
|
178
|
+
const originalStart = formulaStr.indexOf(functionCallMatch[0].substring(0, 10));
|
|
179
|
+
|
|
180
|
+
functionCalls.push({
|
|
181
|
+
name: funcName,
|
|
182
|
+
params: params,
|
|
183
|
+
fullMatch: functionCallMatch[0],
|
|
184
|
+
start: originalStart > -1 ? originalStart : functionCallMatch.index
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 替换字段值,增加对子表单引用子表单场景的特殊处理
|
|
190
|
+
formula = baseReplaceFieldsAndFunctionsOfFormula(VFR, formulaFieldRef, formula, row, functionCalls)
|
|
191
|
+
|
|
144
192
|
if (!formula) {
|
|
145
193
|
return
|
|
146
194
|
}
|
|
@@ -158,26 +206,17 @@ export function baseCalculateFormula(that,formulaStr, row /* VFR, DSV, formulaJs
|
|
|
158
206
|
})
|
|
159
207
|
}
|
|
160
208
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
return (widget.options.keyNameEnabled && widget.options.keyName) || o;
|
|
209
|
+
try {
|
|
210
|
+
const formulaValue = evalFn(formula, DSV, VFR, formulaJs)
|
|
211
|
+
return formulaValue
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.error('公式计算错误:', error, '公式:', formula);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
169
216
|
}
|
|
170
217
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* 替换计算公式中的字段值
|
|
176
|
-
* @param VFR
|
|
177
|
-
* @param formulaFieldRef
|
|
178
|
-
* @returns {*}
|
|
179
|
-
*/
|
|
180
|
-
export function baseReplaceFieldsAndFunctionsOfFormula(VFR, formulaFieldRef, formula, row) {
|
|
218
|
+
// 优化 baseReplaceFieldsAndFunctionsOfFormula 方法,增加对聚合函数的特殊处理
|
|
219
|
+
export function baseReplaceFieldsAndFunctionsOfFormula(VFR, formulaFieldRef, formula, row, functionCalls = []) {
|
|
181
220
|
// let formula = formulaFieldRef.field.options.formula
|
|
182
221
|
const matchResult = formula.match(FORMULA_REG_EXP)
|
|
183
222
|
if (!matchResult) {
|
|
@@ -186,16 +225,16 @@ export function baseReplaceFieldsAndFunctionsOfFormula(VFR, formulaFieldRef, for
|
|
|
186
225
|
|
|
187
226
|
let resultFormula = formula
|
|
188
227
|
let quitFlag = false
|
|
228
|
+
|
|
229
|
+
// 记录所有字段引用及其所属子表单
|
|
230
|
+
let fieldReferences = [];
|
|
231
|
+
|
|
189
232
|
matchResult.forEach(mi => {
|
|
190
233
|
const thirdPart = mi.split('.')[2]
|
|
191
234
|
const nodeType = thirdPart.substring(0, thirdPart.length - 2)
|
|
192
235
|
if (nodeType === 'func') {
|
|
193
236
|
const funcName = mi.split('.')[1]
|
|
194
237
|
resultFormula = resultFormula.replace(mi, funcName)
|
|
195
|
-
|
|
196
|
-
/* if(funcName === "SUMIF" || funcName === "SUMIFS"){
|
|
197
|
-
debugger
|
|
198
|
-
} */
|
|
199
238
|
return
|
|
200
239
|
}
|
|
201
240
|
|
|
@@ -205,64 +244,124 @@ export function baseReplaceFieldsAndFunctionsOfFormula(VFR, formulaFieldRef, for
|
|
|
205
244
|
if (!!fieldSchema) {
|
|
206
245
|
let fieldRef = VFR.getWidgetRef(fieldSchema.options.name)
|
|
207
246
|
if (!!fieldRef) {
|
|
208
|
-
//是否要考虑字符串值的替换??
|
|
209
|
-
// resultFormula = resultFormula.replace(mi, fieldRef.currentValue)
|
|
210
247
|
let fieldKeyName = getFieldKeyName(fieldSchema)
|
|
211
248
|
let value = fieldRef.formModel[fieldKeyName]??null;
|
|
212
249
|
resultFormula = resultFormula.replace(mi, value)
|
|
213
250
|
} else { //getWidgetRef找不到,则可能是子表单字段
|
|
214
251
|
const subFormNameOfField = VFR.getSubFormNameOfWidget(fieldSchema.options.name)
|
|
252
|
+
|
|
253
|
+
// 记录字段引用信息
|
|
254
|
+
fieldReferences.push({
|
|
255
|
+
placeholder: mi,
|
|
256
|
+
fieldName: fieldSchema.options.name,
|
|
257
|
+
subFormName: subFormNameOfField
|
|
258
|
+
});
|
|
259
|
+
|
|
215
260
|
if (!!formulaFieldRef.subFormItemFlag || formulaFieldRef.tableParam || row) {
|
|
216
261
|
/* 如果当前计算字段是子表单字段,要判断公式中的子表单字段是否和当前计算字段是否属于同一子表单!! */
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
if
|
|
236
|
-
let
|
|
262
|
+
// 判断是否是同一子表单
|
|
263
|
+
const isSameSubForm = subFormNameOfField === formulaFieldRef?.parentWidget?.options?.name ||
|
|
264
|
+
subFormNameOfField === formulaFieldRef.subFormName;
|
|
265
|
+
|
|
266
|
+
// 检查该字段是否在某个聚合函数的参数中
|
|
267
|
+
let isInAggregationParam = false;
|
|
268
|
+
functionCalls.forEach(call => {
|
|
269
|
+
if (call.params.includes(mi)) {
|
|
270
|
+
isInAggregationParam = true;
|
|
271
|
+
}
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
/* if(functionCalls.length){
|
|
275
|
+
debugger
|
|
276
|
+
} */
|
|
277
|
+
|
|
278
|
+
if (isSameSubForm && !isInAggregationParam) {
|
|
279
|
+
// 同一子表单且不在聚合函数参数中,使用当前行数据
|
|
280
|
+
if(row){
|
|
281
|
+
let fieldKeyName = getFieldKeyName(fieldSchema)
|
|
282
|
+
let value = row[fieldKeyName]??null;
|
|
237
283
|
resultFormula = resultFormula.replaceAll(mi, value)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
|
|
284
|
+
}else if (subFormNameOfField === formulaFieldRef?.parentWidget?.options?.name) {
|
|
285
|
+
let subFormRowId = formulaFieldRef.tableParam.row._X_ROW_KEY
|
|
286
|
+
fieldRef = VFR.getWidgetRef(fieldSchema.options.name + '_' + subFormRowId)
|
|
287
|
+
if (!!fieldRef) {
|
|
288
|
+
let value = fieldRef.currentValue??null;
|
|
289
|
+
resultFormula = resultFormula.replaceAll(mi, value)
|
|
290
|
+
} else {
|
|
291
|
+
quitFlag = true
|
|
292
|
+
console.error('Field not found: ' + fieldSchema.options.label)
|
|
293
|
+
}
|
|
294
|
+
} else if (subFormNameOfField === formulaFieldRef.subFormName) {
|
|
295
|
+
fieldRef = VFR.getWidgetRef(fieldSchema.options.name + '@row' + formulaFieldRef.subFormRowId)
|
|
296
|
+
if (!!fieldRef) {
|
|
297
|
+
resultFormula = resultFormula.replaceAll(mi, fieldRef.currentValue)
|
|
298
|
+
} else {
|
|
299
|
+
quitFlag = true
|
|
300
|
+
console.error('Field not found: ' + fieldSchema.options.label)
|
|
301
|
+
}
|
|
241
302
|
}
|
|
242
|
-
} else
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
quitFlag = true
|
|
248
|
-
|
|
303
|
+
} else {
|
|
304
|
+
// 不同子表单或在聚合函数参数中,使用全部明细行数据创建数组表达式
|
|
305
|
+
let dataTableRef = VFR.getWidgetRef(subFormNameOfField)
|
|
306
|
+
if (!dataTableRef) {
|
|
307
|
+
console.error('Subform not found: ' + subFormNameOfField);
|
|
308
|
+
quitFlag = true;
|
|
309
|
+
return;
|
|
249
310
|
}
|
|
250
|
-
|
|
251
|
-
|
|
311
|
+
|
|
312
|
+
const subFormValue = VFR.formDataModel[fieldKeyName(dataTableRef.widget)] || [];
|
|
313
|
+
const subFieldName = fieldKeyName(fieldSchema);
|
|
314
|
+
|
|
315
|
+
// 构建有效的数组表达式
|
|
316
|
+
let arrayExpr = '[';
|
|
317
|
+
subFormValue.forEach((vi, idx) => {
|
|
318
|
+
const val = vi[subFieldName] ?? null;
|
|
319
|
+
// 确保数值类型正确处理
|
|
320
|
+
if (val === null || val === undefined || val === '') {
|
|
321
|
+
arrayExpr += (idx === 0) ? '0' : ', 0';
|
|
322
|
+
} else if (typeof val === 'string' && !isNaN(Number(val))) {
|
|
323
|
+
arrayExpr += (idx === 0) ? Number(val) : ', ' + Number(val);
|
|
324
|
+
} else if (typeof val === 'number') {
|
|
325
|
+
arrayExpr += (idx === 0) ? val : ', ' + val;
|
|
326
|
+
} else {
|
|
327
|
+
arrayExpr += (idx === 0) ? '0' : ', 0';
|
|
328
|
+
}
|
|
329
|
+
});
|
|
330
|
+
arrayExpr += ']';
|
|
331
|
+
|
|
332
|
+
resultFormula = resultFormula.replaceAll(mi, arrayExpr);
|
|
252
333
|
}
|
|
253
334
|
} else {
|
|
254
335
|
/* 在主表单字段的计算公式中使用子表单字段,应将子表单所有记录同字段的值代入!! */
|
|
255
|
-
// const subFormValue = VFR.formDataModel[subFormNameOfField]
|
|
256
336
|
let dataTableRef = VFR.getWidgetRef(subFormNameOfField)
|
|
257
337
|
const subFormValue = VFR.formDataModel[fieldKeyName(dataTableRef.widget)]
|
|
258
338
|
let allSubFieldValues = ''
|
|
259
|
-
// const subFieldName = fieldSchema.options.name
|
|
260
339
|
const subFieldName = fieldKeyName(fieldSchema)
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
340
|
+
|
|
341
|
+
// 对于聚合函数,创建数组表达式
|
|
342
|
+
let isInAggregationParam = false;
|
|
343
|
+
functionCalls.forEach(call => {
|
|
344
|
+
if (call.params.includes(mi)) {
|
|
345
|
+
isInAggregationParam = true;
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
if (isInAggregationParam) {
|
|
350
|
+
let arrayExpr = '[';
|
|
351
|
+
subFormValue.forEach((vi, idx) => {
|
|
352
|
+
const val = vi[subFieldName] ?? 0;
|
|
353
|
+
arrayExpr += (idx === 0) ? val : ', ' + val;
|
|
354
|
+
});
|
|
355
|
+
arrayExpr += ']';
|
|
356
|
+
resultFormula = resultFormula.replaceAll(mi, arrayExpr);
|
|
357
|
+
} else {
|
|
358
|
+
// 非聚合函数,使用逗号分隔的字符串
|
|
359
|
+
subFormValue.forEach((vi, idx) => {
|
|
360
|
+
let val = vi[subFieldName]??null
|
|
361
|
+
allSubFieldValues = (idx === 0) ? val : allSubFieldValues + ', ' + val
|
|
362
|
+
})
|
|
363
|
+
resultFormula = resultFormula.replaceAll(mi, allSubFieldValues)
|
|
364
|
+
}
|
|
266
365
|
}
|
|
267
366
|
}
|
|
268
367
|
}
|
|
@@ -270,6 +369,17 @@ export function baseReplaceFieldsAndFunctionsOfFormula(VFR, formulaFieldRef, for
|
|
|
270
369
|
|
|
271
370
|
return quitFlag ? null : resultFormula
|
|
272
371
|
}
|
|
372
|
+
// ... existing code ...
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
function getFieldKeyName(widget) {
|
|
376
|
+
let o = widget.options.name;
|
|
377
|
+
return (widget.options.keyNameEnabled && widget.options.keyName) || o;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function funOtherHanlde(){
|
|
381
|
+
|
|
382
|
+
}
|
|
273
383
|
|
|
274
384
|
export function calculateFormula2(formulaStr, data, that) {
|
|
275
385
|
if(!formulaStr)return
|