Skip to content
Snippets Groups Projects
Commit d922eed1 authored by Paulo Calado's avatar Paulo Calado Committed by Paulo Calado
Browse files

Issue #3340870 by kallado: Return empty array on Exception in function getdata

parent a0d2f06c
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,7 @@ use Google_Client;
use Google_Service_Sheets;
use Drupal\file\Entity\File;
use Google_Service_Drive;
use Google\Service\Exception;
/**
* Class for the service javali_sheets.base .
......@@ -28,7 +29,7 @@ class SpreadSheets {
*
* @var string
*/
private $sheetId;
private $sheet_id;
/**
* The range from config.
......@@ -86,7 +87,7 @@ class SpreadSheets {
* @return array
* an array with the sheet data
*/
public function getdata() {
public function getdata($format = 'FORMATTED_VALUE') {
$client = $this->client;
$service = new \Google_Service_Sheets($client);
$spreadsheetId = $this->sheet_id;
......@@ -107,17 +108,22 @@ class SpreadSheets {
if (!$this->myArrayContainsWord($idCats, $name)) {
return $messenger->addMessage('Sheet not found', 'error');
}
$options = [
'valueRenderOption' => $format
];
$range_format = !empty($this->range) ? $this->sheetName . '!' . $this->range : $this->sheetName;
$response = $service->spreadsheets_values->get($spreadsheetId, $range_format);
$response = $service->spreadsheets_values->get($spreadsheetId, $range_format,$options);
if(!empty($response->values)) {
return $response->values;
}
}
catch (\Google_Service_Exception $e) {
$error = json_decode($e->getMessage(), TRUE);
return $messenger->addMessage($error['error']['message'], 'error');
catch (Exception $e) {
$message = json_decode($e->getMessage(),true);
$messenger->addMessage($message['error']['message'], 'error');
return [];
}
return $messenger->addMessage('An error ocurred!', 'error');
$messenger->addMessage('An error ocurred!', 'error');
return [];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment