Skip to content

04‐API

Teeraphat Kullanankanjana edited this page Oct 29, 2023 · 9 revisions

Import Library

from ggsheet import MicroGoogleSheet

Class Instance

MicroGoogleSheet(googleSheetURL, sheet_name, deploymentID)

Initializes the MicroGoogleSheet class with the Google Sheet URL, sheet name, and deployment ID.

Parameters

  • googleSheetURL(str): The URL of the Google Sheet.
  • sheet_name(str, optional): The name of the sheet within the Google Sheet. Default is "Sheet1".
  • deploymentID(str, optional): The deployment ID of the Google Apps Script web app. The default is an empty string, which can be added later with set_DeploymentID().
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

# Create Instance 
ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name,google_app_deployment_id)

Set Deployment ID

set_DeploymentID(deployment_id)

Sets the deployment ID for the Google Apps Script web app.

Parameters

  • deployment_id(str): The deployment ID to set.
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name)
ggsheet.set_DeploymentID(google_app_deployment_id)

Set Sheet Name

set_SheetName(sheetName)

Sets the name of the sheet within the Google Sheet.

Parameters

  • sheetName (str): The sheet name to set.
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

# Class Instance
ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name)
# Set Deployment ID
ggsheet.set_DeploymentID(google_app_deployment_id)
# Set the sheet name from "Sheet1" to "Sheet2"
ggsheet.set_SheetName("Sheet2")

Generate Google Sheet Script

gen_scriptFile()

Generate a Google Apps Script file for Google Sheet operations.

Call this method in case you can't find the script.gs file.

# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"

# Class Instance
ggsheet = MicroGoogleSheet(google_sheet_url)

# Generate Google Script File
ggsheet.gen_scriptFile()

Update

Update the Google Sheet cell, row, or column.

The update will overwrite the old data.

Update Cell

updateCell(row,column,data)

Update the data to a specific cell.

Parameters

  • row (int): The row number of the cell.
  • column (int): The column number of the cell.
  • data (string): The data to set in the cell.

Update Row

updateRow(row,data)

Update the data in a specific row

Parameters

  • row (int): The row number to update.
  • data (list): A list of data values to update the row.

Update Column

updateColumn(column,data)

Update the data to a specific column

Parameters

  • column (int): The column number to update.
  • data (list): A list of data values to update the column.
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

# Create Instance 
ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name)
ggsheet.set_DeploymentID(google_app_deployment_id)

# Update the data to a specific cell 
ggsheet.updateCell(1,1,"Hello this is my first data")

# Update the data in a specific row 
ggsheet.updateRow(1,[3,2,1,"Row 1 Updated!"])

# Update the data to a specific column 
ggsheet.updateColumn(1,[3,2,1,"Column 1 Updated!"])

Get

Get the data from a specific Google Sheet cell, row, or column as a string or list.

Get Cell

getCell(row,column)

Get the data from a specific cell

Parameters

  • row (int): The row number to retrieve.
  • column (int): The column number to retrieve.

Return

  • cell_data (str): The content of the specified cell.

Get Row

getRow(row)

Get the data from a specific row

Parameters

  • row (int): The row number to retrieve.

Return

  • row_data (list): The content of the specified row.

Get Column

getColumn(column)

Get the data from a specific column

Parameters

  • column (int): The column number to retrieve.

Return

  • column_data (list): The content of the specified column.
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

# Create Instance 
ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name)
ggsheet.set_DeploymentID(google_app_deployment_id)

# Get the data from a specific cell
print(ggsheet.getCell(1,1))

# Get all of the data from a specific row 
print(ggsheet.getRow(1))

# Get all of the data from a specific column
print(ggsheet.getColumn(1))

Delete

Delete the data in a specific cell, row or column.

Delete Cell

deleteCell(row,column)

Delete the data to a specific cell.

Parameters

  • row (int): The row number of the cell.
  • column (int): The column number of the cell.

Delete Row

deleteRow(row)

Delete the data in a specific row

Parameters

  • row (int): The row number to delete.

Delete Column

deleteColumn(column)

Delete the data to a specific column

Parameters

  • column (int): The column number to delete.
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

# Create Instance 
ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name)
ggsheet.set_DeploymentID(google_app_deployment_id)

# Delete the data from a specific cell
ggsheet.deleteCell(1,1)

# Delete the data in a specific row (Row)
ggsheet.deleteRow(1)

Append

Append the data to a specific row or column.

If there is data for the specific target, the old data will be moved one row or column, and then new data will be appended.

Append Cell

appendCell(row,column,data)

Append the data to a specific cell.

Parameters

  • row (int): The row number of the cell.
  • column (int): The column number of the cell.
  • data (string): The data to append in the cell.

Append Row

appendRow(row,data)

Append the data in a specific row

Parameters

  • row (int): The row number to append.
  • data (list): A list of data values to append the row.

Append Column

appendColumn(column,data)

Append the data to a specific column

Parameters

  • column (int): The column number to append.
  • data (list): A list of data values to append the column.
# Google Sheet Credential 
google_sheet_url = "https://docs.google.com/spreadsheets/d/xxxxxxxxx/edit#gid=0"
google_sheet_name = "Sheet1"
google_app_deployment_id = "xxxxxxxx"

# Create Instance 
ggsheet = MicroGoogleSheet(google_sheet_url,google_sheet_name)
ggsheet.set_DeploymentID(google_app_deployment_id)

# Append the data to a specific row (Row, Data List)
ggsheet.appendRow(1,[1,2,3,"Row 1 Appended!"])

# Append the data to a specific column (Column, Data List)
ggsheet.appendColumn(1,[1,2,3,"Column 1 Appended!"])