Skip to content

Commit

Permalink
fix(formula): fix expandArrayValueObject
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and wpxp123456 committed Oct 31, 2024
1 parent 7c04c74 commit 368cddc
Showing 1 changed file with 28 additions and 44 deletions.
72 changes: 28 additions & 44 deletions packages/engine-formula/src/engine/utils/array-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,46 @@
* limitations under the License.
*/

import { ArrayValueObject } from '../value-object/array-value-object';
import type { BaseValueObject, IArrayValueObject } from '../value-object/base-value-object';
import { ArrayValueObject } from '../value-object/array-value-object';
import { NullValueObject } from '../value-object/primitive-object';

export function expandArrayValueObject(rowCount: number, columnCount: number, valueObject: BaseValueObject, defaultValue?: BaseValueObject) {
const valueRowCount = valueObject.isArray() ? (valueObject as ArrayValueObject).getRowCount() : 1;
const valueColumnCount = valueObject.isArray() ? (valueObject as ArrayValueObject).getColumnCount() : 1;

const result: BaseValueObject[][] = [];

if (valueObject.isArray()) {
const valueRowCount = (valueObject as ArrayValueObject).getRowCount();
const valueColumnCount = (valueObject as ArrayValueObject).getColumnCount();
for (let r = 0; r < rowCount; r++) {
const row = [];

if (valueRowCount === 1 && valueColumnCount === 1) {
const v = (valueObject as ArrayValueObject).getFirstCell() as BaseValueObject;
for (let r = 0; r < rowCount; r++) {
const row = [];
for (let c = 0; c < columnCount; c++) {
row.push(v);
}
result.push(row);
}
} else if (valueRowCount === 1 && valueColumnCount > 1) {
for (let r = 0; r < rowCount; r++) {
const row = [];
for (let c = 0; c < columnCount; c++) {
const v = (valueObject as ArrayValueObject).getRealValue(0, c) as BaseValueObject || (defaultValue ?? NullValueObject.create());
row.push(v);
}
result.push(row);
for (let c = 0; c < columnCount; c++) {
const value = valueObject.isArray() ? (valueObject as ArrayValueObject).get(r, c) as BaseValueObject : valueObject;

if (valueRowCount === 1 && valueColumnCount === 1) {
row.push(value);
continue;
}
} else if (valueColumnCount === 1 && valueRowCount > 1) {
for (let r = 0; r < rowCount; r++) {
const row = [];
for (let c = 0; c < columnCount; c++) {
const v = (valueObject as ArrayValueObject).getRealValue(r, 0) as BaseValueObject || (defaultValue ?? NullValueObject.create());
row.push(v);
}
result.push(row);

if (valueRowCount === 1 && c < valueColumnCount) {
row.push((valueObject as ArrayValueObject).get(0, c) as BaseValueObject);
continue;
}
} else {
for (let r = 0; r < rowCount; r++) {
const row = [];
for (let c = 0; c < columnCount; c++) {
const v = (valueObject as ArrayValueObject).getRealValue(r, c) as BaseValueObject || (defaultValue ?? NullValueObject.create());
row.push(v);
}
result.push(row);

if (valueColumnCount === 1 && r < valueRowCount) {
row.push((valueObject as ArrayValueObject).get(r, 0) as BaseValueObject);
continue;
}
}
} else {
for (let r = 0; r < rowCount; r++) {
const row = [];
for (let c = 0; c < columnCount; c++) {
row.push(valueObject);

if (r >= valueRowCount || c >= valueColumnCount) {
row.push(defaultValue ?? NullValueObject.create());
continue;
}
result.push(row);

row.push(value);
}

result.push(row);
}

return createNewArray(result, rowCount, columnCount);
Expand Down

0 comments on commit 368cddc

Please sign in to comment.