amotify-ex-spreadsheet 0.0.4 → 0.0.5
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.js +1 -166
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,166 +1 @@
|
|
|
1
|
-
"
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.default = exports.SpreadSheet = void 0;
|
|
16
|
-
const jsmin_1 = __importDefault(require("@mingoo/jsmin"));
|
|
17
|
-
const CDNReader = (url) => {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
if (!url)
|
|
20
|
-
return resolve(false);
|
|
21
|
-
let tag = document.createElement('script');
|
|
22
|
-
tag.src = url;
|
|
23
|
-
tag.addEventListener('load', () => {
|
|
24
|
-
resolve(true);
|
|
25
|
-
});
|
|
26
|
-
(0, jsmin_1.default)(document.body).append(tag);
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
const D = (wb, fn) => {
|
|
30
|
-
return new D.fn.SpreadSheet(wb, fn);
|
|
31
|
-
};
|
|
32
|
-
{
|
|
33
|
-
D.fn = D.prototype = {};
|
|
34
|
-
jsmin_1.default.extend({
|
|
35
|
-
SpreadSheet: function (workbook, fileName) {
|
|
36
|
-
this.ok = true;
|
|
37
|
-
try {
|
|
38
|
-
this.workbook = workbook;
|
|
39
|
-
this.fileName = fileName;
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
console.log(error);
|
|
43
|
-
this.ok = false;
|
|
44
|
-
}
|
|
45
|
-
return this;
|
|
46
|
-
},
|
|
47
|
-
isValid: function () {
|
|
48
|
-
return this.ok;
|
|
49
|
-
},
|
|
50
|
-
getWorkbook: function () {
|
|
51
|
-
return this.workbook;
|
|
52
|
-
},
|
|
53
|
-
updateFileName: function (fileName) {
|
|
54
|
-
this.fileName = fileName;
|
|
55
|
-
return this;
|
|
56
|
-
},
|
|
57
|
-
updateMerges: function (merges) {
|
|
58
|
-
let json = this.toJson();
|
|
59
|
-
json.sheets.forEach((sheet, index) => {
|
|
60
|
-
let sheetMerges = merges[index];
|
|
61
|
-
const worksheet = this.workbook.getWorksheet(sheet.sheetName);
|
|
62
|
-
for (let [from, to] of sheetMerges) {
|
|
63
|
-
if (from && to)
|
|
64
|
-
worksheet.mergeCells(from + ':' + to);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
},
|
|
68
|
-
updateStyles: function (styles) {
|
|
69
|
-
let json = this.toJson();
|
|
70
|
-
json.sheets.forEach((sheet, index) => {
|
|
71
|
-
let sheetStyles = styles[index];
|
|
72
|
-
const worksheet = this.workbook.getWorksheet(sheet.sheetName);
|
|
73
|
-
for (let style of sheetStyles) {
|
|
74
|
-
let { type, target, values } = style;
|
|
75
|
-
let Targets = jsmin_1.default.flatArray(target);
|
|
76
|
-
for (let [key, value] of Object.entries(values))
|
|
77
|
-
for (let Target of Targets)
|
|
78
|
-
worksheet['get' + type.toCapital()](Target)[key] = value;
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
},
|
|
82
|
-
toJson: function () {
|
|
83
|
-
let json = {
|
|
84
|
-
fileName: this.fileName,
|
|
85
|
-
sheets: []
|
|
86
|
-
};
|
|
87
|
-
this.workbook.eachSheet((sheet, id) => {
|
|
88
|
-
let sheetJson = {
|
|
89
|
-
sheetName: sheet.name,
|
|
90
|
-
rows: []
|
|
91
|
-
};
|
|
92
|
-
sheet.eachRow((row, rowIndex) => {
|
|
93
|
-
let [emp, ...values] = row.values;
|
|
94
|
-
sheetJson.rows.push(values);
|
|
95
|
-
});
|
|
96
|
-
json.sheets.push(sheetJson);
|
|
97
|
-
});
|
|
98
|
-
return json;
|
|
99
|
-
},
|
|
100
|
-
export: function (fileType) {
|
|
101
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
102
|
-
fileType = fileType || 'xlsx';
|
|
103
|
-
const uint8Array = yield this.workbook[fileType].writeBuffer();
|
|
104
|
-
const blob = new Blob([uint8Array], { type: 'application/octet-binary' });
|
|
105
|
-
const dummyAnchor = document.createElement('a');
|
|
106
|
-
dummyAnchor.href = (global.URL || global.webkitURL).createObjectURL(blob);
|
|
107
|
-
dummyAnchor.download = this.fileName + '.' + fileType;
|
|
108
|
-
dummyAnchor.click();
|
|
109
|
-
dummyAnchor.remove();
|
|
110
|
-
});
|
|
111
|
-
},
|
|
112
|
-
}, D);
|
|
113
|
-
D.fn.SpreadSheet.prototype = D.fn;
|
|
114
|
-
D.fn.SpreadSheet.prototype.constructor = D;
|
|
115
|
-
}
|
|
116
|
-
let cdn_excelJS = 'https://cdn.amotify.com/@env/@foreign/exceljs.min.js';
|
|
117
|
-
const SpreadSheet = {
|
|
118
|
-
readFromFile: (file) => __awaiter(void 0, void 0, void 0, function* () {
|
|
119
|
-
if (!global.ExcelJS)
|
|
120
|
-
yield CDNReader(cdn_excelJS);
|
|
121
|
-
if (!global.ExcelJS)
|
|
122
|
-
return;
|
|
123
|
-
let fileType = file.type;
|
|
124
|
-
let fileName = file.name.replace(/\.[^/.]+$/, '');
|
|
125
|
-
if (fileType.match(/csv/)) {
|
|
126
|
-
let csv = yield file.text();
|
|
127
|
-
const rows = csv.split(/[\n\r]+/).map(row => row.split(/,/));
|
|
128
|
-
const workbook = new ExcelJS.Workbook();
|
|
129
|
-
const workSheet = workbook.addWorksheet('Sheet1');
|
|
130
|
-
rows.forEach(row => workSheet.addRow(row));
|
|
131
|
-
return D(workbook, fileName);
|
|
132
|
-
}
|
|
133
|
-
const workbook = new ExcelJS.Workbook();
|
|
134
|
-
const buffer = (yield file.convert('binaryString')).target.result;
|
|
135
|
-
return D(yield workbook.xlsx.load(buffer), fileName);
|
|
136
|
-
}),
|
|
137
|
-
readFromJson: (para) => __awaiter(void 0, void 0, void 0, function* () {
|
|
138
|
-
if (!global.ExcelJS)
|
|
139
|
-
yield CDNReader(cdn_excelJS);
|
|
140
|
-
if (!global.ExcelJS)
|
|
141
|
-
return;
|
|
142
|
-
let { sheets, fileName = '' } = para;
|
|
143
|
-
fileName = fileName || jsmin_1.default.uuid.gen();
|
|
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
|
-
worksheet.addRows(rows);
|
|
151
|
-
for (let [from, to] of merges)
|
|
152
|
-
if (from && to)
|
|
153
|
-
worksheet.mergeCells(from + ':' + to);
|
|
154
|
-
for (let style of styles) {
|
|
155
|
-
let { type, target, values } = style;
|
|
156
|
-
let Targets = jsmin_1.default.flatArray(target);
|
|
157
|
-
for (let [key, value] of Object.entries(values))
|
|
158
|
-
for (let Target of Targets)
|
|
159
|
-
worksheet['get' + type.toCapital()](Target)[key] = value;
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
return D(workbook, fileName);
|
|
163
|
-
})
|
|
164
|
-
};
|
|
165
|
-
exports.SpreadSheet = SpreadSheet;
|
|
166
|
-
exports.default = SpreadSheet;
|
|
1
|
+
var u=(e,r,o)=>new Promise((t,l)=>{var h=n=>{try{s(o.next(n))}catch(i){l(i)}},a=n=>{try{s(o.throw(n))}catch(i){l(i)}},s=n=>n.done?t(n.value):Promise.resolve(n.value).then(h,a);s((o=o.apply(e,r)).next())});import m from"@mingoo/jsmin";const g=e=>new Promise((r,o)=>{if(!e)return r(!1);let t=document.createElement("script");t.src=e,t.addEventListener("load",()=>{r(!0)}),m(document.body).append(t)}),c=(e,r)=>new c.fn.SpreadSheet(e,r);c.fn=c.prototype={},m.extend({SpreadSheet:function(e,r){this.ok=!0;try{this.workbook=e,this.fileName=r}catch(o){console.log(o),this.ok=!1}return this},isValid:function(){return this.ok},getWorkbook:function(){return this.workbook},updateFileName:function(e){return this.fileName=e,this},updateMerges:function(e){this.toJson().sheets.forEach((o,t)=>{let l=e[t];const h=this.workbook.getWorksheet(o.sheetName);for(let[a,s]of l)a&&s&&h.mergeCells(a+":"+s)})},updateStyles:function(e){this.toJson().sheets.forEach((o,t)=>{let l=e[t];const h=this.workbook.getWorksheet(o.sheetName);for(let a of l){let{type:s,target:n,values:i}=a,d=m.flatArray(n);for(let[p,f]of Object.entries(i))for(let y of d)h["get"+s.toCapital()](y)[p]=f}})},toJson:function(){let e={fileName:this.fileName,sheets:[]};return this.workbook.eachSheet((r,o)=>{let t={sheetName:r.name,rows:[]};r.eachRow((l,h)=>{let[a,...s]=l.values;t.rows.push(s)}),e.sheets.push(t)}),e},export:function(e){return u(this,null,function*(){e=e||"xlsx";const r=yield this.workbook[e].writeBuffer(),o=new Blob([r],{type:"application/octet-binary"}),t=document.createElement("a");t.href=(global.URL||global.webkitURL).createObjectURL(o),t.download=this.fileName+"."+e,t.click(),t.remove()})}},c),c.fn.SpreadSheet.prototype=c.fn,c.fn.SpreadSheet.prototype.constructor=c;let S="https://cdn.amotify.com/@env/@foreign/exceljs.min.js";const k={readFromFile:e=>u(void 0,null,function*(){if(global.ExcelJS||(yield g(S)),!global.ExcelJS)return;let r=e.type,o=e.name.replace(/\.[^/.]+$/,"");if(r.match(/csv/)){const a=(yield e.text()).split(/[\n\r]+/).map(i=>i.split(/,/)),s=new ExcelJS.Workbook,n=s.addWorksheet("Sheet1");return a.forEach(i=>n.addRow(i)),c(s,o)}const t=new ExcelJS.Workbook,l=(yield e.convert("binaryString")).target.result;return c(yield t.xlsx.load(l),o)}),readFromJson:e=>u(void 0,null,function*(){if(global.ExcelJS||(yield g(S)),!global.ExcelJS)return;let{sheets:r,fileName:o=""}=e;o=o||m.uuid.gen();const t=new ExcelJS.Workbook;return r.forEach((l,h)=>{let{sheetName:a,rows:s,merges:n=[],styles:i=[]}=l;a=a||"\u30B7\u30FC\u30C8"+(h+1),t.addWorksheet(a);const d=t.getWorksheet(a);d.addRows(s);for(let[p,f]of n)p&&f&&d.mergeCells(p+":"+f);for(let p of i){let{type:f,target:y,values:w}=p,b=m.flatArray(y);for(let[x,v]of Object.entries(w))for(let J of b)d["get"+f.toCapital()](J)[x]=v}}),c(t,o)})};export{k as SpreadSheet,k as default};
|