Novis\Spreadsheet\Reader::macros
Returns the names of the macro modules in the workbook.
A macro is a small program that Excel can run, written in the language VBA. An xlsm or xls file can have macros. An xlsx or CSV file has none, so the list is empty. Each name in the list is a tainted string, because it comes from the file.
Good to know: reading a file never runs its macros, and macros reads only their names, not their code. Use macros to check an upload before you import it. For example, you can reject a file with macros and ask the person to save it as xlsx. A damaged macro part throws a ParseError.
The examples below read a workbook without macros, reject an upload that has macros, and check a CSV file.
Return value
Returns array<tainted string>.
Examples
A workbook without macros
<?nvs// Shows that `macros` returns an empty list for a workbook without macros.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
bytes $file = Workbook::fromRows([['item' => 'Widget', 'price' => 2.5]])->bytes();
// An xlsx file cannot have macros. So the list is empty.array<tainted string> $macros = Reader::open($file)->macros();echo Core\Arr::count($macros), " macro modules\n";0 macro modulesReject an upload with macros
<?nvs// Shows how to check an upload for macros before you import it.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
class Import{ // This method returns a message for the person who uploaded the file. public static function check(tainted bytes $upload): string { Reader $reader = Reader::open($upload); uint $count = Core\Arr::count($reader->macros()); if ($count > 0) { return "The file has {$count} macro modules. Save it as xlsx and upload it again."; } return 'The file has no macros. The import can start.'; }}
// This makes the upload. In a real program the bytes come from the request.bytes $upload = Workbook::fromRows([['item' => 'Widget']])->bytes();echo Import::check($upload), "\n";The file has no macros. The import can start.A csv file has no macros
<?nvs// Shows that `macros` works for every file type, and a CSV file has no macros.use Novis\Spreadsheet\Reader;
// A CSV file is only text. So the list is empty, and the same check works for every upload.bytes $file = "Item,Price\nWidget,2.50\n" as bytes;echo Core\Arr::count(Reader::open($file)->macros()), " macro modules\n";0 macro modules