airflow.providers.google.suite.hooks.sheets

This module contains a Google Sheets API hook

Module Contents

class airflow.providers.google.suite.hooks.sheets.GSheetsHook(gcp_conn_id: str = 'google_cloud_default', api_version: str = 'v4', delegate_to: Optional[str] = None, impersonation_chain: Optional[Union[str, Sequence[str]]] = None)[source]

Bases: airflow.providers.google.common.hooks.base_google.GoogleBaseHook

Interact with Google Sheets via Google Cloud connection Reading and writing cells in Google Sheet: https://developers.google.com/sheets/api/guides/values

Parameters
  • gcp_conn_id (str) – The connection ID to use when fetching connection info.

  • api_version (str) – API Version

  • delegate_to (str) – The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled.

  • impersonation_chain (Union[str, Sequence[str]]) – Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account.

get_conn(self)[source]

Retrieves connection to Google Sheets.

Returns

Google Sheets services object.

Return type

Any

get_values(self, spreadsheet_id: str, range_: str, major_dimension: str = 'DIMENSION_UNSPECIFIED', value_render_option: str = 'FORMATTED_VALUE', date_time_render_option: str = 'SERIAL_NUMBER')[source]

Gets values from Google Sheet from a single range https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with

  • range (str) – The A1 notation of the values to retrieve.

  • major_dimension (str) – Indicates which dimension an operation should apply to. DIMENSION_UNSPECIFIED, ROWS, or COLUMNS

  • value_render_option (str) – Determines how values should be rendered in the output. FORMATTED_VALUE, UNFORMATTED_VALUE, or FORMULA

  • date_time_render_option (str) – Determines how dates should be rendered in the output. SERIAL_NUMBER or FORMATTED_STRING

Returns

An array of sheet values from the specified sheet.

Return type

List

batch_get_values(self, spreadsheet_id: str, ranges: List, major_dimension: str = 'DIMENSION_UNSPECIFIED', value_render_option: str = 'FORMATTED_VALUE', date_time_render_option: str = 'SERIAL_NUMBER')[source]

Gets values from Google Sheet from a list of ranges https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with

  • ranges (List) – The A1 notation of the values to retrieve.

  • major_dimension (str) – Indicates which dimension an operation should apply to. DIMENSION_UNSPECIFIED, ROWS, or COLUMNS

  • value_render_option (str) – Determines how values should be rendered in the output. FORMATTED_VALUE, UNFORMATTED_VALUE, or FORMULA

  • date_time_render_option (str) – Determines how dates should be rendered in the output. SERIAL_NUMBER or FORMATTED_STRING

Returns

Google Sheets API response.

Return type

Dict

update_values(self, spreadsheet_id: str, range_: str, values: List, major_dimension: str = 'ROWS', value_input_option: str = 'RAW', include_values_in_response: bool = False, value_render_option: str = 'FORMATTED_VALUE', date_time_render_option: str = 'SERIAL_NUMBER')[source]

Updates values from Google Sheet from a single range https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with.

  • range (str) – The A1 notation of the values to retrieve.

  • values (List) – Data within a range of the spreadsheet.

  • major_dimension (str) – Indicates which dimension an operation should apply to. DIMENSION_UNSPECIFIED, ROWS, or COLUMNS

  • value_input_option (str) – Determines how input data should be interpreted. RAW or USER_ENTERED

  • include_values_in_response (bool) – Determines if the update response should include the values of the cells that were updated.

  • value_render_option (str) – Determines how values should be rendered in the output. FORMATTED_VALUE, UNFORMATTED_VALUE, or FORMULA

  • date_time_render_option (str) – Determines how dates should be rendered in the output. SERIAL_NUMBER or FORMATTED_STRING

Returns

Google Sheets API response.

Return type

Dict

batch_update_values(self, spreadsheet_id: str, ranges: List, values: List, major_dimension: str = 'ROWS', value_input_option: str = 'RAW', include_values_in_response: bool = False, value_render_option: str = 'FORMATTED_VALUE', date_time_render_option: str = 'SERIAL_NUMBER')[source]

Updates values from Google Sheet for multiple ranges https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with

  • ranges (List) – The A1 notation of the values to retrieve.

  • values (List) – Data within a range of the spreadsheet.

  • major_dimension (str) – Indicates which dimension an operation should apply to. DIMENSION_UNSPECIFIED, ROWS, or COLUMNS

  • value_input_option (str) – Determines how input data should be interpreted. RAW or USER_ENTERED

  • include_values_in_response (bool) – Determines if the update response should include the values of the cells that were updated.

  • value_render_option (str) – Determines how values should be rendered in the output. FORMATTED_VALUE, UNFORMATTED_VALUE, or FORMULA

  • date_time_render_option (str) – Determines how dates should be rendered in the output. SERIAL_NUMBER or FORMATTED_STRING

Returns

Google Sheets API response.

Return type

Dict

append_values(self, spreadsheet_id: str, range_: str, values: List, major_dimension: str = 'ROWS', value_input_option: str = 'RAW', insert_data_option: str = 'OVERWRITE', include_values_in_response: bool = False, value_render_option: str = 'FORMATTED_VALUE', date_time_render_option: str = 'SERIAL_NUMBER')[source]

Append values from Google Sheet from a single range https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with

  • range (str) – The A1 notation of the values to retrieve.

  • values (List) – Data within a range of the spreadsheet.

  • major_dimension (str) – Indicates which dimension an operation should apply to. DIMENSION_UNSPECIFIED, ROWS, or COLUMNS

  • value_input_option (str) – Determines how input data should be interpreted. RAW or USER_ENTERED

  • insert_data_option (str) – Determines how existing data is changed when new data is input. OVERWRITE or INSERT_ROWS

  • include_values_in_response (bool) – Determines if the update response should include the values of the cells that were updated.

  • value_render_option (str) – Determines how values should be rendered in the output. FORMATTED_VALUE, UNFORMATTED_VALUE, or FORMULA

  • date_time_render_option (str) – Determines how dates should be rendered in the output. SERIAL_NUMBER or FORMATTED_STRING

Returns

Google Sheets API response.

Return type

Dict

clear(self, spreadsheet_id: str, range_: str)[source]

Clear values from Google Sheet from a single range https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/clear

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with

  • range (str) – The A1 notation of the values to retrieve.

Returns

Google Sheets API response.

Return type

Dict

batch_clear(self, spreadsheet_id: str, ranges: list)[source]

Clear values from Google Sheet from a list of ranges https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchClear

Parameters
  • spreadsheet_id (str) – The Google Sheet ID to interact with

  • ranges (List) – The A1 notation of the values to retrieve.

Returns

Google Sheets API response.

Return type

Dict

get_spreadsheet(self, spreadsheet_id: str)[source]

Retrieves spreadsheet matching the given id.

Parameters

spreadsheet_id (str) – The spreadsheet id.

Returns

An spreadsheet that matches the sheet filter.

get_sheet_titles(self, spreadsheet_id: str, sheet_filter: Optional[List[str]] = None)[source]

Retrieves the sheet titles from a spreadsheet matching the given id and sheet filter.

Parameters
  • spreadsheet_id (str) – The spreadsheet id.

  • sheet_filter (List[str]) – List of sheet title to retrieve from sheet.

Returns

An list of sheet titles from the specified sheet that match the sheet filter.

create_spreadsheet(self, spreadsheet: Dict[str, Any])[source]

Creates a spreadsheet, returning the newly created spreadsheet.

Parameters

spreadsheet (Dict[str, Any]) – an instance of Spreadsheet https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#Spreadsheet

Returns

An spreadsheet object.

Was this entry helpful?