spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← All files
name: dist/Formulas/Info.js
-rw-r--r--
13073
  1"use strict";
  2exports.__esModule = true;
  3var ArgsChecker_1 = require("../Utilities/ArgsChecker");
  4var Errors_1 = require("../Errors");
  5var TypeConverter_1 = require("../Utilities/TypeConverter");
  6var Cell_1 = require("../Cell");
  7/**
  8 * Returns the "value not available" error, "#N/A".
  9 * @constructor
 10 */
 11var NA = function () {
 12    ArgsChecker_1.ArgsChecker.checkLength(arguments, 0, "NA");
 13    throw new Errors_1.NAError("NA Error thrown.");
 14};
 15exports.NA = NA;
 16/**
 17 * Returns true if a value is text.
 18 * @param value - value or reference to check.
 19 * @returns {boolean}.
 20 * @constructor
 21 */
 22var ISTEXT = function (value) {
 23    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISTEXT");
 24    return typeof TypeConverter_1.TypeConverter.firstValue(value) === "string";
 25};
 26exports.ISTEXT = ISTEXT;
 27/**
 28 * Returns true if a value is not text.
 29 * @param value - value or reference to check.
 30 * @returns {boolean}.
 31 * @constructor
 32 */
 33var ISNONTEXT = function (value) {
 34    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISNONTEXT");
 35    return typeof TypeConverter_1.TypeConverter.firstValue(value) !== "string";
 36};
 37exports.ISNONTEXT = ISNONTEXT;
 38/**
 39 * Returns true if value is a boolean (FALSE, or TRUE). Numerical and text values return false.
 40 * @param value - value or reference to check.
 41 * @returns {boolean}
 42 * @constructor
 43 */
 44var ISLOGICAL = function (value) {
 45    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISLOGICAL");
 46    return typeof TypeConverter_1.TypeConverter.firstValue(value) === "boolean";
 47};
 48exports.ISLOGICAL = ISLOGICAL;
 49/**
 50 * Returns true if value or reference is a number.
 51 * @param value - value or reference to check.
 52 * @returns {boolean}
 53 * @constructor
 54 */
 55var ISNUMBER = function (value) {
 56    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISNUMBER");
 57    return typeof TypeConverter_1.TypeConverter.firstValue(value) === "number";
 58};
 59exports.ISNUMBER = ISNUMBER;
 60/**
 61 * Returns true if input is a valid email. Valid domains are Original top-level domains and Country code top-level
 62 * domains.
 63 * @param value - Value to check whether it is an email or not.
 64 * @returns {boolean}
 65 * @constructor
 66 */
 67var ISEMAIL = function (value) {
 68    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISEMAIL");
 69    if (typeof value !== "string") {
 70        return false;
 71    }
 72    var EMAIL_REGEX = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|edu|int|biz|info|mobi|name|aero|jobs|museum|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bl|bm|bn|bo|bq|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cw|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mf|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|za|zm|zw)\b/;
 73    return EMAIL_REGEX.test(TypeConverter_1.TypeConverter.firstValueAsString(value));
 74};
 75exports.ISEMAIL = ISEMAIL;
 76/**
 77 * Returns true if the input is a valid URL.
 78 * @param value - Value to check
 79 * @returns {boolean}
 80 * @constructor
 81 */
 82var ISURL = function (value) {
 83    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISURL");
 84    value = TypeConverter_1.TypeConverter.firstValueAsString(value);
 85    var matches = value.match(/(?:([^:\/?#]+):)?(?:\/\/([^\/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/);
 86    if (/[^a-z0-9\:\/\?\#\[\]\@\!\$\&\'\(\)\*\+\,\;\=\.\-\_\~\%]/i.test(value)) {
 87        return false;
 88    }
 89    if (/%[^0-9a-f]/i.test(value)) {
 90        return false;
 91    }
 92    if (/%[0-9a-f](:?[^0-9a-f]|$)/i.test(value)) {
 93        return false;
 94    }
 95    var authority = matches[2];
 96    var path = matches[3];
 97    if (!(path.length >= 0)) {
 98        return false;
 99    }
100    if (authority && authority.length) {
101        if (!(path.length === 0 || /^\//.test(path))) {
102            return false;
103        }
104    }
105    else {
106        if (/^\/\//.test(path)) {
107            return false;
108        }
109    }
110    return true;
111};
112exports.ISURL = ISURL;
113/**
114 * Returns the value as a number.
115 * @param value - value to return.
116 * @returns {number}
117 * @constructor
118 */
119var N = function (value) {
120    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "N");
121    return TypeConverter_1.TypeConverter.firstValueAsNumber(value);
122};
123exports.N = N;
124/**
125 * Tests if the content of one or several cells is a reference. Verifies the type of references in a cell or a range of
126 * cells. If an error occurs, the function returns a logical or numerical value.
127 * @param value - The value to be tested, to determine whether it is a reference.
128 * @returns {boolean}
129 * @constructor
130 */
131var ISREF = function (value) {
132    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISREF");
133    return TypeConverter_1.TypeConverter.firstValue(value) instanceof Cell_1.Cell;
134};
135exports.ISREF = ISREF;
136/**
137 * Returns the number corresponding to an error value occurring in a different cell. With the aid of this number, an
138 * error message text can be generated. If an error occurs, the function returns a logical or numerical value.
139 * @param value - Contains either the address/reference of the cell in which the error occurs, or the error directly.
140 * Eg: `=ERRORTYPE(NA())`
141 * @constructor
142 */
143var ERRORTYPE = function (value) {
144    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ERRORTYPE");
145    try {
146        value = TypeConverter_1.TypeConverter.firstValue(value);
147    }
148    catch (e) {
149        value = e;
150    }
151    if (value instanceof Cell_1.Cell) {
152        if (value.hasError()) {
153            value = value.getError();
154        }
155        else {
156            throw new Errors_1.NAError("Function ERROR.TYPE parameter 1 value is not an error.");
157        }
158    }
159    if (value instanceof Error) {
160        switch (value.name) {
161            case Errors_1.NULL_ERROR:
162                return 1;
163            case Errors_1.DIV_ZERO_ERROR:
164                return 2;
165            case Errors_1.VALUE_ERROR:
166                return 3;
167            case Errors_1.REF_ERROR:
168                return 4;
169            case Errors_1.NAME_ERROR:
170                return 5;
171            case Errors_1.NUM_ERROR:
172                return 6;
173            case Errors_1.NA_ERROR:
174                return 7;
175            default:
176                return 8;
177        }
178    }
179    else {
180        throw new Errors_1.NAError("Function ERROR.TYPE parameter 1 value is not an error.");
181    }
182};
183exports.ERRORTYPE = ERRORTYPE;
184/**
185 * Returns TRUE if the reference to a cell is blank. This function is used to determine if the content of a cell is
186 * empty. A cell with a formula inside is not empty. If an error occurs, the function returns a logical or numerical
187 * value.
188 * @param value - The content to be tested.
189 * @returns {boolean}
190 * @constructor
191 */
192var ISBLANK = function (value) {
193    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISBLANK");
194    if (value instanceof Cell_1.Cell) {
195        return value.isBlank();
196    }
197    return value === undefined;
198};
199exports.ISBLANK = ISBLANK;
200/**
201 * Returns TRUE if the value refers to any error value except #N/A. You can use this function to control error values
202 * in certain cells. If an error occurs, the function returns a logical or numerical value.
203 * @param value - Any value or expression in which a test is performed to determine whether an error value not equal to
204 * #N/A is present.
205 * @returns {boolean}
206 * @constructor
207 */
208var ISERR = function (value) {
209    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISERR");
210    try {
211        value = TypeConverter_1.TypeConverter.firstValue(value);
212    }
213    catch (e) {
214        return true;
215    }
216    if (value instanceof Cell_1.Cell) {
217        if (value.hasError()) {
218            return value.getError().name !== Errors_1.NA_ERROR;
219        }
220        return false;
221    }
222    if (value instanceof Error) {
223        return value.name !== Errors_1.NA_ERROR;
224    }
225    return false;
226};
227exports.ISERR = ISERR;
228/**
229 * Tests if the cells contain general error values. ISERROR recognizes the #N/A error value. If an error occurs, the
230 * function returns a logical or numerical value.
231 * @param value - is any value where a test is performed to determine whether it is an error value.
232 * @returns {boolean}
233 * @constructor
234 */
235var ISERROR = function (value) {
236    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISERROR");
237    try {
238        value = TypeConverter_1.TypeConverter.firstValue(value);
239    }
240    catch (e) {
241        return true;
242    }
243    if (value instanceof Cell_1.Cell) {
244        return value.hasError();
245    }
246    return (value instanceof Error);
247};
248exports.ISERROR = ISERROR;
249/**
250 * Returns TRUE if a cell contains the #N/A (value not available) error value. If an error occurs, the function returns
251 * a logical or numerical value.
252 * @param value - The value or expression to be tested.
253 * @returns {boolean}
254 * @constructor
255 */
256var ISNA = function (value) {
257    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISNA");
258    try {
259        value = TypeConverter_1.TypeConverter.firstValue(value);
260    }
261    catch (e) {
262        return false;
263    }
264    if (value instanceof Cell_1.Cell) {
265        if (value.hasError()) {
266            return value.getError().name === Errors_1.NA_ERROR;
267        }
268    }
269    if (value instanceof Error) {
270        return value.name === Errors_1.NA_ERROR;
271    }
272    return false;
273};
274exports.ISNA = ISNA;
275/**
276 * Returns the first argument if no error value is present, otherwise returns the second argument if provided, or a
277 * blank if the second argument is absent. Blank value is `null`.
278 * @param value - Value to check for error.
279 * @param valueIfError - [OPTIONAL] - Value to return if no error is present in the first argument.
280 * @returns {any}
281 * @constructor
282 */
283var IFERROR = function (value, valueIfError) {
284    valueIfError = valueIfError || null;
285    ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "IFERROR");
286    if (value instanceof Cell_1.Cell) {
287        if (value.hasError()) {
288            return valueIfError;
289        }
290        return value;
291    }
292    if (ISERROR(value)) {
293        return valueIfError;
294    }
295    return value;
296};
297exports.IFERROR = IFERROR;
298/**
299 * Returns a number corresponding to the type of data passed into the function. 1 = number, 2 = text, 4 = boolean,
300 * 16 = error, 64 = array/range, 128 = any other type of cell.
301 * @param value - Value for which the type will be determined.
302 * @returns {number}
303 * @constructor
304 */
305var TYPE = function (value) {
306    ArgsChecker_1.ArgsChecker.checkLengthWithin(arguments, 1, 2, "TYPE");
307    if (value instanceof Cell_1.Cell) {
308        if (value.hasError()) {
309            return 16;
310        }
311        value = value.getValue();
312    }
313    if (value === null) {
314        return 1;
315    }
316    if (typeof value === "number") {
317        return 1;
318    }
319    if (typeof value === "string") {
320        return 2;
321    }
322    if (typeof value === "boolean") {
323        return 4;
324    }
325    if (value instanceof Error) {
326        return 16;
327    }
328    if (value instanceof Array) {
329        return 64;
330    }
331    return 128;
332};
333exports.TYPE = TYPE;
334/**
335 * Returns the column number of a specified cell, starting with column 1 for A.
336 * @param cell - Cell, defaults to the cell calling this formula, when used in the context of a spreadsheet.
337 * @constructor
338 */
339var COLUMN = function (cell) {
340    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "COLUMN");
341    if (!(cell instanceof Cell_1.Cell)) {
342        throw new Errors_1.NAError("Argument must be a range or reference.");
343    }
344    return cell.getColumn() + 1;
345};
346exports.COLUMN = COLUMN;
347/**
348 * Returns the row number of a specified cell, starting with row 1 for A1.
349 * @param cell - Cell, defaults to the cell calling this formula, when used in the context of a spreadsheet.
350 * @constructor
351 */
352var ROW = function (cell) {
353    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ROW");
354    if (!(cell instanceof Cell_1.Cell)) {
355        throw new Errors_1.NAError("Argument must be a range or reference.");
356    }
357    return cell.getRow() + 1;
358};
359exports.ROW = ROW;
360/**
361 * Returns TRUE if a cell is a formula cell. Must be given a reference.
362 * @param value - To check.
363 * @returns {boolean}
364 * @constructor
365 */
366var ISFORMULA = function (value) {
367    ArgsChecker_1.ArgsChecker.checkLength(arguments, 1, "ISFORMULA");
368    if (value instanceof Array) {
369        if (value.length === 0) {
370            throw new Errors_1.RefError("Reference does not exist.");
371        }
372    }
373    if (!(value instanceof Cell_1.Cell)) {
374        throw new Errors_1.NAError("Argument must be a range");
375    }
376    return value.hasFormula();
377};
378exports.ISFORMULA = ISFORMULA;