Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Action]: GCP: Read from Google Sheet #370

Open
1 task done
dougsillars opened this issue Mar 21, 2023 · 0 comments
Open
1 task done

[Action]: GCP: Read from Google Sheet #370

dougsillars opened this issue Mar 21, 2023 · 0 comments
Labels
gcp good first issue Good for newcomers

Comments

@dougsillars
Copy link
Contributor

Contact Details

No response

Action Name

GCP Read from Google Sheet

Action Inputs

sheetId
Range

Action Outputs

"google sheet imported"

Comments

Yes, you can also use the Google API google-api-python-client to read a Google Sheet as a dataframe in Python. Here are the steps:

Install the google-api-python-client library by running pip install google-api-python-client.

Go to the Google Developers Console and create a new project.

Enable the Google Sheets API by clicking on "Enable APIs and Services" and searching for "Google Sheets API".

Create a set of credentials by clicking on "Create credentials" and selecting "Service account key". Fill in the required information and download the JSON file containing the credentials.

Share the Google Sheet with the email address of the service account that you created in step 4.

In your Python code, import the necessary libraries:

python
Copy code
import pandas as pd
from google.oauth2 import service_account
from googleapiclient.discovery import build
Load the credentials from the JSON file:
python
Copy code
creds = service_account.Credentials.from_service_account_file('path/to/json/file.json')
Build the Google Sheets API service:
python
Copy code
service = build('sheets', 'v4', credentials=creds)
Define the spreadsheet ID and range that you want to read:
python
Copy code
spreadsheet_id = 'your-spreadsheet-id'
range_name = 'Sheet1!A1:C10'
Call the values().get() method of the Google Sheets API service to retrieve the data:
sql
Copy code
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheet_id,
range=range_name
).execute()
Convert the retrieved data into a Pandas dataframe:
css
Copy code
df = pd.DataFrame(result['values'][1:], columns=result['values'][0])
This will create a dataframe with the data from the specified range in the Google Sheet. Note that the first row of the range is assumed to be the column names of the dataframe. If your data doesn't have column names, you can modify the pd.DataFrame() call to specify the column names.

Code of Conduct

  • I agree to follow this project's Code of Conduct
@dougsillars dougsillars added good first issue Good for newcomers gcp labels Mar 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gcp good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant