rapid-spreadjs 1.0.3 → 1.0.6

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/package.json CHANGED
@@ -1,13 +1,14 @@
1
1
  {
2
2
  "name": "rapid-spreadjs",
3
- "version": "1.0.3",
4
- "description": "SpreadJS常用公用处理函数。",
5
- "main": "./src/index.js",
6
- "types": "dist/types/src/index.d.ts",
7
- "license": "MIT",
3
+ "version": "1.0.6",
4
+ "description": "SpreadJS常用公用处理函数,包括设计器、工作簿和工作表的相关处理函数。",
5
+ "type": "module",
6
+ "main": "dist/index.cjs.js",
7
+ "module": "dist/index.esm.js",
8
+ "types": "dist/index.d.ts",
8
9
  "scripts": {
9
10
  "test": "echo \"Error: no test specified\" && exit 1",
10
- "build": "tsc && babel src -d dist"
11
+ "build": "rollup -c"
11
12
  },
12
13
  "keywords": [
13
14
  "utils",
@@ -23,13 +24,16 @@
23
24
  "email": "qubernet@163.com",
24
25
  "url": "https://www.cnblogs.com/qubernet"
25
26
  },
26
- "homepage": "https://www.cnblogs.com/qubernet",
27
- "dependencies": {
28
- "typescript": "^5.7.3"
29
- },
27
+ "license": "MIT",
30
28
  "devDependencies": {
31
- "@babel/cli": "^7.26.4",
32
- "@babel/core": "^7.26.9",
33
- "@babel/preset-env": "^7.26.9"
29
+ "@rollup/plugin-terser": "^0.4.4",
30
+ "@rollup/plugin-typescript": "^12.1.2",
31
+ "rollup": "^4.34.9",
32
+ "tslib": "^2.8.1",
33
+ "typescript": "^5.8.2"
34
+ },
35
+ "dependencies": {
36
+ "@types/file-saver": "^2.0.7",
37
+ "file-saver": "^2.0.5"
34
38
  }
35
39
  }
@@ -0,0 +1,51 @@
1
+ import typescript from "@rollup/plugin-typescript";
2
+ import terser from "@rollup/plugin-terser";
3
+
4
+ export default {
5
+ input: "src/index.ts",
6
+ output: [
7
+ // 未压缩的ES模块
8
+ {
9
+ file: "dist/index.esm.js",
10
+ format: "es",
11
+ sourcemap: true, // 可选:生成SourceMap
12
+ },
13
+ // 压缩后的ES模块
14
+ {
15
+ file: "dist/index.esm.min.js",
16
+ format: "es",
17
+ plugins: [
18
+ // 仅针对此输出启用压缩
19
+ terser({
20
+ compress: { drop_console: true }, // 删除console语句
21
+ format: { comments: false }, // 移除注释
22
+ }),
23
+ ],
24
+ sourcemap: true,
25
+ },
26
+ // 未压缩的CommonJS模块
27
+ {
28
+ file: "dist/index.cjs.js",
29
+ format: "cjs",
30
+ sourcemap: true,
31
+ },
32
+ // 压缩后的CommonJS模块
33
+ {
34
+ file: "dist/index.cjs.min.js",
35
+ format: "cjs",
36
+ plugins: [
37
+ terser({
38
+ compress: { drop_console: true },
39
+ format: { comments: false },
40
+ }),
41
+ ],
42
+ sourcemap: true,
43
+ },
44
+ ],
45
+ plugins: [
46
+ typescript({
47
+ tsconfig: "./tsconfig.json",
48
+ // exclude: ["**/*.test.ts"],
49
+ }),
50
+ ],
51
+ };
package/src/index.ts ADDED
@@ -0,0 +1,11 @@
1
+ /** 导出/types/sheet.ts中的所有type */
2
+ export * from "./types/sheet";
3
+
4
+ /** 导出/utils/common.ts中的所有属性或函数 */
5
+ export * from "./utils/common";
6
+
7
+ /** 导出/utils/sheet.ts中的所有属性或函数 */
8
+ export * from "./utils/sheet";
9
+
10
+ /** 导出/utils/wookbook.ts中的所有属性或函数 */
11
+ export * from "./utils/wookbook";
@@ -0,0 +1,22 @@
1
+ /**
2
+ * 公用工具函数
3
+ */
4
+ export const CommonUtils = {
5
+ /**
6
+ * 获取列的英文字母
7
+ * @param colIndex 列的索引
8
+ * @returns 返回英文字母
9
+ */
10
+ getColLetterName: (colIndex: number): string => {
11
+ colIndex += 1;
12
+
13
+ let letter = "";
14
+ while (colIndex > 0) {
15
+ const remainder = (colIndex - 1) % 26;
16
+ letter = String.fromCharCode(remainder + 65) + letter;
17
+ colIndex = Math.floor((colIndex - 1) / 26);
18
+ }
19
+
20
+ return letter.toUpperCase();
21
+ },
22
+ };
@@ -1,3 +1,4 @@
1
+ // 导入单元格实体
1
2
  import { CellModel } from "../types/sheet";
2
3
 
3
4
  /**
@@ -9,7 +10,7 @@ export const SheetUtils = {
9
10
  * @param spread 工作簿对象
10
11
  * @returns 返回选中单元格的集合,格式如:[{row:,col:,rowCount:,colCount:}]
11
12
  */
12
- getActiveSheetSelectCells: (spread: any) => {
13
+ getActiveSheetSelectCells: (spread: any): Array<CellModel> => {
13
14
  const sheet = spread.getActiveSheet();
14
15
 
15
16
  return sheet.getSelections();
@@ -30,6 +31,399 @@ export const SheetUtils = {
30
31
  cellObj.colCount
31
32
  );
32
33
  },
34
+
35
+ /**
36
+ * 获取某工作表中某范围集合中所有的单元格坐标对象集合
37
+ * 该方法适用的场景如:手动选择了很多个单元格范围,在这些范围中可能包含独立的单元格,也可能包含合并的单元格,这时候就需要用到如下方法获取到所有单元格对象
38
+ * @param sheet 工作表实例
39
+ * @param selectRanges 单元格范围集合,格式如:[{ row: 0, col: 0, rowCount: 2, colCount: 2 }]
40
+ * @param isMulColOrder 是否按照多列的顺序排序单元格(true:按照第一列从上到下、第二列从上到下……的顺序获取单元格数据、false:按照第一行从左到右、第二行从左到右……的顺序获取单元格数据)
41
+ * 返回数组集合,格式如:[{ row: 0, col: 0, rowCount: 1, colCount: 1 }]
42
+ */
43
+ getAllCellObjsByRanges: (
44
+ sheet: any,
45
+ selectRanges: Array<CellModel>,
46
+ isMulColOrder: boolean = false
47
+ ) => {
48
+ // 得到所有的单元格坐标对象集合,格式如:[{ row: 0, col: 0, rowCount: 1, colCount: 1 }]
49
+ let allCellObjs: Array<CellModel> = [];
50
+
51
+ selectRanges.forEach((item, index) => {
52
+ // 得到当前item中所有合并的单元格对象集合
53
+ const itemSpanCells = sheet.getSpans(item);
54
+ itemSpanCells.forEach((itemSpanCell: CellModel) => {
55
+ allCellObjs.push({
56
+ row: itemSpanCell.row,
57
+ col: itemSpanCell.col,
58
+ rowCount: itemSpanCell.rowCount,
59
+ colCount: itemSpanCell.colCount,
60
+ });
61
+ });
62
+
63
+ if (
64
+ item.row === undefined ||
65
+ item.col === undefined ||
66
+ item.rowCount === undefined ||
67
+ item.colCount === undefined
68
+ ) {
69
+ throw new Error("row、col、rowCount、colCount is required.");
70
+ }
71
+
72
+ // 遍历item中涉及到的所有单元格
73
+ for (let i = item.row; i < +item.row + item.rowCount; i++) {
74
+ for (let j = item.col; j < item.col + item.colCount; j++) {
75
+ const curSpanCellObj = sheet.getSpan(i, j);
76
+
77
+ // 检查当前单元格是否为合并单元格
78
+ if (curSpanCellObj == null) {
79
+ allCellObjs.push({
80
+ row: i,
81
+ col: j,
82
+ rowCount: 1,
83
+ colCount: 1,
84
+ });
85
+ }
86
+ }
87
+ }
88
+ });
89
+
90
+ // 常规顺序
91
+ if (!isMulColOrder) {
92
+ // 对单元格集合对象对象进行排序,先按row后按col进行升序排序(注意:此处非常重要,如果不排序,获取到的单元格顺序有可能是乱的【不完全按照模板顺序返回】)
93
+ allCellObjs.sort((a: CellModel, b: CellModel) => {
94
+ if (
95
+ a.row === undefined ||
96
+ b.row === undefined ||
97
+ a.col === undefined ||
98
+ b.col === undefined
99
+ ) {
100
+ throw new Error("row、col is required.");
101
+ }
102
+
103
+ if (a.row == b.row) {
104
+ return a.col - b.col;
105
+ }
106
+
107
+ return a.row - b.row;
108
+ });
109
+ }
110
+
111
+ return allCellObjs;
112
+ },
113
+
114
+ /**
115
+ * 获取某工作表中所有的单元格坐标对象集合
116
+ * @param sheet 工作表实例
117
+ * 返回数组集合,格式如:[{ col: 0, colCount: 2, row: 0, rowCount: 1 }]
118
+ */
119
+ getSheetAllCellObjs: (sheet: any): Array<CellModel> => {
120
+ // 得到当前Sheet单元格范围
121
+ const curSheetCellRange = [
122
+ {
123
+ row: 0,
124
+ col: 0,
125
+ rowCount: sheet.getRowCount(),
126
+ colCount: sheet.getColumnCount(),
127
+ },
128
+ ];
129
+
130
+ // 得到所有的单元格坐标对象集合,格式如:[{ col: 0, colCount: 2, row: 0, rowCount: 1 }]
131
+ const allCellObjs = SheetUtils.getAllCellObjsByRanges(
132
+ sheet,
133
+ curSheetCellRange
134
+ );
135
+
136
+ return allCellObjs;
137
+ },
138
+
139
+ /**
140
+ * 获取某工作表中选中的所有单元格坐标对象集合
141
+ * @param spread 工作簿实例
142
+ * 返回数组集合,格式如:[{ col: 0, row: 0 }]
143
+ */
144
+ getActiveSheetSelectCellObjs: (spread: any): Array<CellModel> => {
145
+ // 当前激活的Sheet
146
+ const sheet = spread.getActiveSheet();
147
+
148
+ // 得到当前Sheet中选择的单元格范围集合
149
+ const curSheetCellRange = sheet.getSelections();
150
+
151
+ // 得到所有的单元格坐标对象集合,格式如:[{ col: 0, colCount: 2, row: 0, rowCount: 1 }]
152
+ const allCellObjs = SheetUtils.getAllCellObjsByRanges(
153
+ sheet,
154
+ curSheetCellRange
155
+ );
156
+
157
+ return allCellObjs;
158
+ },
159
+
160
+ /**
161
+ * 获取单元格类型名称
162
+ * @param GC GC对象
163
+ * @param sheet 工作表实例
164
+ * @param row 单元格行索引
165
+ * @param col 单元格列索引
166
+ * @returns 返回单元格类型名称,如:button、radio、checkbox、checkboxList、hyperLink、comboBox、date、dateRange、calc
167
+ */
168
+ getCellType: (
169
+ GC: any,
170
+ sheet: any,
171
+ row: number,
172
+ col: number
173
+ ): string | undefined => {
174
+ let cellTypeName;
175
+
176
+ // 先根据getCellType来获取
177
+ const cellType = sheet.getCellType(row, col);
178
+
179
+ if (cellType instanceof GC.Spread.Sheets.CellTypes.Button) {
180
+ cellTypeName = "button";
181
+ } else if (
182
+ cellType instanceof GC.Spread.Sheets.CellTypes.RadioButtonList
183
+ ) {
184
+ cellTypeName = "radio";
185
+ } else if (cellType instanceof GC.Spread.Sheets.CellTypes.CheckBox) {
186
+ cellTypeName = "checkbox";
187
+ } else if (
188
+ cellType instanceof GC.Spread.Sheets.CellTypes.CheckBoxList
189
+ ) {
190
+ cellTypeName = "checkboxList";
191
+ } else if (cellType instanceof GC.Spread.Sheets.CellTypes.HyperLink) {
192
+ cellTypeName = "hyperLink";
193
+ } else if (cellType instanceof GC.Spread.Sheets.CellTypes.ComboBox) {
194
+ cellTypeName = "comboBox";
195
+ }
196
+
197
+ // 根据getStyle来获取
198
+ if (cellTypeName == undefined || cellTypeName == null) {
199
+ const cellStyle = sheet.getStyle(row, col);
200
+
201
+ if (
202
+ cellStyle &&
203
+ cellStyle.cellButtons &&
204
+ cellStyle.cellButtons.length > 0
205
+ ) {
206
+ if (cellStyle.cellButtons[0].command == "openDateTimePicker") {
207
+ if (cellStyle.dropDowns && cellStyle.dropDowns.length > 0) {
208
+ if (cellStyle.dropDowns[0].option.showDateRange) {
209
+ cellTypeName = "dateRange";
210
+ } else {
211
+ cellTypeName = "date";
212
+ }
213
+ }
214
+ } else if (
215
+ cellStyle.cellButtons[0].command == "openCalculator"
216
+ ) {
217
+ cellTypeName = "calc";
218
+ }
219
+ }
220
+ }
221
+
222
+ return cellTypeName;
223
+ },
224
+
225
+ /**
226
+ * 设置工作簿中所有工作表的缩放比例
227
+ * @param scale 缩放比例,默认为:1(100%)
228
+ */
229
+ setSheetZoom: (spread: any, scale: number) => {
230
+ // 暂停绘制
231
+ spread.suspendPaint();
232
+
233
+ const sheetCount = spread.getSheetCount();
234
+ for (let i = 0; i < sheetCount; i++) {
235
+ const sheet = spread.getSheet(i);
236
+
237
+ sheet.zoom(scale);
238
+ }
239
+
240
+ // 恢复绘制
241
+ spread.resumePaint();
242
+ },
243
+
244
+ /**
245
+ * 设置当前激活的工作表中选中的单元格背景颜色
246
+ * @param spread 工作簿实例
247
+ * @param color 背景颜色
248
+ */
249
+ setActiveSheetBgColor: (spread: any, color: string) => {
250
+ const sheet = spread.getActiveSheet();
251
+
252
+ // 暂停绘制
253
+ sheet.suspendPaint();
254
+
255
+ //#region 方式1:通过getRange进行批量设置,推荐使用该方法来实现
256
+
257
+ // 获取选择的多个单元格范围集合
258
+ const selections = SheetUtils.getActiveSheetSelectCells(spread);
259
+
260
+ // 循环每个选中的单元格范围
261
+ selections.forEach((sel: CellModel) => {
262
+ // 批量设置每个选中的单元格范围的背景颜色
263
+ sheet
264
+ .getRange(sel.row, sel.col, sel.rowCount, sel.colCount)
265
+ .backColor(color);
266
+ });
267
+
268
+ //#endregion
269
+
270
+ //#region 方式2:通过getCell获取单元格样式对每个单元格进行设置,此方式效率相对较低,不推荐
271
+
272
+ // // 当前激活的工作表中所有选择的单元格
273
+ // const selectCells = SheetUtils.getActiveSheetSelectCellObjs(spread);
274
+
275
+ // selectCells.forEach(item => {
276
+ // // 获取单元格对象
277
+ // const cellObj = sheet.getCell(item.row, item.col);
278
+
279
+ // // 获取单元格样式
280
+ // let cellStyle = sheet.getStyle(item.row, item.col);
281
+ // if (!cellStyle) {
282
+ // // 不存在样式则new一个
283
+ // cellStyle = new GC.Spread.Sheets.Style();
284
+ // }
285
+
286
+ // // 设置单元格背景颜色
287
+ // cellStyle.backColor = color;
288
+
289
+ // // 重新设置单元格样式
290
+ // cellObj.setStyle(cellStyle);
291
+ // });
292
+
293
+ //#endregion
294
+
295
+ // 恢复绘制
296
+ sheet.resumePaint();
297
+ },
298
+
299
+ /**
300
+ * 设置工作簿中所有工作表是否显示分页线
301
+ * @param scale 缩放比例,默认为:1(100%)
302
+ */
303
+ setSheetShowPrintPageLine: (spread: any, isShow: boolean) => {
304
+ // 暂停绘制
305
+ spread.suspendPaint();
306
+
307
+ const sheetCount = spread.getSheetCount();
308
+ for (let i = 0; i < sheetCount; i++) {
309
+ const sheet = spread.getSheet(i);
310
+
311
+ var isVisible = sheet.isPrintLineVisible();
312
+ sheet.isPrintLineVisible(!isVisible);
313
+ }
314
+
315
+ // 恢复绘制
316
+ spread.resumePaint();
317
+ },
318
+
319
+ /**
320
+ * 忽略工作工作表元格中指定字符串不显示,如:#DIV/0!、#VALUE!等
321
+ * @param GC GC对象
322
+ */
323
+ ignoreSheetShowStrs: (GC: any) => {
324
+ // 忽略的字符串集合
325
+ const ignoreStrs: Array<string> = [
326
+ "#DIV/0!",
327
+ "#VALUE!",
328
+ "#NAME?",
329
+ "#N/A",
330
+ "#NULL!",
331
+ "#NUM!",
332
+ "#REF!",
333
+ ];
334
+
335
+ // paint对象
336
+ const ignorePaint = GC.Spread.Sheets.CellTypes.Text.prototype.paint;
337
+
338
+ // 重写paint函数
339
+ GC.Spread.Sheets.CellTypes.Text.prototype.paint = function (
340
+ ctx: any,
341
+ value: any,
342
+ x: any,
343
+ y: any,
344
+ w: any,
345
+ h: any,
346
+ style: any,
347
+ options: any
348
+ ) {
349
+ // 如果在忽略的字符串集合中有匹配的结果,则将其显示为空字符串
350
+ if (ignoreStrs.some((item) => item == value)) {
351
+ ignorePaint.call(this, ctx, "/", x, y, w, h, style, options);
352
+ } else {
353
+ ignorePaint.apply(this, arguments);
354
+ }
355
+
356
+ // // 或使用如下代码
357
+ // if (ignoreStrs.some((item) => item == value)) {
358
+ // value = '';
359
+ // }
360
+ // ignorePaint.apply(this, [ctx, value, x, y, w, h, style, options]);
361
+ };
362
+ },
363
+
364
+ /**
365
+ * 设置工作表显示辅助线(底部和右侧的辅助线)
366
+ * @param sheet 工作表实例
367
+ * @param width 宽度(像素)
368
+ * @param height 高度(像素)
369
+ * @param lineWidth 辅助线宽度(默认为:1像素)
370
+ * @param colorStr 辅助线颜色(默认为:#409eff)
371
+ * @param isHb 是否为横表(默认为:false)
372
+ */
373
+ setSheetAuxiliaryLine: (
374
+ GC: any,
375
+ sheet: any,
376
+ width: number,
377
+ height: number,
378
+ lineWidth: number = 1,
379
+ colorStr: string = "#409eff",
380
+ isHb: boolean = false
381
+ ) => {
382
+ // 创建底部和右侧的浮动对象
383
+ let floatBottom = new GC.Spread.Sheets.FloatingObjects.FloatingObject(
384
+ `floatLineBottom${isHb ? "Hb" : ""}`,
385
+ 0,
386
+ height,
387
+ width,
388
+ lineWidth
389
+ ),
390
+ floatRight = new GC.Spread.Sheets.FloatingObjects.FloatingObject(
391
+ `floatLineRight${isHb ? "Hb" : ""}`,
392
+ width,
393
+ 0,
394
+ lineWidth,
395
+ height
396
+ );
397
+
398
+ // 创建底部和右侧的div对象
399
+ let divBottom = document.createElement("div"),
400
+ divRight = document.createElement("div");
401
+
402
+ // 设置底部div对象样式
403
+ divBottom.style.width = `${width}px`;
404
+ divBottom.style.height = "1px";
405
+ divBottom.style.borderBottom = `${lineWidth}px dashed ${colorStr}`;
406
+
407
+ // 设置右侧div对象样式
408
+ divRight.style.width = "1px";
409
+ divRight.style.height = `${height}px`;
410
+ divRight.style.borderLeft = `${lineWidth}px dashed ${colorStr}`;
411
+
412
+ // floatBottom.fixedPosition(true);
413
+ // floatRight.fixedPosition(true);
414
+
415
+ // 设置底部和右侧浮动对象不允许改变大小、不允许拖动和设置浮动对象的内容
416
+ floatBottom.allowResize(false);
417
+ floatBottom.allowMove(false);
418
+ floatBottom.content(divBottom);
419
+ floatRight.allowResize(false);
420
+ floatRight.allowMove(false);
421
+ floatRight.content(divRight);
422
+
423
+ // 将浮动元素添加到工作表中
424
+ sheet.floatingObjects.add(floatBottom);
425
+ sheet.floatingObjects.add(floatRight);
426
+ },
33
427
  };
34
428
 
35
429
  /**