xlsx.js 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  1. "use strict";
  2. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  3. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  4. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  5. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  6. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  7. function _asyncIterator(iterable) { var method; if (typeof Symbol !== "undefined") { if (Symbol.asyncIterator) { method = iterable[Symbol.asyncIterator]; if (method != null) return method.call(iterable); } if (Symbol.iterator) { method = iterable[Symbol.iterator]; if (method != null) return method.call(iterable); } } throw new TypeError("Object is not async iterable"); }
  8. var fs = require('fs');
  9. var JSZip = require('jszip');
  10. var _require = require('readable-stream'),
  11. PassThrough = _require.PassThrough;
  12. var ZipStream = require('../utils/zip-stream');
  13. var StreamBuf = require('../utils/stream-buf');
  14. var utils = require('../utils/utils');
  15. var XmlStream = require('../utils/xml-stream');
  16. var _require2 = require('../utils/browser-buffer-decode'),
  17. bufferToString = _require2.bufferToString;
  18. var StylesXform = require('./xform/style/styles-xform');
  19. var CoreXform = require('./xform/core/core-xform');
  20. var SharedStringsXform = require('./xform/strings/shared-strings-xform');
  21. var RelationshipsXform = require('./xform/core/relationships-xform');
  22. var ContentTypesXform = require('./xform/core/content-types-xform');
  23. var AppXform = require('./xform/core/app-xform');
  24. var WorkbookXform = require('./xform/book/workbook-xform');
  25. var WorksheetXform = require('./xform/sheet/worksheet-xform');
  26. var DrawingXform = require('./xform/drawing/drawing-xform');
  27. var TableXform = require('./xform/table/table-xform');
  28. var CommentsXform = require('./xform/comment/comments-xform');
  29. var VmlNotesXform = require('./xform/comment/vml-notes-xform');
  30. var theme1Xml = require('./xml/theme1.js');
  31. function fsReadFileAsync(filename, options) {
  32. return new Promise(function (resolve, reject) {
  33. fs.readFile(filename, options, function (error, data) {
  34. if (error) {
  35. reject(error);
  36. } else {
  37. resolve(data);
  38. }
  39. });
  40. });
  41. }
  42. var XLSX = /*#__PURE__*/function () {
  43. function XLSX(workbook) {
  44. _classCallCheck(this, XLSX);
  45. this.workbook = workbook;
  46. } // ===============================================================================
  47. // Workbook
  48. // =========================================================================
  49. // Read
  50. _createClass(XLSX, [{
  51. key: "readFile",
  52. value: function () {
  53. var _readFile = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(filename, options) {
  54. var stream, workbook;
  55. return regeneratorRuntime.wrap(function _callee$(_context) {
  56. while (1) {
  57. switch (_context.prev = _context.next) {
  58. case 0:
  59. _context.next = 2;
  60. return utils.fs.exists(filename);
  61. case 2:
  62. if (_context.sent) {
  63. _context.next = 4;
  64. break;
  65. }
  66. throw new Error("File not found: ".concat(filename));
  67. case 4:
  68. stream = fs.createReadStream(filename);
  69. _context.prev = 5;
  70. _context.next = 8;
  71. return this.read(stream, options);
  72. case 8:
  73. workbook = _context.sent;
  74. stream.close();
  75. return _context.abrupt("return", workbook);
  76. case 13:
  77. _context.prev = 13;
  78. _context.t0 = _context["catch"](5);
  79. stream.close();
  80. throw _context.t0;
  81. case 17:
  82. case "end":
  83. return _context.stop();
  84. }
  85. }
  86. }, _callee, this, [[5, 13]]);
  87. }));
  88. function readFile(_x, _x2) {
  89. return _readFile.apply(this, arguments);
  90. }
  91. return readFile;
  92. }()
  93. }, {
  94. key: "parseRels",
  95. value: function parseRels(stream) {
  96. var xform = new RelationshipsXform();
  97. return xform.parseStream(stream);
  98. }
  99. }, {
  100. key: "parseWorkbook",
  101. value: function parseWorkbook(stream) {
  102. var xform = new WorkbookXform();
  103. return xform.parseStream(stream);
  104. }
  105. }, {
  106. key: "parseSharedStrings",
  107. value: function parseSharedStrings(stream) {
  108. var xform = new SharedStringsXform();
  109. return xform.parseStream(stream);
  110. }
  111. }, {
  112. key: "reconcile",
  113. value: function reconcile(model, options) {
  114. var workbookXform = new WorkbookXform();
  115. var worksheetXform = new WorksheetXform(options);
  116. var drawingXform = new DrawingXform();
  117. var tableXform = new TableXform();
  118. workbookXform.reconcile(model); // reconcile drawings with their rels
  119. var drawingOptions = {
  120. media: model.media,
  121. mediaIndex: model.mediaIndex
  122. };
  123. Object.keys(model.drawings).forEach(function (name) {
  124. var drawing = model.drawings[name];
  125. var drawingRel = model.drawingRels[name];
  126. if (drawingRel) {
  127. drawingOptions.rels = drawingRel.reduce(function (o, rel) {
  128. o[rel.Id] = rel;
  129. return o;
  130. }, {});
  131. (drawing.anchors || []).forEach(function (anchor) {
  132. var hyperlinks = anchor.picture && anchor.picture.hyperlinks;
  133. if (hyperlinks && drawingOptions.rels[hyperlinks.rId]) {
  134. hyperlinks.hyperlink = drawingOptions.rels[hyperlinks.rId].Target;
  135. delete hyperlinks.rId;
  136. }
  137. });
  138. drawingXform.reconcile(drawing, drawingOptions);
  139. }
  140. }); // reconcile tables with the default styles
  141. var tableOptions = {
  142. styles: model.styles
  143. };
  144. Object.values(model.tables).forEach(function (table) {
  145. tableXform.reconcile(table, tableOptions);
  146. });
  147. var sheetOptions = {
  148. styles: model.styles,
  149. sharedStrings: model.sharedStrings,
  150. media: model.media,
  151. mediaIndex: model.mediaIndex,
  152. date1904: model.properties && model.properties.date1904,
  153. drawings: model.drawings,
  154. comments: model.comments,
  155. tables: model.tables,
  156. vmlDrawings: model.vmlDrawings
  157. };
  158. model.worksheets.forEach(function (worksheet) {
  159. worksheet.relationships = model.worksheetRels[worksheet.sheetNo];
  160. worksheetXform.reconcile(worksheet, sheetOptions);
  161. }); // delete unnecessary parts
  162. delete model.worksheetHash;
  163. delete model.worksheetRels;
  164. delete model.globalRels;
  165. delete model.sharedStrings;
  166. delete model.workbookRels;
  167. delete model.sheetDefs;
  168. delete model.styles;
  169. delete model.mediaIndex;
  170. delete model.drawings;
  171. delete model.drawingRels;
  172. delete model.vmlDrawings;
  173. }
  174. }, {
  175. key: "_processWorksheetEntry",
  176. value: function () {
  177. var _processWorksheetEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(stream, model, sheetNo, options, path) {
  178. var xform, worksheet;
  179. return regeneratorRuntime.wrap(function _callee2$(_context2) {
  180. while (1) {
  181. switch (_context2.prev = _context2.next) {
  182. case 0:
  183. xform = new WorksheetXform(options);
  184. _context2.next = 3;
  185. return xform.parseStream(stream);
  186. case 3:
  187. worksheet = _context2.sent;
  188. worksheet.sheetNo = sheetNo;
  189. model.worksheetHash[path] = worksheet;
  190. model.worksheets.push(worksheet);
  191. case 7:
  192. case "end":
  193. return _context2.stop();
  194. }
  195. }
  196. }, _callee2);
  197. }));
  198. function _processWorksheetEntry(_x3, _x4, _x5, _x6, _x7) {
  199. return _processWorksheetEntry2.apply(this, arguments);
  200. }
  201. return _processWorksheetEntry;
  202. }()
  203. }, {
  204. key: "_processCommentEntry",
  205. value: function () {
  206. var _processCommentEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(stream, model, name) {
  207. var xform, comments;
  208. return regeneratorRuntime.wrap(function _callee3$(_context3) {
  209. while (1) {
  210. switch (_context3.prev = _context3.next) {
  211. case 0:
  212. xform = new CommentsXform();
  213. _context3.next = 3;
  214. return xform.parseStream(stream);
  215. case 3:
  216. comments = _context3.sent;
  217. model.comments["../".concat(name, ".xml")] = comments;
  218. case 5:
  219. case "end":
  220. return _context3.stop();
  221. }
  222. }
  223. }, _callee3);
  224. }));
  225. function _processCommentEntry(_x8, _x9, _x10) {
  226. return _processCommentEntry2.apply(this, arguments);
  227. }
  228. return _processCommentEntry;
  229. }()
  230. }, {
  231. key: "_processTableEntry",
  232. value: function () {
  233. var _processTableEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee4(stream, model, name) {
  234. var xform, table;
  235. return regeneratorRuntime.wrap(function _callee4$(_context4) {
  236. while (1) {
  237. switch (_context4.prev = _context4.next) {
  238. case 0:
  239. xform = new TableXform();
  240. _context4.next = 3;
  241. return xform.parseStream(stream);
  242. case 3:
  243. table = _context4.sent;
  244. model.tables["../tables/".concat(name, ".xml")] = table;
  245. case 5:
  246. case "end":
  247. return _context4.stop();
  248. }
  249. }
  250. }, _callee4);
  251. }));
  252. function _processTableEntry(_x11, _x12, _x13) {
  253. return _processTableEntry2.apply(this, arguments);
  254. }
  255. return _processTableEntry;
  256. }()
  257. }, {
  258. key: "_processWorksheetRelsEntry",
  259. value: function () {
  260. var _processWorksheetRelsEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee5(stream, model, sheetNo) {
  261. var xform, relationships;
  262. return regeneratorRuntime.wrap(function _callee5$(_context5) {
  263. while (1) {
  264. switch (_context5.prev = _context5.next) {
  265. case 0:
  266. xform = new RelationshipsXform();
  267. _context5.next = 3;
  268. return xform.parseStream(stream);
  269. case 3:
  270. relationships = _context5.sent;
  271. model.worksheetRels[sheetNo] = relationships;
  272. case 5:
  273. case "end":
  274. return _context5.stop();
  275. }
  276. }
  277. }, _callee5);
  278. }));
  279. function _processWorksheetRelsEntry(_x14, _x15, _x16) {
  280. return _processWorksheetRelsEntry2.apply(this, arguments);
  281. }
  282. return _processWorksheetRelsEntry;
  283. }()
  284. }, {
  285. key: "_processMediaEntry",
  286. value: function () {
  287. var _processMediaEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee6(entry, model, filename) {
  288. var lastDot, extension, name;
  289. return regeneratorRuntime.wrap(function _callee6$(_context6) {
  290. while (1) {
  291. switch (_context6.prev = _context6.next) {
  292. case 0:
  293. lastDot = filename.lastIndexOf('.'); // if we can't determine extension, ignore it
  294. if (!(lastDot >= 1)) {
  295. _context6.next = 6;
  296. break;
  297. }
  298. extension = filename.substr(lastDot + 1);
  299. name = filename.substr(0, lastDot);
  300. _context6.next = 6;
  301. return new Promise(function (resolve, reject) {
  302. var streamBuf = new StreamBuf();
  303. streamBuf.on('finish', function () {
  304. model.mediaIndex[filename] = model.media.length;
  305. model.mediaIndex[name] = model.media.length;
  306. var medium = {
  307. type: 'image',
  308. name: name,
  309. extension: extension,
  310. buffer: streamBuf.toBuffer()
  311. };
  312. model.media.push(medium);
  313. resolve();
  314. });
  315. entry.on('error', function (error) {
  316. reject(error);
  317. });
  318. entry.pipe(streamBuf);
  319. });
  320. case 6:
  321. case "end":
  322. return _context6.stop();
  323. }
  324. }
  325. }, _callee6);
  326. }));
  327. function _processMediaEntry(_x17, _x18, _x19) {
  328. return _processMediaEntry2.apply(this, arguments);
  329. }
  330. return _processMediaEntry;
  331. }()
  332. }, {
  333. key: "_processDrawingEntry",
  334. value: function () {
  335. var _processDrawingEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee7(entry, model, name) {
  336. var xform, drawing;
  337. return regeneratorRuntime.wrap(function _callee7$(_context7) {
  338. while (1) {
  339. switch (_context7.prev = _context7.next) {
  340. case 0:
  341. xform = new DrawingXform();
  342. _context7.next = 3;
  343. return xform.parseStream(entry);
  344. case 3:
  345. drawing = _context7.sent;
  346. model.drawings[name] = drawing;
  347. case 5:
  348. case "end":
  349. return _context7.stop();
  350. }
  351. }
  352. }, _callee7);
  353. }));
  354. function _processDrawingEntry(_x20, _x21, _x22) {
  355. return _processDrawingEntry2.apply(this, arguments);
  356. }
  357. return _processDrawingEntry;
  358. }()
  359. }, {
  360. key: "_processDrawingRelsEntry",
  361. value: function () {
  362. var _processDrawingRelsEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee8(entry, model, name) {
  363. var xform, relationships;
  364. return regeneratorRuntime.wrap(function _callee8$(_context8) {
  365. while (1) {
  366. switch (_context8.prev = _context8.next) {
  367. case 0:
  368. xform = new RelationshipsXform();
  369. _context8.next = 3;
  370. return xform.parseStream(entry);
  371. case 3:
  372. relationships = _context8.sent;
  373. model.drawingRels[name] = relationships;
  374. case 5:
  375. case "end":
  376. return _context8.stop();
  377. }
  378. }
  379. }, _callee8);
  380. }));
  381. function _processDrawingRelsEntry(_x23, _x24, _x25) {
  382. return _processDrawingRelsEntry2.apply(this, arguments);
  383. }
  384. return _processDrawingRelsEntry;
  385. }()
  386. }, {
  387. key: "_processVmlDrawingEntry",
  388. value: function () {
  389. var _processVmlDrawingEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee9(entry, model, name) {
  390. var xform, vmlDrawing;
  391. return regeneratorRuntime.wrap(function _callee9$(_context9) {
  392. while (1) {
  393. switch (_context9.prev = _context9.next) {
  394. case 0:
  395. xform = new VmlNotesXform();
  396. _context9.next = 3;
  397. return xform.parseStream(entry);
  398. case 3:
  399. vmlDrawing = _context9.sent;
  400. model.vmlDrawings["../drawings/".concat(name, ".vml")] = vmlDrawing;
  401. case 5:
  402. case "end":
  403. return _context9.stop();
  404. }
  405. }
  406. }, _callee9);
  407. }));
  408. function _processVmlDrawingEntry(_x26, _x27, _x28) {
  409. return _processVmlDrawingEntry2.apply(this, arguments);
  410. }
  411. return _processVmlDrawingEntry;
  412. }()
  413. }, {
  414. key: "_processThemeEntry",
  415. value: function () {
  416. var _processThemeEntry2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee10(entry, model, name) {
  417. return regeneratorRuntime.wrap(function _callee10$(_context10) {
  418. while (1) {
  419. switch (_context10.prev = _context10.next) {
  420. case 0:
  421. _context10.next = 2;
  422. return new Promise(function (resolve, reject) {
  423. // TODO: stream entry into buffer and store the xml in the model.themes[]
  424. var stream = new StreamBuf();
  425. entry.on('error', reject);
  426. stream.on('error', reject);
  427. stream.on('finish', function () {
  428. model.themes[name] = stream.read().toString();
  429. resolve();
  430. });
  431. entry.pipe(stream);
  432. });
  433. case 2:
  434. case "end":
  435. return _context10.stop();
  436. }
  437. }
  438. }, _callee10);
  439. }));
  440. function _processThemeEntry(_x29, _x30, _x31) {
  441. return _processThemeEntry2.apply(this, arguments);
  442. }
  443. return _processThemeEntry;
  444. }()
  445. /**
  446. * @deprecated since version 4.0. You should use `#read` instead. Please follow upgrade instruction: https://github.com/exceljs/exceljs/blob/master/UPGRADE-4.0.md
  447. */
  448. }, {
  449. key: "createInputStream",
  450. value: function createInputStream() {
  451. throw new Error('`XLSX#createInputStream` is deprecated. You should use `XLSX#read` instead. This method will be removed in version 5.0. Please follow upgrade instruction: https://github.com/exceljs/exceljs/blob/master/UPGRADE-4.0.md');
  452. }
  453. }, {
  454. key: "read",
  455. value: function () {
  456. var _read = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee11(stream, options) {
  457. var chunks, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _value, chunk;
  458. return regeneratorRuntime.wrap(function _callee11$(_context11) {
  459. while (1) {
  460. switch (_context11.prev = _context11.next) {
  461. case 0:
  462. // TODO: Remove once node v8 is deprecated
  463. // Detect and upgrade old streams
  464. if (!stream[Symbol.asyncIterator] && stream.pipe) {
  465. stream = stream.pipe(new PassThrough());
  466. }
  467. chunks = [];
  468. _iteratorNormalCompletion = true;
  469. _didIteratorError = false;
  470. _context11.prev = 4;
  471. _iterator = _asyncIterator(stream);
  472. case 6:
  473. _context11.next = 8;
  474. return _iterator.next();
  475. case 8:
  476. _step = _context11.sent;
  477. _iteratorNormalCompletion = _step.done;
  478. _context11.next = 12;
  479. return _step.value;
  480. case 12:
  481. _value = _context11.sent;
  482. if (_iteratorNormalCompletion) {
  483. _context11.next = 19;
  484. break;
  485. }
  486. chunk = _value;
  487. chunks.push(chunk);
  488. case 16:
  489. _iteratorNormalCompletion = true;
  490. _context11.next = 6;
  491. break;
  492. case 19:
  493. _context11.next = 25;
  494. break;
  495. case 21:
  496. _context11.prev = 21;
  497. _context11.t0 = _context11["catch"](4);
  498. _didIteratorError = true;
  499. _iteratorError = _context11.t0;
  500. case 25:
  501. _context11.prev = 25;
  502. _context11.prev = 26;
  503. if (!(!_iteratorNormalCompletion && _iterator.return != null)) {
  504. _context11.next = 30;
  505. break;
  506. }
  507. _context11.next = 30;
  508. return _iterator.return();
  509. case 30:
  510. _context11.prev = 30;
  511. if (!_didIteratorError) {
  512. _context11.next = 33;
  513. break;
  514. }
  515. throw _iteratorError;
  516. case 33:
  517. return _context11.finish(30);
  518. case 34:
  519. return _context11.finish(25);
  520. case 35:
  521. return _context11.abrupt("return", this.load(Buffer.concat(chunks), options));
  522. case 36:
  523. case "end":
  524. return _context11.stop();
  525. }
  526. }
  527. }, _callee11, this, [[4, 21, 25, 35], [26,, 30, 34]]);
  528. }));
  529. function read(_x32, _x33) {
  530. return _read.apply(this, arguments);
  531. }
  532. return read;
  533. }()
  534. }, {
  535. key: "load",
  536. value: function () {
  537. var _load = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee12(data, options) {
  538. var buffer, model, zip, _i, _Object$values, entry, entryName, stream, content, chunkSize, i, workbook, appXform, appProperties, coreXform, coreProperties, match;
  539. return regeneratorRuntime.wrap(function _callee12$(_context12) {
  540. while (1) {
  541. switch (_context12.prev = _context12.next) {
  542. case 0:
  543. if (options && options.base64) {
  544. buffer = Buffer.from(data.toString(), 'base64');
  545. } else {
  546. buffer = data;
  547. }
  548. model = {
  549. worksheets: [],
  550. worksheetHash: {},
  551. worksheetRels: [],
  552. themes: {},
  553. media: [],
  554. mediaIndex: {},
  555. drawings: {},
  556. drawingRels: {},
  557. comments: {},
  558. tables: {},
  559. vmlDrawings: {}
  560. };
  561. _context12.next = 4;
  562. return JSZip.loadAsync(buffer);
  563. case 4:
  564. zip = _context12.sent;
  565. _i = 0, _Object$values = Object.values(zip.files);
  566. case 6:
  567. if (!(_i < _Object$values.length)) {
  568. _context12.next = 126;
  569. break;
  570. }
  571. entry = _Object$values[_i];
  572. if (entry.dir) {
  573. _context12.next = 123;
  574. break;
  575. }
  576. entryName = entry.name;
  577. if (entryName[0] === '/') {
  578. entryName = entryName.substr(1);
  579. }
  580. stream = void 0;
  581. if (!(entryName.match(/xl\/media\//) || // themes are not parsed as stream
  582. entryName.match(/xl\/theme\/([a-zA-Z0-9]+)[.]xml/))) {
  583. _context12.next = 21;
  584. break;
  585. }
  586. stream = new PassThrough();
  587. _context12.t0 = stream;
  588. _context12.next = 17;
  589. return entry.async('nodebuffer');
  590. case 17:
  591. _context12.t1 = _context12.sent;
  592. _context12.t0.write.call(_context12.t0, _context12.t1);
  593. _context12.next = 36;
  594. break;
  595. case 21:
  596. // use object mode to avoid buffer-string convention
  597. stream = new PassThrough({
  598. writableObjectMode: true,
  599. readableObjectMode: true
  600. });
  601. content = void 0; // https://www.npmjs.com/package/process
  602. if (!process.browser) {
  603. _context12.next = 31;
  604. break;
  605. }
  606. _context12.t2 = bufferToString;
  607. _context12.next = 27;
  608. return entry.async('nodebuffer');
  609. case 27:
  610. _context12.t3 = _context12.sent;
  611. content = (0, _context12.t2)(_context12.t3);
  612. _context12.next = 34;
  613. break;
  614. case 31:
  615. _context12.next = 33;
  616. return entry.async('string');
  617. case 33:
  618. content = _context12.sent;
  619. case 34:
  620. chunkSize = 16 * 1024;
  621. for (i = 0; i < content.length; i += chunkSize) {
  622. stream.write(content.substring(i, i + chunkSize));
  623. }
  624. case 36:
  625. stream.end();
  626. _context12.t4 = entryName;
  627. _context12.next = _context12.t4 === '_rels/.rels' ? 40 : _context12.t4 === 'xl/workbook.xml' ? 44 : _context12.t4 === 'xl/_rels/workbook.xml.rels' ? 53 : _context12.t4 === 'xl/sharedStrings.xml' ? 57 : _context12.t4 === 'xl/styles.xml' ? 61 : _context12.t4 === 'docProps/app.xml' ? 65 : _context12.t4 === 'docProps/core.xml' ? 72 : 78;
  628. break;
  629. case 40:
  630. _context12.next = 42;
  631. return this.parseRels(stream);
  632. case 42:
  633. model.globalRels = _context12.sent;
  634. return _context12.abrupt("break", 123);
  635. case 44:
  636. _context12.next = 46;
  637. return this.parseWorkbook(stream);
  638. case 46:
  639. workbook = _context12.sent;
  640. model.sheets = workbook.sheets;
  641. model.definedNames = workbook.definedNames;
  642. model.views = workbook.views;
  643. model.properties = workbook.properties;
  644. model.calcProperties = workbook.calcProperties;
  645. return _context12.abrupt("break", 123);
  646. case 53:
  647. _context12.next = 55;
  648. return this.parseRels(stream);
  649. case 55:
  650. model.workbookRels = _context12.sent;
  651. return _context12.abrupt("break", 123);
  652. case 57:
  653. model.sharedStrings = new SharedStringsXform();
  654. _context12.next = 60;
  655. return model.sharedStrings.parseStream(stream);
  656. case 60:
  657. return _context12.abrupt("break", 123);
  658. case 61:
  659. model.styles = new StylesXform();
  660. _context12.next = 64;
  661. return model.styles.parseStream(stream);
  662. case 64:
  663. return _context12.abrupt("break", 123);
  664. case 65:
  665. appXform = new AppXform();
  666. _context12.next = 68;
  667. return appXform.parseStream(stream);
  668. case 68:
  669. appProperties = _context12.sent;
  670. model.company = appProperties.company;
  671. model.manager = appProperties.manager;
  672. return _context12.abrupt("break", 123);
  673. case 72:
  674. coreXform = new CoreXform();
  675. _context12.next = 75;
  676. return coreXform.parseStream(stream);
  677. case 75:
  678. coreProperties = _context12.sent;
  679. Object.assign(model, coreProperties);
  680. return _context12.abrupt("break", 123);
  681. case 78:
  682. match = entryName.match(/xl\/worksheets\/sheet(\d+)[.]xml/);
  683. if (!match) {
  684. _context12.next = 83;
  685. break;
  686. }
  687. _context12.next = 82;
  688. return this._processWorksheetEntry(stream, model, match[1], options, entryName);
  689. case 82:
  690. return _context12.abrupt("break", 123);
  691. case 83:
  692. match = entryName.match(/xl\/worksheets\/_rels\/sheet(\d+)[.]xml.rels/);
  693. if (!match) {
  694. _context12.next = 88;
  695. break;
  696. }
  697. _context12.next = 87;
  698. return this._processWorksheetRelsEntry(stream, model, match[1]);
  699. case 87:
  700. return _context12.abrupt("break", 123);
  701. case 88:
  702. match = entryName.match(/xl\/theme\/([a-zA-Z0-9]+)[.]xml/);
  703. if (!match) {
  704. _context12.next = 93;
  705. break;
  706. }
  707. _context12.next = 92;
  708. return this._processThemeEntry(stream, model, match[1]);
  709. case 92:
  710. return _context12.abrupt("break", 123);
  711. case 93:
  712. match = entryName.match(/xl\/media\/([a-zA-Z0-9]+[.][a-zA-Z0-9]{3,4})$/);
  713. if (!match) {
  714. _context12.next = 98;
  715. break;
  716. }
  717. _context12.next = 97;
  718. return this._processMediaEntry(stream, model, match[1]);
  719. case 97:
  720. return _context12.abrupt("break", 123);
  721. case 98:
  722. match = entryName.match(/xl\/drawings\/([a-zA-Z0-9]+)[.]xml/);
  723. if (!match) {
  724. _context12.next = 103;
  725. break;
  726. }
  727. _context12.next = 102;
  728. return this._processDrawingEntry(stream, model, match[1]);
  729. case 102:
  730. return _context12.abrupt("break", 123);
  731. case 103:
  732. match = entryName.match(/xl\/(comments\d+)[.]xml/);
  733. if (!match) {
  734. _context12.next = 108;
  735. break;
  736. }
  737. _context12.next = 107;
  738. return this._processCommentEntry(stream, model, match[1]);
  739. case 107:
  740. return _context12.abrupt("break", 123);
  741. case 108:
  742. match = entryName.match(/xl\/tables\/(table\d+)[.]xml/);
  743. if (!match) {
  744. _context12.next = 113;
  745. break;
  746. }
  747. _context12.next = 112;
  748. return this._processTableEntry(stream, model, match[1]);
  749. case 112:
  750. return _context12.abrupt("break", 123);
  751. case 113:
  752. match = entryName.match(/xl\/drawings\/_rels\/([a-zA-Z0-9]+)[.]xml[.]rels/);
  753. if (!match) {
  754. _context12.next = 118;
  755. break;
  756. }
  757. _context12.next = 117;
  758. return this._processDrawingRelsEntry(stream, model, match[1]);
  759. case 117:
  760. return _context12.abrupt("break", 123);
  761. case 118:
  762. match = entryName.match(/xl\/drawings\/(vmlDrawing\d+)[.]vml/);
  763. if (!match) {
  764. _context12.next = 123;
  765. break;
  766. }
  767. _context12.next = 122;
  768. return this._processVmlDrawingEntry(stream, model, match[1]);
  769. case 122:
  770. return _context12.abrupt("break", 123);
  771. case 123:
  772. _i++;
  773. _context12.next = 6;
  774. break;
  775. case 126:
  776. this.reconcile(model, options); // apply model
  777. this.workbook.model = model;
  778. return _context12.abrupt("return", this.workbook);
  779. case 129:
  780. case "end":
  781. return _context12.stop();
  782. }
  783. }
  784. }, _callee12, this);
  785. }));
  786. function load(_x34, _x35) {
  787. return _load.apply(this, arguments);
  788. }
  789. return load;
  790. }() // =========================================================================
  791. // Write
  792. }, {
  793. key: "addMedia",
  794. value: function () {
  795. var _addMedia = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee14(zip, model) {
  796. return regeneratorRuntime.wrap(function _callee14$(_context14) {
  797. while (1) {
  798. switch (_context14.prev = _context14.next) {
  799. case 0:
  800. _context14.next = 2;
  801. return Promise.all(model.media.map( /*#__PURE__*/function () {
  802. var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee13(medium) {
  803. var filename, data, dataimg64, content;
  804. return regeneratorRuntime.wrap(function _callee13$(_context13) {
  805. while (1) {
  806. switch (_context13.prev = _context13.next) {
  807. case 0:
  808. if (!(medium.type === 'image')) {
  809. _context13.next = 13;
  810. break;
  811. }
  812. filename = "xl/media/".concat(medium.name, ".").concat(medium.extension);
  813. if (!medium.filename) {
  814. _context13.next = 7;
  815. break;
  816. }
  817. _context13.next = 5;
  818. return fsReadFileAsync(medium.filename);
  819. case 5:
  820. data = _context13.sent;
  821. return _context13.abrupt("return", zip.append(data, {
  822. name: filename
  823. }));
  824. case 7:
  825. if (!medium.buffer) {
  826. _context13.next = 9;
  827. break;
  828. }
  829. return _context13.abrupt("return", zip.append(medium.buffer, {
  830. name: filename
  831. }));
  832. case 9:
  833. if (!medium.base64) {
  834. _context13.next = 13;
  835. break;
  836. }
  837. dataimg64 = medium.base64;
  838. content = dataimg64.substring(dataimg64.indexOf(',') + 1);
  839. return _context13.abrupt("return", zip.append(content, {
  840. name: filename,
  841. base64: true
  842. }));
  843. case 13:
  844. throw new Error('Unsupported media');
  845. case 14:
  846. case "end":
  847. return _context13.stop();
  848. }
  849. }
  850. }, _callee13);
  851. }));
  852. return function (_x38) {
  853. return _ref.apply(this, arguments);
  854. };
  855. }()));
  856. case 2:
  857. case "end":
  858. return _context14.stop();
  859. }
  860. }
  861. }, _callee14);
  862. }));
  863. function addMedia(_x36, _x37) {
  864. return _addMedia.apply(this, arguments);
  865. }
  866. return addMedia;
  867. }()
  868. }, {
  869. key: "addDrawings",
  870. value: function addDrawings(zip, model) {
  871. var drawingXform = new DrawingXform();
  872. var relsXform = new RelationshipsXform();
  873. model.worksheets.forEach(function (worksheet) {
  874. var drawing = worksheet.drawing;
  875. if (drawing) {
  876. drawingXform.prepare(drawing, {});
  877. var xml = drawingXform.toXml(drawing);
  878. zip.append(xml, {
  879. name: "xl/drawings/".concat(drawing.name, ".xml")
  880. });
  881. xml = relsXform.toXml(drawing.rels);
  882. zip.append(xml, {
  883. name: "xl/drawings/_rels/".concat(drawing.name, ".xml.rels")
  884. });
  885. }
  886. });
  887. }
  888. }, {
  889. key: "addTables",
  890. value: function addTables(zip, model) {
  891. var tableXform = new TableXform();
  892. model.worksheets.forEach(function (worksheet) {
  893. var tables = worksheet.tables;
  894. tables.forEach(function (table) {
  895. tableXform.prepare(table, {});
  896. var tableXml = tableXform.toXml(table);
  897. zip.append(tableXml, {
  898. name: "xl/tables/".concat(table.target)
  899. });
  900. });
  901. });
  902. }
  903. }, {
  904. key: "addContentTypes",
  905. value: function () {
  906. var _addContentTypes = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee15(zip, model) {
  907. var xform, xml;
  908. return regeneratorRuntime.wrap(function _callee15$(_context15) {
  909. while (1) {
  910. switch (_context15.prev = _context15.next) {
  911. case 0:
  912. xform = new ContentTypesXform();
  913. xml = xform.toXml(model);
  914. zip.append(xml, {
  915. name: '[Content_Types].xml'
  916. });
  917. case 3:
  918. case "end":
  919. return _context15.stop();
  920. }
  921. }
  922. }, _callee15);
  923. }));
  924. function addContentTypes(_x39, _x40) {
  925. return _addContentTypes.apply(this, arguments);
  926. }
  927. return addContentTypes;
  928. }()
  929. }, {
  930. key: "addApp",
  931. value: function () {
  932. var _addApp = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee16(zip, model) {
  933. var xform, xml;
  934. return regeneratorRuntime.wrap(function _callee16$(_context16) {
  935. while (1) {
  936. switch (_context16.prev = _context16.next) {
  937. case 0:
  938. xform = new AppXform();
  939. xml = xform.toXml(model);
  940. zip.append(xml, {
  941. name: 'docProps/app.xml'
  942. });
  943. case 3:
  944. case "end":
  945. return _context16.stop();
  946. }
  947. }
  948. }, _callee16);
  949. }));
  950. function addApp(_x41, _x42) {
  951. return _addApp.apply(this, arguments);
  952. }
  953. return addApp;
  954. }()
  955. }, {
  956. key: "addCore",
  957. value: function () {
  958. var _addCore = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee17(zip, model) {
  959. var coreXform;
  960. return regeneratorRuntime.wrap(function _callee17$(_context17) {
  961. while (1) {
  962. switch (_context17.prev = _context17.next) {
  963. case 0:
  964. coreXform = new CoreXform();
  965. zip.append(coreXform.toXml(model), {
  966. name: 'docProps/core.xml'
  967. });
  968. case 2:
  969. case "end":
  970. return _context17.stop();
  971. }
  972. }
  973. }, _callee17);
  974. }));
  975. function addCore(_x43, _x44) {
  976. return _addCore.apply(this, arguments);
  977. }
  978. return addCore;
  979. }()
  980. }, {
  981. key: "addThemes",
  982. value: function () {
  983. var _addThemes = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee18(zip, model) {
  984. var themes;
  985. return regeneratorRuntime.wrap(function _callee18$(_context18) {
  986. while (1) {
  987. switch (_context18.prev = _context18.next) {
  988. case 0:
  989. themes = model.themes || {
  990. theme1: theme1Xml
  991. };
  992. Object.keys(themes).forEach(function (name) {
  993. var xml = themes[name];
  994. var path = "xl/theme/".concat(name, ".xml");
  995. zip.append(xml, {
  996. name: path
  997. });
  998. });
  999. case 2:
  1000. case "end":
  1001. return _context18.stop();
  1002. }
  1003. }
  1004. }, _callee18);
  1005. }));
  1006. function addThemes(_x45, _x46) {
  1007. return _addThemes.apply(this, arguments);
  1008. }
  1009. return addThemes;
  1010. }()
  1011. }, {
  1012. key: "addOfficeRels",
  1013. value: function () {
  1014. var _addOfficeRels = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee19(zip) {
  1015. var xform, xml;
  1016. return regeneratorRuntime.wrap(function _callee19$(_context19) {
  1017. while (1) {
  1018. switch (_context19.prev = _context19.next) {
  1019. case 0:
  1020. xform = new RelationshipsXform();
  1021. xml = xform.toXml([{
  1022. Id: 'rId1',
  1023. Type: XLSX.RelType.OfficeDocument,
  1024. Target: 'xl/workbook.xml'
  1025. }, {
  1026. Id: 'rId2',
  1027. Type: XLSX.RelType.CoreProperties,
  1028. Target: 'docProps/core.xml'
  1029. }, {
  1030. Id: 'rId3',
  1031. Type: XLSX.RelType.ExtenderProperties,
  1032. Target: 'docProps/app.xml'
  1033. }]);
  1034. zip.append(xml, {
  1035. name: '_rels/.rels'
  1036. });
  1037. case 3:
  1038. case "end":
  1039. return _context19.stop();
  1040. }
  1041. }
  1042. }, _callee19);
  1043. }));
  1044. function addOfficeRels(_x47) {
  1045. return _addOfficeRels.apply(this, arguments);
  1046. }
  1047. return addOfficeRels;
  1048. }()
  1049. }, {
  1050. key: "addWorkbookRels",
  1051. value: function () {
  1052. var _addWorkbookRels = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee20(zip, model) {
  1053. var count, relationships, xform, xml;
  1054. return regeneratorRuntime.wrap(function _callee20$(_context20) {
  1055. while (1) {
  1056. switch (_context20.prev = _context20.next) {
  1057. case 0:
  1058. count = 1;
  1059. relationships = [{
  1060. Id: "rId".concat(count++),
  1061. Type: XLSX.RelType.Styles,
  1062. Target: 'styles.xml'
  1063. }, {
  1064. Id: "rId".concat(count++),
  1065. Type: XLSX.RelType.Theme,
  1066. Target: 'theme/theme1.xml'
  1067. }];
  1068. if (model.sharedStrings.count) {
  1069. relationships.push({
  1070. Id: "rId".concat(count++),
  1071. Type: XLSX.RelType.SharedStrings,
  1072. Target: 'sharedStrings.xml'
  1073. });
  1074. }
  1075. model.worksheets.forEach(function (worksheet) {
  1076. worksheet.rId = "rId".concat(count++);
  1077. relationships.push({
  1078. Id: worksheet.rId,
  1079. Type: XLSX.RelType.Worksheet,
  1080. Target: "worksheets/sheet".concat(worksheet.id, ".xml")
  1081. });
  1082. });
  1083. xform = new RelationshipsXform();
  1084. xml = xform.toXml(relationships);
  1085. zip.append(xml, {
  1086. name: 'xl/_rels/workbook.xml.rels'
  1087. });
  1088. case 7:
  1089. case "end":
  1090. return _context20.stop();
  1091. }
  1092. }
  1093. }, _callee20);
  1094. }));
  1095. function addWorkbookRels(_x48, _x49) {
  1096. return _addWorkbookRels.apply(this, arguments);
  1097. }
  1098. return addWorkbookRels;
  1099. }()
  1100. }, {
  1101. key: "addSharedStrings",
  1102. value: function () {
  1103. var _addSharedStrings = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee21(zip, model) {
  1104. return regeneratorRuntime.wrap(function _callee21$(_context21) {
  1105. while (1) {
  1106. switch (_context21.prev = _context21.next) {
  1107. case 0:
  1108. if (model.sharedStrings && model.sharedStrings.count) {
  1109. zip.append(model.sharedStrings.xml, {
  1110. name: 'xl/sharedStrings.xml'
  1111. });
  1112. }
  1113. case 1:
  1114. case "end":
  1115. return _context21.stop();
  1116. }
  1117. }
  1118. }, _callee21);
  1119. }));
  1120. function addSharedStrings(_x50, _x51) {
  1121. return _addSharedStrings.apply(this, arguments);
  1122. }
  1123. return addSharedStrings;
  1124. }()
  1125. }, {
  1126. key: "addStyles",
  1127. value: function () {
  1128. var _addStyles = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee22(zip, model) {
  1129. var xml;
  1130. return regeneratorRuntime.wrap(function _callee22$(_context22) {
  1131. while (1) {
  1132. switch (_context22.prev = _context22.next) {
  1133. case 0:
  1134. xml = model.styles.xml;
  1135. if (xml) {
  1136. zip.append(xml, {
  1137. name: 'xl/styles.xml'
  1138. });
  1139. }
  1140. case 2:
  1141. case "end":
  1142. return _context22.stop();
  1143. }
  1144. }
  1145. }, _callee22);
  1146. }));
  1147. function addStyles(_x52, _x53) {
  1148. return _addStyles.apply(this, arguments);
  1149. }
  1150. return addStyles;
  1151. }()
  1152. }, {
  1153. key: "addWorkbook",
  1154. value: function () {
  1155. var _addWorkbook = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee23(zip, model) {
  1156. var xform;
  1157. return regeneratorRuntime.wrap(function _callee23$(_context23) {
  1158. while (1) {
  1159. switch (_context23.prev = _context23.next) {
  1160. case 0:
  1161. xform = new WorkbookXform();
  1162. zip.append(xform.toXml(model), {
  1163. name: 'xl/workbook.xml'
  1164. });
  1165. case 2:
  1166. case "end":
  1167. return _context23.stop();
  1168. }
  1169. }
  1170. }, _callee23);
  1171. }));
  1172. function addWorkbook(_x54, _x55) {
  1173. return _addWorkbook.apply(this, arguments);
  1174. }
  1175. return addWorkbook;
  1176. }()
  1177. }, {
  1178. key: "addWorksheets",
  1179. value: function () {
  1180. var _addWorksheets = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee24(zip, model) {
  1181. var worksheetXform, relationshipsXform, commentsXform, vmlNotesXform;
  1182. return regeneratorRuntime.wrap(function _callee24$(_context24) {
  1183. while (1) {
  1184. switch (_context24.prev = _context24.next) {
  1185. case 0:
  1186. // preparation phase
  1187. worksheetXform = new WorksheetXform();
  1188. relationshipsXform = new RelationshipsXform();
  1189. commentsXform = new CommentsXform();
  1190. vmlNotesXform = new VmlNotesXform(); // write sheets
  1191. model.worksheets.forEach(function (worksheet) {
  1192. var xmlStream = new XmlStream();
  1193. worksheetXform.render(xmlStream, worksheet);
  1194. zip.append(xmlStream.xml, {
  1195. name: "xl/worksheets/sheet".concat(worksheet.id, ".xml")
  1196. });
  1197. if (worksheet.rels && worksheet.rels.length) {
  1198. xmlStream = new XmlStream();
  1199. relationshipsXform.render(xmlStream, worksheet.rels);
  1200. zip.append(xmlStream.xml, {
  1201. name: "xl/worksheets/_rels/sheet".concat(worksheet.id, ".xml.rels")
  1202. });
  1203. }
  1204. if (worksheet.comments.length > 0) {
  1205. xmlStream = new XmlStream();
  1206. commentsXform.render(xmlStream, worksheet);
  1207. zip.append(xmlStream.xml, {
  1208. name: "xl/comments".concat(worksheet.id, ".xml")
  1209. });
  1210. xmlStream = new XmlStream();
  1211. vmlNotesXform.render(xmlStream, worksheet);
  1212. zip.append(xmlStream.xml, {
  1213. name: "xl/drawings/vmlDrawing".concat(worksheet.id, ".vml")
  1214. });
  1215. }
  1216. });
  1217. case 5:
  1218. case "end":
  1219. return _context24.stop();
  1220. }
  1221. }
  1222. }, _callee24);
  1223. }));
  1224. function addWorksheets(_x56, _x57) {
  1225. return _addWorksheets.apply(this, arguments);
  1226. }
  1227. return addWorksheets;
  1228. }()
  1229. }, {
  1230. key: "_finalize",
  1231. value: function _finalize(zip) {
  1232. var _this = this;
  1233. return new Promise(function (resolve, reject) {
  1234. zip.on('finish', function () {
  1235. resolve(_this);
  1236. });
  1237. zip.on('error', reject);
  1238. zip.finalize();
  1239. });
  1240. }
  1241. }, {
  1242. key: "prepareModel",
  1243. value: function prepareModel(model, options) {
  1244. // ensure following properties have sane values
  1245. model.creator = model.creator || 'ExcelJS';
  1246. model.lastModifiedBy = model.lastModifiedBy || 'ExcelJS';
  1247. model.created = model.created || new Date();
  1248. model.modified = model.modified || new Date();
  1249. model.useSharedStrings = options.useSharedStrings !== undefined ? options.useSharedStrings : true;
  1250. model.useStyles = options.useStyles !== undefined ? options.useStyles : true; // Manage the shared strings
  1251. model.sharedStrings = new SharedStringsXform(); // add a style manager to handle cell formats, fonts, etc.
  1252. model.styles = model.useStyles ? new StylesXform(true) : new StylesXform.Mock(); // prepare all of the things before the render
  1253. var workbookXform = new WorkbookXform();
  1254. var worksheetXform = new WorksheetXform();
  1255. workbookXform.prepare(model);
  1256. var worksheetOptions = {
  1257. sharedStrings: model.sharedStrings,
  1258. styles: model.styles,
  1259. date1904: model.properties.date1904,
  1260. drawingsCount: 0,
  1261. media: model.media
  1262. };
  1263. worksheetOptions.drawings = model.drawings = [];
  1264. worksheetOptions.commentRefs = model.commentRefs = [];
  1265. var tableCount = 0;
  1266. model.tables = [];
  1267. model.worksheets.forEach(function (worksheet) {
  1268. // assign unique filenames to tables
  1269. worksheet.tables.forEach(function (table) {
  1270. tableCount++;
  1271. table.target = "table".concat(tableCount, ".xml");
  1272. table.id = tableCount;
  1273. model.tables.push(table);
  1274. });
  1275. worksheetXform.prepare(worksheet, worksheetOptions);
  1276. }); // TODO: workbook drawing list
  1277. }
  1278. }, {
  1279. key: "write",
  1280. value: function () {
  1281. var _write = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee25(stream, options) {
  1282. var model, zip;
  1283. return regeneratorRuntime.wrap(function _callee25$(_context25) {
  1284. while (1) {
  1285. switch (_context25.prev = _context25.next) {
  1286. case 0:
  1287. options = options || {};
  1288. model = this.workbook.model;
  1289. zip = new ZipStream.ZipWriter(options.zip);
  1290. zip.pipe(stream);
  1291. this.prepareModel(model, options); // render
  1292. _context25.next = 7;
  1293. return this.addContentTypes(zip, model);
  1294. case 7:
  1295. _context25.next = 9;
  1296. return this.addOfficeRels(zip, model);
  1297. case 9:
  1298. _context25.next = 11;
  1299. return this.addWorkbookRels(zip, model);
  1300. case 11:
  1301. _context25.next = 13;
  1302. return this.addWorksheets(zip, model);
  1303. case 13:
  1304. _context25.next = 15;
  1305. return this.addSharedStrings(zip, model);
  1306. case 15:
  1307. _context25.next = 17;
  1308. return this.addDrawings(zip, model);
  1309. case 17:
  1310. _context25.next = 19;
  1311. return this.addTables(zip, model);
  1312. case 19:
  1313. _context25.next = 21;
  1314. return Promise.all([this.addThemes(zip, model), this.addStyles(zip, model)]);
  1315. case 21:
  1316. _context25.next = 23;
  1317. return this.addMedia(zip, model);
  1318. case 23:
  1319. _context25.next = 25;
  1320. return Promise.all([this.addApp(zip, model), this.addCore(zip, model)]);
  1321. case 25:
  1322. _context25.next = 27;
  1323. return this.addWorkbook(zip, model);
  1324. case 27:
  1325. return _context25.abrupt("return", this._finalize(zip));
  1326. case 28:
  1327. case "end":
  1328. return _context25.stop();
  1329. }
  1330. }
  1331. }, _callee25, this);
  1332. }));
  1333. function write(_x58, _x59) {
  1334. return _write.apply(this, arguments);
  1335. }
  1336. return write;
  1337. }()
  1338. }, {
  1339. key: "writeFile",
  1340. value: function writeFile(filename, options) {
  1341. var _this2 = this;
  1342. var stream = fs.createWriteStream(filename);
  1343. return new Promise(function (resolve, reject) {
  1344. stream.on('finish', function () {
  1345. resolve();
  1346. });
  1347. stream.on('error', function (error) {
  1348. reject(error);
  1349. });
  1350. _this2.write(stream, options).then(function () {
  1351. stream.end();
  1352. });
  1353. });
  1354. }
  1355. }, {
  1356. key: "writeBuffer",
  1357. value: function () {
  1358. var _writeBuffer = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee26(options) {
  1359. var stream;
  1360. return regeneratorRuntime.wrap(function _callee26$(_context26) {
  1361. while (1) {
  1362. switch (_context26.prev = _context26.next) {
  1363. case 0:
  1364. stream = new StreamBuf();
  1365. _context26.next = 3;
  1366. return this.write(stream, options);
  1367. case 3:
  1368. return _context26.abrupt("return", stream.read());
  1369. case 4:
  1370. case "end":
  1371. return _context26.stop();
  1372. }
  1373. }
  1374. }, _callee26, this);
  1375. }));
  1376. function writeBuffer(_x60) {
  1377. return _writeBuffer.apply(this, arguments);
  1378. }
  1379. return writeBuffer;
  1380. }()
  1381. }]);
  1382. return XLSX;
  1383. }();
  1384. XLSX.RelType = require('./rel-type');
  1385. module.exports = XLSX;
  1386. //# sourceMappingURL=xlsx.js.map