Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Google Sheets - List sheets from spreadsheet #2346

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
288 changes: 288 additions & 0 deletions Google Sheets/Google_Sheets_List_sheets_from_spreadsheet.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "0fb936be-f97e-4e36-a37c-51cf40f58811",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"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:** [SaiKiran M](https://www.linkedin.com/in/msaikiran9/)"
]
},
{
"cell_type": "markdown",
"id": "bcfdb331-57e6-4644-9817-1de75d42cc81",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-11-08 (Created: 2023-11-08)"
]
},
{
"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."
]
},
{
"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": []
},
"outputs": [],
"source": [
"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"
]
},
{
"cell_type": "markdown",
"id": "a6e10857-d5f1-454e-ae40-49a88da3be43",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n",
"**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 = '1kKonmq9Cyssss6Sxh6jr_KZ6nzsWcJikVu0YW_ddd' # Replace with the id of your existing spreadsheet"
]
},
{
"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": [
"The following snippet authorizes and gets the sheets associated with a spreadsheet ."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96de32a5-3a25-43bd-9443-2108afcccacb",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"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}\")"
]
},
{
"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": []
},
"outputs": [],
"source": [
"# List the sheets in the spreadsheet\n",
"print(\"Sheets in the spreadsheet:\")\n",
"for sheet in sheets:\n",
" print(sheet.title)"
]
},
{
"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
}
Loading