Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/vtable-sheet/examples/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,9 @@ export const menus = [
{
path: 'sheet',
name: 'history'
},
{
path: 'sheet',
name: 'issue-5204-formula-manager'
}
];
63 changes: 63 additions & 0 deletions packages/vtable-sheet/examples/sheet/issue-5204-formula-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { VTableSheet } from '../../src/index';

const CONTAINER_ID = 'vTable';

const columns = [
{ title: '测试公式计算', field: 'test', width: 120 },
{
title: 'B商品营业额',
field: 'Bmoney',
width: 120
},
{
title: 'A商品营业额',
field: 'Amoney',
width: 120
}
];

const dataA = Array.from({ length: 12 }, (_, index) => ({
test: `${index}-row`,
Bmoney: `${(index + 1) * 50}`,
Amoney: `${(index + 2) * 50}`
}));

const columnsB = [
{ title: '测试公式计算', width: 120 },
{
title: 'B商品营业额',
width: 120
},
{
title: 'A商品营业额',
width: 120
}
];

const dataB = [['1', 2, 3]];

export function createTable() {
const container = document.getElementById(CONTAINER_ID)!;
window.sheetInstance = new VTableSheet(container, {
showFormulaBar: false,
showSheetTab: true,
defaultRowHeight: 32,
defaultColWidth: 120,
sheets: [
{
sheetKey: 'Issue-5204-a',
sheetTitle: 'data是Array<Object>',
columns,
data: dataA as any,
active: true
},
{
sheetKey: 'Issue-5204-b',
sheetTitle: 'data是Array<Array<any>>',
columns: columnsB,
data: dataB as any,
active: true
}
]
});
}
22 changes: 20 additions & 2 deletions packages/vtable-sheet/src/managers/formula-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,34 @@ export class FormulaManager implements IFormulaManager {
try {
//将columns中的title追加到data中
const headerRows: unknown[][] = [];
const headerKeyColMap = new Map();
for (let i = 0; i < tableInstance.columnHeaderLevelCount; i++) {
const headerRow: unknown[] = [];
for (let j = 0; j < tableInstance.colCount; j++) {
const cellValue = tableInstance.getCellValue(j, i);
const filedDefine = tableInstance.getHeaderDefine(j, i);
if (cellValue === filedDefine.title) {
headerKeyColMap.set(j, filedDefine.field);
}
headerRow.push(cellValue);
}
headerRows.push(headerRow);
}
const dataCopy = JSON.parse(JSON.stringify(data));
const colCount = tableInstance.colCount;
let dataCopy: unknown[][];
const firstRow = data[0];
if (firstRow && typeof firstRow === 'object' && !Array.isArray(firstRow)) {
dataCopy = data.map((row: Record<string, any>) => {
const rowTemp = new Array<unknown>(colCount);
for (let col = 0; col < colCount; col++) {
const field = headerKeyColMap.get(col);
rowTemp[col] = field !== undefined ? row[field] ?? null : null;
}
return rowTemp;
});
} else {
dataCopy = JSON.parse(JSON.stringify(data));
}

const toNormalizeData = tableInstance.columnHeaderLevelCount > 0 ? [...headerRows].concat(dataCopy) : dataCopy;

Expand Down Expand Up @@ -1372,7 +1391,6 @@ export class FormulaManager implements IFormulaManager {
// 如果还没有对应的 WorkSheet 实例,跳过,后续按需再补充
return;
}

const normalizedData = this.normalizeSheetData(worksheetInstance.getData(), worksheetInstance.tableInstance);
this.addSheet(sheetKey, normalizedData, sheetDefine.sheetTitle);
});
Expand Down
Loading