From bcac640aa945c14d43b07e73a97b96d4d7e32e9a Mon Sep 17 00:00:00 2001 From: Nassif Bourguig Date: Wed, 26 Apr 2017 11:19:46 +0200 Subject: [PATCH] Fix issue with an exception raised when there there is no extra columns / rows to delete.[#9] --- src/Sheet/TranslationsSheet.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Sheet/TranslationsSheet.php b/src/Sheet/TranslationsSheet.php index 930a9d6..93729b6 100644 --- a/src/Sheet/TranslationsSheet.php +++ b/src/Sheet/TranslationsSheet.php @@ -75,10 +75,6 @@ public function styleDocument() $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->groupColumnIndex(), $this->coordinates()->groupColumnIndex() + 1, 80), $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->keyColumnIndex(), $this->coordinates()->keyColumnIndex() + 1, 240), $this->spreadsheet->api()->fixedColumnWidthRequest($this->getId(), $this->coordinates()->sourceFileColumnIndex(), $this->coordinates()->sourceFileColumnIndex() + 1, 360), - - // Delete extra columns and rows - $this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()), - $this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()), ]; // Fixed locales translations column width @@ -90,6 +86,17 @@ public function styleDocument() // Send requests $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); + + // Delete extra columns and rows if any + try { + $this->spreadsheet->api()->addBatchRequests([ + $this->spreadsheet->api()->deleteRowsFrom($this->getId(), $this->coordinates()->getRowsCount()), + $this->spreadsheet->api()->deleteColumnsFrom($this->getId(), $this->coordinates()->getColumnsCount()), + ])->sendBatchRequests(); + } catch (\Google_Service_Exception $e) { + // If there is no extra columns or rows Google api will raise an exception : + // ... Invalid requests[xxx].deleteDimension: Cannot delete a column that doesn't exist ... + } } public function prepareForWrite() @@ -130,7 +137,7 @@ public function isTranslationsLocked() { $protectedRanges = $this->spreadsheet->api()->getSheetProtectedRanges($this->getId(), 'TRANSLATIONS'); - return ! empty($protectedRanges) && count($protectedRanges) > 0; + return !empty($protectedRanges) && count($protectedRanges) > 0; } public function removeAllProtectedRanges() @@ -142,7 +149,7 @@ public function removeAllProtectedRanges() $requests[] = $this->spreadsheet->api()->deleteProtectedRange($protectedRange->protectedRangeId); } - if (! empty($requests)) { + if (!empty($requests)) { $this->spreadsheet->api()->addBatchRequests($requests)->sendBatchRequests(); } }