Skip to content

Instantly share code, notes, and snippets.

@qazd
Created December 13, 2016 09:01
Show Gist options
  • Save qazd/c3a2f2a8161358d6274474b44f274421 to your computer and use it in GitHub Desktop.
Save qazd/c3a2f2a8161358d6274474b44f274421 to your computer and use it in GitHub Desktop.
Google Spreadsheet cell inserting memory overhead fix.

How to use it

$spreadsheet = 'MyTable.xlsx';
$sheet = 'List1';

$service = new Google\Spreadsheet\SpreadsheetService();

$worksheet = $service->getSpreadsheets()
    ->getByTitle($spreadsheet)
    ->getWorksheets()
    ->getByTitle($sheet);
            
$cellFeed = new Google\Spreadsheet\CellFeedInsert($worksheet);

$batchRequest = new Google\Spreadsheet\Batch\BatchRequest();

$batchRequest->addEntry($cellFeed->createCell(2, 1, '111'));
$batchRequest->addEntry($cellFeed->createCell(3, 1, '222'));
$batchRequest->addEntry($cellFeed->createCell(4, 1, '333'));
$batchRequest->addEntry($cellFeed->createCell(5, 1, '=SUM(A2:A4)'));
$batchRequest->addEntry($cellFeed->createCell(100, 1, 'Very important cell'));

$cellFeed->insertBatch($batchRequest);
<?php
namespace Google\Spreadsheet;
use Google\Spreadsheet\CellFeed;
use Google\Spreadsheet\Worksheet;
/**
* Insert data to sheet without loading sheet's content to memory
* Work only with data you want to add to sheet.
*/
class CellFeedInsert extends CellFeed
{
/**
* @param Worksheet $worksheet
*/
public function __construct(Worksheet $worksheet)
{
$this->worksheet = $worksheet;
}
/**
* Get the feed post url
*
* @return string
*/
public function getPostUrl()
{
return $this->worksheet->getCellFeedUrl();
}
/**
* Get the feed post url for batch inserting
*
* @return string
*/
public function getBatchUrl()
{
return $this->getPostUrl() . '/batch';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment