Skip to content

Novis\Spreadsheet\Style::Align

Where the text of a cell is placed from left to right.

You set it with the style field align of Style::of. Style::of turns the string into a Style::Align case:

  • 'left' is Left, and 'right' is Right.
  • 'center' is Center. It is common for a header row.
  • 'fill' is Fill. Excel repeats the text until the cell is full.
  • 'justify' is Justify. Excel spreads the words of a text with several lines across the full width of the cell.

Any other string throws a LogicError. Without align, Excel puts text on the left and numbers on the right. The style field valign sets whether the text is at the top, the middle or the bottom of the cell.

Examples

Center the header

<?nvs
// Shows a header row with its text in the center of each cell.
use Novis\Spreadsheet\Style;
use Novis\Spreadsheet\Workbook;
// The style field `align` is a string. `Style::of` turns it into a `Style::Align` case.
// This prints "center".
var $header = Style::of({ bold: true, align: 'center' });
echo $header->fields->align == Style::Align::Center ? 'center' : 'not center', "\n";
var $book = Workbook::createInMemory();
$book->addSheet('Prices', { headerStyle: $header })->addRow(['item' => 'Tea', 'price' => 2.5]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// The file tells Excel where the text goes. This prints `true`.
var $styles = Core\Zip::read($file, 'xl/styles.xml') as string;
echo Core\Str::contains($styles, "<alignment horizontal=\"center\"/>") ? 'true' : 'false', "\n";
Output
center
true