From 3a6b49ca5884e1e5e2f7a3fe8d4f6ea76719564e Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Tue, 31 Oct 2023 19:38:01 +0100 Subject: [PATCH 1/4] feat(Google Sheets): Add List sheets from spreadsheet --- ..._Sheets_List_sheets_from_spreadsheet.ipynb | 241 ++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb diff --git a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb new file mode 100644 index 0000000000..ff70f1f4b8 --- /dev/null +++ b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb @@ -0,0 +1,241 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "0fb936be-f97e-4e36-a37c-51cf40f58811", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "\"Naas\"" + ] + }, + { + "cell_type": "markdown", + "id": "a122cf90-c6db-4305-98bb-2af8b05cbb99", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "# Google Sheets - List sheets from spreadsheet" + ] + }, + { + "cell_type": "markdown", + "id": "4fa24a26-48ce-4221-a968-27bdb5ab43fc", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Tags:** #googlesheets #spreadsheet #list #sheets #python #api" + ] + }, + { + "cell_type": "markdown", + "id": "c1b9ade5-1eb1-4016-8830-f5b73b9d5094", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" + ] + }, + { + "cell_type": "markdown", + "id": "bcfdb331-57e6-4644-9817-1de75d42cc81", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Last update:** 2023-10-31 (Created: 2023-10-31)" + ] + }, + { + "cell_type": "markdown", + "id": "42a1082c-3359-44ad-9222-2a362ad5d19b", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**Description:** This notebook will list all the sheets from a Google Sheets spreadsheet. It is usefull for organizations to quickly get an overview of the content of a spreadsheet." + ] + }, + { + "cell_type": "markdown", + "id": "7c01b8e3-87a8-4084-bed4-0d46c1a4a6c0", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "**References:**\n- [Google Sheets API Documentation](https://developers.google.com/sheets/api/reference/rest/)\n- [Google Sheets API Python Quickstart](https://developers.google.com/sheets/api/quickstart/python)" + ] + }, + { + "cell_type": "markdown", + "id": "f1f32a63-4c71-4dfa-87c0-e31ba4d47803", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Input" + ] + }, + { + "cell_type": "markdown", + "id": "55dd39a3-3c5c-4aae-aab5-b1101a2ffa54", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Import libraries" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "479eaaab-3665-44e7-b89b-50e0a16524ab", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "import os\nimport pickle\nimport googleapiclient.discovery\nfrom google_auth_oauthlib.flow import InstalledAppFlow\nfrom google.auth.transport.requests import Request", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "a6e10857-d5f1-454e-ae40-49a88da3be43", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Setup variables\n- **SCOPES**: `https://www.googleapis.com/auth/spreadsheets.readonly`\n- **SPREADSHEET_ID**: ID of the spreadsheet to list sheets from" + ] + }, + { + "cell_type": "markdown", + "id": "b6e27fa6-2284-4e87-ab11-c910613ec935", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Model" + ] + }, + { + "cell_type": "markdown", + "id": "7af9afc5-e1ef-4017-87f2-f86abb168636", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### List sheets from spreadsheet" + ] + }, + { + "cell_type": "markdown", + "id": "96f2b867-e6cc-47c2-8ac9-337f18949307", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "This function will list all the sheets from a Google Sheets spreadsheet." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96de32a5-3a25-43bd-9443-2108afcccacb", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "def list_sheets(spreadsheet_id):\n \"\"\"\n List all the sheets from a Google Sheets spreadsheet.\n\n Parameters:\n spreadsheet_id (str): ID of the spreadsheet to list sheets from\n\n Returns:\n list: list of sheets\n \"\"\"\n # Setup credentials\n SCOPES = [\"https://www.googleapis.com/auth/spreadsheets.readonly\"]\n creds = None\n if os.path.exists(\"token.pickle\"):\n with open(\"token.pickle\", \"rb\") as token:\n creds = pickle.load(token)\n if not creds or not creds.valid:\n if creds and creds.expired and creds.refresh_token:\n creds.refresh(Request())\n else:\n flow = InstalledAppFlow.from_client_secrets_file(\"credentials.json\", SCOPES)\n creds = flow.run_local_server(port=0)\n with open(\"token.pickle\", \"wb\") as token:\n pickle.dump(creds, token)\n # List sheets\n service = googleapiclient.discovery.build(\"sheets\", \"v4\", credentials=creds)\n sheets = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()\n return sheets[\"sheets\"]", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "ae2d5a0c-61c5-45d1-854d-18887212df68", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "## Output" + ] + }, + { + "cell_type": "markdown", + "id": "ab4add94-cfe8-4438-a163-caffd212ac73", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + "### Display result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c3f9cbc0-558c-4997-932b-6137ebdf9821", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": "# Setup variables\nSPREADSHEET_ID = \"1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms\"\n# List sheets\nsheets = list_sheets(SPREADSHEET_ID)\n# Display result\nprint(f\"Sheets from spreadsheet {SPREADSHEET_ID}:\")\nfor sheet in sheets:\n print(f'- {sheet[\"properties\"][\"title\"]}')", + "outputs": [] + }, + { + "cell_type": "markdown", + "id": "9765e8cd-4a40-42a9-b6a0-c3970ac1fab8", + "metadata": { + "papermill": {}, + "tags": [] + }, + "source": [ + " " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + }, + "widgets": { + "application/vnd.jupyter.widget-state+json": { + "state": {}, + "version_major": 2, + "version_minor": 0 + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} \ No newline at end of file From 6c42f8512505f0e73c6962b54ea07ff690dfb70a Mon Sep 17 00:00:00 2001 From: M Sai Kiran Date: Mon, 6 Nov 2023 14:18:15 +0100 Subject: [PATCH 2/4] flow changed to console --- ..._Sheets_List_sheets_from_spreadsheet.ipynb | 74 ++++++++++++++++--- 1 file changed, 63 insertions(+), 11 deletions(-) diff --git a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb index ff70f1f4b8..0fefb512af 100644 --- a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb +++ b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb @@ -41,7 +41,7 @@ "tags": [] }, "source": [ - "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)" + "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/) , [SaiKiran M](https://www.linkedin.com/in/msaikiran9/)" ] }, { @@ -63,7 +63,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook will list all the sheets from a Google Sheets spreadsheet. It is usefull for organizations to quickly get an overview of the content of a spreadsheet." + "**Description:** This notebook will list all the sheets from a Google Sheets spreadsheet. It is useful for organizations to quickly get an overview of the content of a spreadsheet." ] }, { @@ -74,7 +74,9 @@ "tags": [] }, "source": [ - "**References:**\n- [Google Sheets API Documentation](https://developers.google.com/sheets/api/reference/rest/)\n- [Google Sheets API Python Quickstart](https://developers.google.com/sheets/api/quickstart/python)" + "**References:**\n", + "- [Google Sheets API Documentation](https://developers.google.com/sheets/api/reference/rest/)\n", + "- [Google Sheets API Python Quickstart](https://developers.google.com/sheets/api/quickstart/python)" ] }, { @@ -107,8 +109,14 @@ "papermill": {}, "tags": [] }, - "source": "import os\nimport pickle\nimport googleapiclient.discovery\nfrom google_auth_oauthlib.flow import InstalledAppFlow\nfrom google.auth.transport.requests import Request", - "outputs": [] + "outputs": [], + "source": [ + "import os\n", + "import pickle\n", + "import googleapiclient.discovery\n", + "from google_auth_oauthlib.flow import InstalledAppFlow\n", + "from google.auth.transport.requests import Request" + ] }, { "cell_type": "markdown", @@ -118,7 +126,9 @@ "tags": [] }, "source": [ - "### Setup variables\n- **SCOPES**: `https://www.googleapis.com/auth/spreadsheets.readonly`\n- **SPREADSHEET_ID**: ID of the spreadsheet to list sheets from" + "### Setup variables\n", + "- **SCOPES**: `https://www.googleapis.com/auth/spreadsheets.readonly`\n", + "- **SPREADSHEET_ID**: ID of the spreadsheet to list sheets from" ] }, { @@ -162,8 +172,41 @@ "papermill": {}, "tags": [] }, - "source": "def list_sheets(spreadsheet_id):\n \"\"\"\n List all the sheets from a Google Sheets spreadsheet.\n\n Parameters:\n spreadsheet_id (str): ID of the spreadsheet to list sheets from\n\n Returns:\n list: list of sheets\n \"\"\"\n # Setup credentials\n SCOPES = [\"https://www.googleapis.com/auth/spreadsheets.readonly\"]\n creds = None\n if os.path.exists(\"token.pickle\"):\n with open(\"token.pickle\", \"rb\") as token:\n creds = pickle.load(token)\n if not creds or not creds.valid:\n if creds and creds.expired and creds.refresh_token:\n creds.refresh(Request())\n else:\n flow = InstalledAppFlow.from_client_secrets_file(\"credentials.json\", SCOPES)\n creds = flow.run_local_server(port=0)\n with open(\"token.pickle\", \"wb\") as token:\n pickle.dump(creds, token)\n # List sheets\n service = googleapiclient.discovery.build(\"sheets\", \"v4\", credentials=creds)\n sheets = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()\n return sheets[\"sheets\"]", - "outputs": [] + "outputs": [], + "source": [ + "def list_sheets(spreadsheet_id):\n", + " \"\"\"\n", + " List all the sheets from a Google Sheets spreadsheet.\n", + "\n", + " Parameters:\n", + " spreadsheet_id (str): ID of the spreadsheet to list sheets from\n", + "\n", + " Returns:\n", + " list: list of sheets\n", + " \"\"\"\n", + " # Setup credentials\n", + " SCOPES = [\"https://www.googleapis.com/auth/spreadsheets.readonly\"]\n", + " creds = None\n", + " if os.path.exists(\"token.pickle\"):\n", + " with open(\"token.pickle\", \"rb\") as token:\n", + " creds = pickle.load(token)\n", + " if not creds or not creds.valid:\n", + " if creds and creds.expired and creds.refresh_token:\n", + " creds.refresh(Request())\n", + " else:\n", + " try:\n", + " flow = InstalledAppFlow.from_client_secrets_file(\"credentials.json\", SCOPES)\n", + " creds = flow.run_console()\n", + " except Exception as e:\n", + " print(f\"Error during OAuth flow: {str(e)}\")\n", + " return None\n", + " with open(\"token.pickle\", \"wb\") as token:\n", + " pickle.dump(creds, token)\n", + " # List sheets\n", + " service = googleapiclient.discovery.build(\"sheets\", \"v4\", credentials=creds)\n", + " sheets = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()\n", + " return sheets[\"sheets\"]" + ] }, { "cell_type": "markdown", @@ -195,8 +238,17 @@ "papermill": {}, "tags": [] }, - "source": "# Setup variables\nSPREADSHEET_ID = \"1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms\"\n# List sheets\nsheets = list_sheets(SPREADSHEET_ID)\n# Display result\nprint(f\"Sheets from spreadsheet {SPREADSHEET_ID}:\")\nfor sheet in sheets:\n print(f'- {sheet[\"properties\"][\"title\"]}')", - "outputs": [] + "outputs": [], + "source": [ + "# Setup variables\n", + "SPREADSHEET_ID = \"1bt2D7DPsmPYTQI0IOziAxYMbI9eRADN8gxTKMSmE7aw\"\n", + "# List sheets\n", + "sheets = list_sheets(SPREADSHEET_ID)\n", + "# Display result\n", + "print(f\"Sheets from spreadsheet {SPREADSHEET_ID}:\")\n", + "for sheet in sheets:\n", + " print(f'- {sheet[\"properties\"][\"title\"]}')" + ] }, { "cell_type": "markdown", @@ -238,4 +290,4 @@ }, "nbformat": 4, "nbformat_minor": 5 -} \ No newline at end of file +} From dee69716575ca0d05c45f1b9ac0457a19a94be21 Mon Sep 17 00:00:00 2001 From: M Sai Kiran Date: Tue, 7 Nov 2023 06:10:11 +0100 Subject: [PATCH 3/4] updated for service accounts --- ..._Sheets_List_sheets_from_spreadsheet.ipynb | 89 +++++++++---------- 1 file changed, 42 insertions(+), 47 deletions(-) diff --git a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb index 0fefb512af..efd0142888 100644 --- a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb +++ b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb @@ -111,11 +111,13 @@ }, "outputs": [], "source": [ - "import os\n", - "import pickle\n", - "import googleapiclient.discovery\n", - "from google_auth_oauthlib.flow import InstalledAppFlow\n", - "from google.auth.transport.requests import Request" + "try:\n", + " import gspread\n", + " from oauth2client.service_account import ServiceAccountCredentials\n", + "except ImportError:\n", + " !pip install gspread oauth2client\n", + " import gspread\n", + " from oauth2client.service_account import ServiceAccountCredentials" ] }, { @@ -127,8 +129,27 @@ }, "source": [ "### Setup variables\n", - "- **SCOPES**: `https://www.googleapis.com/auth/spreadsheets.readonly`\n", - "- **SPREADSHEET_ID**: ID of the spreadsheet to list sheets from" + "**Pre-requisite**\n", + "- [Create a service account and download JSON](https://cloud.google.com/iam/docs/service-accounts-create)\n", + "- [Enable Google Sheets API](https://console.cloud.google.com/apis/library/sheets.googleapis.com)\n", + "- Share your Google Sheet spreadsheet with service account client email\n", + "\n", + "**Mandatory**\n", + "- `credential_path`: path to access service account.\n", + "- `spreadsheet_id`: ID of the spreadsheet to list sheets from ." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a75d65c1-1969-4f7c-9a19-819a06e03e9d", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "credential_path = \"./credentials.json\"\n", + "spreadsheet_id = '1bt2D7DPsmPYTQI0IOziAxYMbI9eRADN8gxTKMSmE7aw' # Replace with the id of your existing spreadsheet" ] }, { @@ -161,7 +182,7 @@ "tags": [] }, "source": [ - "This function will list all the sheets from a Google Sheets spreadsheet." + "The following snippet authorizes and gets the sheets associated with a spreadsheet ." ] }, { @@ -174,38 +195,16 @@ }, "outputs": [], "source": [ - "def list_sheets(spreadsheet_id):\n", - " \"\"\"\n", - " List all the sheets from a Google Sheets spreadsheet.\n", - "\n", - " Parameters:\n", - " spreadsheet_id (str): ID of the spreadsheet to list sheets from\n", - "\n", - " Returns:\n", - " list: list of sheets\n", - " \"\"\"\n", - " # Setup credentials\n", - " SCOPES = [\"https://www.googleapis.com/auth/spreadsheets.readonly\"]\n", - " creds = None\n", - " if os.path.exists(\"token.pickle\"):\n", - " with open(\"token.pickle\", \"rb\") as token:\n", - " creds = pickle.load(token)\n", - " if not creds or not creds.valid:\n", - " if creds and creds.expired and creds.refresh_token:\n", - " creds.refresh(Request())\n", - " else:\n", - " try:\n", - " flow = InstalledAppFlow.from_client_secrets_file(\"credentials.json\", SCOPES)\n", - " creds = flow.run_console()\n", - " except Exception as e:\n", - " print(f\"Error during OAuth flow: {str(e)}\")\n", - " return None\n", - " with open(\"token.pickle\", \"wb\") as token:\n", - " pickle.dump(creds, token)\n", - " # List sheets\n", - " service = googleapiclient.discovery.build(\"sheets\", \"v4\", credentials=creds)\n", - " sheets = service.spreadsheets().get(spreadsheetId=spreadsheet_id).execute()\n", - " return sheets[\"sheets\"]" + "try:\n", + " scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']\n", + " credentials = ServiceAccountCredentials.from_json_keyfile_name(credential_path, scope)\n", + " client = gspread.authorize(credentials)\n", + " spreadsheet = client.open_by_key(spreadsheet_id)\n", + " sheets = spreadsheet.worksheets()\n", + "except gspread.exceptions.SpreadsheetNotFound:\n", + " print(f\"Spreadsheet with ID '{spreadsheet_id}' not found.\")\n", + "except gspread.exceptions.APIError as e:\n", + " print(f\"An error occurred while accessing the spreadsheet: {e}\")" ] }, { @@ -240,14 +239,10 @@ }, "outputs": [], "source": [ - "# Setup variables\n", - "SPREADSHEET_ID = \"1bt2D7DPsmPYTQI0IOziAxYMbI9eRADN8gxTKMSmE7aw\"\n", - "# List sheets\n", - "sheets = list_sheets(SPREADSHEET_ID)\n", - "# Display result\n", - "print(f\"Sheets from spreadsheet {SPREADSHEET_ID}:\")\n", + "# List the sheets in the spreadsheet\n", + "print(\"Sheets in the spreadsheet:\")\n", "for sheet in sheets:\n", - " print(f'- {sheet[\"properties\"][\"title\"]}')" + " print(sheet.title)" ] }, { From 30a5494296e48f025d027f196f22308e7e5c4278 Mon Sep 17 00:00:00 2001 From: Florent Ravenel Date: Wed, 8 Nov 2023 14:55:00 +0100 Subject: [PATCH 4/4] feat: update name and description --- .../Google_Sheets_List_sheets_from_spreadsheet.ipynb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb index efd0142888..6787cb881d 100644 --- a/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb +++ b/Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb @@ -41,7 +41,7 @@ "tags": [] }, "source": [ - "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/) , [SaiKiran M](https://www.linkedin.com/in/msaikiran9/)" + "**Author:** [SaiKiran M](https://www.linkedin.com/in/msaikiran9/)" ] }, { @@ -52,7 +52,7 @@ "tags": [] }, "source": [ - "**Last update:** 2023-10-31 (Created: 2023-10-31)" + "**Last update:** 2023-11-08 (Created: 2023-11-08)" ] }, { @@ -63,7 +63,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook will list all the sheets from a Google Sheets spreadsheet. It is useful for organizations to quickly get an overview of the content of a spreadsheet." + "**Description:** This notebook will list all the sheets from a Google Sheets spreadsheet." ] }, { @@ -149,7 +149,7 @@ "outputs": [], "source": [ "credential_path = \"./credentials.json\"\n", - "spreadsheet_id = '1bt2D7DPsmPYTQI0IOziAxYMbI9eRADN8gxTKMSmE7aw' # Replace with the id of your existing spreadsheet" + "spreadsheet_id = '1kKonmq9Cyssss6Sxh6jr_KZ6nzsWcJikVu0YW_ddd' # Replace with the id of your existing spreadsheet" ] }, {