Skip to content

Commit 523a6b6

Browse files
authored
Merge pull request #3485 from PHPOffice/Examples-NumberFormat-Wizards
Examples for using some of the new NumberFormat Wizards
2 parents 9d82cec + aa9e1ed commit 523a6b6

File tree

7 files changed

+436
-1
lines changed

7 files changed

+436
-1
lines changed

samples/Basic/45_Quadratic_equation_solver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
<?php
22

33
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
4+
use PhpOffice\PhpSpreadsheet\Helper\Sample;
45
use PhpOffice\PhpSpreadsheet\Settings;
56

67
require __DIR__ . '/../Header.php';
8+
9+
$helper = new Sample();
10+
if ($helper->isCli()) {
11+
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
12+
13+
return;
14+
}
715
?>
816
<form action="45_Quadratic_equation_solver.php" method="POST">
917
Enter the coefficients for the Ax<sup>2</sup> + Bx + C = 0
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
4+
use PhpOffice\PhpSpreadsheet\Helper\Sample;
5+
use PhpOffice\PhpSpreadsheet\Settings;
6+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
7+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
8+
9+
require __DIR__ . '/../../Header.php';
10+
11+
$helper = new Sample();
12+
if ($helper->isCli()) {
13+
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
14+
15+
return;
16+
}
17+
18+
$currencies = [
19+
'$' => 'US Dollars ($)',
20+
'' => 'Euro (€)',
21+
'¥' => 'Japanese Yen (¥)',
22+
'£' => 'Pound Sterling (£)',
23+
'' => 'Rupee (₹)',
24+
'' => 'Rouble (₽)',
25+
];
26+
27+
?>
28+
<form action=Accounting.php method="POST">
29+
<div class="mb-3 row">
30+
<label for="number" class="col-sm-2 col-form-label">Sample Number Value</label>
31+
<div class="col-sm-10">
32+
<input name="number" type="text" size="8" value="<?php echo (isset($_POST['number'])) ? htmlentities($_POST['number'], Settings::htmlEntityFlags()) : '1234.5678'; ?>">
33+
</div>
34+
</div>
35+
<div class="mb-3 row">
36+
<hr />
37+
</div>
38+
<div class="mb-3 row">
39+
<label for="currency" class="col-sm-2 col-form-label">Currency</label>
40+
<div class="col-sm-10">
41+
<select name="currency" class="form-select">
42+
<?php foreach ($currencies as $currencySymbol => $currencyName) {
43+
echo "<option value=\"{$currencySymbol}\" " . ((isset($_POST['currency']) && $_POST['currency'] === $currencySymbol) ? 'selected' : '') . ">{$currencyName}</option>", PHP_EOL;
44+
} ?>
45+
</select>
46+
</div>
47+
</div>
48+
<div class="mb-3 row">
49+
<label for="decimals" class="col-sm-2 col-form-label">Decimal Places</label>
50+
<div class="col-sm-10">
51+
<input name="decimals" type="number" size="2" min="0" max="14" value="<?php echo (isset($_POST['decimals'])) ? htmlentities($_POST['decimals'], Settings::htmlEntityFlags()) : '2'; ?>">
52+
</div>
53+
</div>
54+
<div class="mb-3 row">
55+
<label for="thousands" class="col-sm-2 col-form-label">Use Thousands Separator</label>
56+
<div class="col-sm-10">
57+
<input name="thousands" type="checkbox" <?php echo (isset($_POST['thousands'])) ? 'value="on"' : ''; ?> <?php echo (isset($_POST['thousands'])) ? 'checked' : ''; ?>>
58+
</div>
59+
</div>
60+
<div class="mb-3 row">
61+
<label for="position" class="col-sm-2 col-form-label">Currency Position</label>
62+
<div class="col-sm-10">
63+
<input name="position" type="radio" value="1" <?php echo ((isset($_POST['position']) === false) || (isset($_POST['position']) && $_POST['position'] === '1')) ? 'checked' : ''; ?>>Leading
64+
<input name="position" type="radio" value="0" <?php echo (isset($_POST['position']) && $_POST['position'] === '0') ? 'checked' : ''; ?>>Trailing
65+
</div>
66+
</div>
67+
<div class="mb-3 row">
68+
<label for="spacing" class="col-sm-2 col-form-label">Currency Spacing</label>
69+
<div class="col-sm-10">
70+
<input name="spacing" type="radio" value="1" <?php echo (isset($_POST['spacing']) && $_POST['spacing'] === '1') ? 'checked' : ''; ?>>Yes
71+
<input name="spacing" type="radio" value="0" <?php echo ((isset($_POST['spacing']) === false) || (isset($_POST['spacing']) && $_POST['spacing'] === '0')) ? 'checked' : ''; ?>>No
72+
</div>
73+
</div>
74+
<div class="mb-3 row">
75+
<div class="col-sm-10">
76+
<input class="btn btn-primary" name="submit" type="submit" value="Display Mask"><br />
77+
</div>
78+
</div>
79+
</form>
80+
81+
<?php
82+
/** If the user has submitted the form, then we need to use the wizard to build a mask and display the result */
83+
if (isset($_POST['submit'])) {
84+
if (!is_numeric($_POST['number'])) {
85+
$helper->log('The Sample Number Value must be numeric');
86+
} elseif (!is_numeric($_POST['decimals']) || strpos($_POST['decimals'], '.') !== false || (int) $_POST['decimals'] < 0) {
87+
$helper->log('The Decimal Places value must be positive integer');
88+
} else {
89+
try {
90+
$wizard = new Wizard\Accounting($_POST['currency'], $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']);
91+
$mask = $wizard->format();
92+
$example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask);
93+
$helper->log('<hr /><b>Code:</b><br />');
94+
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
95+
$helper->log(
96+
"\$mask = Wizard\\Accounting('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" .
97+
(isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR') .
98+
', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL') .
99+
', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING') .
100+
');<br />'
101+
);
102+
$helper->log('echo (string) $mask;');
103+
$helper->log('<hr /><b>Mask:</b><br />');
104+
$helper->log($mask . '<br />');
105+
$helper->log('<br /><b>Example:</b><br />');
106+
$helper->log($example);
107+
} catch (SpreadsheetException $e) {
108+
$helper->log("Exception: {$e->getMessage()}");
109+
}
110+
}
111+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
4+
use PhpOffice\PhpSpreadsheet\Helper\Sample;
5+
use PhpOffice\PhpSpreadsheet\Settings;
6+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
7+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
8+
9+
require __DIR__ . '/../../Header.php';
10+
11+
$helper = new Sample();
12+
if ($helper->isCli()) {
13+
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
14+
15+
return;
16+
}
17+
18+
$currencies = [
19+
'$' => 'US Dollars ($)',
20+
'' => 'Euro (€)',
21+
'¥' => 'Japanese Yen (¥)',
22+
'£' => 'Pound Sterling (£)',
23+
'' => 'Rupee (₹)',
24+
'' => 'Rouble (₽)',
25+
];
26+
27+
?>
28+
<form action="Currency.php" method="POST">
29+
<div class="mb-3 row">
30+
<label for="number" class="col-sm-2 col-form-label">Sample Number Value</label>
31+
<div class="col-sm-10">
32+
<input name="number" type="text" size="8" value="<?php echo (isset($_POST['number'])) ? htmlentities($_POST['number'], Settings::htmlEntityFlags()) : '1234.5678'; ?>">
33+
</div>
34+
</div>
35+
<div class="mb-3 row">
36+
<hr />
37+
</div>
38+
<div class="mb-3 row">
39+
<label for="currency" class="col-sm-2 col-form-label">Currency</label>
40+
<div class="col-sm-10">
41+
<select name="currency" class="form-select">
42+
<?php foreach ($currencies as $currencySymbol => $currencyName) {
43+
echo "<option value=\"{$currencySymbol}\" " . ((isset($_POST['currency']) && $_POST['currency'] === $currencySymbol) ? 'selected' : '') . ">{$currencyName}</option>", PHP_EOL;
44+
} ?>
45+
</select>
46+
</div>
47+
</div>
48+
<div class="mb-3 row">
49+
<label for="decimals" class="col-sm-2 col-form-label">Decimal Places</label>
50+
<div class="col-sm-10">
51+
<input name="decimals" type="number" size="2" min="0" max="14" value="<?php echo (isset($_POST['decimals'])) ? htmlentities($_POST['decimals'], Settings::htmlEntityFlags()) : '2'; ?>">
52+
</div>
53+
</div>
54+
<div class="mb-3 row">
55+
<label for="thousands" class="col-sm-2 col-form-label">Use Thousands Separator</label>
56+
<div class="col-sm-10">
57+
<input name="thousands" type="checkbox" <?php echo (isset($_POST['thousands'])) ? 'value="on"' : ''; ?> <?php echo (isset($_POST['thousands'])) ? 'checked' : ''; ?>>
58+
</div>
59+
</div>
60+
<div class="mb-3 row">
61+
<label for="position" class="col-sm-2 col-form-label">Currency Position</label>
62+
<div class="col-sm-10">
63+
<input name="position" type="radio" value="1" <?php echo ((isset($_POST['position']) === false) || (isset($_POST['position']) && $_POST['position'] === '1')) ? 'checked' : ''; ?>>Leading
64+
<input name="position" type="radio" value="0" <?php echo (isset($_POST['position']) && $_POST['position'] === '0') ? 'checked' : ''; ?>>Trailing
65+
</div>
66+
</div>
67+
<div class="mb-3 row">
68+
<label for="spacing" class="col-sm-2 col-form-label">Currency Spacing</label>
69+
<div class="col-sm-10">
70+
<input name="spacing" type="radio" value="1" <?php echo (isset($_POST['spacing']) && $_POST['spacing'] === '1') ? 'checked' : ''; ?>>Yes
71+
<input name="spacing" type="radio" value="0" <?php echo ((isset($_POST['spacing']) === false) || (isset($_POST['spacing']) && $_POST['spacing'] === '0')) ? 'checked' : ''; ?>>No
72+
</div>
73+
</div>
74+
<div class="mb-3 row">
75+
<div class="col-sm-10">
76+
<input class="btn btn-primary" name="submit" type="submit" value="Display Mask"><br />
77+
</div>
78+
</div>
79+
</form>
80+
81+
<?php
82+
/** If the user has submitted the form, then we need to use the wizard to build a mask and display the result */
83+
if (isset($_POST['submit'])) {
84+
if (!is_numeric($_POST['number'])) {
85+
$helper->log('The Sample Number Value must be numeric');
86+
} elseif (!is_numeric($_POST['decimals']) || strpos($_POST['decimals'], '.') !== false || (int) $_POST['decimals'] < 0) {
87+
$helper->log('The Decimal Places value must be positive integer');
88+
} else {
89+
try {
90+
$wizard = new Wizard\Currency($_POST['currency'], $_POST['decimals'], isset($_POST['thousands']), (bool) $_POST['position'], (bool) $_POST['spacing']);
91+
$mask = $wizard->format();
92+
$example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask);
93+
$helper->log('<hr /><b>Code:</b><br />');
94+
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
95+
$helper->log(
96+
"\$mask = Wizard\\Currency('{$_POST['currency']}', {$_POST['decimals']}, Wizard\\Number::" .
97+
(isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR') .
98+
', Wizard\Currency::' . (((bool) $_POST['position']) ? 'LEADING_SYMBOL' : 'TRAILING_SYMBOL') .
99+
', Wizard\Currency::' . (((bool) $_POST['spacing']) ? 'SYMBOL_WITH_SPACING' : 'SYMBOL_WITHOUT_SPACING') .
100+
');<br />'
101+
);
102+
$helper->log('echo (string) $mask;');
103+
$helper->log('<hr /><b>Mask:</b><br />');
104+
$helper->log($mask . '<br />');
105+
$helper->log('<br /><b>Example:</b><br />');
106+
$helper->log($example);
107+
} catch (SpreadsheetException $e) {
108+
$helper->log("Exception: {$e->getMessage()}");
109+
}
110+
}
111+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
4+
use PhpOffice\PhpSpreadsheet\Helper\Sample;
5+
use PhpOffice\PhpSpreadsheet\Settings;
6+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
7+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
8+
9+
require __DIR__ . '/../../Header.php';
10+
11+
$helper = new Sample();
12+
if ($helper->isCli()) {
13+
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
14+
15+
return;
16+
}
17+
?>
18+
<form action="Number.php" method="POST">
19+
<div class="mb-3 row">
20+
<label for="number" class="col-sm-2 col-form-label">Sample Number Value</label>
21+
<div class="col-sm-10">
22+
<input name="number" type="text" size="8" value="<?php echo (isset($_POST['number'])) ? htmlentities($_POST['number'], Settings::htmlEntityFlags()) : '1234.5678'; ?>">
23+
</div>
24+
</div>
25+
<div class="mb-3 row">
26+
<hr />
27+
</div>
28+
<div class="mb-3 row">
29+
<label for="decimals" class="col-sm-2 col-form-label">Decimal Places</label>
30+
<div class="col-sm-10">
31+
<input name="decimals" type="number" size="2" min="0" max="14" value="<?php echo (isset($_POST['decimals'])) ? htmlentities($_POST['decimals'], Settings::htmlEntityFlags()) : '2'; ?>">
32+
</div>
33+
</div>
34+
<div class="mb-3 row">
35+
<label for="thousands" class="col-sm-2 col-form-label">Use Thousands Separator</label>
36+
<div class="col-sm-10">
37+
<input name="thousands" type="checkbox" <?php echo (isset($_POST['thousands'])) ? 'value="on"' : ''; ?> <?php echo (isset($_POST['thousands'])) ? 'checked' : ''; ?>>
38+
</div>
39+
</div>
40+
<div class="mb-3 row">
41+
<div class="col-sm-10">
42+
<input class="btn btn-primary" name="submit" type="submit" value="Display Mask"><br />
43+
</div>
44+
</div>
45+
</form>
46+
47+
<?php
48+
/** If the user has submitted the form, then we need to use the wizard to build a mask and display the result */
49+
if (isset($_POST['submit'])) {
50+
if (!is_numeric($_POST['number'])) {
51+
$helper->log('The Sample Number Value must be numeric');
52+
} elseif (!is_numeric($_POST['decimals']) || strpos($_POST['decimals'], '.') !== false || (int) $_POST['decimals'] < 0) {
53+
$helper->log('The Decimal Places value must be positive integer');
54+
} else {
55+
try {
56+
$wizard = new Wizard\Number($_POST['decimals'], isset($_POST['thousands']));
57+
$mask = $wizard->format();
58+
$example = NumberFormat::toFormattedString((float) $_POST['number'], $mask);
59+
$helper->log('<hr /><b>Code:</b><br />');
60+
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
61+
$helper->log(
62+
"\$mask = Wizard\\Number({$_POST['decimals']}, Wizard\\Number::" .
63+
(isset($_POST['thousands']) ? 'WITH_THOUSANDS_SEPARATOR' : 'WITHOUT_THOUSANDS_SEPARATOR') .
64+
');<br />'
65+
);
66+
$helper->log('echo (string) $mask;');
67+
$helper->log('<hr /><b>Mask:</b>');
68+
$helper->log($mask);
69+
$helper->log('<b>Example:</b>');
70+
$helper->log($example);
71+
} catch (SpreadsheetException $e) {
72+
$helper->log("Exception: {$e->getMessage()}");
73+
}
74+
}
75+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
4+
use PhpOffice\PhpSpreadsheet\Helper\Sample;
5+
use PhpOffice\PhpSpreadsheet\Settings;
6+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
7+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
8+
9+
require __DIR__ . '/../../Header.php';
10+
11+
$helper = new Sample();
12+
if ($helper->isCli()) {
13+
$helper->log('This example should only be run from a Web Browser' . PHP_EOL);
14+
15+
return;
16+
}
17+
?>
18+
<form action="Percentage.php" method="POST">
19+
<div class="mb-3 row">
20+
<label for="number" class="col-sm-2 col-form-label">Sample Number Value</label>
21+
<div class="col-sm-10">
22+
<input name="number" type="text" size="8" value="<?php echo (isset($_POST['number'])) ? htmlentities($_POST['number'], Settings::htmlEntityFlags()) : '1234.5678'; ?>">
23+
</div>
24+
</div>
25+
<div class="mb-3 row">
26+
<hr />
27+
</div>
28+
<div class="mb-3 row">
29+
<label for="decimals" class="col-sm-2 col-form-label">Decimal Places</label>
30+
<div class="col-sm-10">
31+
<input name="decimals" type="number" size="2" min="0" max="14" value="<?php echo (isset($_POST['decimals'])) ? htmlentities($_POST['decimals'], Settings::htmlEntityFlags()) : '2'; ?>">
32+
</div>
33+
</div>
34+
<div class="mb-3 row">
35+
<div class="col-sm-10">
36+
<input class="btn btn-primary" name="submit" type="submit" value="Display Mask"><br />
37+
</div>
38+
</div>
39+
</form>
40+
41+
<?php
42+
/** If the user has submitted the form, then we need to use the wizard to build a mask and display the result */
43+
if (isset($_POST['submit'])) {
44+
if (!is_numeric($_POST['number'])) {
45+
$helper->log('The Sample Number Value must be numeric');
46+
} elseif (!is_numeric($_POST['decimals']) || strpos($_POST['decimals'], '.') !== false || (int) $_POST['decimals'] < 0) {
47+
$helper->log('The Decimal Places value must be positive integer');
48+
} else {
49+
try {
50+
$wizard = new Wizard\Percentage($_POST['decimals']);
51+
$mask = $wizard->format();
52+
$example = (string) NumberFormat::toFormattedString((float) $_POST['number'], $mask);
53+
$helper->log('<hr /><b>Code:</b><br />');
54+
$helper->log('use PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;');
55+
$helper->log("\$mask = Wizard\\Percentage({$_POST['decimals']});<br />");
56+
$helper->log('echo (string) $mask;');
57+
$helper->log('<hr /><b>Mask:</b><br />');
58+
$helper->log($mask . '<br />');
59+
$helper->log('<br /><b>Example:</b><br />');
60+
$helper->log($example);
61+
} catch (SpreadsheetException $e) {
62+
$helper->log("Exception: {$e->getMessage()}");
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)