@speed-sheet/extension-formula 0.1.1
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/dist/index.d.ts +191 -0
- package/dist/index.js +1331 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Sheet, Extension } from '@speed-sheet/core';
|
|
2
|
+
import { CellAttributes } from '@speed-sheet/shared';
|
|
3
|
+
|
|
4
|
+
interface FormulaContext {
|
|
5
|
+
readonly activeSheetId: string;
|
|
6
|
+
resolveSheetId(nameOrId: string | undefined): string | null;
|
|
7
|
+
getSheetName(sheetId: string): string;
|
|
8
|
+
resolveCellIds(sheetId: string, r: number, c: number): {
|
|
9
|
+
rowId: string;
|
|
10
|
+
colId: string;
|
|
11
|
+
} | null;
|
|
12
|
+
idsToDisplay(sheetId: string, rowId: string, colId: string): {
|
|
13
|
+
r: number;
|
|
14
|
+
c: number;
|
|
15
|
+
} | null;
|
|
16
|
+
expandIdRange(sheetId: string, rowId0: string, colId0: string, rowId1: string, colId1: string): Array<{
|
|
17
|
+
rowId: string;
|
|
18
|
+
colId: string;
|
|
19
|
+
}>;
|
|
20
|
+
/** Scalar by stable ids (canonical for evaluation). */
|
|
21
|
+
getScalarById(sheetId: string, rowId: string, colId: string, visiting: Set<string>): number | string | boolean | null;
|
|
22
|
+
/** Display-coordinate scalar (A1 boundary only). */
|
|
23
|
+
getScalar(sheetId: string, r: number, c: number, visiting: Set<string>): number | string | boolean | null;
|
|
24
|
+
}
|
|
25
|
+
declare function createFormulaContext(sheet: Sheet): FormulaContext;
|
|
26
|
+
|
|
27
|
+
interface FormulaRangeHighlight {
|
|
28
|
+
sheetId: string;
|
|
29
|
+
row: [number, number];
|
|
30
|
+
column: [number, number];
|
|
31
|
+
color: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface FormulaStorage {
|
|
35
|
+
evaluating: boolean;
|
|
36
|
+
dependents: Map<string, Set<string>>;
|
|
37
|
+
sheet: Sheet | null;
|
|
38
|
+
}
|
|
39
|
+
declare const FormulaExtension: Extension<FormulaStorage>;
|
|
40
|
+
declare function getFormulaHighlights(sheet: Sheet, formula: string): FormulaRangeHighlight[];
|
|
41
|
+
|
|
42
|
+
/** Excel 风格公式错误(显示值 + 提示文案) */
|
|
43
|
+
type FormulaErrorCode = 'ERROR' | 'VALUE' | 'NAME' | 'REF' | 'DIV0' | 'NA' | 'NUM' | 'NULL';
|
|
44
|
+
interface FormulaErrorDef {
|
|
45
|
+
code: FormulaErrorCode;
|
|
46
|
+
/** 单元格显示,如 #VALUE! */
|
|
47
|
+
display: string;
|
|
48
|
+
/** 悬停提示(中文) */
|
|
49
|
+
message: string;
|
|
50
|
+
}
|
|
51
|
+
declare const FORMULA_ERRORS: Record<FormulaErrorCode, FormulaErrorDef>;
|
|
52
|
+
declare function formulaErrorResult(code: FormulaErrorCode): {
|
|
53
|
+
value: null;
|
|
54
|
+
m: string;
|
|
55
|
+
error: FormulaErrorCode;
|
|
56
|
+
errorMessage: string;
|
|
57
|
+
};
|
|
58
|
+
declare function isFormulaErrorDisplay(text: string): boolean;
|
|
59
|
+
declare function getFormulaErrorMessage(code: string | undefined, fallback?: string): string;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 公式编辑态字符串操作(headless,与 UI 框架无关)。
|
|
63
|
+
* 供 vue3 / 其他宿主在公式栏、单元格内联编辑、点选引用时复用。
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 是否处于「点选插入引用」态(而非仅公式编辑态)。
|
|
68
|
+
* @param refPickSession 已为 true 时等同 Luckysheet rangestart(上一次点选后保持到提交/取消)
|
|
69
|
+
*/
|
|
70
|
+
declare function canPickFormulaRefAtCaret(formula: string, caret: number): boolean;
|
|
71
|
+
declare function canPickFormulaRef(formula: string, caret: number, refPickSession: boolean): boolean;
|
|
72
|
+
declare function isFormulaInput(text: string): boolean;
|
|
73
|
+
declare const isFormulaText: typeof isFormulaInput;
|
|
74
|
+
declare function getCellFormulaInitialFromCell(cell: CellAttributes | null | undefined, sheet?: Sheet, sheetId?: string): string;
|
|
75
|
+
/** 进入公式编辑时的初始文本:已有公式则保留,否则为 `=` */
|
|
76
|
+
declare function getCellFormulaInitial(sheet: Sheet, r: number, c: number): string;
|
|
77
|
+
declare function formatRangeA1(r0: number, c0: number, r1: number, c1: number): string;
|
|
78
|
+
declare function buildSheetRefToken(sheet: Sheet, r0: number, c0: number, r1?: number, c1?: number, sheetId?: string): string;
|
|
79
|
+
/** 在公式中插入/替换引用:运算符后拼接,否则替换光标处引用(或最后一个引用) */
|
|
80
|
+
declare function patchFormulaWithRef(formula: string, caret: number, refToken: string): {
|
|
81
|
+
text: string;
|
|
82
|
+
caret: number;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
interface FormulaResult {
|
|
86
|
+
value: number | string | boolean | null;
|
|
87
|
+
m: string;
|
|
88
|
+
error?: FormulaErrorCode;
|
|
89
|
+
errorMessage?: string;
|
|
90
|
+
}
|
|
91
|
+
declare function evaluateFormulaString(raw: string, ctx: FormulaContext, visiting?: Set<string>): FormulaResult;
|
|
92
|
+
|
|
93
|
+
/** A1 风格引用解析(与 Excel / Luckysheet 子集对齐) */
|
|
94
|
+
interface CellAddress {
|
|
95
|
+
r: number;
|
|
96
|
+
c: number;
|
|
97
|
+
}
|
|
98
|
+
interface RangeAddress {
|
|
99
|
+
row: [number, number];
|
|
100
|
+
column: [number, number];
|
|
101
|
+
}
|
|
102
|
+
interface SheetRef {
|
|
103
|
+
/** 工作表名(展示名)或 id */
|
|
104
|
+
sheet?: string;
|
|
105
|
+
cell?: CellAddress;
|
|
106
|
+
range?: RangeAddress;
|
|
107
|
+
}
|
|
108
|
+
/** 列字母 → 0-based 列号 */
|
|
109
|
+
declare function letterToCol(letters: string): number;
|
|
110
|
+
declare function colToLetter(c: number): string;
|
|
111
|
+
declare function formatA1(r: number, c: number): string;
|
|
112
|
+
/** 解析 A1 或 A1:B10 */
|
|
113
|
+
declare function parseRefToken(token: string): SheetRef | null;
|
|
114
|
+
/** 从公式文本中提取全部 A1 引用 token(跳过 `#r_…:c_…#` 内部区域) */
|
|
115
|
+
declare function extractRefTokens(formula: string): string[];
|
|
116
|
+
|
|
117
|
+
declare function hasInternalRefs(formula: string): boolean;
|
|
118
|
+
|
|
119
|
+
/** User-facing A1 formula → canonical internal formula (stored in `f`). */
|
|
120
|
+
declare function displayFormulaToInternal(displayFormula: string, ctx: FormulaContext, defaultSheetId: string): string;
|
|
121
|
+
/** Stored internal formula → formula-bar A1 display. */
|
|
122
|
+
declare function internalFormulaToDisplay(internalFormula: string, ctx: FormulaContext, defaultSheetId: string): string;
|
|
123
|
+
|
|
124
|
+
declare const FORMULA_REF_COLORS: readonly ["#1a73e8", "#0d9d57", "#e37400", "#9c27b0", "#d93025"];
|
|
125
|
+
interface FormulaRefSpan {
|
|
126
|
+
start: number;
|
|
127
|
+
end: number;
|
|
128
|
+
token: string;
|
|
129
|
+
color: string;
|
|
130
|
+
}
|
|
131
|
+
/** 公式内引用 token 着色区间 */
|
|
132
|
+
declare function getFormulaRefSpans(formula: string): FormulaRefSpan[];
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 公式内置函数注册表类型。
|
|
136
|
+
* 函数名以 @formulajs/formulajs 为准;分类对齐 formulajs.info / Excel。
|
|
137
|
+
*/
|
|
138
|
+
type FormulaCategoryId = 'math' | 'statistical' | 'financial' | 'engineering' | 'text' | 'logical' | 'date' | 'lookup' | 'information' | 'database' | 'compatibility';
|
|
139
|
+
interface FormulaCategoryMeta {
|
|
140
|
+
id: FormulaCategoryId;
|
|
141
|
+
/** 菜单/面板分类名(中文) */
|
|
142
|
+
label: string;
|
|
143
|
+
labelEn: string;
|
|
144
|
+
/** formulajs.info 原始分类名 */
|
|
145
|
+
formulajsCategory: string;
|
|
146
|
+
}
|
|
147
|
+
interface FormulaBuiltinEntry {
|
|
148
|
+
/** 规范函数名(大写) */
|
|
149
|
+
name: string;
|
|
150
|
+
aliases?: string[];
|
|
151
|
+
category: FormulaCategoryId;
|
|
152
|
+
/** 短标签,如「求和」 */
|
|
153
|
+
label: string;
|
|
154
|
+
labelEn?: string;
|
|
155
|
+
/** 语法摘要,如 SUM(数值1, [数值2], …) */
|
|
156
|
+
syntax: string;
|
|
157
|
+
/** 一行说明(帮助气泡) */
|
|
158
|
+
hint: string;
|
|
159
|
+
/** 详细定义(函数面板) */
|
|
160
|
+
description?: string;
|
|
161
|
+
/** 示例 */
|
|
162
|
+
example?: string;
|
|
163
|
+
/** 另请参阅 */
|
|
164
|
+
seeAlso?: string[];
|
|
165
|
+
/** speed-sheet 引擎已接入求值 */
|
|
166
|
+
implemented?: boolean;
|
|
167
|
+
/** @formulajs/formulajs 包内存在实现 */
|
|
168
|
+
formulajs?: boolean;
|
|
169
|
+
/** 常用函数区 */
|
|
170
|
+
featured?: boolean;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
declare const FORMULA_BUILTIN_REGISTRY: readonly FormulaBuiltinEntry[];
|
|
174
|
+
|
|
175
|
+
declare const FORMULA_CATEGORIES: readonly FormulaCategoryMeta[];
|
|
176
|
+
declare function categoryLabel(id: FormulaCategoryId, locale?: 'zh' | 'en'): string;
|
|
177
|
+
|
|
178
|
+
declare function resolveBuiltinName(name: string): string | undefined;
|
|
179
|
+
declare function isRegisteredBuiltin(name: string): boolean;
|
|
180
|
+
declare function getBuiltinEntry(name: string): FormulaBuiltinEntry | undefined;
|
|
181
|
+
declare function getFeaturedBuiltins(): FormulaBuiltinEntry[];
|
|
182
|
+
declare function getBuiltinsByCategory(category: FormulaCategoryId): FormulaBuiltinEntry[];
|
|
183
|
+
/** 菜单用:有函数的分类(保持 FORMULA_CATEGORIES 顺序) */
|
|
184
|
+
declare function getCategoriesWithBuiltins(): FormulaCategoryId[];
|
|
185
|
+
declare function searchBuiltins(query: string): FormulaBuiltinEntry[];
|
|
186
|
+
/** 从公式文本解析当前正在输入的函数名(=SUM( → SUM) */
|
|
187
|
+
declare function parseActiveFunctionName(formula: string, caret: number): string | null;
|
|
188
|
+
|
|
189
|
+
declare function cellPatchFromFormulaResult(formula: string, result: FormulaResult): Partial<CellAttributes>;
|
|
190
|
+
|
|
191
|
+
export { FORMULA_BUILTIN_REGISTRY, FORMULA_CATEGORIES, FORMULA_ERRORS, FORMULA_REF_COLORS, type FormulaBuiltinEntry, type FormulaCategoryId, type FormulaErrorCode, FormulaExtension, type FormulaRangeHighlight, type FormulaRefSpan, type FormulaResult, type FormulaStorage, buildSheetRefToken, canPickFormulaRef, canPickFormulaRefAtCaret, categoryLabel, cellPatchFromFormulaResult, colToLetter, createFormulaContext, displayFormulaToInternal, evaluateFormulaString, extractRefTokens, formatA1, formatRangeA1, formulaErrorResult, getBuiltinEntry, getBuiltinsByCategory, getCategoriesWithBuiltins, getCellFormulaInitial, getCellFormulaInitialFromCell, getFeaturedBuiltins, getFormulaErrorMessage, getFormulaHighlights, getFormulaRefSpans, hasInternalRefs, internalFormulaToDisplay, isFormulaErrorDisplay, isFormulaInput, isFormulaText, isRegisteredBuiltin, letterToCol, parseActiveFunctionName, parseRefToken, patchFormulaWithRef, resolveBuiltinName, searchBuiltins };
|