@modelmap/a1-notation
Version:
Work with A1 notation like "A1" or "A1:B2"
3 lines (2 loc) • 4.47 kB
JavaScript
var A1=function(t){"use strict";function r(t){return parseInt(t,10)}function s(t){let r,s="";for(;t>0;)r=(t-1)%26,s=String.fromCharCode(r+65)+s,t=(t-r-1)/26;return s}function i(t){return t.toString()}function n(t){return/^[A-Z]+\d+(:[A-Z]+\d+)?$/i.test(t)}function e(t,r=!0){let s="number"==typeof t&&t%1==0;return r?s&&+t>0:s}class o extends Error{constructor(t){let r=JSON.stringify(t);super(r),this.name="A1Error",this.message=r}wasString(){return this.message="Invalid A1 notation: "+this.message,this}wasNumber(){return this.message="Invalid A1 number(s): "+this.message,this}wasUnknown(){return this.message="Invalid A1 argument(s): "+this.message,this}}class h{constructor(t,r,s,i){if(this._colStart=0,this._rowStart=0,this._colEnd=0,this._rowEnd=0,this._converter=1,!arguments.length)throw(new o).wasUnknown();let n=typeof t;if("number"===n)this._initNumber.apply(this,arguments);else{if("string"!==n)throw new o(t).wasUnknown();this._initString.apply(this,arguments)}}static _parse(t,s){let[,i,n,e,o]=t.toUpperCase().match(this._reg);e=e||i,o=o||n;let h=this._A1Col(i,s),a=this._A1Col(e,s),w=r(n),_=r(o);return{cs:a>h?h:a,rs:_>w?w:_,ce:a>h?a:h,re:_>w?_:w}}static _A1Col(t,r){return 1===r?function(t){let r=0,s=t.length;for(let i=0;i<s;i++)r+=(t.charCodeAt(i)-64)*Math.pow(26,s-i-1);return r}(t):function(t){let r,s,i,n=0,e="A".charCodeAt(0),o="Z".charCodeAt(0)-e+1;for(r=0,s=t.length;r<s;r++)i=t.charCodeAt(r),n=n*o+i-e+1;return n}(t)}static isValid(t){return n(t)}static getCol(t,r=1){if(!n(t))throw new o(t).wasString();return this._parse(t,r).cs}static getLastCol(t,r=1){if(!n(t))throw new o(t).wasString();return this._parse(t,r).ce}static toCol(t){if(!e(t))throw new o(t).wasNumber();return s(t)}static getRow(t){if(!n(t))throw new o(t).wasString();return this._parse(t,1).rs}static getLastRow(t){if(!n(t))throw new o(t).wasString();return this._parse(t,1).re}static toRow(t){if(!e(t))throw new o(t).wasNumber();return i(t)}static getWidth(t,r=1){if(!n(t))throw new o(t).wasString();let{ce:s,cs:i}=this._parse(t,r);return s-i+1}static getHeight(t){if(!n(t))throw new o(t).wasString();let{re:r,rs:s}=this._parse(t,1);return r-s+1}_initNumber(...t){let[r,s,i,n]=t;i=i||1,n=n||1;let h=[r,s,i,n];if(!h.every(t=>e(t)))throw new o(h.join(", ")).wasNumber();this._colStart=r,this._rowStart=s,this._colEnd=r+n-1,this._rowEnd=s+i-1}_initString(...t){let[r,s]=t,i=s?`${r}:${s}`:r;if(!n(i))throw new o(i).wasString();let{cs:e,rs:a,ce:w,re:_}=h._parse(i,this._converter);this._colStart=e,this._rowStart=a,this._colEnd=w,this._rowEnd=_}get(){let t=s(this._colStart)+i(this._rowStart),r=s(this._colEnd)+i(this._rowEnd);return t===r?t:`${t}:${r}`}toString(){return this.get()}toJSON(){return{colStart:this._colStart,rowStart:this._rowStart,colEnd:this._colEnd,rowEnd:this._rowEnd,a1:this.get(),rowsCount:this._rowEnd-this._rowStart+1,colsCount:this._colEnd-this._colStart+1}}getCol(){return this._colStart}getLastCol(){return this._colEnd}getRow(){return this._rowStart}getLastRow(){return this._rowEnd}getWidth(){return this._colEnd-this._colStart+1}getHeight(){return this._rowEnd-this._rowStart+1}copy(){return new h(this.get())}addX(t){if(!e(t,!1))throw new o(t).wasUnknown();return t>=0?this._colEnd+=t:this._colStart+=t,this._colStart<=0&&(this._colStart=1),this}addY(t){if(!e(t,!1))throw new o(t).wasUnknown();return t>=0?this._rowEnd+=t:this._rowStart+=t,this._rowStart<=0&&(this._rowStart=1),this}add(t,r){return this.addX(t),this.addY(r),this}removeX(t){if(!e(t,!1))throw new o(t).wasUnknown();return t>=0?(this._colEnd-=t,this._colEnd<this._colStart&&(this._colEnd=this._colStart)):(this._colStart-=t,this._colStart>this._colEnd&&(this._colStart=this._colEnd)),this}removeY(t){if(!e(t,!1))throw new o(t).wasUnknown();return t>=0?(this._rowEnd-=t,this._rowEnd<this._rowStart&&(this._rowEnd=this._rowStart)):(this._rowStart-=t,this._rowStart>this._rowEnd&&(this._rowStart=this._rowEnd)),this}remove(t,r){return this.removeX(t),this.removeY(r),this}shiftX(t){if(!e(t,!1))throw new o(t).wasUnknown();let r=this._colEnd-this._colStart,s=this._colStart+t,i=this._colEnd+t;return this._colStart=s>0?s:1,this._colEnd=s>0?i:r+1,this}shiftY(t){if(!e(t,!1))throw new o(t).wasUnknown();let r=this._rowEnd-this._rowStart,s=this._rowStart+t,i=this._rowEnd+t;return this._rowStart=s>0?s:1,this._rowEnd=s>0?i:r+1,this}shift(t,r){return this.shiftX(t),this.shiftY(r),this}}return h._reg=/^([A-Z]+)(\d+)(?::([A-Z]+)(\d+))?$/,t.A1=h,t}({});
//# sourceMappingURL=index.next.min.js.map