Create a Google Sheet Class for Working with Google Sheets
- Dominant language
- Python
- Stars
- 2
- Forks
- 0
- Avg merge
- 1m
- Merged PRs (30d)
- 1
Description
Starter code:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pandas as pd
class GoogleSheets:
def __init__(self, credentials_file, sheet_name):
"""
Initialize the GoogleSheets class.
:param credentials_file: Path to the Google API credentials JSON file.
:param sheet_name: Name of the Google Sheet to interact with.
"""
self.credentials_file = credentials_file
self.sheet_name = sheet_name
self.client = self.authenticate()
self.sheet = self.client.open(sheet_name)
def authenticate(self):
"""
Authenticate and create a Google Sheets client.
:return: Google Sheets client.
"""
scope = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive'
]
creds = ServiceAccountCredentials.from_json_keyfile_name(self.credentials_file, scope)
client = gspread.authorize(creds)
return client
def get_worksheet(self, worksheet_name):
"""
Get a worksheet by name.
:param worksheet_name: Name of the worksheet.
:return: Worksheet object.
"""
return self.sheet.worksheet(worksheet_name)
def read_worksheet(self, worksheet_name):
"""
Read data from a worksheet into a Pandas DataFrame.
:param worksheet_name: Name of the worksheet.
:return: DataFrame containing the worksheet data.
"""
worksheet = self.get_worksheet(worksheet_name)
data = worksheet.get_all_records()
return pd.DataFrame(data)
def write_to_worksheet(self, worksheet_name, dataframe):
"""
Write a Pandas DataFrame to a worksheet.
:param worksheet_name: Name of the worksheet.
:param dataframe: DataFrame containing the data to write.
"""
worksheet = self.get_worksheet(worksheet_name)
worksheet.clear()
worksheet.update([dataframe.columns.values.tolist()] + dataframe.values.tolist())
def append_to_worksheet(self, worksheet_name, dataframe):
"""
Append a Pandas DataFrame to the end of a worksheet.
:param worksheet_name: Name of the worksheet.
:param dataframe: DataFrame containing the data to append.
"""
worksheet = self.get_worksheet(worksheet_name)
worksheet.append_rows(dataframe.values.tolist())
def create_worksheet(self, worksheet_name, rows=1000, cols=26):
"""
Create a new worksheet.
:param worksheet_name: Name of the worksheet to create.
:param rows: Number of rows in the new worksheet.
:param cols: Number of columns in the new worksheet.
"""
self.sheet.add_worksheet(title=worksheet_name, rows=rows, cols=cols)
def delete_worksheet(self, worksheet_name):
"""
Delete a worksheet by name.
:param worksheet_name: Name of the worksheet to delete.
"""
worksheet = self.get_worksheet(worksheet_name)
self.sheet.del_worksheet(worksheet)
def list_worksheets(self):
"""
List all worksheets in the Google Sheet.
:return: List of worksheet names.
"""
worksheets = self.sheet.worksheets()
return [worksheet.title for worksheet in worksheets]
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.