Skip to content

Commit c6e84fb

Browse files
authored
Merge pull request #3049 from PHPOffice/Calculation-Examples
Update Excel function samples for Date/Time and Engineering functions
2 parents 8513c64 + 4706749 commit c6e84fb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2659
-3
lines changed

samples/Calculations/DateTime/DATEDIF.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
[2000, 1, 1],
2525
[2019, 2, 14],
2626
[2020, 7, 4],
27+
[2020, 2, 29],
2728
];
2829
$testDateCount = count($testDates);
2930

samples/Calculations/DateTime/DAYS.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
[2000, 1, 1],
2525
[2019, 2, 14],
2626
[2020, 7, 4],
27+
[2020, 2, 29],
2728
[2029, 12, 31],
2829
[2525, 1, 1],
2930
];
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'NETWORKDAYS';
9+
$description = 'Returns the number of whole working days between start_date and end_date. Working days exclude weekends and any dates identified in holidays';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$publicHolidays = [
19+
[2022, 1, 3, '=DATE(G1, H1, I1)', 'New Year'],
20+
[2022, 4, 15, '=DATE(G2, H2, I2)', 'Good Friday'],
21+
[2022, 4, 18, '=DATE(G3, H3, I3)', 'Easter Monday'],
22+
[2022, 5, 2, '=DATE(G4, H4, I4)', 'Early May Bank Holiday'],
23+
[2022, 6, 2, '=DATE(G5, H5, I5)', 'Spring Bank Holiday'],
24+
[2022, 6, 3, '=DATE(G6, H6, I6)', 'Platinum Jubilee Bank Holiday'],
25+
[2022, 8, 29, '=DATE(G7, H7, I7)', 'Summer Bank Holiday'],
26+
[2022, 12, 26, '=DATE(G8, H8, I8)', 'Boxing Day'],
27+
[2022, 12, 27, '=DATE(G9, H9, I9)', 'Christmas Day'],
28+
];
29+
30+
$holidayCount = count($publicHolidays);
31+
$worksheet->fromArray($publicHolidays, null, 'G1', true);
32+
33+
$worksheet->getStyle('J1:J' . $holidayCount)
34+
->getNumberFormat()
35+
->setFormatCode('yyyy-mm-dd');
36+
37+
$worksheet->setCellValue('A1', '=DATE(2022,1,1)');
38+
39+
for ($numberOfMonths = 0; $numberOfMonths < 12; ++$numberOfMonths) {
40+
$worksheet->setCellValue('B' . ($numberOfMonths + 1), '=EOMONTH(A1, ' . $numberOfMonths . ')');
41+
$worksheet->setCellValue('C' . ($numberOfMonths + 1), '=NETWORKDAYS(A1, B' . ($numberOfMonths + 1) . ')');
42+
$worksheet->setCellValue('D' . ($numberOfMonths + 1), '=NETWORKDAYS(A1, B' . ($numberOfMonths + 1) . ', J1:J' . $holidayCount . ')');
43+
}
44+
45+
$worksheet->getStyle('A1')
46+
->getNumberFormat()
47+
->setFormatCode('yyyy-mm-dd');
48+
49+
$worksheet->getStyle('B1:B12')
50+
->getNumberFormat()
51+
->setFormatCode('yyyy-mm-dd');
52+
53+
// Test the formulae
54+
$helper->log('UK Public Holidays');
55+
$holidayData = $worksheet->rangeToArray('J1:K' . $holidayCount, null, true, true, true);
56+
$helper->displayGrid($holidayData);
57+
58+
for ($row = 1; $row <= 12; ++$row) {
59+
$helper->log(sprintf(
60+
'Between %s and %s is %d working days; %d with public holidays',
61+
$worksheet->getCell('A1')->getFormattedValue(),
62+
$worksheet->getCell('B' . $row)->getFormattedValue(),
63+
$worksheet->getCell('C' . $row)->getCalculatedValue(),
64+
$worksheet->getCell('D' . $row)->getCalculatedValue()
65+
));
66+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'WORKDAY';
9+
$description = 'Returns a number that represents a date that is the indicated number of working days before or after a starting date. Working days exclude weekends and any dates identified as holidays';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$publicHolidays = [
19+
[2022, 1, 3, '=DATE(G1, H1, I1)', 'New Year'],
20+
[2022, 4, 15, '=DATE(G2, H2, I2)', 'Good Friday'],
21+
[2022, 4, 18, '=DATE(G3, H3, I3)', 'Easter Monday'],
22+
[2022, 5, 2, '=DATE(G4, H4, I4)', 'Early May Bank Holiday'],
23+
[2022, 6, 2, '=DATE(G5, H5, I5)', 'Spring Bank Holiday'],
24+
[2022, 6, 3, '=DATE(G6, H6, I6)', 'Platinum Jubilee Bank Holiday'],
25+
[2022, 8, 29, '=DATE(G7, H7, I7)', 'Summer Bank Holiday'],
26+
[2022, 12, 26, '=DATE(G8, H8, I8)', 'Boxing Day'],
27+
[2022, 12, 27, '=DATE(G9, H9, I9)', 'Christmas Day'],
28+
];
29+
30+
$holidayCount = count($publicHolidays);
31+
$worksheet->fromArray($publicHolidays, null, 'G1', true);
32+
33+
$worksheet->getStyle('J1:J' . $holidayCount)
34+
->getNumberFormat()
35+
->setFormatCode('yyyy-mm-dd');
36+
37+
$worksheet->setCellValue('A1', '=DATE(2022,1,1)');
38+
39+
$workdayStep = 10;
40+
for ($days = $workdayStep; $days <= 366; $days += $workdayStep) {
41+
$worksheet->setCellValue('B' . ((int) $days / $workdayStep + 1), $days);
42+
$worksheet->setCellValue('C' . ((int) $days / $workdayStep + 1), '=WORKDAY(A1, B' . ((int) $days / $workdayStep + 1) . ')');
43+
$worksheet->setCellValue('D' . ((int) $days / $workdayStep + 1), '=WORKDAY(A1, B' . ((int) $days / $workdayStep + 1) . ', J1:J' . $holidayCount . ')');
44+
}
45+
46+
$worksheet->getStyle('A1')
47+
->getNumberFormat()
48+
->setFormatCode('yyyy-mm-dd');
49+
50+
$worksheet->getStyle('C1:D50')
51+
->getNumberFormat()
52+
->setFormatCode('yyyy-mm-dd');
53+
54+
// Test the formulae
55+
$helper->log('UK Public Holidays');
56+
$holidayData = $worksheet->rangeToArray('J1:K' . $holidayCount, null, true, true, true);
57+
$helper->displayGrid($holidayData);
58+
59+
for ($days = $workdayStep; $days <= 366; $days += $workdayStep) {
60+
$helper->log(sprintf(
61+
'%d workdays from %s is %s; %s with public holidays',
62+
$worksheet->getCell('B' . ((int) $days / $workdayStep + 1))->getFormattedValue(),
63+
$worksheet->getCell('A1')->getFormattedValue(),
64+
$worksheet->getCell('C' . ((int) $days / $workdayStep + 1))->getFormattedValue(),
65+
$worksheet->getCell('D' . ((int) $days / $workdayStep + 1))->getFormattedValue()
66+
));
67+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Date/Time';
8+
$functionName = 'DAYS360';
9+
$description = 'Returns the number of days between two dates based on a 360-day year';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$testDates = [
19+
[1900, 1, 1],
20+
[1904, 1, 1],
21+
[1936, 3, 17],
22+
[1960, 12, 19],
23+
[1999, 12, 31],
24+
[2000, 1, 1],
25+
[2019, 2, 14],
26+
[2020, 7, 4],
27+
[2020, 2, 29],
28+
[2029, 12, 31],
29+
[2525, 1, 1],
30+
];
31+
$testDateCount = count($testDates);
32+
33+
$worksheet->fromArray($testDates, null, 'A1', true);
34+
35+
for ($row = 1; $row <= $testDateCount; ++$row) {
36+
$worksheet->setCellValue('D' . $row, '=DATE(A' . $row . ',B' . $row . ',C' . $row . ')');
37+
$worksheet->setCellValue('E' . $row, '=D' . $row);
38+
$worksheet->setCellValue('F' . $row, '=DATE(2022,12,31)');
39+
$worksheet->setCellValue('G' . $row, '=YEARFRAC(D' . $row . ', F' . $row . ')');
40+
$worksheet->setCellValue('H' . $row, '=YEARFRAC(D' . $row . ', F' . $row . ', 1)');
41+
$worksheet->setCellValue('I' . $row, '=YEARFRAC(D' . $row . ', F' . $row . ', 2)');
42+
$worksheet->setCellValue('J' . $row, '=YEARFRAC(D' . $row . ', F' . $row . ', 3)');
43+
$worksheet->setCellValue('K' . $row, '=YEARFRAC(D' . $row . ', F' . $row . ', 4)');
44+
}
45+
$worksheet->getStyle('E1:F' . $testDateCount)
46+
->getNumberFormat()
47+
->setFormatCode('yyyy-mm-dd');
48+
49+
// Test the formulae
50+
for ($row = 1; $row <= $testDateCount; ++$row) {
51+
$helper->log(sprintf(
52+
'Between: %s and %s',
53+
$worksheet->getCell('E' . $row)->getFormattedValue(),
54+
$worksheet->getCell('F' . $row)->getFormattedValue()
55+
));
56+
$helper->log(sprintf(
57+
'Days: %f - US (NASD) 30/360',
58+
$worksheet->getCell('G' . $row)->getCalculatedValue()
59+
));
60+
$helper->log(sprintf(
61+
'Days: %f - Actual',
62+
$worksheet->getCell('H' . $row)->getCalculatedValue()
63+
));
64+
$helper->log(sprintf(
65+
'Days: %f - Actual/360',
66+
$worksheet->getCell('I' . $row)->getCalculatedValue()
67+
));
68+
$helper->log(sprintf(
69+
'Days: %f - Actual/365',
70+
$worksheet->getCell('J' . $row)->getCalculatedValue()
71+
));
72+
$helper->log(sprintf(
73+
'Days: %f - European 30/360',
74+
$worksheet->getCell('K' . $row)->getCalculatedValue()
75+
));
76+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$category = 'Engineering';
9+
$functionName = 'BESSELI';
10+
$description = 'Returns the modified Bessel function, which is equivalent to the Bessel function evaluated for purely imaginary arguments';
11+
12+
$helper->titles($category, $functionName, $description);
13+
14+
// Create new PhpSpreadsheet object
15+
$spreadsheet = new Spreadsheet();
16+
$worksheet = $spreadsheet->getActiveSheet();
17+
18+
for ($n = 0; $n <= 5; ++$n) {
19+
for ($x = 0; $x <= 5; $x = $x + 0.25) {
20+
Calculation::getInstance($spreadsheet)->flushInstance();
21+
$worksheet->setCellValue('A1', "=BESSELI({$x}, {$n})");
22+
23+
$helper->log(sprintf(
24+
'%s = %f',
25+
$worksheet->getCell('A1')->getValue(),
26+
$worksheet->getCell('A1')->getCalculatedValue()
27+
));
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$category = 'Engineering';
9+
$functionName = 'BESSELJ';
10+
$description = 'Returns the Bessel function';
11+
12+
$helper->titles($category, $functionName, $description);
13+
14+
// Create new PhpSpreadsheet object
15+
$spreadsheet = new Spreadsheet();
16+
$worksheet = $spreadsheet->getActiveSheet();
17+
18+
for ($n = 0; $n <= 5; ++$n) {
19+
for ($x = 0; $x <= 5; $x = $x + 0.25) {
20+
Calculation::getInstance($spreadsheet)->flushInstance();
21+
$worksheet->setCellValue('A1', "=BESSELJ({$x}, {$n})");
22+
23+
$helper->log(sprintf(
24+
'%s = %f',
25+
$worksheet->getCell('A1')->getValue(),
26+
$worksheet->getCell('A1')->getCalculatedValue()
27+
));
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$category = 'Engineering';
9+
$functionName = 'BESSELK';
10+
$description = 'Returns the modified Bessel function, which is equivalent to the Bessel functions evaluated for purely imaginary arguments';
11+
12+
$helper->titles($category, $functionName, $description);
13+
14+
// Create new PhpSpreadsheet object
15+
$spreadsheet = new Spreadsheet();
16+
$worksheet = $spreadsheet->getActiveSheet();
17+
18+
for ($n = 0; $n <= 5; ++$n) {
19+
for ($x = 0; $x <= 5; $x = $x + 0.25) {
20+
Calculation::getInstance($spreadsheet)->flushInstance();
21+
$worksheet->setCellValue('A1', "=BESSELK({$x}, {$n})");
22+
23+
$helper->log(sprintf(
24+
'%s = %f',
25+
$worksheet->getCell('A1')->getValue(),
26+
$worksheet->getCell('A1')->getCalculatedValue()
27+
));
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
5+
6+
require __DIR__ . '/../../Header.php';
7+
8+
$category = 'Engineering';
9+
$functionName = 'BESSELY';
10+
$description = 'Returns the Bessel function, which is also called the Weber function or the Neumann function';
11+
12+
$helper->titles($category, $functionName, $description);
13+
14+
// Create new PhpSpreadsheet object
15+
$spreadsheet = new Spreadsheet();
16+
$worksheet = $spreadsheet->getActiveSheet();
17+
18+
for ($n = 0; $n <= 5; ++$n) {
19+
for ($x = 0; $x <= 5; $x = $x + 0.25) {
20+
Calculation::getInstance($spreadsheet)->flushInstance();
21+
$worksheet->setCellValue('A1', "=BESSELY({$x}, {$n})");
22+
23+
$helper->log(sprintf(
24+
'%s = %f',
25+
$worksheet->getCell('A1')->getValue(),
26+
$worksheet->getCell('A1')->getCalculatedValue()
27+
));
28+
}
29+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Spreadsheet;
4+
5+
require __DIR__ . '/../../Header.php';
6+
7+
$category = 'Engineering';
8+
$functionName = 'BIN2DEC';
9+
$description = 'Converts a binary number to decimal';
10+
11+
$helper->titles($category, $functionName, $description);
12+
13+
// Create new PhpSpreadsheet object
14+
$spreadsheet = new Spreadsheet();
15+
$worksheet = $spreadsheet->getActiveSheet();
16+
17+
// Add some data
18+
$testData = [
19+
[101],
20+
[110110],
21+
[1000000],
22+
[11111111],
23+
[100010101],
24+
[110001100],
25+
[111111111],
26+
[1111111111],
27+
[1100110011],
28+
[1000000000],
29+
];
30+
$testDataCount = count($testData);
31+
32+
$worksheet->fromArray($testData, null, 'A1', true);
33+
34+
for ($row = 1; $row <= $testDataCount; ++$row) {
35+
$worksheet->setCellValue('B' . $row, '=BIN2DEC(A' . $row . ')');
36+
}
37+
38+
// Test the formulae
39+
for ($row = 1; $row <= $testDataCount; ++$row) {
40+
$helper->log(sprintf(
41+
'(B%d): Binary %s is decimal %s',
42+
$row,
43+
$worksheet->getCell('A' . $row)->getValue(),
44+
$worksheet->getCell('B' . $row)->getCalculatedValue(),
45+
));
46+
}

0 commit comments

Comments
 (0)