baptouuuu / spreadsheet
Spreadsheet object representation
1.0.0
2016-07-16 14:07 UTC
Requires
- php: ~7.0
- innmind/filesystem: ^1.2
- innmind/immutable: ^1.9
This package is auto-updated.
Last update: 2024-12-05 19:19:47 UTC
README
Library providing an object modeling of a spreadsheet (all objects are immutable). It comes with objects to build a spreadsheet out of a file and dump a spreadsheet into a file.
Installation
composer require baptouuuu/spreadsheet
Usage
use Spreadsheet\{ Spreadsheet, Sheet, Cell, Position }; $sheet = new Sheet('sheet 1'); $sheet = $sheet ->add( new Cell( new Position('A', 1), 'A1 value' ) ) ->add( new Cell( new Position('B', 2), 'B2 value' ) ); $spreadsheet = new Spreadsheet('My Spreadsheet'); $spreadsheet = $spreadsheet->add($sheet);
Transform a spreadsheet to a CSV
use Spreadsheet\{ Writer\CsvWriter, Formatter\FormatterInterface, File\Csv }; use Innmind\Immutable\Map; $writer = new CsvWriter(';', true, new Map('string', FormatterInterface::class)); $file = $writer->write($spreadsheet); $file instanceof Csv; //true (string) $file->name(); //sheet 1.csv (string) $file->content(); /* A;B A1; ;B2 */
In case your spreadsheet contains more than one sheet, the writer will return an instance of DirectoryInterface
.
Building a spreadsheet from a CSV
use Spreadsheet\{ Reader\CsvReader, File\Csv, Position }; use Innmind\Filesystem\Stream\StringStream; $reader = new CsvReader(';', true); $spreadsheet = $reader->read( new Csv( 'sheet.csv', new StringStream(<<<CSV A;B A1;B1 CSV ) ) ); $spreadsheet->name(); //sheet $spreadsheet->get('sheet')->name(); //sheet $spreadsheet->get('sheet')->get(new Position('A', 1))->value(); //A1 $spreadsheet->get('sheet')->get(new Position('B', 1))->value(); //B1
If you give an instance of DirectoryInterface
to the reader, each file will be a sheet in your spreadhseet.