Google Drive Sdk
Google Drive SDK module for Google Drive operations.
This module provides utilities for interacting with Google Drive API, specifically for creating and managing folders and spreadsheets.
create_google_drive_folder(folder_name, master_folder_id, credentials=None, client_email=None, private_key=None)
Create a new folder in Google Drive within a specified master folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_name
|
str
|
The name of the new folder. |
required |
master_folder_id
|
str
|
The ID of the master folder. |
required |
credentials
|
Any | None
|
The credentials for Google Drive API. If None, will be obtained from client_email and private_key. |
None
|
client_email
|
str | None
|
The client email for Google Drive API. |
None
|
private_key
|
str | None
|
Base64-encoded private key for Google Drive API. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The ID of the newly created folder. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there's an error creating the folder. |
create_spreadsheet(title, drive_folder_id, client_email=None, private_key=None)
Create a new spreadsheet in the Google Drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
title
|
str
|
The title of the spreadsheet. |
required |
drive_folder_id
|
str
|
The ID of the folder to create the spreadsheet in. |
required |
client_email
|
str | None
|
The client email. |
None
|
private_key
|
str | None
|
The private key. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The ID of the spreadsheet. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there's an error creating the spreadsheet. |
get_or_create_folder(folder_name, parent_folder_id, credentials=None, client_email=None, private_key=None)
Get or create a folder in the Google Drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_name
|
str
|
The name of the folder to create or get. |
required |
parent_folder_id
|
str
|
The ID of the parent folder to create the folder in. |
required |
credentials
|
Any | None
|
The credentials. |
None
|
client_email
|
str | None
|
The client email. |
None
|
private_key
|
str | None
|
The private key. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The folder ID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If parent_folder_id is empty. |
get_or_create_spreadsheet(file_name, folder_drive_id, client_email=None, private_key=None)
Get or create a spreadsheet in the Google Drive.
This function searches for an existing spreadsheet with the given name in the specified folder. If found, it returns the spreadsheet ID. If not found, it creates a new spreadsheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The name of the spreadsheet to create or get. |
required |
folder_drive_id
|
str
|
The ID of the folder to create the spreadsheet in. |
required |
client_email
|
str | None
|
The client email for authentication. If None, reads from environment variables. |
None
|
private_key
|
str | None
|
The private key for authentication (base64-encoded). If None, reads from environment variables. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The spreadsheet ID. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If multiple spreadsheets with the same name are found. |
Exception
|
If there's an error creating or searching for the spreadsheet. |
Example
spreadsheet_id = get_or_create_spreadsheet( ... file_name="My Experiment Results", ... folder_drive_id="1abc...xyz", ... ) print(spreadsheet_id) '1def...uvw'
search_file(file_name, folder_drive_id, client_email=None, private_key=None)
Search for a file in the Google Drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name
|
str
|
The name of the file to search for. |
required |
folder_drive_id
|
str
|
The ID of the folder to search in. |
required |
client_email
|
str | None
|
The client email. |
None
|
private_key
|
str | None
|
The private key. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The file ID if found, else None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If multiple spreadsheets with the same name are found. |
search_folder(folder_name, parent_folder_id, credentials=None, client_email=None, private_key=None)
Search for a folder in the Google Drive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_name
|
str
|
The name of the folder to search for. |
required |
parent_folder_id
|
str
|
The ID of the master folder to search in. |
required |
credentials
|
Any | None
|
The credentials. |
None
|
client_email
|
str | None
|
The client email. |
None
|
private_key
|
str | None
|
The private key. |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The folder ID if found, else None. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If multiple folders with the same name are found. |
search_folder_in_master_folder(master_folder_id, folder_name, credentials=None, client_email=None, private_key=None)
Search for folders within a specified master folder that match a specific name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
master_folder_id
|
str
|
The ID of the master Google Drive folder to search in. |
required |
folder_name
|
str
|
The name of the folder to search for. |
required |
credentials
|
Any | None
|
The credentials for Google Drive API. If None, will be obtained from client_email and private_key. |
None
|
client_email
|
str | None
|
The client email for Google Drive API. |
None
|
private_key
|
str | None
|
Base64-encoded private key for Google Drive API. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of folder IDs that match the criteria. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there's an error searching for the folder. |
search_sheets_in_folder(folder_id, title_value, client_email=None, private_key=None)
Search for Google Sheets within a specified folder whose title matches a value exactly.
An exact match (name = '...') is used rather than a substring match so that callers such as
get_or_create_spreadsheet resolve the same title to a single spreadsheet. A substring match
would also return sheets whose names merely start with title_value (e.g. time-suffixed runs),
yielding multiple results for what should be one sheet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_id
|
str
|
The ID of the Google Drive folder to search in. |
required |
title_value
|
str
|
The exact title to match against the spreadsheets. |
required |
client_email
|
str | None
|
The client email for Google Sheets API. |
None
|
private_key
|
str | None
|
Base64-encoded private key for Google Sheets API. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: A list of spreadsheet IDs that match the criteria. |
Raises:
| Type | Description |
|---|---|
Exception
|
If there's an error searching for the sheets. |