workbook.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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(
  53. 'tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { argb: "rbg value" } }'
  54. );
  55. options = {
  56. properties: {
  57. tabColor: {argb: options},
  58. },
  59. };
  60. } else if (options.argb || options.theme || options.indexed) {
  61. // eslint-disable-next-line no-console
  62. console.trace(
  63. 'tabColor argument is now deprecated. Please use workbook.addWorksheet(name, {properties: { tabColor: { ... } }'
  64. );
  65. options = {
  66. properties: {
  67. tabColor: options,
  68. },
  69. };
  70. }
  71. }
  72. const lastOrderNo = this._worksheets.reduce((acc, ws) => ((ws && ws.orderNo) > acc ? ws.orderNo : acc), 0);
  73. const worksheetOptions = Object.assign({}, options, {
  74. id,
  75. name,
  76. orderNo: lastOrderNo + 1,
  77. workbook: this,
  78. });
  79. const worksheet = new Worksheet(worksheetOptions);
  80. this._worksheets[id] = worksheet;
  81. return worksheet;
  82. }
  83. removeWorksheetEx(worksheet) {
  84. delete this._worksheets[worksheet.id];
  85. }
  86. removeWorksheet(id) {
  87. const worksheet = this.getWorksheet(id);
  88. if (worksheet) {
  89. worksheet.destroy();
  90. }
  91. }
  92. getWorksheet(id) {
  93. if (id === undefined) {
  94. return this._worksheets.find(Boolean);
  95. }
  96. if (typeof id === 'number') {
  97. return this._worksheets[id];
  98. }
  99. if (typeof id === 'string') {
  100. return this._worksheets.find(worksheet => worksheet && worksheet.name === id);
  101. }
  102. return undefined;
  103. }
  104. get worksheets() {
  105. // return a clone of _worksheets
  106. return this._worksheets
  107. .slice(1)
  108. .sort((a, b) => a.orderNo - b.orderNo)
  109. .filter(Boolean);
  110. }
  111. eachSheet(iteratee) {
  112. this.worksheets.forEach(sheet => {
  113. iteratee(sheet, sheet.id);
  114. });
  115. }
  116. get definedNames() {
  117. return this._definedNames;
  118. }
  119. clearThemes() {
  120. // Note: themes are not an exposed feature, meddle at your peril!
  121. this._themes = undefined;
  122. }
  123. addImage(image) {
  124. // TODO: validation?
  125. const id = this.media.length;
  126. this.media.push(Object.assign({}, image, {type: 'image'}));
  127. return id;
  128. }
  129. getImage(id) {
  130. return this.media[id];
  131. }
  132. get model() {
  133. return {
  134. creator: this.creator || 'Unknown',
  135. lastModifiedBy: this.lastModifiedBy || 'Unknown',
  136. lastPrinted: this.lastPrinted,
  137. created: this.created,
  138. modified: this.modified,
  139. properties: this.properties,
  140. worksheets: this.worksheets.map(worksheet => worksheet.model),
  141. sheets: this.worksheets.map(ws => ws.model).filter(Boolean),
  142. definedNames: this._definedNames.model,
  143. views: this.views,
  144. company: this.company,
  145. manager: this.manager,
  146. title: this.title,
  147. subject: this.subject,
  148. keywords: this.keywords,
  149. category: this.category,
  150. description: this.description,
  151. language: this.language,
  152. revision: this.revision,
  153. contentStatus: this.contentStatus,
  154. themes: this._themes,
  155. media: this.media,
  156. calcProperties: this.calcProperties,
  157. };
  158. }
  159. set model(value) {
  160. this.creator = value.creator;
  161. this.lastModifiedBy = value.lastModifiedBy;
  162. this.lastPrinted = value.lastPrinted;
  163. this.created = value.created;
  164. this.modified = value.modified;
  165. this.company = value.company;
  166. this.manager = value.manager;
  167. this.title = value.title;
  168. this.subject = value.subject;
  169. this.keywords = value.keywords;
  170. this.category = value.category;
  171. this.description = value.description;
  172. this.language = value.language;
  173. this.revision = value.revision;
  174. this.contentStatus = value.contentStatus;
  175. this.properties = value.properties;
  176. this.calcProperties = value.calcProperties;
  177. this._worksheets = [];
  178. value.worksheets.forEach(worksheetModel => {
  179. const {id, name, state} = worksheetModel;
  180. const orderNo = value.sheets && value.sheets.findIndex(ws => ws.id === id);
  181. const worksheet = (this._worksheets[id] = new Worksheet({
  182. id,
  183. name,
  184. orderNo,
  185. state,
  186. workbook: this,
  187. }));
  188. worksheet.model = worksheetModel;
  189. });
  190. this._definedNames.model = value.definedNames;
  191. this.views = value.views;
  192. this._themes = value.themes;
  193. this.media = value.media || [];
  194. }
  195. }
  196. module.exports = Workbook;