Skip to content

Novis\Spreadsheet\Sheet::addValidation

$sheet->addValidation(string $range, Validation $rule): Sheet$sheet->addValidation(
    string $range,
    Validation $rule,
): Sheet

Checks what a person types into the cells of $range in Excel, and returns the sheet.

$range is a range, whole columns such as A:A, or several ranges with a space between them. $rule has exactly one kind of rule. list is the values a person can choose from. whole allows a whole number, decimal any number, date a date and length a text of a given length. Each of these four takes min, max or both. formula is an Excel formula that must be true. input and inputTitle are a message Excel shows when the cell is selected. error and errorTitle are the message for a wrong value. allowEmpty is true unless you set it to false.

Good to know: Excel checks only what a person types. Your program must still check every value. A rule with no kind, or with two kinds, throws a LogicError. So does a number rule without min and max, and a comma in a list value. A CSV file has no validation, so addValidation does nothing there.

The examples below offer a list of choices, allow a whole number between two limits, and show the error for a rule with two kinds.

Parameters

  • $rangestring
  • $ruleValidation

Return value

Returns Sheet.

Changelog

VersionChange
0.0.6Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart.

Examples

A list of choices

<?nvs
// Shows how to let a person choose the status of an order from a list.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([['order' => 1042, 'status' => 'open'], ['order' => 1043, 'status' => 'paid']]);
// Excel shows a list with these three values in each cell from B2 to B100.
$sheet->addValidation('B2:B100', { list: ['open', 'paid', 'sent'] });
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. This line checks that the list is in it.
var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;
echo Core\Str::contains($xml, '<formula1>"open,paid,sent"</formula1>') ? 'the list is in the file' : 'no list', "\n";
Output
the list is in the file

A whole number between two limits

<?nvs
// Shows how to allow only a whole number from 1 to 99, with a message for a wrong value.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Order form');
$sheet->addRow(['item' => 'Widget', 'qty' => 1]);
// `input` shows when a person selects the cell. `error` shows when the value is wrong.
$sheet->addValidation('B2:B50', {
whole: { min: 1, max: 99 },
input: 'Type a number from 1 to 99.',
errorTitle: 'Wrong quantity',
error: 'The quantity is a whole number from 1 to 99.',
});
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. These lines check the limits and the message.
var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;
echo Core\Str::contains($xml, '<formula1>1</formula1><formula2>99</formula2>') ? 'from 1 to 99' : 'no limits', "\n";
echo Core\Str::contains($xml, 'errorTitle="Wrong quantity"') ? 'the error has a title' : 'no title', "\n";
Output
from 1 to 99
the error has a title

A rule with two kinds

<?nvs
// Shows the error for a rule with two kinds, and for a number rule without limits.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
// A rule has exactly one of `list`, `whole`, `decimal`, `date`, `length` or `formula`.
try {
$sheet->addValidation('B2:B9', { list: ['open', 'paid'], whole: { min: 1 } });
} catch (LogicError $e) {
echo $e->message, "\n";
}
// `decimal` needs `min`, `max` or both.
try {
$sheet->addValidation('C2:C9', { decimal: {} });
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The validation of `B2:B9` needs one of `list`, `whole`, `decimal`, `date`, `length` or `formula`. It has 2.
The validation of `C2:C9` needs `min`, `max` or both.