workbook.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. 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); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. var Worksheet = require('./worksheet');
  6. var DefinedNames = require('./defined-names');
  7. var XLSX = require('../xlsx/xlsx');
  8. var CSV = require('../csv/csv'); // Workbook requirements
  9. // Load and Save from file and stream
  10. // Access/Add/Delete individual worksheets
  11. // Manage String table, Hyperlink table, etc.
  12. // Manage scaffolding for contained objects to write to/read from
  13. var Workbook = /*#__PURE__*/function () {
  14. function Workbook() {
  15. _classCallCheck(this, Workbook);
  16. this.category = '';
  17. this.company = '';
  18. this.created = new Date();
  19. this.description = '';
  20. this.keywords = '';
  21. this.manager = '';
  22. this.modified = this.created;
  23. this.properties = {};
  24. this.calcProperties = {};
  25. this._worksheets = [];
  26. this.subject = '';
  27. this.title = '';
  28. this.views = [];
  29. this.media = [];
  30. this._definedNames = new DefinedNames();
  31. }
  32. _createClass(Workbook, [{
  33. key: "addWorksheet",
  34. value: function addWorksheet(name, options) {
  35. var id = this.nextId;
  36. if (name && name.length > 31) {
  37. // eslint-disable-next-line no-console
  38. console.warn("Worksheet name ".concat(name, " exceeds 31 chars. This will be truncated"));
  39. } // Illegal character in worksheet name: asterisk (*), question mark (?),
  40. // colon (:), forward slash (/ \), or bracket ([])
  41. if (/[*?:/\\[\]]/.test(name)) {
  42. throw new Error("Worksheet name ".concat(name, " cannot include any of the following characters: * ? : \\ / [ ]"));
  43. }
  44. if (/(^')|('$)/.test(name)) {
  45. throw new Error("The first or last character of worksheet name cannot be a single quotation mark: ".concat(name));
  46. }
  47. name = (name || "sheet".concat(id)).substring(0, 31);
  48. if (this._worksheets.find(function (ws) {
  49. return ws && ws.name.toLowerCase() === name.toLowerCase();
  50. })) {
  51. throw new Error("Worksheet name already exists: ".concat(name));
  52. } // if options is a color, call it tabColor (and signal deprecated message)
  53. if (options) {
  54. if (typeof options === 'string') {
  55. // eslint-disable-next-line no-console
  56. console.trace('tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { argb: "rbg value" } }');
  57. options = {
  58. properties: {
  59. tabColor: {
  60. argb: options
  61. }
  62. }
  63. };
  64. } else if (options.argb || options.theme || options.indexed) {
  65. // eslint-disable-next-line no-console
  66. console.trace('tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { ... } }');
  67. options = {
  68. properties: {
  69. tabColor: options
  70. }
  71. };
  72. }
  73. }
  74. var lastOrderNo = this._worksheets.reduce(function (acc, ws) {
  75. return (ws && ws.orderNo) > acc ? ws.orderNo : acc;
  76. }, 0);
  77. var worksheetOptions = Object.assign({}, options, {
  78. id: id,
  79. name: name,
  80. orderNo: lastOrderNo + 1,
  81. workbook: this
  82. });
  83. var worksheet = new Worksheet(worksheetOptions);
  84. this._worksheets[id] = worksheet;
  85. return worksheet;
  86. }
  87. }, {
  88. key: "removeWorksheetEx",
  89. value: function removeWorksheetEx(worksheet) {
  90. delete this._worksheets[worksheet.id];
  91. }
  92. }, {
  93. key: "removeWorksheet",
  94. value: function removeWorksheet(id) {
  95. var worksheet = this.getWorksheet(id);
  96. if (worksheet) {
  97. worksheet.destroy();
  98. }
  99. }
  100. }, {
  101. key: "getWorksheet",
  102. value: function getWorksheet(id) {
  103. if (id === undefined) {
  104. return this._worksheets.find(Boolean);
  105. }
  106. if (typeof id === 'number') {
  107. return this._worksheets[id];
  108. }
  109. if (typeof id === 'string') {
  110. return this._worksheets.find(function (worksheet) {
  111. return worksheet && worksheet.name === id;
  112. });
  113. }
  114. return undefined;
  115. }
  116. }, {
  117. key: "eachSheet",
  118. value: function eachSheet(iteratee) {
  119. this.worksheets.forEach(function (sheet) {
  120. iteratee(sheet, sheet.id);
  121. });
  122. }
  123. }, {
  124. key: "clearThemes",
  125. value: function clearThemes() {
  126. // Note: themes are not an exposed feature, meddle at your peril!
  127. this._themes = undefined;
  128. }
  129. }, {
  130. key: "addImage",
  131. value: function addImage(image) {
  132. // TODO: validation?
  133. var id = this.media.length;
  134. this.media.push(Object.assign({}, image, {
  135. type: 'image'
  136. }));
  137. return id;
  138. }
  139. }, {
  140. key: "getImage",
  141. value: function getImage(id) {
  142. return this.media[id];
  143. }
  144. }, {
  145. key: "xlsx",
  146. get: function get() {
  147. if (!this._xlsx) this._xlsx = new XLSX(this);
  148. return this._xlsx;
  149. }
  150. }, {
  151. key: "csv",
  152. get: function get() {
  153. if (!this._csv) this._csv = new CSV(this);
  154. return this._csv;
  155. }
  156. }, {
  157. key: "nextId",
  158. get: function get() {
  159. // find the next unique spot to add worksheet
  160. for (var i = 1; i < this._worksheets.length; i++) {
  161. if (!this._worksheets[i]) {
  162. return i;
  163. }
  164. }
  165. return this._worksheets.length || 1;
  166. }
  167. }, {
  168. key: "worksheets",
  169. get: function get() {
  170. // return a clone of _worksheets
  171. return this._worksheets.slice(1).sort(function (a, b) {
  172. return a.orderNo - b.orderNo;
  173. }).filter(Boolean);
  174. }
  175. }, {
  176. key: "definedNames",
  177. get: function get() {
  178. return this._definedNames;
  179. }
  180. }, {
  181. key: "model",
  182. get: function get() {
  183. return {
  184. creator: this.creator || 'Unknown',
  185. lastModifiedBy: this.lastModifiedBy || 'Unknown',
  186. lastPrinted: this.lastPrinted,
  187. created: this.created,
  188. modified: this.modified,
  189. properties: this.properties,
  190. worksheets: this.worksheets.map(function (worksheet) {
  191. return worksheet.model;
  192. }),
  193. sheets: this.worksheets.map(function (ws) {
  194. return ws.model;
  195. }).filter(Boolean),
  196. definedNames: this._definedNames.model,
  197. views: this.views,
  198. company: this.company,
  199. manager: this.manager,
  200. title: this.title,
  201. subject: this.subject,
  202. keywords: this.keywords,
  203. category: this.category,
  204. description: this.description,
  205. language: this.language,
  206. revision: this.revision,
  207. contentStatus: this.contentStatus,
  208. themes: this._themes,
  209. media: this.media,
  210. calcProperties: this.calcProperties
  211. };
  212. },
  213. set: function set(value) {
  214. var _this = this;
  215. this.creator = value.creator;
  216. this.lastModifiedBy = value.lastModifiedBy;
  217. this.lastPrinted = value.lastPrinted;
  218. this.created = value.created;
  219. this.modified = value.modified;
  220. this.company = value.company;
  221. this.manager = value.manager;
  222. this.title = value.title;
  223. this.subject = value.subject;
  224. this.keywords = value.keywords;
  225. this.category = value.category;
  226. this.description = value.description;
  227. this.language = value.language;
  228. this.revision = value.revision;
  229. this.contentStatus = value.contentStatus;
  230. this.properties = value.properties;
  231. this.calcProperties = value.calcProperties;
  232. this._worksheets = [];
  233. value.worksheets.forEach(function (worksheetModel) {
  234. var id = worksheetModel.id,
  235. name = worksheetModel.name,
  236. state = worksheetModel.state;
  237. var orderNo = value.sheets && value.sheets.findIndex(function (ws) {
  238. return ws.id === id;
  239. });
  240. var worksheet = _this._worksheets[id] = new Worksheet({
  241. id: id,
  242. name: name,
  243. orderNo: orderNo,
  244. state: state,
  245. workbook: _this
  246. });
  247. worksheet.model = worksheetModel;
  248. });
  249. this._definedNames.model = value.definedNames;
  250. this.views = value.views;
  251. this._themes = value.themes;
  252. this.media = value.media || [];
  253. }
  254. }]);
  255. return Workbook;
  256. }();
  257. module.exports = Workbook;
  258. //# sourceMappingURL=workbook.js.map