Skip to content

Commit 4cf98db

Browse files
committed
Added try/catch in get_sheet_raw_values() to prevent fatal errors if an error occurs when communicating with Google.
1 parent a3840a1 commit 4cf98db

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

class-object-type-google-sheet.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,38 @@ public function get_sheet_raw_values( $spreadsheet_id, $sheet = null ) {
126126
return $this->sheet_values_runtime_cache[ $spreadsheet_id ];
127127
}
128128

129-
$spreadsheet = $this->service_sheets->spreadsheets->get( $spreadsheet_id );
130-
$sheets = $spreadsheet->getSheets();
129+
try {
130+
$spreadsheet = $this->service_sheets->spreadsheets->get( $spreadsheet_id );
131+
$sheets = $spreadsheet->getSheets();
132+
133+
// Limitation: use first available sheet. Ideally we would have the ability to drill down in primary properties
134+
// @todo maybe we list all spreadsheets and their sheets as primary properties?
135+
if ( ! $sheet ) {
136+
$sheet = apply_filters( 'gppa_google_sheets_selected_sheet', $sheets[0], $sheets, $spreadsheet_id );
137+
}
131138

132-
// Limitation: use first available sheet. Ideally we would have the ability to drill down in primary properties
133-
// @todo maybe we list all spreadsheets and their sheets as primary properties?
134-
if ( ! $sheet ) {
135-
$sheet = apply_filters( 'gppa_google_sheets_selected_sheet', $sheets[0], $sheets, $spreadsheet_id );
136-
}
139+
$response = $this->service_sheets->spreadsheets_values->get( $spreadsheet_id, $sheet->getProperties()->title );
140+
$values = $response->getValues();
137141

138-
$response = $this->service_sheets->spreadsheets_values->get( $spreadsheet_id, $sheet->getProperties()->title );
139-
$values = $response->getValues();
142+
foreach ( $values as $value_index => $value ) {
143+
if ( $value_index === 0 ) {
144+
array_unshift( $values[ $value_index ], self::ROW_NUMBER_ID );
140145

141-
foreach ( $values as $value_index => $value ) {
142-
if ( $value_index === 0 ) {
143-
array_unshift( $values[ $value_index ], self::ROW_NUMBER_ID );
146+
continue;
147+
}
144148

145-
continue;
149+
// Add row number to values to serve as the ID of the object.
150+
array_unshift( $values[ $value_index ], $value_index );
146151
}
147152

148-
// Add row number to values to serve as the ID of the object.
149-
array_unshift( $values[ $value_index ], $value_index );
150-
}
151-
152-
$this->sheet_values_runtime_cache[ $spreadsheet_id ] = $values;
153+
$this->sheet_values_runtime_cache[ $spreadsheet_id ] = $values;
153154

154-
return $values;
155+
return $values;
156+
} catch ( Exception $e ) {
157+
error_log( 'Unable to fetch from Google Sheets: ' . $e->getMessage() );
155158

159+
return array();
160+
}
156161
}
157162

158163
/**

0 commit comments

Comments
 (0)