@corbe30/fortune-excel 2.2.4 → 2.2.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/README.md CHANGED
@@ -7,29 +7,33 @@
7
7
  <div align="center">
8
8
 
9
9
  <p>
10
- <a href="http://npmjs.com/package/@corbe30/fortune-excel" alt="fortuneExcel on npm">
11
- <img src="https://img.shields.io/npm/v/@corbe30/fortune-excel" /></a> <a href="http://npmjs.com/package/@corbe30/fortune-excel" alt="fortuneExcel downloads">
12
- <img src="https://img.shields.io/npm/d18m/%40corbe30%2Ffortune-excel" /></a>
10
+ <a href="http://npmjs.com/package/@corbe30/fortune-excel" alt="fortuneExcel on npm">
11
+ <img src="https://img.shields.io/npm/v/@corbe30/fortune-excel" />
12
+ </a>
13
+ <a href="http://npmjs.com/package/@corbe30/fortune-excel" alt="fortuneExcel downloads">
14
+ <img src="https://img.shields.io/npm/d18m/%40corbe30%2Ffortune-excel" />
15
+ </a>
16
+ <a href="https://corbe30.github.io/FortuneExcel/" alt="fortuneExcel storybok">
17
+ <img src="https://img.shields.io/badge/storybook-FF4785" />
18
+ </a>
13
19
  </p>
14
20
 
15
21
  </div>
16
22
 
17
23
  ## Usage
18
24
 
19
- You can check the example in [Storybook](https://github.com/Corbe30/FortuneExcel/blob/main/src/stories/Page.tsx).
20
-
21
25
  1. Install the package:
22
26
  ```js
23
27
  npm i @corbe30/fortune-excel
24
28
  ```
25
29
 
26
- 2. Add import/export toolbar item in fortune-sheet
27
- > `<FortuneExcelHelper />` is a hidden component.
30
+ 2. Import/export toolbar item ([code example](https://github.com/Corbe30/FortuneExcel/tree/main/src/stories/Plugin.tsx))
31
+ > Note: `<FortuneExcelHelper />` is a hidden component.
28
32
  ```js
29
33
  import { FortuneExcelHelper, importToolBarItem, exportToolBarItem } from "@corbe30/fortune-excel";
30
34
 
31
35
  function App() {
32
- const workbookRef = useRef();
36
+ const sheetRef = useRef();
33
37
  const [key, setKey] = useState(0);
34
38
  const [sheets, setSheets] = useState(data);
35
39
 
@@ -39,13 +43,13 @@ You can check the example in [Storybook](https://github.com/Corbe30/FortuneExcel
39
43
  setKey={setKey}
40
44
  setSheets={setSheets}
41
45
  sheetRef={sheetRef}
42
- config={{ // this is the default config object
46
+ config={{ // default = all values are true
43
47
  import: { xlsx: true, csv: true },
44
48
  export: { xlsx: true, csv: true },
45
49
  }}
46
50
  />
47
51
  <Workbook
48
- key={key} data={sheets} ref={workbookRef}
52
+ key={key} data={sheets} ref={sheetRef}
49
53
  customToolbarItems={[importToolBarItem(), exportToolBarItem()]}
50
54
  />
51
55
  </>
@@ -53,7 +57,7 @@ You can check the example in [Storybook](https://github.com/Corbe30/FortuneExcel
53
57
  }
54
58
  ```
55
59
 
56
- 3. You can programmatically import/export as well:
60
+ 3. Programmatic import/export ([code example](https://github.com/Corbe30/FortuneExcel/tree/main/src/stories/Manual.tsx))
57
61
  ```js
58
62
  import { transformFortuneToExcel } from "@corbe30/fortune-excel";
59
63
 
@@ -61,12 +65,24 @@ You can check the example in [Storybook](https://github.com/Corbe30/FortuneExcel
61
65
  const exportedFile = await transformFortuneToExcel(
62
66
  sheetRef.current,
63
67
  "xlsx", // or "csv"; default = "xlsx"
64
- true // if you want to start automatic download; default = true
68
+ true // start automatic download; default = true
65
69
  );
66
70
  console.log("Exported file data:", exportedFile);
67
71
  };
68
72
 
69
- <button onClick={manualExport}>Try Manual Export!</button>
73
+ <button onClick={manualExport}>Export</button>
74
+ ```
75
+ ```js
76
+ import { transformExcelToFortune } from "@corbe30/fortune-excel";
77
+
78
+ const manualImport = async (event) => {
79
+ await transformExcelToFortune(
80
+ event.target.files[0], // file type (csv/xlsx) is automatically identified
81
+ setSheets,
82
+ setKey,
83
+ sheetRef.current
84
+ )
85
+ }
70
86
  ```
71
87
 
72
88
  ## Authors and acknowledgment
@@ -20,6 +20,7 @@ var FortuneBase_1 = require("./FortuneBase");
20
20
  var emf_1 = require("../common/emf");
21
21
  var ImageList = /** @class */ (function () {
22
22
  function ImageList(files) {
23
+ var _a;
23
24
  if (files == null) {
24
25
  return;
25
26
  }
@@ -28,7 +29,7 @@ var ImageList = /** @class */ (function () {
28
29
  // let reg = new RegExp("xl/media/image1.png", "g");
29
30
  if (fileKey.indexOf("xl/media/") > -1) {
30
31
  var fileNameArr = fileKey.split(".");
31
- var suffix = fileNameArr[fileNameArr.length - 1].toLowerCase();
32
+ var suffix = (_a = fileNameArr[fileNameArr.length - 1]) === null || _a === void 0 ? void 0 : _a.toLowerCase();
32
33
  if (suffix in
33
34
  { png: 1, jpeg: 1, jpg: 1, gif: 1, bmp: 1, tif: 1, webp: 1, emf: 1 }) {
34
35
  if (suffix == "emf") {
@@ -46,22 +46,23 @@ var HandleZip = /** @class */ (function () {
46
46
  this.uploadFile = file;
47
47
  }
48
48
  HandleZip.prototype.unzipFile = function () {
49
+ var _a;
49
50
  return __awaiter(this, void 0, void 0, function () {
50
- var zip, fileList, _i, _a, _b, _path, zipEntry, fileName, fileNameArr, suffix, fileType, data;
51
- return __generator(this, function (_c) {
52
- switch (_c.label) {
51
+ var zip, fileList, _i, _b, _c, _path, zipEntry, fileName, fileNameArr, suffix, fileType, data;
52
+ return __generator(this, function (_d) {
53
+ switch (_d.label) {
53
54
  case 0: return [4 /*yield*/, jszip_1.default.loadAsync(this.uploadFile)];
54
55
  case 1:
55
- zip = _c.sent();
56
+ zip = _d.sent();
56
57
  fileList = {};
57
- _i = 0, _a = Object.entries(zip.files);
58
- _c.label = 2;
58
+ _i = 0, _b = Object.entries(zip.files);
59
+ _d.label = 2;
59
60
  case 2:
60
- if (!(_i < _a.length)) return [3 /*break*/, 5];
61
- _b = _a[_i], _path = _b[0], zipEntry = _b[1];
61
+ if (!(_i < _b.length)) return [3 /*break*/, 5];
62
+ _c = _b[_i], _path = _c[0], zipEntry = _c[1];
62
63
  fileName = zipEntry.name;
63
64
  fileNameArr = fileName.split(".");
64
- suffix = fileNameArr[fileNameArr.length - 1].toLowerCase();
65
+ suffix = (_a = fileNameArr[fileNameArr.length - 1]) === null || _a === void 0 ? void 0 : _a.toLowerCase();
65
66
  fileType = "string";
66
67
  if (suffix in
67
68
  {
@@ -80,12 +81,12 @@ var HandleZip = /** @class */ (function () {
80
81
  }
81
82
  return [4 /*yield*/, zipEntry.async(fileType)];
82
83
  case 3:
83
- data = _c.sent();
84
+ data = _d.sent();
84
85
  if (fileType == "base64") {
85
86
  data = "data:image/" + suffix + ";base64," + data;
86
87
  }
87
88
  fileList[zipEntry.name] = data; // XXX: ignore EMF ArrayBuffer :/
88
- _c.label = 4;
89
+ _d.label = 4;
89
90
  case 4:
90
91
  _i++;
91
92
  return [3 /*break*/, 2];
@@ -46,11 +46,12 @@ var ImportHelper = function (props) {
46
46
  var setSheets = props.setSheets, setKey = props.setKey, sheetRef = props.sheetRef, config = props.config;
47
47
  var acceptTypes = "".concat(config.xlsx ? ".xlsx," : "").concat(config.csv ? ".csv" : "");
48
48
  return (react_1.default.createElement("input", { type: "file", id: "ImportHelper", accept: acceptTypes, onChange: function (e) { return __awaiter(void 0, void 0, void 0, function () {
49
- return __generator(this, function (_a) {
50
- switch (_a.label) {
51
- case 0: return [4 /*yield*/, (0, Transform_1.transformExcelToFortune)(e, setSheets, setKey, sheetRef)];
49
+ var _a, _b;
50
+ return __generator(this, function (_c) {
51
+ switch (_c.label) {
52
+ case 0: return [4 /*yield*/, (0, Transform_1.transformExcelToFortune)((_b = (_a = e === null || e === void 0 ? void 0 : e.target) === null || _a === void 0 ? void 0 : _a.files) === null || _b === void 0 ? void 0 : _b[0], setSheets, setKey, sheetRef.current)];
52
53
  case 1:
53
- _a.sent();
54
+ _c.sent();
54
55
  return [2 /*return*/];
55
56
  }
56
57
  });
@@ -1,3 +1,3 @@
1
1
  import { IFileType } from "./ICommon";
2
- export declare const transformExcelToFortune: (e: any, setSheets: any, setKey: any, sheetRef: any) => Promise<void>;
2
+ export declare const transformExcelToFortune: (file: any, setSheets: any, setKey: any, sheetRef: any) => Promise<void>;
3
3
  export declare const transformFortuneToExcel: (luckysheetRef: any, fileType?: IFileType, download?: boolean) => Promise<Blob>;
@@ -85,40 +85,39 @@ var convertCsvToExcel = function (file) { return __awaiter(void 0, void 0, void
85
85
  }
86
86
  });
87
87
  }); };
88
- var transformExcelToFortune = function (e, setSheets, setKey, sheetRef) { return __awaiter(void 0, void 0, void 0, function () {
89
- var file, excelFile, fileName, files, fortuneFile, lsh;
90
- return __generator(this, function (_a) {
91
- switch (_a.label) {
88
+ var transformExcelToFortune = function (file, setSheets, setKey, sheetRef) { return __awaiter(void 0, void 0, void 0, function () {
89
+ var excelFile, fileName, files, fortuneFile, lsh;
90
+ var _a, _b;
91
+ return __generator(this, function (_c) {
92
+ switch (_c.label) {
92
93
  case 0:
93
- file = e.target.files[0];
94
94
  fileName = file.name;
95
- if (!(file.type === 'text/csv' || file.name.toLowerCase().endsWith('.csv'))) return [3 /*break*/, 2];
95
+ if (!(file.type === 'text/csv' || ((_b = (_a = file === null || file === void 0 ? void 0 : file.name) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.endsWith('.csv')))) return [3 /*break*/, 2];
96
96
  return [4 /*yield*/, convertCsvToExcel(file)];
97
97
  case 1:
98
98
  // convert CSV to xlsx
99
- excelFile = _a.sent();
99
+ excelFile = _c.sent();
100
100
  fileName = file.name.replace(/\.csv$/i, '.xlsx');
101
101
  return [3 /*break*/, 4];
102
102
  case 2: return [4 /*yield*/, file.arrayBuffer()];
103
103
  case 3:
104
104
  // process as excel file
105
- excelFile = _a.sent();
106
- _a.label = 4;
105
+ excelFile = _c.sent();
106
+ _c.label = 4;
107
107
  case 4: return [4 /*yield*/, new HandleZip_1.HandleZip(new File([excelFile], fileName)).unzipFile()];
108
108
  case 5:
109
- files = _a.sent();
109
+ files = _c.sent();
110
110
  fortuneFile = new FortuneFile_1.FortuneFile(files, fileName);
111
111
  fortuneFile.Parse();
112
112
  lsh = fortuneFile.serialize();
113
113
  setSheets(lsh.sheets);
114
114
  setKey(function (k) { return k + 1; });
115
115
  setTimeout(function () {
116
- var _a, _b;
117
- for (var _i = 0, _c = lsh.sheets; _i < _c.length; _i++) {
118
- var sheet = _c[_i];
116
+ for (var _i = 0, _a = lsh.sheets; _i < _a.length; _i++) {
117
+ var sheet = _a[_i];
119
118
  var config = sheet.config;
120
- (_a = sheetRef.current) === null || _a === void 0 ? void 0 : _a.setColumnWidth((config === null || config === void 0 ? void 0 : config.columnlen) || {}, { id: sheet.id });
121
- (_b = sheetRef.current) === null || _b === void 0 ? void 0 : _b.setRowHeight((config === null || config === void 0 ? void 0 : config.rowlen) || {}, { id: sheet.id });
119
+ sheetRef.setColumnWidth((config === null || config === void 0 ? void 0 : config.columnlen) || {}, { id: sheet.id });
120
+ sheetRef.setRowHeight((config === null || config === void 0 ? void 0 : config.rowlen) || {}, { id: sheet.id });
122
121
  }
123
122
  }, 1);
124
123
  return [2 /*return*/];
@@ -1222,7 +1222,7 @@ exports.ToContext2D.prototype._setStyle = function (gst, ctx) {
1222
1222
  dsh[i] = _flt(dsh[i] * scl);
1223
1223
  ctx.setLineDash(dsh);
1224
1224
  ctx.miterLimit = gst.mlimit * scl;
1225
- var fn = gst.font.Tf, ln = fn.toLowerCase();
1225
+ var fn = gst.font.Tf, ln = (fn === null || fn === void 0 ? void 0 : fn.toLowerCase()) || "";
1226
1226
  var p0 = ln.indexOf("bold") != -1 ? "bold " : "";
1227
1227
  var p1 = ln.indexOf("italic") != -1 || ln.indexOf("oblique") != -1 ? "italic " : "";
1228
1228
  ctx.font = p0 + p1 + gst.font.Tfs + 'px "' + fn + '"';
@@ -249,30 +249,8 @@ function rgbToHex(rgb) {
249
249
  }
250
250
  return rgb;
251
251
  }
252
- function hexToRgb(hex) {
253
- var sColor = hex.toLowerCase();
254
- //十六进制颜色值的正则表达式
255
- var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
256
- // 如果是16进制颜色
257
- if (sColor && reg.test(sColor)) {
258
- if (sColor.length === 4) {
259
- var sColorNew = "#";
260
- for (var i = 1; i < 4; i += 1) {
261
- sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1));
262
- }
263
- sColor = sColorNew;
264
- }
265
- //处理六位的颜色值
266
- var sColorChange = [];
267
- for (var i = 1; i < 7; i += 2) {
268
- sColorChange.push(parseInt("0x" + sColor.slice(i, i + 2)));
269
- }
270
- return "RGB(" + sColorChange.join(",") + ")";
271
- }
272
- return sColor;
273
- }
274
252
  function hexToRgbArray(hex) {
275
- var sColor = hex.toLowerCase();
253
+ var sColor = hex === null || hex === void 0 ? void 0 : hex.toLowerCase();
276
254
  //十六进制颜色值的正则表达式
277
255
  var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
278
256
  // 如果是16进制颜色
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "workspaces": [
4
4
  "src/*"
5
5
  ],
6
- "version": "2.2.4",
6
+ "version": "2.2.6",
7
7
  "description": "An Excel import/export import library for FortuneSheet",
8
8
  "main": "dist/main.js",
9
9
  "types": "dist/main.d.ts",
@@ -49,5 +49,6 @@
49
49
  "prepare": "tsc",
50
50
  "storybook": "storybook dev -p 6006",
51
51
  "build-storybook": "storybook build"
52
- }
52
+ },
53
+ "packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
53
54
  }