Skip to main content

Helper functions wrapper over the google sheets python api

Project description

gsheets_helper

Helper functions wrapper over the google sheets python api

Getting started

Install

pip install gsheets_helper

Google credentials

Obtain a Google oauth2 credential file.

Refer appendix section

Usage

Basics

Understanding spreadsheet_id and sheet_id.

Open you google sheet in a browser and check the url

https://docs.google.com/spreadsheets/d/XXXX/edit#gid=YYYY

XXXX -> spreadsheet_id
YYYY -> sheet_id
Be default, the first sheet in a google sheet will always have sheet_id=0

Getting Started - initialization

from gsheets_helper import GSheetsHelper

gsheets_health = GSheetsHelper(SCOPES, "path/to/credentials.json")  # refer appendix on how to get credential.json

# Example 
SCOPES = ['https://www.googleapis.com/auth/spreadsheets',
          'https://www.googleapis.com/auth/fitness.activity.read',
          'https://www.googleapis.com/auth/fitness.activity.write',
          'https://www.googleapis.com/auth/fitness.nutrition.read',
          'https://www.googleapis.com/auth/fitness.nutrition.write',
          'https://www.googleapis.com/auth/fitness.sleep.read',
          'https://www.googleapis.com/auth/fitness.sleep.write'
          ]
gsheets_health = GSheetsHelper(SCOPES, "path/to/credentials.json")

Read the sheet from the provided range name and return the values

get_sheet_data(self, spreadsheet_id, range_name)

#Example usage
RANGE_NAME = 'Sample!A2:D'
r = get_sheet_data(spreadsheet_id, range_name))
print(r)
# convert to panda dataframe, if required
pd1 = pd.DataFrame(r, columns=["A", "B", "C", "D"])
print(pd1)

Appending row to existing sheet


gsheets_health.append_row_to_sheet(spreadsheet_id, range_name, body)

# Example 
values = [
    [
        col1val, col2val, col3val          #bascially a single row
    ]
    # Additional rows ...
]
body = {
   'values': values
}
gsheets_health.append_row_to_sheet(spreadsheet_id, range_name, body)

Read the auth token generated from the session.

# Primary usage is to use the auth token for further querying google apis
# Could also be useful to generate the token and then use in a different tool like Postman
get_auth_token(self)

# Example usage
    response = requests.post(
        "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate",
        headers={
            "Authorization": "Bearer " + getAuthToken(),
            "content-type": "application/json",
        },
        data=json.dumps({
            "aggregateBy": [{
                "dataTypeName": "com.google.sleep.segment"
            }],
            "startTimeMillis": 1631817000000,
            "endTimeMillis": 1631903340000
        })
    )

Get rows matching a particular string in a column

get_matching_rows(self, spreadsheet_id, range_name, column_to_search, string_to_search, columns)

#Example usage
matching_rows = gsheets_health.get_matching_rows(spreadsheet_id, range_name, "Date", d_string, columns)

Deleting an existing row in the sheet

#Can be used in conjuction to getting matching rows to delete any record

#Example usage
gsheets_health.delete_row_matching_row(spreadsheet_id, sheet_id, int(matching_rows[0]) + 1)

Appendix

Generating google credentials

oauth2

  • Authentication flow occurs on code execution - choose the google account that has access to the document.
Creating credential in Google Cloud console

https://console.cloud.google.com/apis/credentials -> Create credentials -> Oauthclient ID

Select options:

  • Application type = Other
  • Name = whatever you want Creates and downloads credential file with name format client_secret_xxx-xxx.apps.googleusercontent.com.json

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gsheets_helper-0.0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

gsheets_helper-0.0.1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file gsheets_helper-0.0.1.tar.gz.

File metadata

  • Download URL: gsheets_helper-0.0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for gsheets_helper-0.0.1.tar.gz
Algorithm Hash digest
SHA256 77653a5916dcf4228b0ba091eda389af2cd36f13384bdc35b9b97caebb28d212
MD5 82de8d7965ffa8276e0baae9aee8e7de
BLAKE2b-256 bbbc542b103ec487c8022fd79a25bc60de9baa6e56f062cd0dd927466d741c91

See more details on using hashes here.

File details

Details for the file gsheets_helper-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: gsheets_helper-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for gsheets_helper-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5fbde06899528272289caa3000823c2326d4e4ef53b13148b9bf30e54d47c16b
MD5 d6ea941099282298ca7ca34cd9bffbcd
BLAKE2b-256 683ab23c1780b32d9a1e25d1c07c20cdc49b99416db16997fe1d80093ba29fb3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page