Skip to content

Commit

Permalink
flow changed to console
Browse files Browse the repository at this point in the history
  • Loading branch information
MSaiKiran9 committed Nov 6, 2023
1 parent 3a6b49c commit 6c42f85
Showing 1 changed file with 63 additions and 11 deletions.
74 changes: 63 additions & 11 deletions Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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/)"
]
},
{
Expand All @@ -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."
]
},
{
Expand All @@ -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)"
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -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"
]
},
{
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -238,4 +290,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}

0 comments on commit 6c42f85

Please sign in to comment.