amotify-ex-spreadsheet 0.0.8 → 1.0.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 +3 -7
- package/dist/index.js +173 -1
- package/esbuild.ts +1 -0
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Input } from 'amotify/@fn';
|
|
2
|
-
|
|
3
|
-
var ExcelJS: any;
|
|
4
|
-
}
|
|
2
|
+
import * as ExcelJS from 'exceljs';
|
|
5
3
|
declare namespace SpreadSheet {
|
|
6
4
|
interface Instance {
|
|
7
5
|
fn: Class;
|
|
@@ -10,7 +8,7 @@ declare namespace SpreadSheet {
|
|
|
10
8
|
}
|
|
11
9
|
interface Class {
|
|
12
10
|
isValid(): boolean;
|
|
13
|
-
getWorkbook():
|
|
11
|
+
getWorkbook(): ExcelJS.Workbook;
|
|
14
12
|
updateFileName(fileName: string): void;
|
|
15
13
|
updateMerges(merges: Merge[][]): void;
|
|
16
14
|
updateStyles(styles: Style[][]): void;
|
|
@@ -38,9 +36,7 @@ declare namespace SpreadSheet {
|
|
|
38
36
|
type Style = {
|
|
39
37
|
type: 'cell' | 'row' | 'column';
|
|
40
38
|
target: string | number | (string | number)[];
|
|
41
|
-
values:
|
|
42
|
-
[key: string]: any;
|
|
43
|
-
};
|
|
39
|
+
values: ExcelJS.Style;
|
|
44
40
|
};
|
|
45
41
|
type Methods = {
|
|
46
42
|
readFromFile(file: Input.Filer.CustomFile): Promise<SpreadSheet.Class>;
|
package/dist/index.js
CHANGED
|
@@ -1 +1,173 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.default = exports.SpreadSheet = void 0;
|
|
36
|
+
const jmini_1 = __importStar(require("jmini"));
|
|
37
|
+
const ExcelJS = __importStar(require("exceljs"));
|
|
38
|
+
const D = (wb, fn) => {
|
|
39
|
+
return new D.fn.SpreadSheet(wb, fn);
|
|
40
|
+
};
|
|
41
|
+
{
|
|
42
|
+
D.fn = D.prototype = {};
|
|
43
|
+
jmini_1.default.extend({
|
|
44
|
+
SpreadSheet: function (workbook, fileName) {
|
|
45
|
+
this.ok = true;
|
|
46
|
+
try {
|
|
47
|
+
this.workbook = workbook;
|
|
48
|
+
this.fileName = fileName;
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.log(error);
|
|
52
|
+
this.ok = false;
|
|
53
|
+
}
|
|
54
|
+
return this;
|
|
55
|
+
},
|
|
56
|
+
isValid: function () {
|
|
57
|
+
return this.ok;
|
|
58
|
+
},
|
|
59
|
+
getWorkbook: function () {
|
|
60
|
+
return this.workbook;
|
|
61
|
+
},
|
|
62
|
+
updateFileName: function (fileName) {
|
|
63
|
+
this.fileName = fileName;
|
|
64
|
+
return this;
|
|
65
|
+
},
|
|
66
|
+
updateMerges: function (merges) {
|
|
67
|
+
let json = this.toJson();
|
|
68
|
+
json.sheets.forEach((sheet, index) => {
|
|
69
|
+
let sheetMerges = merges[index];
|
|
70
|
+
const worksheet = this.workbook.getWorksheet(sheet.sheetName);
|
|
71
|
+
for (let [from, to] of sheetMerges) {
|
|
72
|
+
if (from && to)
|
|
73
|
+
worksheet.mergeCells(from + ':' + to);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
updateStyles: function (styles) {
|
|
78
|
+
let json = this.toJson();
|
|
79
|
+
json.sheets.forEach((sheet, index) => {
|
|
80
|
+
let sheetStyles = styles[index];
|
|
81
|
+
const worksheet = this.workbook.getWorksheet(sheet.sheetName);
|
|
82
|
+
for (let style of sheetStyles) {
|
|
83
|
+
let { type, target, values } = style;
|
|
84
|
+
let Targets = jmini_1.default.flatArray(target);
|
|
85
|
+
for (let [key, value] of Object.entries(values))
|
|
86
|
+
for (let Target of Targets)
|
|
87
|
+
worksheet['get' + type.toCapital()](Target)[key] = value;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
toJson: function () {
|
|
92
|
+
let json = {
|
|
93
|
+
fileName: this.fileName,
|
|
94
|
+
sheets: []
|
|
95
|
+
};
|
|
96
|
+
this.workbook.eachSheet((sheet, id) => {
|
|
97
|
+
let sheetJson = {
|
|
98
|
+
sheetName: sheet.name,
|
|
99
|
+
rows: []
|
|
100
|
+
};
|
|
101
|
+
sheet.eachRow((row, rowIndex) => {
|
|
102
|
+
let [emp, ...values] = row.values;
|
|
103
|
+
sheetJson.rows.push(values);
|
|
104
|
+
});
|
|
105
|
+
json.sheets.push(sheetJson);
|
|
106
|
+
});
|
|
107
|
+
return json;
|
|
108
|
+
},
|
|
109
|
+
export: function (fileType) {
|
|
110
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
fileType = fileType || 'xlsx';
|
|
112
|
+
const uint8Array = yield this.workbook[fileType].writeBuffer();
|
|
113
|
+
const blob = new Blob([uint8Array], { type: 'application/octet-binary' });
|
|
114
|
+
const dummyAnchor = document.createElement('a');
|
|
115
|
+
dummyAnchor.href = (global.URL || global.webkitURL).createObjectURL(blob);
|
|
116
|
+
dummyAnchor.download = this.fileName + '.' + fileType;
|
|
117
|
+
dummyAnchor.click();
|
|
118
|
+
dummyAnchor.remove();
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
}, D);
|
|
122
|
+
D.fn.SpreadSheet.prototype = D.fn;
|
|
123
|
+
D.fn.SpreadSheet.prototype.constructor = D;
|
|
124
|
+
}
|
|
125
|
+
const SpreadSheet = {
|
|
126
|
+
readFromFile: (file) => __awaiter(void 0, void 0, void 0, function* () {
|
|
127
|
+
let fileType = file.type;
|
|
128
|
+
let fileName = file.name.replace(/\.[^/.]+$/, '');
|
|
129
|
+
if (fileType.match(/csv/)) {
|
|
130
|
+
let csv = yield file.text();
|
|
131
|
+
const rows = csv.split(/[\n\r]+/).map(row => row.split(/,/));
|
|
132
|
+
const workbook = new ExcelJS.Workbook();
|
|
133
|
+
const workSheet = workbook.addWorksheet('Sheet1');
|
|
134
|
+
rows.forEach(row => workSheet.addRow(row));
|
|
135
|
+
return D(workbook, fileName);
|
|
136
|
+
}
|
|
137
|
+
const workbook = new ExcelJS.Workbook();
|
|
138
|
+
const buffer = (yield file.convert('binaryString')).target.result;
|
|
139
|
+
return D(yield workbook.xlsx.load(buffer), fileName);
|
|
140
|
+
}),
|
|
141
|
+
readFromJson: (para) => __awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
let { sheets, fileName = '' } = para;
|
|
143
|
+
fileName = fileName || (0, jmini_1.UUID)();
|
|
144
|
+
const workbook = new ExcelJS.Workbook();
|
|
145
|
+
sheets.forEach((sheet, index) => {
|
|
146
|
+
let { sheetName, rows, merges = [], styles = [] } = sheet;
|
|
147
|
+
sheetName = sheetName || 'シート' + (index + 1);
|
|
148
|
+
workbook.addWorksheet(sheetName);
|
|
149
|
+
const worksheet = workbook.getWorksheet(sheetName);
|
|
150
|
+
if (!worksheet)
|
|
151
|
+
return;
|
|
152
|
+
worksheet.addRows(rows);
|
|
153
|
+
for (let [from, to] of merges) {
|
|
154
|
+
if (from && to)
|
|
155
|
+
worksheet.mergeCells(from + ':' + to);
|
|
156
|
+
}
|
|
157
|
+
for (let style of styles) {
|
|
158
|
+
let { type, target, values } = style;
|
|
159
|
+
let Targets = jmini_1.default.flatArray(target);
|
|
160
|
+
for (let [key, value] of Object.entries(values)) {
|
|
161
|
+
for (let t of Targets) {
|
|
162
|
+
let fnName = 'get' + type.toCapital();
|
|
163
|
+
let cell = worksheet[fnName](t);
|
|
164
|
+
cell[key] = value;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
return D(workbook, fileName);
|
|
170
|
+
})
|
|
171
|
+
};
|
|
172
|
+
exports.SpreadSheet = SpreadSheet;
|
|
173
|
+
exports.default = SpreadSheet;
|
package/esbuild.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "amotify-ex-spreadsheet",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "amotify spreadsheet extension",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -26,9 +26,10 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/IkkoKoyama/amotify-ex-spreadsheet#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"amotify": "^0.2.
|
|
29
|
+
"amotify": "^0.2.239",
|
|
30
30
|
"esbuild": "^0.20.2",
|
|
31
|
-
"
|
|
31
|
+
"exceljs": "^4.4.0",
|
|
32
|
+
"jmini": "^0.0.102",
|
|
32
33
|
"prop-types": "^15.8.1",
|
|
33
34
|
"react": "^18.3.1",
|
|
34
35
|
"typescript": "^5.4.5"
|