@univerjs/engine-formula 1.0.2 → 1.0.4

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/lib/cjs/index.js CHANGED
@@ -9770,6 +9770,89 @@ function baseValueObjectToArrayValueObject(valueObject) {
9770
9770
  if (valueObject.isArray()) return valueObject;
9771
9771
  return ArrayValueObject.createByArray([[valueObject.getValue()]]);
9772
9772
  }
9773
+ function getEqualityValueKeys(value) {
9774
+ const keys = [];
9775
+ if (value.isString()) keys.push(normalizeTextForComparison(String(value.getValue())));
9776
+ else if (!value.isNumber()) return keys;
9777
+ const number = value.isNumber() ? value : createCriteriaValueObject(String(value.getValue()), value.getDateSystem());
9778
+ if (number.isNumber() && Number.isFinite(Number(number.getValue()))) keys.push(stripErrorMargin(Number(number.getValue())));
9779
+ return keys;
9780
+ }
9781
+ function buildEqualityIndex(range) {
9782
+ const index = /* @__PURE__ */ new Map();
9783
+ range.iterator((value, row, column) => {
9784
+ if (!value) return;
9785
+ const position = [row, column];
9786
+ for (const key of getEqualityValueKeys(value)) {
9787
+ let matches = index.get(key);
9788
+ if (!matches) {
9789
+ matches = [];
9790
+ index.set(key, matches);
9791
+ }
9792
+ matches.push(position);
9793
+ }
9794
+ });
9795
+ return index;
9796
+ }
9797
+ function getIndexedPositions(index, keys, range) {
9798
+ const buckets = keys.map((key) => index.get(key) ?? []);
9799
+ if (buckets.reduce((count, bucket) => count + bucket.length, 0) >= range.getRowCount() * range.getColumnCount() / 2) return;
9800
+ if (buckets.length === 1) return buckets[0];
9801
+ return Array.from(new Set(buckets.flat())).sort((a, b) => a[0] - b[0] || a[1] - b[1]);
9802
+ }
9803
+ function pickRangePositions(range, positions) {
9804
+ return ArrayValueObject.create({
9805
+ calculateValueList: positions.map(([row, column]) => [range.get(row, column)]),
9806
+ rowCount: positions.length,
9807
+ columnCount: 1,
9808
+ unitId: "",
9809
+ sheetId: "",
9810
+ row: -1,
9811
+ column: -1
9812
+ });
9813
+ }
9814
+ function getExactCriteriaKeys(criteria) {
9815
+ if (!criteria || !criteria.isString() && !criteria.isNumber()) return;
9816
+ const [operator, value] = criteria.isString() ? findCompareToken(String(criteria.getValue()), criteria.getDateSystem()) : ["=", criteria];
9817
+ if (operator !== "=" || value.isDateFormat()) return;
9818
+ if (value.isString() && (value.getValue() === "" || isWildcard(String(value.getValue())))) return;
9819
+ const keys = getEqualityValueKeys(value);
9820
+ return keys.length > 0 ? keys : void 0;
9821
+ }
9822
+ function getCriteriaCacheKey(criteria) {
9823
+ if (criteria.length === 0 || criteria.some((value) => getExactCriteriaKeys(value) === void 0)) return;
9824
+ const values = criteria.map((value) => value.getValue());
9825
+ return values.length === 1 ? values[0] : JSON.stringify(values);
9826
+ }
9827
+ function getCriteriaPositions(pairs, indexes, row, column) {
9828
+ for (const { range, criteriaArray } of pairs) {
9829
+ const keys = getExactCriteriaKeys(criteriaArray.get(row, column));
9830
+ if (!keys) continue;
9831
+ let index = indexes.get(range);
9832
+ if (!index) {
9833
+ index = buildEqualityIndex(range);
9834
+ indexes.set(range, index);
9835
+ }
9836
+ const positions = getIndexedPositions(index, keys, range);
9837
+ if (positions !== void 0) return positions;
9838
+ }
9839
+ }
9840
+ function getConditionalAggregateResult(formulaName, comparison, targetRange) {
9841
+ if (formulaName === "COUNTIFS") {
9842
+ let count = 0;
9843
+ comparison.iterator((value) => {
9844
+ if ((value === null || value === void 0 ? void 0 : value.isBoolean()) && value.getValue() === true) count++;
9845
+ });
9846
+ return NumberValueObject.create(count);
9847
+ }
9848
+ const picked = targetRange === null || targetRange === void 0 ? void 0 : targetRange.pick(comparison);
9849
+ switch (formulaName) {
9850
+ case "SUMIFS": return picked.sum();
9851
+ case "AVERAGEIFS": return picked.sum().divided(picked.count());
9852
+ case "MAXIFS": return picked.getColumnCount() === 0 ? NumberValueObject.create(0) : picked.max();
9853
+ case "MINIFS": return picked.getColumnCount() === 0 ? NumberValueObject.create(0) : picked.min();
9854
+ }
9855
+ }
9773
9856
  /**
9774
9857
  * Get the paired range and criteria result for COUNTIFS, SUMIFS, etc.
9775
9858
  */
@@ -9788,50 +9871,48 @@ function getPairedRangeAndCriteriaResult(variants, params) {
9788
9871
  }
9789
9872
  if (rangeAndCriteriaArrays.length === 0) return [];
9790
9873
  const results = [];
9791
- /**
9792
- * Iterate through all criteria values for each dimension, calculate the comparison result with the corresponding range, and then calculate the Boolean intersection of all comparison results as the final result for that dimension criteria value.
9793
- * Then calculate this dimension criteria value's result.
9794
- * This avoiding store the all dimension comparison result in memory and then calculating all dimension criteria value's result, which may cause memory overflow when the range is large and there are multiple dimension criteria.
9795
- * For example, `=COUNTIFS(Q$3:Q$10002,C$3:C$5002,R$3:R$10002,L6)`.
9796
- */
9874
+ const arrayPairs = rangeAndCriteriaArrays.filter((_, index) => {
9875
+ const criteria = variants[index * 2 + 1];
9876
+ return criteria.isArray() && criteria.getRowCount() * criteria.getColumnCount() > 1;
9877
+ });
9878
+ const useEqualityIndex = isNumberSensitive && maxRowLength * maxColumnLength > 1 && arrayPairs.length > 0;
9879
+ const repeatedResults = /* @__PURE__ */ new Map();
9880
+ const equalityIndexes = /* @__PURE__ */ new Map();
9797
9881
  rangeAndCriteriaArrays[0].criteriaArray.iterator((_, rowIndex, columnIndex) => {
9798
9882
  let finalCompareResult;
9799
- for (let i = 0; i < rangeAndCriteriaArrays.length; i++) {
9800
- const { range, criteriaArray } = rangeAndCriteriaArrays[i];
9883
+ let positions;
9884
+ const cacheKey = useEqualityIndex ? getCriteriaCacheKey(arrayPairs.map(({ criteriaArray }) => criteriaArray.get(rowIndex, columnIndex))) : void 0;
9885
+ const cached = cacheKey === void 0 ? void 0 : repeatedResults.get(cacheKey);
9886
+ if (cached) {
9887
+ results[rowIndex] ??= [];
9888
+ results[rowIndex][columnIndex] = cached;
9889
+ return;
9890
+ }
9891
+ if (useEqualityIndex) {
9892
+ positions = getCriteriaPositions(arrayPairs, equalityIndexes, rowIndex, columnIndex);
9893
+ if ((positions === null || positions === void 0 ? void 0 : positions.length) === 0) {
9894
+ results[rowIndex] ??= [];
9895
+ results[rowIndex][columnIndex] = formulaName === "AVERAGEIFS" ? ErrorValueObject.create("#DIV/0!") : NumberValueObject.create(0);
9896
+ return;
9897
+ }
9898
+ }
9899
+ rangeAndCriteriaArrays.forEach(({ range: sourceRange, criteriaArray }) => {
9900
+ const range = positions === void 0 ? sourceRange : pickRangePositions(sourceRange, positions);
9801
9901
  const criteriaValueObject = criteriaArray.get(rowIndex, columnIndex);
9802
- if (!criteriaValueObject) continue;
9902
+ if (!criteriaValueObject) return;
9803
9903
  let compareResult = valueObjectCompare(range, criteriaValueObject);
9804
9904
  if (isNumberSensitive) compareResult = filterSameValueObjectResult(compareResult, range, criteriaValueObject);
9805
9905
  if (finalCompareResult === void 0) {
9806
9906
  finalCompareResult = compareResult;
9807
- continue;
9907
+ return;
9808
9908
  }
9809
9909
  finalCompareResult = booleanObjectIntersection(finalCompareResult, compareResult);
9810
- }
9811
- let result;
9812
- if (formulaName === "COUNTIFS") {
9813
- let count = 0;
9814
- finalCompareResult.iterator((value) => {
9815
- if ((value === null || value === void 0 ? void 0 : value.isBoolean()) && value.getValue() === true) count++;
9816
- });
9817
- result = NumberValueObject.create(count);
9818
- } else if (formulaName === "SUMIFS") result = targetRange.pick(finalCompareResult).sum();
9819
- else if (formulaName === "AVERAGEIFS") {
9820
- const picked = targetRange.pick(finalCompareResult);
9821
- const sum = picked.sum();
9822
- const count = picked.count();
9823
- result = sum.divided(count);
9824
- } else if (formulaName === "MAXIFS") {
9825
- const picked = targetRange.pick(finalCompareResult);
9826
- if (picked.getColumnCount() === 0) result = NumberValueObject.create(0);
9827
- else result = picked.max();
9828
- } else if (formulaName === "MINIFS") {
9829
- const picked = targetRange.pick(finalCompareResult);
9830
- if (picked.getColumnCount() === 0) result = NumberValueObject.create(0);
9831
- else result = picked.min();
9832
- }
9910
+ });
9911
+ const aggregateRange = positions === void 0 || !targetRange ? targetRange : pickRangePositions(targetRange, positions);
9912
+ const result = getConditionalAggregateResult(formulaName, finalCompareResult, aggregateRange);
9833
9913
  if (!results[rowIndex]) results[rowIndex] = [];
9834
9914
  results[rowIndex][columnIndex] = result;
9915
+ if (cacheKey !== void 0 && result) repeatedResults.set(cacheKey, result);
9835
9916
  });
9836
9917
  return results;
9837
9918
  }
@@ -9993,6 +10074,8 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
9993
10074
  _defineProperty(this, "_currentSubUnitId", "");
9994
10075
  _defineProperty(this, "_currentUnitId", "");
9995
10076
  _defineProperty(this, "_runtimeData", Object.create(null));
10077
+ _defineProperty(this, "_sourceFormulaData", Object.create(null));
10078
+ _defineProperty(this, "_currentSourceFormula", void 0);
9996
10079
  _defineProperty(this, "_runtimeOtherData", Object.create(null));
9997
10080
  _defineProperty(this, "_unitArrayFormulaRange", Object.create(null));
9998
10081
  _defineProperty(this, "_unitArrayFormulaEmbeddedMap", Object.create(null));
@@ -10105,6 +10188,8 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10105
10188
  reset() {
10106
10189
  this._formulaExecuteStage = 0;
10107
10190
  this._runtimeData = Object.create(null);
10191
+ this._sourceFormulaData = Object.create(null);
10192
+ this._currentSourceFormula = void 0;
10108
10193
  this._runtimeOtherData = Object.create(null);
10109
10194
  this._unitArrayFormulaRange = Object.create(null);
10110
10195
  this._unitArrayFormulaEmbeddedMap = Object.create(null);
@@ -10126,6 +10211,7 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10126
10211
  clearReferenceToRangeCache();
10127
10212
  }
10128
10213
  setCurrent(row, column, rowCount, columnCount, sheetId, unitId) {
10214
+ var _this$_currentConfigS, _this$_currentConfigS2;
10129
10215
  this._externalReferenceUnavailable = false;
10130
10216
  this._currentRow = row;
10131
10217
  this._currentColumn = column;
@@ -10133,6 +10219,18 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10133
10219
  this._currentColumnCount = columnCount;
10134
10220
  this._currentSubUnitId = sheetId;
10135
10221
  this._currentUnitId = unitId;
10222
+ const cell = (_this$_currentConfigS = this._currentConfigService.getUnitData()[unitId]) === null || _this$_currentConfigS === void 0 || (_this$_currentConfigS = _this$_currentConfigS[sheetId]) === null || _this$_currentConfigS === void 0 ? void 0 : _this$_currentConfigS.cellData.getValue(row, column);
10223
+ const formula = (_this$_currentConfigS2 = this._currentConfigService.getFormulaData()[unitId]) === null || _this$_currentConfigS2 === void 0 || (_this$_currentConfigS2 = _this$_currentConfigS2[sheetId]) === null || _this$_currentConfigS2 === void 0 || (_this$_currentConfigS2 = _this$_currentConfigS2[row]) === null || _this$_currentConfigS2 === void 0 ? void 0 : _this$_currentConfigS2[column];
10224
+ if (formula) {
10225
+ const isSharedFollower = (formula.x ?? 0) !== 0 || (formula.y ?? 0) !== 0;
10226
+ this._currentSourceFormula = {
10227
+ f: isSharedFollower ? "" : formula.f,
10228
+ si: formula.si
10229
+ };
10230
+ } else this._currentSourceFormula = (cell === null || cell === void 0 ? void 0 : cell.f) || (cell === null || cell === void 0 ? void 0 : cell.si) ? {
10231
+ f: cell.f ?? "",
10232
+ si: cell.si ?? void 0
10233
+ } : void 0;
10136
10234
  }
10137
10235
  setFunctionRefInfoOverride(rowCount, columnCount) {
10138
10236
  this._functionRefInfoOverrideStack.push({
@@ -10183,9 +10281,9 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10183
10281
  }
10184
10282
  setRuntimeData(functionVariant) {
10185
10283
  if (this._externalReferenceUnavailable) {
10186
- var _this$_currentConfigS, _this$_currentConfigS2;
10187
- const cell = (_this$_currentConfigS = this._currentConfigService.getUnitData()[this._currentUnitId]) === null || _this$_currentConfigS === void 0 || (_this$_currentConfigS = _this$_currentConfigS[this._currentSubUnitId]) === null || _this$_currentConfigS === void 0 ? void 0 : _this$_currentConfigS.cellData.getValue(this._currentRow, this._currentColumn);
10188
- const formula = (_this$_currentConfigS2 = this._currentConfigService.getFormulaData()[this._currentUnitId]) === null || _this$_currentConfigS2 === void 0 || (_this$_currentConfigS2 = _this$_currentConfigS2[this._currentSubUnitId]) === null || _this$_currentConfigS2 === void 0 || (_this$_currentConfigS2 = _this$_currentConfigS2[this._currentRow]) === null || _this$_currentConfigS2 === void 0 || (_this$_currentConfigS2 = _this$_currentConfigS2[this._currentColumn]) === null || _this$_currentConfigS2 === void 0 ? void 0 : _this$_currentConfigS2.f;
10284
+ var _this$_currentConfigS3, _this$_currentConfigS4;
10285
+ const cell = (_this$_currentConfigS3 = this._currentConfigService.getUnitData()[this._currentUnitId]) === null || _this$_currentConfigS3 === void 0 || (_this$_currentConfigS3 = _this$_currentConfigS3[this._currentSubUnitId]) === null || _this$_currentConfigS3 === void 0 ? void 0 : _this$_currentConfigS3.cellData.getValue(this._currentRow, this._currentColumn);
10286
+ const formula = (_this$_currentConfigS4 = this._currentConfigService.getFormulaData()[this._currentUnitId]) === null || _this$_currentConfigS4 === void 0 || (_this$_currentConfigS4 = _this$_currentConfigS4[this._currentSubUnitId]) === null || _this$_currentConfigS4 === void 0 || (_this$_currentConfigS4 = _this$_currentConfigS4[this._currentRow]) === null || _this$_currentConfigS4 === void 0 || (_this$_currentConfigS4 = _this$_currentConfigS4[this._currentColumn]) === null || _this$_currentConfigS4 === void 0 ? void 0 : _this$_currentConfigS4.f;
10189
10287
  if ((cell === null || cell === void 0 ? void 0 : cell.f) && cell.v != null && (formula == null || formula === cell.f)) return;
10190
10288
  }
10191
10289
  const row = this._currentRow;
@@ -10244,7 +10342,7 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10244
10342
  const hasDataInArrayFormulaRange = this._checkIfArrayFormulaRangeHasData(unitId, sheetId, row, column, arrayRange);
10245
10343
  const isArrayFormulaExceeded = this._checkIfArrayFormulaExceeded(rowCount, columnCount, arrayRange);
10246
10344
  if (hasDataInArrayFormulaRange || isArrayFormulaExceeded) {
10247
- var _this$_currentConfigS3;
10345
+ var _this$_currentConfigS5;
10248
10346
  const errorObject = this._getValueObjectOfRuntimeData(ErrorValueObject.create("#SPILL!"));
10249
10347
  sheetData.setValue(row, column, errorObject);
10250
10348
  clearArrayUnitData.setValue(row, column, errorObject);
@@ -10255,7 +10353,7 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10255
10353
  */
10256
10354
  const unitData = this._currentConfigService.getUnitData();
10257
10355
  const arrayData = this._currentConfigService.getArrayFormulaCellData();
10258
- const previousArrayFormulaRange = (_this$_currentConfigS3 = this._currentConfigService.getArrayFormulaRange()[unitId]) === null || _this$_currentConfigS3 === void 0 || (_this$_currentConfigS3 = _this$_currentConfigS3[sheetId]) === null || _this$_currentConfigS3 === void 0 || (_this$_currentConfigS3 = _this$_currentConfigS3[row]) === null || _this$_currentConfigS3 === void 0 ? void 0 : _this$_currentConfigS3[column];
10356
+ const previousArrayFormulaRange = (_this$_currentConfigS5 = this._currentConfigService.getArrayFormulaRange()[unitId]) === null || _this$_currentConfigS5 === void 0 || (_this$_currentConfigS5 = _this$_currentConfigS5[sheetId]) === null || _this$_currentConfigS5 === void 0 || (_this$_currentConfigS5 = _this$_currentConfigS5[row]) === null || _this$_currentConfigS5 === void 0 ? void 0 : _this$_currentConfigS5[column];
10259
10357
  objectValueRefOrArray.iterator((_, rowIndex, columnIndex) => {
10260
10358
  var _unitData$unitId, _arrayData$unitId;
10261
10359
  const currentRow = rowIndex - startRow + row;
@@ -10295,6 +10393,12 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10295
10393
  } else {
10296
10394
  const valueObject = this._getValueObjectOfRuntimeData(functionVariant);
10297
10395
  sheetData.setValue(row, column, valueObject);
10396
+ if (this._currentSourceFormula != null) {
10397
+ const unitFormulas = this._sourceFormulaData[unitId] ??= {};
10398
+ const sheetFormulas = unitFormulas[sheetId] ??= {};
10399
+ const rowFormulas = sheetFormulas[row] ??= {};
10400
+ rowFormulas[column] = { ...this._currentSourceFormula };
10401
+ }
10298
10402
  if (functionVariant.isString() && functionVariant.isImage()) {
10299
10403
  const imageInfo = functionVariant.getImageInfo();
10300
10404
  if (imageInfo) this._runtimeImageFormulaData.push({
@@ -10366,6 +10470,7 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10366
10470
  getAllRuntimeData() {
10367
10471
  return {
10368
10472
  unitData: this.getUnitData(),
10473
+ sourceFormulaData: this._sourceFormulaData,
10369
10474
  arrayFormulaRange: this.getUnitArrayFormula(),
10370
10475
  arrayFormulaEmbedded: this.getUnitArrayFormulaEmbeddedMap(),
10371
10476
  unitOtherData: this.getRuntimeOtherData(),
@@ -10392,11 +10497,11 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10392
10497
  FORMULA_REF_TO_ARRAY_CACHE.clear();
10393
10498
  }
10394
10499
  _checkIfArrayFormulaRangeHasData(formulaUnitId, formulaSheetId, formulaRow, formulaColumn, arrayRange) {
10395
- var _this$_currentConfigS4;
10500
+ var _this$_currentConfigS6;
10396
10501
  const { startRow, startColumn, endRow, endColumn } = arrayRange;
10397
10502
  const unitData = this._currentConfigService.getUnitData();
10398
10503
  const arrayData = this._currentConfigService.getArrayFormulaCellData();
10399
- const previousArrayFormulaRange = (_this$_currentConfigS4 = this._currentConfigService.getArrayFormulaRange()[formulaUnitId]) === null || _this$_currentConfigS4 === void 0 || (_this$_currentConfigS4 = _this$_currentConfigS4[formulaSheetId]) === null || _this$_currentConfigS4 === void 0 || (_this$_currentConfigS4 = _this$_currentConfigS4[formulaRow]) === null || _this$_currentConfigS4 === void 0 ? void 0 : _this$_currentConfigS4[formulaColumn];
10504
+ const previousArrayFormulaRange = (_this$_currentConfigS6 = this._currentConfigService.getArrayFormulaRange()[formulaUnitId]) === null || _this$_currentConfigS6 === void 0 || (_this$_currentConfigS6 = _this$_currentConfigS6[formulaSheetId]) === null || _this$_currentConfigS6 === void 0 || (_this$_currentConfigS6 = _this$_currentConfigS6[formulaRow]) === null || _this$_currentConfigS6 === void 0 ? void 0 : _this$_currentConfigS6[formulaColumn];
10400
10505
  for (let r = startRow; r <= endRow; r++) for (let c = startColumn; c <= endColumn; c++) {
10401
10506
  var _this$_runtimeData, _arrayData$formulaUni, _unitData$formulaUnit;
10402
10507
  if (r === formulaRow && formulaColumn === c) continue;
@@ -10432,8 +10537,8 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10432
10537
  * @returns
10433
10538
  */
10434
10539
  _isInOtherArrayFormulaRange(formulaUnitId, formulaSheetId, formulaRow, formulaColumn, r, c) {
10435
- var _this$_currentConfigS5;
10436
- const arrayFormulaRange = (_this$_currentConfigS5 = this._currentConfigService.getArrayFormulaRange()[formulaUnitId]) === null || _this$_currentConfigS5 === void 0 ? void 0 : _this$_currentConfigS5[formulaSheetId];
10540
+ var _this$_currentConfigS7;
10541
+ const arrayFormulaRange = (_this$_currentConfigS7 = this._currentConfigService.getArrayFormulaRange()[formulaUnitId]) === null || _this$_currentConfigS7 === void 0 ? void 0 : _this$_currentConfigS7[formulaSheetId];
10437
10542
  if (arrayFormulaRange == null) return false;
10438
10543
  let isCellOverlapping = false;
10439
10544
  new _univerjs_core.ObjectMatrix(arrayFormulaRange).forValue((rangeRow, rangeCol, range) => {
@@ -10446,8 +10551,8 @@ let FormulaRuntimeService = class FormulaRuntimeService extends _univerjs_core.D
10446
10551
  return isCellOverlapping;
10447
10552
  }
10448
10553
  _isCurrentSingleCellArrayFormulaRange(unitId, sheetId, row, column) {
10449
- var _this$_currentConfigS6;
10450
- const range = (_this$_currentConfigS6 = this._currentConfigService.getArrayFormulaRange()[unitId]) === null || _this$_currentConfigS6 === void 0 || (_this$_currentConfigS6 = _this$_currentConfigS6[sheetId]) === null || _this$_currentConfigS6 === void 0 || (_this$_currentConfigS6 = _this$_currentConfigS6[row]) === null || _this$_currentConfigS6 === void 0 ? void 0 : _this$_currentConfigS6[column];
10554
+ var _this$_currentConfigS8;
10555
+ const range = (_this$_currentConfigS8 = this._currentConfigService.getArrayFormulaRange()[unitId]) === null || _this$_currentConfigS8 === void 0 || (_this$_currentConfigS8 = _this$_currentConfigS8[sheetId]) === null || _this$_currentConfigS8 === void 0 || (_this$_currentConfigS8 = _this$_currentConfigS8[row]) === null || _this$_currentConfigS8 === void 0 ? void 0 : _this$_currentConfigS8[column];
10451
10556
  return range != null && range.startRow === range.endRow && range.startColumn === range.endColumn;
10452
10557
  }
10453
10558
  _isInArrayFormulaRange(range, r, c) {
@@ -14807,13 +14912,13 @@ let CalculateFormulaService = class CalculateFormulaService extends _univerjs_co
14807
14912
  this._runtimeService.setRuntimeFeatureRange(featureId, featureRange);
14808
14913
  }
14809
14914
  async execute(formulaDatasetConfig) {
14810
- this._runtimeService.setFormulaExecuteStage(1);
14811
- this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
14812
- this._currentConfigService.load(formulaDatasetConfig);
14813
- this._runtimeService.reset();
14814
- const cycleReferenceCount = formulaDatasetConfig.maxIteration || 1;
14815
- this._isCalculateTreeModel = formulaDatasetConfig.isCalculateTreeModel || false;
14816
- this._executeLock.acquire("FORMULA_EXECUTION_LOCK", async () => {
14915
+ return this._executeLock.acquire("FORMULA_EXECUTION_LOCK", async () => {
14916
+ this._runtimeService.setFormulaExecuteStage(1);
14917
+ this._executionInProgressListener$.next(this._runtimeService.getRuntimeState());
14918
+ this._currentConfigService.load(formulaDatasetConfig);
14919
+ this._runtimeService.reset();
14920
+ const cycleReferenceCount = formulaDatasetConfig.maxIteration || 1;
14921
+ this._isCalculateTreeModel = formulaDatasetConfig.isCalculateTreeModel || false;
14817
14922
  for (let i = 0; i < cycleReferenceCount; i++) {
14818
14923
  this._runtimeService.setFormulaCycleIndex(i);
14819
14924
  const executed = await this._executeStep();
@@ -15186,7 +15291,7 @@ let CalculateController = class CalculateController extends _univerjs_core.Dispo
15186
15291
  if (dependencyTreeModelData.length > 0) this._commandService.executeCommand(SetFormulaDependencyCalculationResultMutation.id, { result: dependencyTreeModelData }, { onlyLocal: true });
15187
15292
  }
15188
15293
  async _applyResult(data) {
15189
- const { unitData, unitOtherData, arrayFormulaRange, arrayFormulaCellData, clearArrayFormulaCellData, arrayFormulaEmbedded, imageFormulaData } = data;
15294
+ const { unitData, sourceFormulaData, unitOtherData, arrayFormulaRange, arrayFormulaCellData, clearArrayFormulaCellData, arrayFormulaEmbedded, imageFormulaData } = data;
15190
15295
  if (!unitData) {
15191
15296
  this._applyTreeResult(data);
15192
15297
  console.error("No sheetData from Formula Engine!");
@@ -15206,6 +15311,7 @@ let CalculateController = class CalculateController extends _univerjs_core.Dispo
15206
15311
  this._applyTreeResult(data);
15207
15312
  this._commandService.executeCommand(SetFormulaCalculationResultMutation.id, {
15208
15313
  unitData: convertRuntimeToUnitData(unitData),
15314
+ sourceFormulaData,
15209
15315
  unitOtherData
15210
15316
  }, { onlyLocal: true });
15211
15317
  }
@@ -36245,6 +36351,19 @@ var Sumif = class extends BaseFunction {
36245
36351
  let _criteria = criteria;
36246
36352
  if (criteria.isReferenceObject()) _criteria = criteria.toArrayValueObject();
36247
36353
  if (_criteria.isArray()) {
36354
+ const criteriaArray = _criteria;
36355
+ const rangeArray = range.toArrayValueObject();
36356
+ const targetArray = sumRange ? sumRange.toArrayValueObject() : rangeArray;
36357
+ if (criteriaArray.getRowCount() * criteriaArray.getColumnCount() > 1 && rangeArray.getRowCount() === targetArray.getRowCount() && rangeArray.getColumnCount() === targetArray.getColumnCount()) {
36358
+ const values = getPairedRangeAndCriteriaResult([rangeArray, criteriaArray], {
36359
+ formulaName: "SUMIFS",
36360
+ maxRowLength: criteriaArray.getRowCount(),
36361
+ maxColumnLength: criteriaArray.getColumnCount(),
36362
+ isNumberSensitive: true,
36363
+ targetRange: targetArray
36364
+ });
36365
+ return criteriaArray.mapValue((_, row, column) => values[row][column]);
36366
+ }
36248
36367
  const resultArray = _criteria.mapValue((criteriaObject) => this._handleSingleObject(range, criteriaObject, sumRange));
36249
36368
  if (resultArray.getRowCount() === 1 && resultArray.getColumnCount() === 1) return resultArray.get(0, 0);
36250
36369
  return resultArray;
@@ -37230,6 +37349,19 @@ var Averageif = class extends BaseFunction {
37230
37349
  let _criteria = criteria;
37231
37350
  if (criteria.isReferenceObject()) _criteria = criteria.toArrayValueObject();
37232
37351
  if (_criteria.isArray()) {
37352
+ const criteriaArray = _criteria;
37353
+ const rangeArray = range.toArrayValueObject();
37354
+ const targetArray = averageRange ? averageRange.toArrayValueObject() : rangeArray;
37355
+ if (criteriaArray.getRowCount() * criteriaArray.getColumnCount() > 1 && rangeArray.getRowCount() === targetArray.getRowCount() && rangeArray.getColumnCount() === targetArray.getColumnCount()) {
37356
+ const values = getPairedRangeAndCriteriaResult([rangeArray, criteriaArray], {
37357
+ formulaName: "AVERAGEIFS",
37358
+ maxRowLength: criteriaArray.getRowCount(),
37359
+ maxColumnLength: criteriaArray.getColumnCount(),
37360
+ isNumberSensitive: true,
37361
+ targetRange: targetArray
37362
+ });
37363
+ return criteriaArray.mapValue((_, row, column) => values[row][column]);
37364
+ }
37233
37365
  const resultArray = _criteria.mapValue((criteriaObject) => this._handleSingleObject(range, criteriaObject, averageRange));
37234
37366
  if (resultArray.getRowCount() === 1 && resultArray.getColumnCount() === 1) return resultArray.get(0, 0);
37235
37367
  return resultArray;
@@ -37648,6 +37780,18 @@ var Countif = class extends BaseFunction {
37648
37780
  }
37649
37781
  }
37650
37782
  if (_criteria.isArray()) {
37783
+ const criteriaArray = _criteria;
37784
+ const rangeArray = range.toArrayValueObject();
37785
+ if (criteriaArray.getRowCount() * criteriaArray.getColumnCount() > 1) {
37786
+ const normalized = criteria.isReferenceObject() ? criteriaArray.mapValue((value) => value.isNull() ? NumberValueObject.create(0) : value) : criteriaArray;
37787
+ const values = getPairedRangeAndCriteriaResult([rangeArray, normalized], {
37788
+ formulaName: "COUNTIFS",
37789
+ maxRowLength: criteriaArray.getRowCount(),
37790
+ maxColumnLength: criteriaArray.getColumnCount(),
37791
+ isNumberSensitive: true
37792
+ });
37793
+ return criteriaArray.mapValue((_, row, column) => values[row][column]);
37794
+ }
37651
37795
  const resultArray = _criteria.mapValue((criteriaObject) => this._handleSingleObject(range, criteria.isReferenceObject() && criteriaObject.isNull() ? NumberValueObject.create(0) : criteriaObject));
37652
37796
  if (resultArray.getRowCount() === 1 && resultArray.getColumnCount() === 1) return resultArray.get(0, 0);
37653
37797
  return resultArray;
@@ -43630,7 +43774,7 @@ function getObjectValue(result, isUseStrip = false) {
43630
43774
  //#endregion
43631
43775
  //#region package.json
43632
43776
  var name = "@univerjs/engine-formula";
43633
- var version = "1.0.2";
43777
+ var version = "1.0.4";
43634
43778
 
43635
43779
  //#endregion
43636
43780
  //#region src/services/global-computing-status.service.ts