asimlqt / php-google-spreadsheet-client

A PHP library for accessing and manipulating Google Spreadsheets
Other
543 stars 154 forks source link

How do you do a batch? #100

Closed hdwebpros closed 8 years ago

hdwebpros commented 9 years ago

I need to add multiple (few hundreds) rows into google spreadsheet. Currently I'm doing it in a loop which is extremely slow, because rows are added one by one. How can I do a batch request using this?

lauramoraes commented 9 years ago

+1! I have the same problem!

dneykov commented 8 years ago

anybody found solution for this? Thanks

asimlqt commented 8 years ago

You need to retrieve a CellFeed first. Once you have that you create CellEntry's and add them to a BatchRequest. Finally upload the batch using the CellFeed class. e.g.

$serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($accessToken);
Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);

$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
$spreadsheet = $spreadsheetFeed->getByTitle("test");

$worksheet = $spreadsheet->getWorksheets()->getByTitle('Sheet1');
$cellFeed = $worksheet->getCellFeed();

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

$batchRequest->addEntry($cellFeed->createInsertionCell(2, 1, "row 2 cell 1 content"));
$batchRequest->addEntry($cellFeed->createInsertionCell(3, 1, "row 3 cell 1 content"));
$batchRequest->addEntry($cellFeed->createInsertionCell(4, 1, "row 4 cell 1 content"));

$batchResponse = $cellFeed->insertBatch($batchRequest);

if($batchResponse->hasErrors()) {
    //
}
sagark1510 commented 8 years ago

@asimlqt I have tried your solution and it works to add cells in batch but after that if I try to add a new row using the code below.

 $serviceRequest = new Google\Spreadsheet\DefaultServiceRequest($accessToken);
 Google\Spreadsheet\ServiceRequestFactory::setInstance($serviceRequest);

 $spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
 $spreadsheetFeed = $spreadsheetService->getSpreadsheets();
 $spreadsheet = $spreadsheetFeed->getByTitle("test");

 $worksheet = $spreadsheet->getWorksheets()->getByTitle('Sheet1');
 $cellFeed = $worksheet->getCellFeed();

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

 $batchRequest->addEntry($cellFeed->createInsertionCell(1, 1, "Name"));
 $batchRequest->addEntry($cellFeed->createInsertionCell(1, 2, "Number"));
 $batchRequest->addEntry($cellFeed->createInsertionCell(1, 3, "Email"));

 $batchResponse = $cellFeed->insertBatch($batchRequest);

 if($batchResponse->hasErrors()) {
     //
 }

 $listFeed = $worksheet->getListFeed();
 $row = array(
      "name" => "sagar",
      "number" => "549679985904",
      "email" => 'abc@abc.com'
 );

 $listFeed->insert($row);

It gives me 'Error in Google Request' error. Any idea why it's happening?

asimlqt commented 8 years ago

@sagark1510 I'm not sure why off the top of my head, the code seems ok to me.

I'll look into it and let you know what I find.

asimlqt commented 8 years ago

@sagark1510

Thanks for spotting this. It has now been fixed and tagged as 2.3.6