Skip to content

Commit

Permalink
merge #103
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiooshiro committed May 7, 2024
1 parent bd2dd3c commit e460ea6
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ coverage
*.xlsx#
/test/fund*.xlsx
yarn-error.log
/dist
xlsx-calc-*.tgz
51 changes: 44 additions & 7 deletions src/formulas.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ let formulas = {
'SUBSTITUTE': substitute,
'CEILING': ceiling,
'FILTER': throwErrors(FILTER),
'DATEDIF': datediff,
'EOMONTH': eomonth,
};

function choose(option) {
Expand Down Expand Up @@ -134,11 +136,11 @@ function sumproduct() {
return 0;
},
consistentSizeRanges = function (matrixArray) {
var getRowCount = function(matrix) {
var getRowCount = function(matrix = []) {
return matrix.length;
},
getColCount = function(matrix) {
return matrix[0].length;
getColCount = function(matrix = []) {
return matrix[0] && matrix[0].length;
},
rowCount = getRowCount(matrixArray[0]),
colCount = getColCount(matrixArray[0]);
Expand All @@ -152,7 +154,8 @@ function sumproduct() {
return true;
};

if (!arguments || arguments.length === 0) {
if (!arguments || arguments.length === 0 || !arguments[0]) {

throw Error('#VALUE!');
}
if (!consistentSizeRanges(arguments)) {
Expand Down Expand Up @@ -794,7 +797,7 @@ function day(date) {
if (!date.getDate) {
throw Error('#VALUE!');
}
var day = date.getDate();
var day = date.getUTCDate();
if (isNaN(day)) {
throw Error('#VALUE!');
}
Expand All @@ -805,7 +808,7 @@ function month(date) {
if (!date.getMonth) {
throw Error('#VALUE!');
}
var month = date.getMonth();
var month = date.getUTCMonth();
if (isNaN(month)) {
throw Error('#VALUE!');
}
Expand All @@ -816,13 +819,47 @@ function year(date) {
if (!date.getFullYear) {
throw Error('#VALUE!');
}
var year = date.getFullYear();
var year = date.getUTCFullYear();
if (isNaN(year)) {
throw Error('#VALUE!');
}
return year;
}

function datediff(date1, date2, unit) {
date1 = new Date(date1);
date2 = new Date(date2);

if (!date1 || !date2 || date1 == 'Invalid Date' || date2 == 'Invalid Date') {
throw Error('#VALUE!');
}

unit = unit.replace(/[^DMY]/ig, "");

switch(unit) {
case "M":
return date2.getMonth() - date1.getMonth() + (12 * (date2.getFullYear() - date1.getFullYear()))
case "Y":
return Math.abs(date2.getUTCFullYear() - date1.getUTCFullYear());
case "D": default:
var timeDiff = Math.abs(date2 - date1);
return Math.ceil(timeDiff / (1000 * 3600 * 24));
}
}

function eomonth(date, months) {
date = new Date(date);
if (!date || date == 'Invalid Date') {
throw Error('#VALUE!');
}
months = months || 0;
var endofmonth = new Date(date.getUTCFullYear(), date.getUTCMonth()+months+1, 0);
endofmonth.setUTCHours(0);
endofmonth.setUTCMinutes(0);
endofmonth.setUTCSeconds(0);
return endofmonth;
}

function right(text, number) {
number = (number === undefined) ? 1 : parseFloat(number);

Expand Down
94 changes: 94 additions & 0 deletions test/1-basic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,86 @@ describe('XLSX_CALC', function() {
});
});

describe('DATEDIF', function () {
it('calcs DATEDIF("2000/1/1", "2001/1/1", "D")', function() {
workbook.Sheets.Sheet1.A1.f = `DATEDIF('2000-01-01', '2001-01-01', 'D')`;
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A1.v, 366);
});

it('calcs DATEDIF("2000/1/1", "2001/1/1", "M")', function() {
workbook.Sheets.Sheet1.A1.f = `DATEDIF('2000-01-01', '2001-01-01', 'M')`;
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A1.v, 12);
});

it('calcs DATEDIF("2000/1/1", "2001/1/1", "Y")', function() {
workbook.Sheets.Sheet1.A1.f = `DATEDIF('2000-01-01', '2001-01-01', 'Y')`;
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A1.v, 1);
});

it('should throw #VALUE error if applied to an invalid date', function () {
workbook.Sheets.Sheet1.A1.f = `DATEDIF('NOT A DAY', '2001-01-01', 'Y')`;
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A1.w, "#VALUE!");
});

it('should return days between two values', function () {
workbook.Sheets.Sheet1.A1 = {
t: 'd',
v: new Date('2019-05-01'),
w: '2019-05-01'
};
workbook.Sheets.Sheet1.A2 = {
t: 'd',
v: new Date('2019-06-01'),
w: '2019-06-01'
};
workbook.Sheets.Sheet1.A3 = { f: 'DATEDIF(A1,A2,"D")' };
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A3.v, 31);
assert.equal(workbook.Sheets.Sheet1.A3.t, 'n');
});
});

describe('EOMONTH', function () {
it('calcs EOMONTH("2000/1/1", 0)', function() {
workbook.Sheets.Sheet1.A1.f = `EOMONTH("2000/1/1", 0)`;
XLSX_CALC(workbook);
assert.deepEqual(workbook.Sheets.Sheet1.A1.v, new Date("2000-01-31"));
});

it('calcs EOMONTH("2000/1/1", 1)', function() {
workbook.Sheets.Sheet1.A1.f = `EOMONTH("2000/1/1", 1)`;
XLSX_CALC(workbook);
assert.deepEqual(workbook.Sheets.Sheet1.A1.v, new Date("2000-02-29"));
});

it('calcs EOMONTH("2000/12/1", 1)', function() {
workbook.Sheets.Sheet1.A1.f = `EOMONTH("2000/12/1", 10)`;
XLSX_CALC(workbook);
assert.deepEqual(workbook.Sheets.Sheet1.A1.v, new Date("2001-10-31"));
});

it('should throw #VALUE error if applied to an invalid date', function () {
workbook.Sheets.Sheet1.A1.f = `EOMONTH('NOT A DAY', 0)`;
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.A1.w, "#VALUE!");
});

it('should return end of a month', function () {
workbook.Sheets.Sheet1.A1 = {
t: 'd',
v: new Date('2019-05-01'),
w: '2019-05-01'
};
workbook.Sheets.Sheet1.A3 = { f: 'EOMONTH(A1,1)' };
XLSX_CALC(workbook);
assert.deepEqual(workbook.Sheets.Sheet1.A3.v, new Date('2019-06-30'));
});
});

describe('RIGHT', function () {
it('should return n last characters of a string value', function () {
workbook.Sheets.Sheet1.A1.v = 'test value';
Expand Down Expand Up @@ -1504,6 +1584,20 @@ describe('XLSX_CALC', function() {
XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.C1.v, 54);
});
it('should handle no values for the given arrays', function () {
workbook.Sheets.Sheet1 = {
A1: { v: 'Array 1' },
A2: { v: 3 },
A4: { v: 8 },
D1: { v: 'Array 2' },
D2: { v: 2 },
D4: { v: 6 },
C1: { f: "SUMPRODUCT(,)" }
};

XLSX_CALC(workbook);
assert.equal(workbook.Sheets.Sheet1.C1.w, '#VALUE!');
});
it('shows "#VALUE!" error value if the array arguments dont have the same dimensions', function () {
workbook.Sheets.Sheet1 = {
A1: { v: 'Array 1' },
Expand Down

0 comments on commit e460ea6

Please sign in to comment.