PHP code example of emilianozublena / sheetsu-php

1. Go to this page and download the library: Download emilianozublena/sheetsu-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

emilianozublena / sheetsu-php example snippets



use Sheetsu\Sheetsu;

$sheetsu = new Sheetsu([
    'sheetId' => 'sheetId'
]);


use Sheetsu\Sheetsu;

$sheetsu = new Sheetsu([
    'sheetId'   => 'sheetId',
    'key'       => 'key',
    'secret'    => 'secret'
]);

# Initialize after creation
$sheetsu = new Sheetsu();
$sheetsu->initialize([
    'sheetId'   => 'sheetId',
    'key'       => 'key',
    'secret'    => 'secret'
])

$collection = new Collection();
$collection->addMultiple([
    Model::create(['name' => 'John']),
    Model::create(['name' => 'Steve'])
]);
$response = $sheetsu->create($collection);

# Adds single row from array
$sheetsu->create(['name' => 'John']);
# Adds multiple rows from array
$sheetsu->create([
    ['name' => 'John'],
    ['name' => 'Steve']
]);
# Adds single row from Model
$sheetsu->create(Model::create(['name' => 'John']));
# Adds multiple rows from Collection
$collection = new Collection();
$collection->addMultiple([
    Model::create(['name' => 'John']),
    Model::create(['name' => 'Steve'])
]);
$response = $sheetsu->create($collection);

$response = $sheetsu->read();
$collection = $response->getCollection();

# Get first two rows from sheet starting from the first row
$response = $sheetsu->read(2, 0);
$collection = $response->getCollection();

# Get all rows where column 'id' is 'foo' and column 'value' is 'bar'
$response = $sheetsu->search([
    'id'    => 'foo',
    'value' => 'bar'
]);
$collection = $response->getCollection();

# Get all rows where column 'First name' is 'Peter' and column 'Score' is '42'
$response = $sheetsu->search([
    'First name'    => 'Peter',
    'Score'         => '42'
]);
$collection = $response->getCollection();

# Get first two row where column 'First name' is 'Peter',
# column 'Score' is '42' from sheet named "Sheet3"
$response = $sheetsu->search([
    'First name'    => 'Peter',
    'Score'         => '42'
], 2, 0);
$collection = $response->getCollection();

# Get first two row where column 'First name' is 'Peter',
# column 'Score' is '42' from sheet named "Sheet3"
# with ignore case
$response = $sheetsu->search([
    'First name'    => 'Peter',
    'Score'         => '42'
], 2, 0, true);
$collection = $response->getCollection();

# Update all columns where 'name' is 'Peter' to have 'score' = 99 and 'last name' = 'Griffin'
$model = Model::create(['score' => '99', 'last name' => 'Griffin']);
$response = $sheetsu->update('name', 'Peter', $model);

# Delete all rows where 'name' equals 'Peter'
$response = $sheetsu->delete('name', 'Peter');

# Change active sheetsu to 'THIS'
$sheetsu->sheet('THIS')

# Change active sheetsu to 'THIS'
$sheetsu->sheet('THIS')->read();

# Back to whole spreadsheet
$sheetsu->whole()

# Back to whole spreadsheet
$sheetsu->whole()->read();

$response = $sheetsu->read();
#if you need only the error messages, you can get the errors like this
$errors = $response->getErrors();
$firstError = $response->getError();
#if you need to get the exceptions thrown, do it like this.
$exceptions = $response->getExceptions();
$firstException = $response->getException();

composer