excel.file.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * DevExtreme (exporter/excel/excel.file.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. Object.defineProperty(exports, "__esModule", {
  11. value: true
  12. });
  13. exports.default = void 0;
  14. var _type = require("../../core/utils/type");
  15. var _excel = require("./excel.tag_helper");
  16. var _excel2 = _interopRequireDefault(_excel);
  17. var _excel3 = require("./excel.cell_format_helper");
  18. var _excel4 = _interopRequireDefault(_excel3);
  19. var _excel5 = require("./excel.fill_helper");
  20. var _excel6 = _interopRequireDefault(_excel5);
  21. var _excel7 = require("./excel.font_helper");
  22. var _excel8 = _interopRequireDefault(_excel7);
  23. var _excel9 = require("./excel.number_format_helper");
  24. var _excel10 = _interopRequireDefault(_excel9);
  25. function _interopRequireDefault(obj) {
  26. return obj && obj.__esModule ? obj : {
  27. "default": obj
  28. }
  29. }
  30. function _classCallCheck(instance, Constructor) {
  31. if (!(instance instanceof Constructor)) {
  32. throw new TypeError("Cannot call a class as a function")
  33. }
  34. }
  35. function _defineProperties(target, props) {
  36. for (var i = 0; i < props.length; i++) {
  37. var descriptor = props[i];
  38. descriptor.enumerable = descriptor.enumerable || false;
  39. descriptor.configurable = true;
  40. if ("value" in descriptor) {
  41. descriptor.writable = true
  42. }
  43. Object.defineProperty(target, descriptor.key, descriptor)
  44. }
  45. }
  46. function _createClass(Constructor, protoProps, staticProps) {
  47. if (protoProps) {
  48. _defineProperties(Constructor.prototype, protoProps)
  49. }
  50. if (staticProps) {
  51. _defineProperties(Constructor, staticProps)
  52. }
  53. Object.defineProperty(Constructor, "prototype", {
  54. writable: false
  55. });
  56. return Constructor
  57. }
  58. var ExcelFile = function() {
  59. function ExcelFile() {
  60. _classCallCheck(this, ExcelFile);
  61. this._cellFormatTags = [];
  62. this._fillTags = [];
  63. this._fontTags = [];
  64. this._numberFormatTags = [];
  65. this._fillTags.push(_excel6.default.tryCreateTag({
  66. patternFill: {
  67. patternType: "none"
  68. }
  69. }))
  70. }
  71. _createClass(ExcelFile, [{
  72. key: "registerCellFormat",
  73. value: function(cellFormat) {
  74. var result;
  75. var cellFormatTag = _excel4.default.tryCreateTag(cellFormat, {
  76. registerFill: this.registerFill.bind(this),
  77. registerFont: this.registerFont.bind(this),
  78. registerNumberFormat: this.registerNumberFormat.bind(this)
  79. });
  80. if ((0, _type.isDefined)(cellFormatTag)) {
  81. for (var i = 0; i < this._cellFormatTags.length; i++) {
  82. if (_excel4.default.areEqual(this._cellFormatTags[i], cellFormatTag)) {
  83. result = i;
  84. break
  85. }
  86. }
  87. if (void 0 === result) {
  88. result = this._cellFormatTags.push(cellFormatTag) - 1
  89. }
  90. }
  91. return result
  92. }
  93. }, {
  94. key: "generateCellFormatsXml",
  95. value: function() {
  96. var cellFormatTagsAsXmlStringsArray = this._cellFormatTags.map(function(tag) {
  97. return _excel4.default.toXml(tag)
  98. });
  99. return _excel2.default.toXml("cellXfs", {
  100. count: cellFormatTagsAsXmlStringsArray.length
  101. }, cellFormatTagsAsXmlStringsArray.join(""))
  102. }
  103. }, {
  104. key: "registerFill",
  105. value: function(fill) {
  106. var result;
  107. var fillTag = _excel6.default.tryCreateTag(fill);
  108. if ((0, _type.isDefined)(fillTag)) {
  109. for (var i = 0; i < this._fillTags.length; i++) {
  110. if (_excel6.default.areEqual(this._fillTags[i], fillTag)) {
  111. result = i;
  112. break
  113. }
  114. }
  115. if (void 0 === result) {
  116. if (this._fillTags.length < 2) {
  117. this._fillTags.push(_excel6.default.tryCreateTag({
  118. patternFill: {
  119. patternType: "Gray125"
  120. }
  121. }))
  122. }
  123. result = this._fillTags.push(fillTag) - 1
  124. }
  125. }
  126. return result
  127. }
  128. }, {
  129. key: "generateFillsXml",
  130. value: function() {
  131. var tagsAsXmlStringsArray = this._fillTags.map(function(tag) {
  132. return _excel6.default.toXml(tag)
  133. });
  134. return _excel2.default.toXml("fills", {
  135. count: tagsAsXmlStringsArray.length
  136. }, tagsAsXmlStringsArray.join(""))
  137. }
  138. }, {
  139. key: "registerFont",
  140. value: function(font) {
  141. var result;
  142. var fontTag = _excel8.default.tryCreateTag(font);
  143. if ((0, _type.isDefined)(fontTag)) {
  144. for (var i = 0; i < this._fontTags.length; i++) {
  145. if (_excel8.default.areEqual(this._fontTags[i], fontTag)) {
  146. result = i;
  147. break
  148. }
  149. }
  150. if (void 0 === result) {
  151. result = this._fontTags.push(fontTag) - 1
  152. }
  153. }
  154. return result
  155. }
  156. }, {
  157. key: "generateFontsXml",
  158. value: function() {
  159. var xmlStringsArray = this._fontTags.map(function(tag) {
  160. return _excel8.default.toXml(tag)
  161. });
  162. return _excel2.default.toXml("fonts", {
  163. count: xmlStringsArray.length
  164. }, xmlStringsArray.join(""))
  165. }
  166. }, {
  167. key: "_convertNumberFormatIndexToId",
  168. value: function(index) {
  169. var CUSTOM_FORMAT_ID_START_VALUE = 165;
  170. return CUSTOM_FORMAT_ID_START_VALUE + index
  171. }
  172. }, {
  173. key: "registerNumberFormat",
  174. value: function(numberFormat) {
  175. var result;
  176. var tag = _excel10.default.tryCreateTag(numberFormat);
  177. if ((0, _type.isDefined)(tag)) {
  178. for (var i = 0; i < this._numberFormatTags.length; i++) {
  179. if (_excel10.default.areEqual(this._numberFormatTags[i], tag)) {
  180. result = this._numberFormatTags[i][_excel10.default.ID_PROPERTY_NAME];
  181. break
  182. }
  183. }
  184. if (void 0 === result) {
  185. tag[_excel10.default.ID_PROPERTY_NAME] = this._convertNumberFormatIndexToId(this._numberFormatTags.length);
  186. result = tag[_excel10.default.ID_PROPERTY_NAME];
  187. this._numberFormatTags.push(tag)
  188. }
  189. }
  190. return result
  191. }
  192. }, {
  193. key: "generateNumberFormatsXml",
  194. value: function() {
  195. if (this._numberFormatTags.length > 0) {
  196. var xmlStringsArray = this._numberFormatTags.map(function(tag) {
  197. return _excel10.default.toXml(tag)
  198. });
  199. return _excel2.default.toXml("numFmts", {
  200. count: xmlStringsArray.length
  201. }, xmlStringsArray.join(""))
  202. } else {
  203. return ""
  204. }
  205. }
  206. }], [{
  207. key: "copyCellFormat",
  208. value: function(source) {
  209. return _excel4.default.copy(source)
  210. }
  211. }]);
  212. return ExcelFile
  213. }();
  214. exports.default = ExcelFile;
  215. module.exports = ExcelFile;