Skip to content

Commit

Permalink
Fix issue with an exception raised when there there is no extra colum…
Browse files Browse the repository at this point in the history
…ns / rows to delete.[#9]
  • Loading branch information
nbourguig committed Apr 26, 2017
1 parent 385d293 commit bcac640
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/Sheet/TranslationsSheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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();
}
}
Expand Down

0 comments on commit bcac640

Please sign in to comment.