diff --git a/Google Sheets/Google_Sheets_Create_new_sheet.ipynb b/Google Sheets/Google_Sheets_Create_new_sheet.ipynb index 5efa879530..cab3d4b23a 100644 --- a/Google Sheets/Google_Sheets_Create_new_sheet.ipynb +++ b/Google Sheets/Google_Sheets_Create_new_sheet.ipynb @@ -63,7 +63,7 @@ "tags": [] }, "source": [ - "**Description:** This notebook will show how to create a new sheet in Google Sheets using Python. It is useful for organizations that need to create new sheets in Google Sheets." + "**Description:** This notebook will show how to create a new sheet in Google Sheets using Python . It is useful for organizations that need to create new sheets in Google Sheets." ] }, { @@ -111,18 +111,13 @@ }, "outputs": [], "source": [ - "import naas\n", - "import json\n", "try:\n", - " from google.oauth2 import service_account\n", - " from googleapiclient.discovery import build\n", - " from googleapiclient.errors import HttpError\n", - "except ModuleNotFoundError:\n", - " !pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib\n", - " from google.oauth2 import service_account\n", - " from googleapiclient.discovery import build\n", - " from googleapiclient.errors import HttpError\n", - "#step 1: make sure all these modules are installed and imported !" + " 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" ] }, { @@ -134,9 +129,15 @@ }, "source": [ "### Setup variables\n", - "- **SCOPES**: list of scopes to be used in the authentication\n", - "- **SPREADSHEET_ID**: ID of the spreadsheet\n", - "- **SHEET_NAME**: name of the new sheet" + "**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_name`: Name of the spreadsheet you want to create the new sheet in\n", + "- `sheet_name`: Name of the new sheet" ] }, { @@ -149,10 +150,9 @@ }, "outputs": [], "source": [ - "SCOPES = [\"https://www.googleapis.com/auth/drive\", \"https://www.googleapis.com/auth/spreadsheets\"]\n", - "sheetName=\"sheet1\"\n", - "# SCOPES are used in the code to define access permissions \n", - "# sheetName is our required output sheet name (edit the name as required )." + "credential_path = \"./credentials.json\"\n", + "spreadsheet_name = \"My_Spreadsheet\"\n", + "sheet_name = \"custom sheet 2\"" ] }, { @@ -177,17 +177,6 @@ "### Create new sheet" ] }, - { - "cell_type": "markdown", - "id": "9ae12770-9a68-4e9f-9155-41e9cee1e2ff", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "This function will create a new sheet in the spreadsheet with the name specified in the variable SHEET_NAME." - ] - }, { "cell_type": "code", "execution_count": null, @@ -198,44 +187,23 @@ }, "outputs": [], "source": [ - "# Step 2: Define the service account credentials as a dictionary \n", - "# Step 3: (goto google cloud console > your-project > IAM and Admin > service accounts > fill in the details.)\n", - "# Step 4: then generate a key & save it as a secret string using : naas.secret.add(name=\"key\",secret=json.dumps(x.json))\n", + "# Define the scope and credentials\n", + "scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']\n", + "credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)\n", "\n", - "credentials_info = json.loads(naas.secret.get(name=\"key\"))\n", - "\n", - "def create_google_sheet(credentials_info, personal_email):\n", - " # Use the credentials dictionary to create the service account credentials\n", - " creds = service_account.Credentials.from_service_account_info(credentials_info, scopes=SCOPES)\n", - "\n", - " try:\n", - " service = build('sheets', 'v4', credentials=creds)\n", - "\n", - " # Create a new spreadsheet\n", - " spreadsheet = service.spreadsheets().create(body={\"properties\": {\"title\": \"My New Spreadsheet\"}}).execute()\n", - "\n", - " print(f\"Created new spreadsheet with ID: {spreadsheet['spreadsheetId']}\")\n", - "\n", - " # Access and print the URL of the created Google Sheet\n", - " sheet_url = spreadsheet.get(\"spreadsheetUrl\")\n", - " print(\"URL of the created Google Sheet:\", sheet_url)\n", - "\n", - " # Share the Google Sheet with your personal email or anyone using the Google Drive API\n", - " drive_service = build('drive', 'v3', credentials=creds)\n", - " permission = {\n", - " 'type': 'user',\n", - " 'role': 'writer',\n", - " 'emailAddress': personal_email\n", - " }\n", - " drive_service.permissions().create(fileId=spreadsheet['spreadsheetId'], body=permission).execute()\n", - "\n", - " print(f\"Shared the Google Sheet with {personal_email}\")\n", - "\n", - " except HttpError as err:\n", - " print(err)\n", + "# Authenticate and open the spreadsheet\n", + "try:\n", + " # Authenticate and open the existing spreadsheet\n", + " client = gspread.authorize(credentials)\n", + " spreadsheet = client.open(spreadsheet_name)\n", + "except:\n", + " # If the spreadsheet doesn't exist, create a new one and open it\n", + " client = gspread.authorize(credentials)\n", + " spreadsheet = client.create(spreadsheet_name)\n", "\n", - "# Specify your personal / anyother account Gmail address.\n", - "personal_email = 's2612369@gmail.com'" + "# Create a new sheet\n", + "new_spreadsheet = spreadsheet.add_worksheet(title=sheet_name, rows=\"100\", cols=\"20\")\n", + "print(f\"A new sheet named '{sheet_name}' has been created successfully.\")" ] }, { @@ -270,8 +238,8 @@ }, "outputs": [], "source": [ - "# step 5: Call the function to create the Google Sheet and share it with your anyother accounts.\n", - "create_google_sheet(credentials_info, personal_email)" + "print(\"Go To Spreadsheet\")\n", + "print(f\"https://docs.google.com/spreadsheets/d/{new_spreadsheet.url.split('spreadsheets')[1]}\")" ] }, { diff --git a/Google Sheets/Google_Sheets_Create_new_sheet_from_spreadsheet_name.ipynb b/Google Sheets/Google_Sheets_Create_new_sheet_from_spreadsheet_name.ipynb deleted file mode 100644 index 6a81ee6b8e..0000000000 --- a/Google Sheets/Google_Sheets_Create_new_sheet_from_spreadsheet_name.ipynb +++ /dev/null @@ -1,272 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "45283c5b-e4ba-4f14-a588-d8f6946bf299", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "\"Naas\"" - ] - }, - { - "cell_type": "markdown", - "id": "34916989-8f10-490e-9c06-90bfd277a5b4", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "# Google Sheets - Create new sheet from spreadsheet name" - ] - }, - { - "cell_type": "markdown", - "id": "506fc569-acd4-4428-8818-edf54178c99e", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "**Tags:** #googlesheets #spreadsheet #create #sheet #python #data #gcp #serviceaccount" - ] - }, - { - "cell_type": "markdown", - "id": "76a4db83-2238-4594-a3d8-d7d29d9f1bbb", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)" - ] - }, - { - "cell_type": "markdown", - "id": "00c9b2c5-ca31-4296-93a9-2aeeae673559", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "**Last update:** 2023-10-31 (Created: 2023-10-31)" - ] - }, - { - "cell_type": "markdown", - "id": "159b6f3b-db71-4076-bae7-88ca5667c5ea", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "**Description:** This notebook will show how to create a new sheet in Google Sheets from a spreadsheet name with a service account." - ] - }, - { - "cell_type": "markdown", - "id": "fd9db422-f3da-4ba1-81d8-9f5fb7ad2648", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "**References:**\n", - "- [Google Sheets API](https://developers.google.com/sheets/api)" - ] - }, - { - "cell_type": "markdown", - "id": "c18640fd-94b4-4676-874f-90529a7c114e", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "## Input" - ] - }, - { - "cell_type": "markdown", - "id": "07361904-df0f-4523-be56-d6b795419347", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "### Import libraries" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c4f9e873-44e0-409a-b0aa-0bb2983d9600", - "metadata": { - "papermill": {}, - "tags": [] - }, - "outputs": [], - "source": [ - "import gspread\n", - "from oauth2client.service_account import ServiceAccountCredentials" - ] - }, - { - "cell_type": "markdown", - "id": "a08eae33-585e-4b47-96bc-46c15f2fdda2", - "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_name`: Name of the spreadsheet you want to create the new sheet in\n", - "- `sheet_name`: Name of the new sheet" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "90ae9ed9-fa8a-407c-953b-a58231611064", - "metadata": { - "papermill": {}, - "tags": [] - }, - "outputs": [], - "source": [ - "credential_path = \"credentials.json\"\n", - "spreadsheet_name = \"My Spreadsheet\"\n", - "sheet_name = \"New Sheet\"" - ] - }, - { - "cell_type": "markdown", - "id": "633ea3c1-4169-4bbc-b442-a7c5f6240783", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "## Model" - ] - }, - { - "cell_type": "markdown", - "id": "f14c054a-6447-4706-b7a5-4bcec8639352", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "### Create new sheet" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "83fd5162-b0aa-4968-a2a0-4d0d33e64904", - "metadata": { - "tags": [] - }, - "outputs": [], - "source": [ - "# Define the scope and credentials\n", - "scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']\n", - "credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)\n", - "\n", - "# Authenticate and open the spreadsheet\n", - "client = gspread.authorize(credentials)\n", - "spreadsheet = client.open(spreadsheet_name)\n", - "\n", - "# Create a new sheet\n", - "new_spreadsheet = spreadsheet.add_worksheet(title=sheet_name, rows=\"100\", cols=\"20\")\n", - "print(f\"A new sheet named '{sheet_name}' has been created successfully.\")" - ] - }, - { - "cell_type": "markdown", - "id": "df38ef91-0325-4bf1-835a-cdb6e7fda503", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "## Output" - ] - }, - { - "cell_type": "markdown", - "id": "e5ad10a6-c057-4645-bf7f-c9efba2207e6", - "metadata": { - "papermill": {}, - "tags": [] - }, - "source": [ - "### Display result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b7372838-a17f-4749-bd55-56a1de96fd05", - "metadata": { - "papermill": {}, - "tags": [] - }, - "outputs": [], - "source": [ - "print(\"Go To Spreadsheet\")\n", - "print(f\"https://docs.google.com/spreadsheets/d/{new_spreadsheet.url.split('spreadsheets')[1]}\")" - ] - }, - { - "cell_type": "markdown", - "id": "006ac03f-ed27-4db3-9f6e-de3e7e94c3a5", - "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 -}