I'm trying to create a chatbot using dialog flow , for this i have my data in excel sheet and i want to use this as a database source for my bot! Is there any way that i can do it without writing several intents?
Dialogflow has no direct connection to Google Sheets or the Sheets API. If you want to use a Sheet as the source of some answers, you will need to integrate it through Dialogflow Intents and a Fulfillment webhook.
In your webhook, you will need to make calls to the Sheets API to get the info you want to be able to return.
Related
Can I read a google spreadsheet which is open to people, but doesn't have a share option? There's a discussion here, but it's I need to have an authorization to click the share option.
Even copying by URL to my own Google spreadsheet may serve the purpose.
Update:
The idea was once I create a Google API, I should be able to create a .json file with a client email. In the share option, I'm supposed to provide the client email of .json file. You may see: Accessing Google Spreadsheet Data using Python.
This is the spreadsheet page where I'm not finding any Share option: https://docs.google.com/spreadsheets/d/e/2PACX-1vSc_2y5N0I67wDU38DjDh35IZSIS30rQf7_NYZhtYYGU1jJYT6_kDx4YpF-qw0LSlGsBYP8pqM_a1Pd/pubhtml#
Issue:
Publishing the contents of a spreadsheet to the web is not the same as making a spreadsheet public.
The URL you shared refers to spreadsheet contents that were published to the web following these steps. This published website is not the same as the original file where the data comes from, and so it doesn't have most of its functionalities, like a Share button (it doesn't make sense to have a Share button anyway, since this URL is already public).
Solution:
If you want to access the spreadsheet data using a Service Account, you would have to do one of the following (better to use method 1 if you have access to the spreadsheet):
Share the spreadsheet itself (not the published contents) with the Service Account, as explained in the link you referenced.
Use your application to fetch the website contents from the provided URL.
Reference:
Make Google Docs, Sheets, Slides & Forms public
I'm trying to make a function for creating Excel file and upload the excel file to Google Sheets (Google Drive maybe) via API (that's possible I think). Then, the function will publish Google Sheets to Web (like as Publish to Web function in Google Sheets web UI) in order to embed the Google Sheets in my website for Preview.
I could not figure out the API endpoint to do Publish To Web via Python.
There is the only Stackoverflow link I found out that someone did it via JS code (Google Sheets API: How to "publish to web" for embeddable sheet?), but it requires Authentication at the client side. For my case, I want to implement the function in Backend (server) with an Authorization Token (hard-code or managed by backend)
Answer:
Unfortunately there is not API endpoint for Google Drive nor Google Sheets which have the same functionality as the Publish to the web menu item in the Google Sheets UI.
More Information & Feature Request:
As it is possible to use the result of the Publish to the web menu item to embed a Sheet into a web page or Google Sites, it could be a useful API method for either Sheets or even Drive.
You can however let Google know that this is a feature that is important for the Sheets API and that you would like to request they implement it. Google's Issue Tracker[1] is a place for developers to report issues and make feature requests for their development services.
The page to file a Feature Request for the Google Sheets API can be found here[2].
References:
Google's Issue Tracker
File a Google Sheets API Feature Request
Instead of creating an excel sheet, try uploading directly to google sheets using gspread.
If you have an excel sheet you want to go through, try reading the cell in excel then writing that cell in google sheets
(Not my links, but should be a good reference to get started)
https://gspread.readthedocs.io/en/latest/
https://pypi.org/project/gspread/
https://www.youtube.com/watch?v=cnPlKLEGR7E
I have a google sheet which I set so that anyone can edit.
I noticed that in the gspread package, you can get a spreadsheet from the link alone. Example:
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
Since anyone can edit, providing an auth code for access is not necessary. However, it seems that you always need to initialize it using an auth code.
Is there a way to edit a google sheet without any auth code?
This may get confused as a duplicate of this post Using gspread to read from a Google Drive Spreadsheet without logging in however, the solutions are to either have a client provide their auth code, or download the file directly, but no option to edit the file.
You want to edit Google Spreadsheet without logging in the Google for retrieving the authorization code.
You want to achieve this using gspread with python.
You have already been able to get and put values for Google Spreadsheet with Sheets API.
If my understanding is correct, how about this workaround? Please think of this as just one of several workarounds.
Workaround:
In this workaround, the service account is used. When the service account is used, you can get and put values for Google Spreadsheet without logging in Google account using the gspread.
In order to access to Google Spreadsheet with the service account, please do the following flow.
Create the service account and download the JSON file for using the service account.
Share the email address of the service account to the Google Spreadsheet you want to use.
You can see the email address at the downloaded JSON file.
Sample script:
The sample script for using the script of sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0') with the service account is as follows.
import gspread
from oauth2client.service_account import ServiceAccountCredentials
scope = ['https://www.googleapis.com/auth/spreadsheets']
credentials = ServiceAccountCredentials.from_json_keyfile_name('### downloaded JSON file ###', scope)
gc = gspread.authorize(credentials)
sht1 = gc.open_by_key('1wj9L7Hn779GKP0s4MblkU1wHcqaVcG_E2YKAo1vdof0')
Please set ### downloaded JSON file ###.
References:
Using OAuth 2.0 for Server to Server Applications
Create a G Suite service account
Unfortunately you can't what you want without authentication. Is a mandatory thing.
In the end if you take a look at Gspread you could see that is actually using the Google Sheets API. And through the API you need to authenticate even when accessing (like in your case) public data.
There are other ways to authenticate with the API but sincerely I don't know if they are supported by Gspread (I believe that they are not).
So you could use some workaround proposed in the question that you mentioned.
I have an access to the not-mine private spreadsheet on the Google Sheet. When I access to that spreadsheet from browser, everything is fine. But, when I try to retrieve the content of that spreadsheet through Google Sheets API using Python, I am getting 403 error - "The caller does not have permission".
The problem is I can not ask the owner to give me one more permission for my API.
Can I retrieve the content of that spreadsheet somehow? Maybe using some tools?
You prolly can access it because you got the link from someone. But to access it using the API will require you to be added in the permissions. Worth checking would be this Google Drive SDK: Sharing Files tutorial video and the Permissions docs.
I am trying to download specific sheet from a spread-sheet (on Google Drive) but unable to find a method to do so. I am using Python Client API library (v3) and passing file_id and mimeType in export_media() function as shown below:
request = service.files().export_media(fileId=file_id,mimeType='text/csv')
media_request = http.MediaIoBaseDownload(local_fd, request)
This code always export the sheet which is present at first place. Can you please describe a method through which I can download specific sheet/sheets by providing gid or any other parameter.
I don't think the Drive API has a feature to specify a sheet name.
Two workarounds spring to mind...
You could use the Sheets API (https://developers.google.com/sheets/api/reference/rest/) and write your own csv formatter. It sounds more complex than it is. It's probably 10 lines of code, especially if you go for Tab Separated instead of Comma Separated.
Use the Google Spreadsheet File/Publish to the Web feature to publish a csv of any given sheet. Note that the content will be public, so anybody with the link (which is pretty obtuse) would be able to read the data.
You can use an old visualization API URL (see other answer)
f'https://docs.google.com/spreadsheets/d/{doc_id}/gviz/tq?tqx=out:csv&sheet={sheet_name}'
To make this request using the Google API Python library, you can use the credentials you already have and create an HTTP client instance yourself:
http_client = googleapiclient.discovery._auth.authorized_http(creds)
response, content = http_client.request(url)
Check response.status before you proceed.
Note that this API behaves a bit differently than your regular CSV export. Specifically there are some things I saw it does with headers - it will make them disappear if they are not set to Plain Text on a numeric column (see here), or merge multiple text rows appearing in the top of your sheet as a single header row.