UNPKG

xlsx-datafill

Version:

Scalable, template based data population for Excel XLSX spreadsheets.

3 lines (2 loc) 12.9 kB
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).XlsxDataFill=e()}}((function(){return function e(t,r,n){function l(o,i){if(!r[o]){if(!t[o]){var s="function"==typeof require&&require;if(!i&&s)return s(o,!0);if(a)return a(o,!0);var c=new Error("Cannot find module '"+o+"'");throw c.code="MODULE_NOT_FOUND",c}var u=r[o]={exports:{}};t[o][0].call(u.exports,(function(e){return l(t[o][1][e]||e)}),u,u.exports,e,t,r,n)}return r[o].exports}for(var a="function"==typeof require&&require,o=0;o<n.length;o++)l(n[o]);return l}({1:[function(e,t,r){(function(r){"use strict";function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function l(e){return function(e){if(Array.isArray(e))return a(e)}(e)||function(e){if("undefined"!=typeof Symbol&&Symbol.iterator in Object(e))return Array.from(e)}(e)||function(e,t){if(!e)return;if("string"==typeof e)return a(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(r);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return a(e,t)}(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function a(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function o(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}var i="undefined"!=typeof window?window._:void 0!==r?r._:null,s={templateRegExp:/\{\{([^}]*)\}\}/,fieldSplitter:"|",joinText:",",mergeCells:!0,duplicateCells:!1,followFormulae:!1,copyStyle:!0,callbacksMap:{"":function(e){return i.keys(e)},$:function(e){return i.values(e)}}},c=/('?([^!]*)?'?!)?([A-Z]+\d+)(:([A-Z]+\d+))?/,u=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._opts=i.defaultsDeep({},r,s),this._rowSizes={},this._colSizes={},this._access=t}var t,r,a;return t=e,(r=[{key:"options",value:function(e){return null!==e?(i.merge(this._opts,e),this):this._opts}},{key:"fillData",value:function(e){var t=this,r={};return this.collectTemplates((function(e){var n={template:e,dependents:[],formulas:[],processed:!1};if(e.reference){var l=r[e.reference];if(!l)throw new Error("Unable to find a reference '".concat(e.reference,"'!"));e.formula?l.formulas.push(n):l.dependents.push(n),n.offset=t._access.cellDistance(l.template.cell,e.cell)}r[e.id]=n})),i.each(r,(function(r){if(!r.processed){if(r.template.formula)throw new Error("Non-referencing formula found '".concat(r.extractor,"'. Use a non-templated one!"));t.applyFill(r,e,r.template.cell)}})),this}},{key:"getHandler",value:function(e){var t=this._opts.callbacksMap[e];if(t){if("function"!=typeof t)throw new Error("Handler '".concat(e,"' is not a function!"));return t}throw new Error("Handler '".concat(e,"' cannot be found!"))}},{key:"parseExtractor",value:function(e){var t=e.split(":"),r=i.trim(t[1]);return 1==t.length?{path:e,handler:null}:{path:i.trim(t[0]),handler:this.getHandler(r)}}},{key:"applyDataStyle",value:function(e,t,r){var n=this,l=r.styles;return this._opts.copyStyle&&this._access.copyStyle(e,r.cell),l&&t&&i.each(l,(function(r){if(i.startsWith(r.name,":"))n.getHandler(r.name.substr(1)).call(n._opts,t,e);else if(!i.startsWith(r.name,"!")){var l=n.extractValues(t,r.extractor,e);l&&n._access.setCellStyle(e,r.name,JSON.parse(l))}})),this}},{key:"getTemplateOpts",value:function(e){if(!e.styles)return this._opts;var t=i.clone(this._opts);return i.each(e.styles,(function(e){i.startsWith(e.name,"!")&&(t[e.name.substr(1)]=JSON.parse(e.extractor))})),t}},{key:"parseTemplate",value:function(e){var t=this._access.cellValue(e);if(null==t||"string"!=typeof t)return null;var r=t.match(this._opts.templateRegExp);if(!r||!this._opts.followFormulae&&"formula"===this._access.cellType(e))return null;var n=r[1].split(this._opts.fieldSplitter).map(i.trim),l=n[4]?n[4].split(","):null,a=n[2]||"",o=this._access.buildRef(e,n[0]);if(n.length<2)throw new Error("Not enough components of the template '".concat(r[0],"'"));if(n[0]&&!o)throw new Error("Invalid reference passed: '".concat(n[0],"'"));return{id:this._access.cellRef(e),reference:o,iterators:n[1].split(/x|\*/).map(i.trim),extractor:a,formula:a.startsWith("="),cell:e,cellSize:this._access.cellSize(e),padding:(n[3]||"").split(/:|,|x|\*/).map((function(e){return parseInt(e)||0})),styles:l?i.map(l,(function(e){var t=i.trim(e).split("=");return{name:i.trim(t[0]),extractor:i.trim(t[1])}})):null}}},{key:"sortTemplates",value:function(e){for(var t=[],r={},n={},a=[],o=0;o<e.length;++o){var s=e[o];n[s.id]=o,s.reference?(r[s.reference]=r[s.reference]||[]).push(s.id):a.push(s.id)}for(;a.length>0;){var c=e[n[a.shift()]];t.push(c),r[c.id]&&a.push.apply(a,l(r[c.id]))}if(t.length<e.length)throw new Error('A reference cycle found, involving "'.concat(i.map(i.xor(e,t),"id").join(","),'"!'));return t}},{key:"collectTemplates",value:function(e){var t=this,r=[];return this._access.forAllCells((function(e){var n=t.parseTemplate(e);n&&r.push(n)})),this.sortTemplates(r).forEach(e)}},{key:"extractValues",value:function(e,t,r){var n=this,l=this.parseExtractor(t),a=l.path,o=l.handler;if(Array.isArray(e)){if(void 0!==e.sizes)e=t?i.map(e,(function(e){return n.extractValues(e,t,r)})):e;else if(!o)return e.join(this._opts.joinText||",")}else e=i.get(e,a,e);return o?o.call(this._opts,e,r):e}},{key:"extractData",value:function(e,t,r){var l=this,a=t[r],o=[],s=!1,c=null;if("1"==a&&(s=!0,a=t[++r]),!a)return e;var u=this.parseExtractor(a);if(c=i.get(e,u.path,e),"function"==typeof u.handler&&(c=u.handler.call(this._opts,c)),!Array.isArray(c)&&"object"===n(c))return c;if(r<t.length-1&&(o=(c=i.map(c,(function(e){return l.extractData(e,t,r+1)})))[0].sizes||[]),!c)throw new Error("The iterator '".concat(a,"' extracted no data!"));if("object"!==n(c))throw new Error("The data extracted from iterator '".concat(a,"' is neither an array, nor object!"));return o.unshift(s?-c.length:c.length),c.sizes=o,c}},{key:"putValues",value:function(e,t,r){var n=this;if(!e)throw new Error("Crash! Null reference cell in 'putValues()'!");var l=t.sizes,a=this.extractValues(t,r.extractor,e);if(Array.isArray(a)&&l&&l.length){if(!(l.length<=2))throw new Error("Values extracted with '".concat(r.extractor,"' are more than 2 dimension!'"));l[0]<0?(l=[1,-l[0]],a=[a],t=[t]):1==l.length&&(l=l.concat([1]),a=i.chunk(a,1),t=i.chunk(t,1)),this._access.getCellRange(e,l[0]-1,l[1]-1).forEach((function(e,l,o){n._access.setCellValue(e,a[l][o]),n.applyDataStyle(e,t[l][o],r)}))}else this._access.setCellValue(e,a),this.applyDataStyle(e,t,r),l=r.cellSize;return l}},{key:"applyFill",value:function(e,t,r){var n=this,l=e.template,a=this.extractData(t,l.iterators,0),o=[1,1];if(e.dependents&&e.dependents.length){for(var s=r,c=function(e,t){return o[t]=Math.max(o[t],e)},u=function(t){for(var r=a[t],u=0;u<e.dependents.length;++u){var f=e.dependents[u],h=n._access.offsetCell(s,f.offset[0],f.offset[1]);i.forEach(n.applyFill(f,r,h),c)}i.forEach(n.putValues(s,r,l),c);var p=o[0],d=o[1],y=l.padding[0]||0,m=l.padding[1]||0;if(a.sizes[0]<0?(l.padding.length<2&&(m=y),p=y=0,o[1]=1):a.sizes.length<2&&(d=m=0,o[0]=1),p>1||d>1){var v=n._access.getCellRange(s,Math.max(p-1,0),Math.max(d-1,0)),g=n.getTemplateOpts(l);!0===g.mergeCells||"both"===g.mergeCell||p>1&&"vertical"===g.mergeCells||d>1&&"horizontal"===g.mergeCells?n._access.rangeMerged(v,!0):(!0===g.duplicateCells||"both"===g.duplicateCells||p>1&&"vertical"===g.duplicateCells||d>1&&"horizontal"===g.duplicateCells)&&n._access.duplicateCell(s,v),v.forEach((function(e){return n.applyDataStyle(e,r,l)}))}s=n._access.offsetCell(s,p+y,d+m)},f=0;f<a.length;++f)u(f);i.forEach(this._access.cellDistance(r,s),c)}else o=this.putValues(r,a,l);return i.forEach(e.formulas,(function(e){return n.applyFormula(e,o,r)})),e.processed=!0,o}},{key:"shiftFormula",value:function(e,t,r){for(var n="";;){var l=e.match(c);if(!l)break;var a,o=this._access.getCell(l[3],l[2]);(t[0]>0||t[1]>0)&&(o=this._access.offsetCell(o,t[0],t[1])),a=l[5]?this._access.rangeRef(this._access.getCellRange(o,r[0],r[1]),!!l[2]):this._access.cellRef(o,!!l[2]),n+=e.substr(0,l.index)+a,e=e.substr(l.index+l[0].length)}return n+=e}},{key:"applyFormula",value:function(e,t,r){r=this._access.offsetCell(r,e.offset[0],e.offset[1]);var n,l=e.template,a=i.trim(l.iterators[0]),o=this._access.cellDistance(l.cell,r),s=l.extractor;if(e.processed=!0,this._access.setCellValue(r,null),t[0]<2&&t[1]<2||"both"===a)s=this.shiftFormula(s,o,[0,0]),n=this._access.getCellRange(r,t[0]-1,t[1]-1);else if("cols"===a)s=this.shiftFormula(s,o,[t[0]-1,0]),n=this._access.getCellRange(r,0,t[1]-1);else{if("rows"!==a)return void this._access.setCellFormula(r,this.shiftFormula(s,o,[t[0]-1,t[1]-1]));s=this.shiftFormula(s,o,[0,t[1]-1]),n=this._access.getCellRange(r,t[0]-1,0)}this._access.setRangeFormula(n,s)}}])&&o(t.prototype,r),a&&o(t,a),e}();u.XlsxPopulateAccess=e("./XlsxPopulateAccess"),u.version="1.0.3",t.exports=u}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./XlsxPopulateAccess":2}],2:[function(e,t,r){(function(e){"use strict";function r(e){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function n(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}var l="undefined"!=typeof window?window._:void 0!==e?e._:null,a=null,o=function(){function e(t,r){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this._workbook=t,this._rowSizes={},this._colSizes={},a=r.RichText}var t,o,i;return t=e,(o=[{key:"workbook",value:function(){return this._workbook}},{key:"cellValue",value:function(e){var t=e.value();return t instanceof a?t.text():t}},{key:"setCellValue",value:function(e,t){return e.value(t),this}},{key:"cellType",value:function(e){if(e.formula())return"formula";if(e.hyperlink())return"hyperlink";var t=e.value();return t instanceof a?"richtext":t instanceof Date?"date":r(t)}},{key:"setCellFormula",value:function(e,t){return e.formula(l.trimStart(t," =")),this}},{key:"cellDistance",value:function(e,t){return[t.rowNumber()-e.rowNumber(),t.columnNumber()-e.columnNumber()]}},{key:"cellSize",value:function(e){var t=this,r=e.address(),n=[1,1];return l.forEach(e.sheet()._mergeCells,(function(l){var a=l.attributes.ref.split(":");if(a[0]==r)return++(n=t.cellDistance(e,e.sheet().cell(a[1])))[0],++n[1],!1})),n}},{key:"setCellStyle",value:function(e,t,r){return e.style(t,r),this}},{key:"cellRef",value:function(e,t){return null==t&&(t=!0),e.address({includeSheetName:t})}},{key:"buildRef",value:function(e,t,r){return null==r&&(r=!0),t?e.sheet().cell(t).address({includeSheetName:r}):null}},{key:"getCell",value:function(e,t){return(null==t?this._workbook.activeSheet():this._workbook.sheet(t)).cell(e)}},{key:"duplicateCell",value:function(e,t){return t.value(e.value()),this}},{key:"getCellRange",value:function(e,t,r){return e.rangeTo(e.relativeCell(t,r))}},{key:"offsetCell",value:function(e,t,r){return e.relativeCell(t,r)}},{key:"rangeMerged",value:function(e,t){return void 0===t?e.merged():(e.merged(t),this)}},{key:"setRangeFormula",value:function(e,t){return e.formula(l.trimStart(t," =")),this}},{key:"rangeRef",value:function(e,t){return null==t&&(t=!0),e.address({includeSheetName:t})}},{key:"forAllCells",value:function(e){return this._workbook.sheets().forEach((function(t){var r=t.usedRange();r&&r.forEach(e)})),this}},{key:"copyStyle",value:function(e,t){if(!t||!e)throw new Error("Crash! Null 'src' or 'dest' for copyStyle()!");if(t==e)return this;void 0!==t._style?e.style(t._style):t._styleId>0&&(e._styleId=t._styleId);var r=e.sheet().name(),n="'".concat(r,"':").concat(e.rowNumber()),l="'".concat(r,"':").concat(e.columnNumber());return void 0===this._rowSizes[n]&&e.row().height(this._rowSizes[n]=t.row().height()),void 0===this._colSizes[l]&&e.column().width(this._colSizes[l]=t.column().width()),this}}])&&n(t.prototype,o),i&&n(t,i),e}();t.exports=o}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{}]},{},[1])(1)})); //# sourceMappingURL=xlsx-datafill.min.js.map