Skip to content

Commit

Permalink
实现统计相关的函数
Browse files Browse the repository at this point in the history
  • Loading branch information
wuduoyi committed Apr 1, 2024
1 parent 263355c commit c3953e5
Show file tree
Hide file tree
Showing 19 changed files with 1,900 additions and 73 deletions.
22 changes: 18 additions & 4 deletions fis-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ if (fis.project.currentMedia() === 'publish-sdk') {
'!office-viewer/**',
'!fflate/**',
'!numfmt/**',
'!jstat/**',
'!amis-formula/lib/doc.js'
],

Expand Down Expand Up @@ -581,7 +582,12 @@ if (fis.project.currentMedia() === 'publish-sdk') {
'echarts-wordcloud/**'
],

'office-viewer.js': ['office-viewer/**', 'fflate/**', 'numfmt/**'],
'office-viewer.js': [
'office-viewer/**',
'fflate/**',
'numfmt/**',
'jstat/**'
],
'json-view.js': 'react-json-view/**',
'fomula-doc.js': 'amis-formula/lib/doc.js',

Expand Down Expand Up @@ -611,7 +617,8 @@ if (fis.project.currentMedia() === 'publish-sdk') {
'!markdown-it-html5-media/**',
'!office-viewer/**',
'!fflate/**',
'!numfmt/**'
'!numfmt/**',
'!jstat/**'
]
}),
postpackager: [
Expand Down Expand Up @@ -847,6 +854,7 @@ if (fis.project.currentMedia() === 'publish-sdk') {
'!amis-formula/**',
'!fflate/**',
'!numfmt/**',
'!jstat/**',
'!office-viewer/**',
'!amis-core/**',
'!amis-ui/**',
Expand Down Expand Up @@ -913,7 +921,12 @@ if (fis.project.currentMedia() === 'publish-sdk') {
'!/examples/components/EChartsEditor/Common.tsx'
],

'pkg/office-viewer.js': ['office-viewer/**', 'fflate/**', 'numfmt/**'],
'pkg/office-viewer.js': [
'office-viewer/**',
'fflate/**',
'numfmt/**',
'jstat/**'
],

'pkg/rest.js': [
'**.{js,jsx,ts,tsx}',
Expand All @@ -939,7 +952,8 @@ if (fis.project.currentMedia() === 'publish-sdk') {
'!markdown-it/**',
'!markdown-it-html5-media/**',
'!fflate/**',
'!numfmt/**'
'!numfmt/**',
'!jstat/**'
],

'pkg/npm.css': ['node_modules/*/**.css', '!monaco-editor/**', '!amis/**'],
Expand Down
1 change: 1 addition & 0 deletions packages/office-viewer/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@
"ISREF",
"ISTEXT",
"JIS",
"jstat",
"KURT",
"LARGE",
"Lbls",
Expand Down
6 changes: 0 additions & 6 deletions packages/office-viewer/src/excel/formula/FormulaEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ export interface FormulaEnv {
* @param sheetName 工作表名
*/
getByRange(range: RangeRef, sheetName?: string): CellValue[];

/**
* 获取公式缓存值,避免重复计算
* @param formula 公式
*/
getFormulaCache(formula: string): EvalResult | undefined;
}

/**
Expand Down
22 changes: 12 additions & 10 deletions packages/office-viewer/src/excel/formula/__tests__/eval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ import {RangeRef} from '../../types/RangeRef';
import {FormulaEnv} from '../FormulaEnv';
import {evalFormula} from '../eval/evalFormula';

const vars: Map<string, CellValue> = new Map([
['a', {row: 1, col: 1, text: '1', value: '1'}],
['b', {row: 1, col: 2, text: '2', value: '2'}]
const vars: Map<string, RangeRef> = new Map([
['a', {startRow: 1, startCol: 1, endRow: 1, endCol: 1}],
['b', {startRow: 1, startCol: 2, endRow: 1, endCol: 2}]
]);

const env = {
getByVariable: (name: string, sheetName?: string) => {
if (vars.has(name)) {
return [vars.get(name)!];
getNameRange: (name: string, sheetName?: string) => {
return vars.get(name)!;
},
getByRange: (range: RangeRef, sheetName?: string) => {
if (range.startCol === 1) {
return [{row: 1, col: 1, text: '1', value: 1}];
}
if (range.startCol === 2) {
return [{row: 1, col: 2, text: '2', value: 2}];
}
return [];
},
getByRange: (range: RangeRef, sheetName?: string) => {},
getFormulaCache: (formula: string) => {
return undefined;
}
} as FormulaEnv;

Expand Down
8 changes: 8 additions & 0 deletions packages/office-viewer/src/excel/formula/eval/EvalResult.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {RangeRef} from '../../types/RangeRef';

export type EvalResult =
| undefined
| number
Expand All @@ -18,6 +20,12 @@ export type UnionValue = {
children: EvalResult[];
};

export type CellResult = {
type: 'Cell';
range: RangeRef;
value: EvalResult;
};

export function isUnionValue(value: EvalResult): boolean {
return typeof value === 'object' && 'type' in value && value.type === 'Union';
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import '../functions/math';
import '../functions/trigonometry';
import '../functions/text';
import '../functions/statistical';
import '../functions/distribution';
import {visitIntersection} from './visitIntersection';

export class FormulaVisitor {
Expand Down
68 changes: 68 additions & 0 deletions packages/office-viewer/src/excel/formula/eval/evalCriterial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {Criteria} from '../parser/parseCriteria';

const type2Number = {boolean: 3, string: 2, number: 1};

type typeName = keyof typeof type2Number;

type Value = string | number | boolean | undefined;

function compareOp(value1: Value, infix: Criteria['op'], value2: Value) {
if (!value1) {
value1 = 0;
}
if (!value2) {
value2 = 0;
}

if (Array.isArray(value2)) {
return compareOp(value1, infix, value2[0]);
}

const type1 = typeof value1 as typeName,
type2 = typeof value2 as typeName;

if (type1 === type2) {
// same type comparison
switch (infix) {
case '=':
return value1 === value2;
case '>':
return value1 > value2;
case '<':
return value1 < value2;
case '<>':
return value1 !== value2;
case '<=':
return value1 <= value2;
case '>=':
return value1 >= value2;
}
} else {
switch (infix) {
case '=':
return false;
case '>':
return type2Number[type1] > type2Number[type2];
case '<':
return type2Number[type1] < type2Number[type2];
case '<>':
return true;
case '<=':
return type2Number[type1] <= type2Number[type2];
case '>=':
return type2Number[type1] >= type2Number[type2];
}
}
throw Error('Infix.compareOp: Should not reach here.');
}

export function evalCriterial(
criteria: Criteria,
value: string | number | boolean
): boolean {
if (criteria.op === 'wc') {
return criteria.match === (criteria.value as RegExp).test('' + value);
}

return compareOp(value, criteria.op, criteria.value as Value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,8 @@ export function buildEnv(data: Data, vars: Map<string, string> = new Map()) {
return result;
}

function getFormulaCache(formula: string) {
return undefined;
}

return {
getNameRange,
getByRange,
getFormulaCache
getByRange
} as FormulaEnv;
}
Loading

0 comments on commit c3953e5

Please sign in to comment.