workbook.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. 'use strict';
  2. const Worksheet = require('./worksheet');
  3. const DefinedNames = require('./defined-names');
  4. const XLSX = require('../xlsx/xlsx');
  5. const CSV = require('../csv/csv');
  6. // Workbook requirements
  7. // Load and Save from file and stream
  8. // Access/Add/Delete individual worksheets
  9. // Manage String table, Hyperlink table, etc.
  10. // Manage scaffolding for contained objects to write to/read from
  11. class Workbook {
  12. constructor() {
  13. this.category = '';
  14. this.company = '';
  15. this.created = new Date();
  16. this.description = '';
  17. this.keywords = '';
  18. this.manager = '';
  19. this.modified = this.created;
  20. this.properties = {};
  21. this.calcProperties = {};
  22. this._worksheets = [];
  23. this.subject = '';
  24. this.title = '';
  25. this.views = [];
  26. this.media = [];
  27. this._definedNames = new DefinedNames();
  28. }
  29. get xlsx() {
  30. if (!this._xlsx) this._xlsx = new XLSX(this);
  31. return this._xlsx;
  32. }
  33. get csv() {
  34. if (!this._csv) this._csv = new CSV(this);
  35. return this._csv;
  36. }
  37. get nextId() {
  38. // find the next unique spot to add worksheet
  39. for (let i = 1; i < this._worksheets.length; i++) {
  40. if (!this._worksheets[i]) {
  41. return i;
  42. }
  43. }
  44. return this._worksheets.length || 1;
  45. }
  46. addWorksheet(name, options) {
  47. const id = this.nextId;
  48. // if options is a color, call it tabColor (and signal deprecated message)
  49. if (options) {
  50. if (typeof options === 'string') {
  51. // eslint-disable-next-line no-console
  52. console.trace('tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { argb: "rbg value" } }');
  53. options = {
  54. properties: {
  55. tabColor: {
  56. argb: options
  57. }
  58. }
  59. };
  60. } else if (options.argb || options.theme || options.indexed) {
  61. // eslint-disable-next-line no-console
  62. console.trace('tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { ... } }');
  63. options = {
  64. properties: {
  65. tabColor: options
  66. }
  67. };
  68. }
  69. }
  70. const lastOrderNo = this._worksheets.reduce((acc, ws) => (ws && ws.orderNo) > acc ? ws.orderNo : acc, 0);
  71. const worksheetOptions = Object.assign({}, options, {
  72. id,
  73. name,
  74. orderNo: lastOrderNo + 1,
  75. workbook: this
  76. });
  77. const worksheet = new Worksheet(worksheetOptions);
  78. this._worksheets[id] = worksheet;
  79. return worksheet;
  80. }
  81. removeWorksheetEx(worksheet) {
  82. delete this._worksheets[worksheet.id];
  83. }
  84. removeWorksheet(id) {
  85. const worksheet = this.getWorksheet(id);
  86. if (worksheet) {
  87. worksheet.destroy();
  88. }
  89. }
  90. getWorksheet(id) {
  91. if (id === undefined) {
  92. return this._worksheets.find(Boolean);
  93. }
  94. if (typeof id === 'number') {
  95. return this._worksheets[id];
  96. }
  97. if (typeof id === 'string') {
  98. return this._worksheets.find(worksheet => worksheet && worksheet.name === id);
  99. }
  100. return undefined;
  101. }
  102. get worksheets() {
  103. // return a clone of _worksheets
  104. return this._worksheets.slice(1).sort((a, b) => a.orderNo - b.orderNo).filter(Boolean);
  105. }
  106. eachSheet(iteratee) {
  107. this.worksheets.forEach(sheet => {
  108. iteratee(sheet, sheet.id);
  109. });
  110. }
  111. get definedNames() {
  112. return this._definedNames;
  113. }
  114. clearThemes() {
  115. // Note: themes are not an exposed feature, meddle at your peril!
  116. this._themes = undefined;
  117. }
  118. addImage(image) {
  119. // TODO: validation?
  120. const id = this.media.length;
  121. this.media.push(Object.assign({}, image, {
  122. type: 'image'
  123. }));
  124. return id;
  125. }
  126. getImage(id) {
  127. return this.media[id];
  128. }
  129. get model() {
  130. return {
  131. creator: this.creator || 'Unknown',
  132. lastModifiedBy: this.lastModifiedBy || 'Unknown',
  133. lastPrinted: this.lastPrinted,
  134. created: this.created,
  135. modified: this.modified,
  136. properties: this.properties,
  137. worksheets: this.worksheets.map(worksheet => worksheet.model),
  138. sheets: this.worksheets.map(ws => ws.model).filter(Boolean),
  139. definedNames: this._definedNames.model,
  140. views: this.views,
  141. company: this.company,
  142. manager: this.manager,
  143. title: this.title,
  144. subject: this.subject,
  145. keywords: this.keywords,
  146. category: this.category,
  147. description: this.description,
  148. language: this.language,
  149. revision: this.revision,
  150. contentStatus: this.contentStatus,
  151. themes: this._themes,
  152. media: this.media,
  153. calcProperties: this.calcProperties
  154. };
  155. }
  156. set model(value) {
  157. this.creator = value.creator;
  158. this.lastModifiedBy = value.lastModifiedBy;
  159. this.lastPrinted = value.lastPrinted;
  160. this.created = value.created;
  161. this.modified = value.modified;
  162. this.company = value.company;
  163. this.manager = value.manager;
  164. this.title = value.title;
  165. this.subject = value.subject;
  166. this.keywords = value.keywords;
  167. this.category = value.category;
  168. this.description = value.description;
  169. this.language = value.language;
  170. this.revision = value.revision;
  171. this.contentStatus = value.contentStatus;
  172. this.properties = value.properties;
  173. this.calcProperties = value.calcProperties;
  174. this._worksheets = [];
  175. value.worksheets.forEach(worksheetModel => {
  176. const {
  177. id,
  178. name,
  179. state
  180. } = worksheetModel;
  181. const orderNo = value.sheets && value.sheets.findIndex(ws => ws.id === id);
  182. const worksheet = this._worksheets[id] = new Worksheet({
  183. id,
  184. name,
  185. orderNo,
  186. state,
  187. workbook: this
  188. });
  189. worksheet.model = worksheetModel;
  190. });
  191. this._definedNames.model = value.definedNames;
  192. this.views = value.views;
  193. this._themes = value.themes;
  194. this.media = value.media || [];
  195. }
  196. }
  197. module.exports = Workbook;
  198. //# sourceMappingURL=workbook.js.map