Version History - Google Excel using gspread - python

I am using gspread library in my python script to connect to the Google excel sheet and I was wondering if there is a way to also grab the version history via gspread library?

From I am using gspread library in my python script to connect to the Google excel sheet, in your situation, I guessed that Google excel sheet is Google Spreadsheet.
About I was wondering if there is a way to also grab the version history via gspread library?, unfortunately, in the current stage, it seems that gspread has no methods for retrieving the revision list of Spreadsheet. But, when googleapis for python is used, the revision list of the Spreadsheet can be retrieved.
In this answer, I would like to propose to retrieve the revision list of Spreadsheet using the client of gspread. Because, in the recent version of gspread, googlapis can be easily used using the client of gspread. And also, gspread includes the scope for using Drive API. I thought that when the client of gspread is used, it might be useful for your situation. The sample script is as follows.
Sample script:
import gspread
from googleapiclient.discovery import build
client = gspread.oauth(
credentials_filename="###", # Please set your file.
authorized_user_filename="###", # Please set your file.
)
spreadsheetId = "###" # Please set your Spreadsheet ID.
service = build("drive", "v3", credentials=client.auth)
revisions = service.revisions().list(fileId=spreadsheetId).execute()
print(revisions)
When this script is run, the revision list can be retrieved from the Spreadsheet.
Note:
For example, when you want to access the data of the specific version, I thought that the following threads might be useful.
Google Drive API V3: get the content of a revision
How to get older versions of Google Spreadsheet data?
Revert Revision of an Excel File - Drive API
Reference:
Revisions: list

Related

How to R&W to google sheets via url with python

I am trying to build a google sheets based chat with python and I'm having trouble understanding how to read&write from the spread sheet on my drive (without using Google API of course, explanation why at the end*)
So far I've gotten to a place where I can get the file, but I cant read the content. Like so:
import pandas as pd
import requests
from io import StringIO
orig_url='https://docs.google.com/spreadsheets/d/1bnCDl1DqRLqO8xHx3sjWdkydYC7rEb3vjpXUZ3ps2tY/edit?usp=sharing'
file_id = orig_url.split('/')[-2]
dwn_url='https://drive.google.com/uc?export=download&id=' + file_id
url = requests.get(dwn_url).text
csv_raw = StringIO(url)
dfs = pd.read_csv(csv_raw)
print(dfs.head())
P.S. I looked online for many other resources, from I can tell they are all using google API
*I am building the chat app as part of a course and using API's is not a part of it yet, for that reason I cannot use google API
Please refer to the Google api documentation specifically Google sheets API: https://developers.google.com/sheets/api
There you can read about how to enable the sheets api, create authentication tokens and how to use the api calls in your preferred language.
If you don't want to use Google API, you can try sheetdb.io. It's a tool that turns a Google Spreadsheet into a JSON API. So you can use HTTP requests to read and write to a Google Spreadsheet.

How to link a Google Form to a Google Sheet using Python?

Here's my setup and what I want to accomplish:
I currently have a Google Form set up for users to submit information, which is then logged in a Google Sheet.
I then, use PyDrive to save the contents of the Google Sheet as a csv.
Pandas then reads that csv, and deletes data within the csv based on certain criteria.
After that, I use PyDrive to re-upload the "fixed" csv to Google sheets, saving over the old one.
Here's the problem:
whenever I do this, it terminates the link between the Google Sheet and Google Form. Is there some way I can preserve this link or re-link the Form and the Sheet?
Here is the snippet of code I have been playing with:
submissions_data = drive.CreateFile({'id': ext['SUBMISSIONS_ID']})
submissions_data.GetContentFile("data.csv", mimetype = "text/csv")
df = pd.read_csv("data.csv")
df.drop([0],inplace=True)
df.to_csv("data.csv", index=False)
submissions_data.SetContentFile("data.csv")`
Preferably, I would like to find a way to do this using PyDrive, but anything will work at this point. Any help is appreciated! Thank you!
Use Google Drive API with oauth2client and gspread to update the existing spreadsheet directly.
Go to the Google APIs Console. Create a new project. Click Enable
API.
Enable the Google Drive API. Create credentials
for a Web Server to access Application Data.
Name the service account and grant it a Project Role of Editor.
Download the JSON file.
Copy the JSON file to your code directory and rename it to
client_secret.json
Find the client_email inside client_secret.json. Back in your spreadsheet, click the Share button in the top right, and paste the client email into the People field to give it edit rights. Hit Send.
pip install gspread oauth2client
#spreadsheet.py
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# use creds to create a client to interact with the Google Drive API
scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope)
client = gspread.authorize(creds)
# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Copy of Legislators 2017").sheet1
# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)
You can write to the spreadsheet by changing a specific cell:
sheet.update_cell(1, 1, "I just wrote to a spreadsheet using Python!")
Or you can insert a row in the spreadsheet:
row = ["I'm","inserting","a","row","into","a,","Spreadsheet","with","Python"]
index = 1
sheet.insert_row(row, index)
Check the gspread API reference for the full details on these functions along with a few dozen others.

Dump data from python cron job to google spreadsheets

I am creating a CSV which contains the report as a result of a cronjob. I want to share this CSV via Google spreadsheets - the report itself is versioned, so I would just dump the CSV contents into the same worksheet in the same spreadsheet each and every single time.
I have found gspread which looked very promising but unfortunately gives me NoValidUrlKeyFound Errors. The Python example for interacting with the Spreadsheets API v4 (to be found here) requires interactivity because of the OAuth flow.
Can someone point me in the right direction? Ideally I would just do:
client = spreadsheet.client(credential_file)
client.open_spreadheet(url_or_something).open_worksheet(0).dump(csv)
print("Done")
I have found the answer in a Github issue:
Using gspread, follow the official guide to receive an oauth token. You will end up with a JSON file holding the credentials.
In order to access a spreadsheet with these credentials however, you need to share the spreadsheet with the account represented by them. In the JSON file there is a property client_email. Share the document with that e-mail address and voila, you'll be able to open it!

Google Drive API python to export each worksheet of spreadsheet separately in csv format

I am referring to below Google Drive api to export Google spreadsheet as CSV.
https://developers.google.com/drive/v2/web/manage-downloads
Its working fine But as mentioned in guide it downloads only 1st sheet into csv format. I am looking for a way to download all worksheets into csv format separately.
gdata library of python is not working after OAuth1 has has been deprecated.
Please suggest if someone has done it successfully in OAuth2.
Drive API itself does not offer a way to export a specific worksheet, but with a valid bearer token you can just download it in the desired format via its Google Drive url. The pattern is https://docs.google.com/spreadsheets/d/{{document_id}}/export?format=tsv&gid={{gid}}. The document_id and gid are visiable in the browser URL bar when you open the sheet in Google Drive. Replace tsv with whatever format you need.
I'm not good enough in Python, but I created a demo app in Node.js: git#github.com:joerx/drive-exporter.git. The general flow is:
Obtain an access token via the Google APIs OAuth2 flow
Figure out sheet_id and gid
Construct download url
Make a regular HTTP request passing the token as Authorization: Bearer {{token}}
For public sheets you can skip the authorisation part.
General documentation for using Drive API and OAuth2 (with examples in Python) is here
If you need to programmatically determine the gid, this might help: How to convert Google spreadsheet's worksheet string id to integer index (GID)?

Accessing Google Drive Spreadsheets with Python Gspread

I just started using Gspread and am trying to access one of my google docs spreadsheets in my google drive. I followed the instructions and went to Google API console and created a JSON file. When I run this code I get no errors:
import gspread
import json
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open("mwsSearch-b3d5d5d9c956.json"))
scope = ["https://spreadsheets.google.com/feeds"]
credentials = SignedJwtAssertionCredentials(json_key['client_email'], bytes(json_key['private_key'], 'utf-8'), scope=scope)
gc = gspread.authorize(credentials)
My next step is to try to open a google spreadsheet. I created a spreadsheet titled "Mike" in the main folder of my Google Drive, but have tried to access it via:
gc.open_by_url("https://docs.google.com/spreadsheets/d/1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA/edit#gid=0")
gc.open("Mike")
gc.open_by_key("1HH7BKsnB2Rd5rlAr7S2H3avUtO4GkqeWrXJfYKKooNA")
All three of these return the same error:
gspread.exceptions.SpreadsheetNotFound
I am thinking that maybe the api access is linking to another cloud storage through the project, and not my individual google drive, and that is why it is not accessing it. Could someone with more experience in this please point me in the right direction on what I am doing wrong. All help is appreciated. Thank you.
You'll need to add the email which was created with the JSON key to the spreadsheet you want to access. It will be something like 9876.....#developer.gserviceaccount.com. You'll find it as the "client email" in your JSON file and your credential page.
SignedJwtAssertionCredentials has been deprecated.
Look at http://gspread.readthedocs.org/en/latest/oauth2.html
Go to Google Sheets and share your spreadsheet with an email you have in your json_key['client_email']. Otherwise you’ll get a SpreadsheetNotFound exception when trying to open it.

Categories

Resources