@4399ywkf/editor 0.4.3 → 0.5.0

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/xlsx.d.ts ADDED
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Univer 快照的结构化镜像。
3
+ *
4
+ * 为什么不直接 `import type { IWorkbookData } from "@univerjs/core"`:
5
+ * 那是**可选 peer**。没装 `@univerjs/presets` 的项目一解析不到这个模块,
6
+ * 整个包的 typecheck 就红了 —— 而「拿快照出 xlsx」恰恰是最不该逼人装 871KB
7
+ * 渲染引擎的场景(服务端定时导出、Node 里跑批都只有 JSON)。
8
+ *
9
+ * 所以这里只声明导出真正读到的字段,字段名与取值语义和 `@univerjs/core`
10
+ * 逐一对齐。Univer 加字段不会影响这里;改字段含义会 —— 那种时候本文件是
11
+ * 唯一需要改的地方。
12
+ */
13
+ type CellValue = string | number | boolean;
14
+ interface ColorStyle {
15
+ /** `#RRGGBB` / `rgb(r,g,b)` / `rgba(r,g,b,a)` */
16
+ rgb?: string | null;
17
+ /** 主题色 token。xlsx 侧解析不了,直接忽略 */
18
+ th?: number;
19
+ }
20
+ /** 下划线 / 删除线 / 上划线 */
21
+ interface TextDecorationData {
22
+ /** 0 关 1 开 */
23
+ s?: number;
24
+ cl?: ColorStyle | null;
25
+ t?: number;
26
+ }
27
+ interface BorderStyleData {
28
+ /** 对齐 `BorderStyleTypes` */
29
+ s?: number;
30
+ cl?: ColorStyle | null;
31
+ }
32
+ interface BorderData {
33
+ t?: BorderStyleData | null;
34
+ r?: BorderStyleData | null;
35
+ b?: BorderStyleData | null;
36
+ l?: BorderStyleData | null;
37
+ }
38
+ interface TextRotation {
39
+ /** 角度,-90 ~ 90,正数逆时针 */
40
+ a?: number;
41
+ /** 1 = 竖排 */
42
+ v?: number;
43
+ }
44
+ interface StyleData {
45
+ /** fontFamily */
46
+ ff?: string | null;
47
+ /** fontSize,pt */
48
+ fs?: number;
49
+ /** italic */
50
+ it?: number;
51
+ /** bold */
52
+ bl?: number;
53
+ /** underline */
54
+ ul?: TextDecorationData | null;
55
+ /** strikethrough */
56
+ st?: TextDecorationData | null;
57
+ /** overline —— xlsx 没有对应表达 */
58
+ ol?: TextDecorationData | null;
59
+ /** background */
60
+ bg?: ColorStyle | null;
61
+ /** border */
62
+ bd?: BorderData | null;
63
+ /** font color */
64
+ cl?: ColorStyle | null;
65
+ /** 上下标 */
66
+ va?: number | null;
67
+ /** 数字格式 */
68
+ n?: {
69
+ pattern?: string;
70
+ } | null;
71
+ /** textRotation */
72
+ tr?: TextRotation | null;
73
+ /** horizontalAlign */
74
+ ht?: number | null;
75
+ /** verticalAlign */
76
+ vt?: number | null;
77
+ /** wrapStrategy */
78
+ tb?: number | null;
79
+ }
80
+ /** 富文本单元格。导出只取纯文本 —— xlsx 的富文本要拆成 `<si><r>` 段,收益不抵复杂度 */
81
+ interface RichTextData {
82
+ body?: {
83
+ dataStream?: string;
84
+ } | null;
85
+ }
86
+ interface CellData {
87
+ v?: CellValue | null;
88
+ /** 对齐 `CellValueType` */
89
+ t?: number | null;
90
+ /** 原始公式,含前导 `=` */
91
+ f?: string | null;
92
+ /** 共享公式组 id。同组里只有一个单元格带 `f` */
93
+ si?: string | null;
94
+ /** 数组公式的引用范围 */
95
+ ref?: string | null;
96
+ /** 样式 id 或内联样式 */
97
+ s?: StyleData | string | null;
98
+ /** 富文本 */
99
+ p?: RichTextData | null;
100
+ }
101
+ interface RowData {
102
+ /** 高度 px */
103
+ h?: number;
104
+ /** 自适应高度 px,`ia` 为 1 时用它 */
105
+ ah?: number;
106
+ /** 是否自适应 */
107
+ ia?: number;
108
+ /** 隐藏 */
109
+ hd?: number;
110
+ s?: StyleData | string | null;
111
+ }
112
+ interface ColumnData {
113
+ /** 宽度 px */
114
+ w?: number;
115
+ hd?: number;
116
+ s?: StyleData | string | null;
117
+ }
118
+ interface CellRange {
119
+ startRow: number;
120
+ startColumn: number;
121
+ endRow: number;
122
+ endColumn: number;
123
+ }
124
+ interface FreezeData {
125
+ /** 冻结列数 */
126
+ xSplit: number;
127
+ /** 冻结行数 */
128
+ ySplit: number;
129
+ startRow: number;
130
+ startColumn: number;
131
+ }
132
+ /** `IObjectMatrixPrimitiveType<T>`:行号 → 列号 → 值,两层都是稀疏对象 */
133
+ type Matrix<T> = Record<number, Record<number, T> | undefined>;
134
+ interface WorksheetData {
135
+ id?: string;
136
+ name?: string;
137
+ /** `#RRGGBB` */
138
+ tabColor?: string;
139
+ /** 1 = 隐藏 */
140
+ hidden?: number;
141
+ freeze?: FreezeData;
142
+ rowCount?: number;
143
+ columnCount?: number;
144
+ /** px */
145
+ defaultColumnWidth?: number;
146
+ /** px */
147
+ defaultRowHeight?: number;
148
+ mergeData?: CellRange[];
149
+ cellData?: Matrix<CellData>;
150
+ rowData?: Record<number, RowData | undefined>;
151
+ columnData?: Record<number, ColumnData | undefined>;
152
+ /** 0 = 不显示网格线 */
153
+ showGridlines?: number;
154
+ /** 1 = 从右向左 */
155
+ rightToLeft?: number;
156
+ defaultStyle?: StyleData | string | null;
157
+ }
158
+ interface WorkbookData {
159
+ id?: string;
160
+ name?: string;
161
+ /** 工作表顺序。缺省时按 `sheets` 的键序 */
162
+ sheetOrder?: string[];
163
+ sheets?: Record<string, WorksheetData | undefined>;
164
+ /** 样式表:`CellData.s` 为字符串时查这里 */
165
+ styles?: Record<string, StyleData | null | undefined>;
166
+ defaultStyle?: StyleData | string | null;
167
+ /**
168
+ * 各插件自己序列化的数据(图片、条件格式、数据校验、筛选……)。
169
+ * 导出读不了它们的私有结构,但**能据此如实报告丢了什么** —— 见 `warnings`。
170
+ */
171
+ resources?: Array<{
172
+ id?: string;
173
+ name: string;
174
+ data: string;
175
+ }>;
176
+ }
177
+
178
+ /**
179
+ * Univer 快照 → xlsx 的各个 XML 部件。
180
+ *
181
+ * 纯函数、无 DOM、无引擎依赖:Node 里拿一份 JSON 就能出文件,
182
+ * 不需要把 871KB 的 Univer 渲染引擎拉起来。
183
+ *
184
+ * **能保住的**:多工作表、值与类型、公式(含共享公式与缓存值)、数字格式、
185
+ * 字体/颜色/填充/边框/对齐/换行/旋转、合并单元格、行高列宽、隐藏行列、
186
+ * 冻结窗格、网格线开关、标签颜色、工作表隐藏。
187
+ *
188
+ * **保不住的**:图片、图表、透视表、条件格式、数据校验、筛选、批注、超链接。
189
+ * 它们不在 `cellData` 里,而是各插件自己序列化进 `resources` 的私有结构 ——
190
+ * 逐个反解等于把 Univer 的插件模型抄一遍。所以这里不假装支持,
191
+ * 而是扫一遍 `resources` **如实报告丢了什么**(见 `warnings`),
192
+ * 让宿主能把话原样告诉用户,而不是让人打开文件才发现。
193
+ */
194
+
195
+ interface SerializeXlsxOptions {
196
+ /**
197
+ * 是否写入公式的缓存值,默认 true。
198
+ *
199
+ * 关掉的话单元格只剩 `<f>`,Excel 打开时自己重算 —— 值一定是对的,
200
+ * 但在**不重算的**消费方(网页预览、pandas、部分在线表格)里会显示成空。
201
+ * 开着则 Univer 算出来的值直接可见,代价是 Univer 与 Excel 的函数实现
202
+ * 有差异时会先看到 Univer 的结果,直到用户在 Excel 里触发重算。
203
+ */
204
+ cachedFormulaValues?: boolean;
205
+ /** docProps 里的作者 */
206
+ creator?: string;
207
+ /** docProps 里的时间。传固定值可得到逐字节确定的产物(测试用) */
208
+ created?: Date;
209
+ }
210
+ interface SerializeXlsxResult {
211
+ /** zip 条目:路径 → 内容 */
212
+ files: Record<string, Uint8Array>;
213
+ /** 导出时丢掉/降级了什么。**要展示给用户**,别吞 */
214
+ warnings: string[];
215
+ /** 计数,用于量化保真度 */
216
+ stats: Record<string, number>;
217
+ }
218
+ declare function serializeXlsx(workbook: WorkbookData, options?: SerializeXlsxOptions): SerializeXlsxResult;
219
+
220
+ /**
221
+ * `@4399ywkf/editor/xlsx` —— 表格导出。
222
+ *
223
+ * 和 `./docx` 同一套思路:**在本地生成,文档不出内网**,不依赖任何转换服务,
224
+ * 也不引 SheetJS —— 打包只多出 fflate(本来就有)和这几百行 OOXML 拼装。
225
+ *
226
+ * ```ts
227
+ * import { downloadXlsx } from "@4399ywkf/editor/xlsx"
228
+ *
229
+ * // SheetView 的 onReady 给的就是 univerAPI
230
+ * <DocumentView format="sheet" onReady={(api) => (ref.current = api)} />
231
+ * downloadXlsx(ref.current, "年度报表.xlsx")
232
+ * ```
233
+ *
234
+ * 服务端 / 定时任务只有 JSON 也能用 —— 整条链路不碰 DOM:
235
+ *
236
+ * ```ts
237
+ * import { xlsxFromSnapshot } from "@4399ywkf/editor/xlsx"
238
+ * const { bytes, warnings } = xlsxFromSnapshot(snapshot)
239
+ * ```
240
+ *
241
+ * `warnings` 一定要透给用户。Univer 把图片、条件格式、数据校验这些存在插件数据里,
242
+ * 不在单元格模型内,导出必然丢 —— 打开文件才发现远比导出时被告知糟糕。
243
+ */
244
+
245
+ declare const XLSX_MIME = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
246
+ interface XlsxResult {
247
+ bytes: Uint8Array;
248
+ /** 导出时丢掉/降级了什么。**请展示给用户** */
249
+ warnings: string[];
250
+ stats: Record<string, number>;
251
+ }
252
+ /** 纯函数:Univer 快照 → .xlsx 字节。不依赖 DOM 与引擎,服务端 / 测试直接可用。 */
253
+ declare function xlsxFromSnapshot(workbook: WorkbookData, options?: SerializeXlsxOptions): XlsxResult;
254
+ /**
255
+ * 从 Univer 实例取快照。
256
+ *
257
+ * 参数类型是 `unknown` 而不是 `FUniver`:`@univerjs/presets` 是可选 peer,
258
+ * 写成具体类型会让没装它的项目 typecheck 直接红。运行时按 facade 的两个
259
+ * 方法名依次试探(`getSnapshot` 是新名字,`save` 是旧的)。
260
+ */
261
+ declare function snapshotFromUniver(univerAPI: unknown): WorkbookData;
262
+ /** Univer 实例或快照 → .xlsx 字节 */
263
+ declare function exportXlsx(source: WorkbookData | unknown, options?: SerializeXlsxOptions): XlsxResult;
264
+ /** Univer 实例或快照 → Blob,便于交给上传接口 */
265
+ declare function xlsxBlob(source: WorkbookData | unknown, options?: SerializeXlsxOptions): Blob;
266
+ /**
267
+ * 触发浏览器下载。返回 warnings / stats —— 别丢,那是「这次导出丢了什么」的唯一出口。
268
+ *
269
+ * 只在浏览器里可用;Node 环境请用 `xlsxFromSnapshot` 自己落盘。
270
+ */
271
+ declare function downloadXlsx(source: WorkbookData | unknown, filename?: string, options?: SerializeXlsxOptions): XlsxResult;
272
+
273
+ export { type CellData, type CellRange, type CellValue, type ColumnData, type RowData, type SerializeXlsxOptions, type SerializeXlsxResult, type StyleData, type WorkbookData, type WorksheetData, XLSX_MIME, type XlsxResult, downloadXlsx, exportXlsx, serializeXlsx, snapshotFromUniver, xlsxBlob, xlsxFromSnapshot };