Loop through PDFs in Google Drive, display on Rails site - ruby-on-rails

I have a client storing PDFs in a public Google Drive folder. I would like to loop through that folder and display a clickable link (i.e. filename) on my Rails web page. I'm reading through the Google Drive API information here:
https://developers.google.com/drive/api/v3/about-sdk
Can anyone recommend additional methods or tutorials that might be useful for a rails app?
Alternatively, would it be better to use Amazon S3 and Paperclip EDIT. 5/25/18 Paperclip is deprecated. Active Storage has been recommended for file upload for file upload and storage? (Not sure I want to go this route. I think my client wants to create his file in Drive and voila! It appears on his web page. Just not sure that's possible.)
Newbie programmer here. Thanks.

To get a list of files from a Google Drive folder you'll need to use the Files:list method from the Drive API
In the Files:list page I generally find it useful to use the "Try this API" to get sample responses from the Google API.
To list all the files for a given Folder ID you'll want to use the following syntax for the q parameter:
'<folder_id_here>' in parents
For example:
'1Cz3KdJlzMj-vmN6OMd_nQyUKWlkdsfujo' in parents
For Ruby example specific to the Drive API, see:
https://developers.google.com/drive/api/v3/folder
For complete Ruby examples, maybe check out the following:
https://github.com/google/google-api-ruby-client-samples

Related

Get one stable link for Multiple excel sheets with the same name from Google Drive into PowerBi

On daily basis I am getting an attachment in email. Basically that attachment is an excel spreadsheet. I am pushing that spreadsheet to google drive folder ./attachments using Microsoft Power Automate. The main purpose of pushing that spreadsheet into google drive is to load into PowerBi for Analytics purposes.
In PowerBi I am using the "Web" connector to import the file and it's working fine. The sample link is below.
https://docs.google.com/spreadsheets/u/2/d/1eBJR6wrcFrdjv4Lbf_Wq3MQOeUwBbgLw/export?format=xlsx
The above link exports the file into powerBi and hence I can load data into PowerBi.
The problem is, on daily basis I am getting new file in drive and the Unique Id of the file is not same. In the above example the unique Id 1eBJR6wrcFrdjv4Lbf_Wq3MQOeUwBbgLw will be different for the second file even though I am renaming the file with the same name using Microsoft Power Automate when pushing to Google Drive. eg: "PowerBi load file.xlsx". Is that possible that I can get a stable link for all the files with the same name?.
I have also shared the whole folder ./attachments and tried to get the link of the file but that doesn't work. eg:
https://drive.google.com/drive/folders/1h1VuPtXfWflgIQw7ecMTwweoLblADscq/PowerBi Analytics file.xlsx/export?format=xlsx
Any help, suggestions will be really appreciated.
Thanks everyone.
I believe your goals as follows.
You want to retrieve the file IDs from a filename.
You want to retrieve the file IDs from a shared folder.
Answer for question 1:
In order to retrieve the file IDs from a filename, I think that the method of "Files: list" of Drive API can be used.
The endpoint is as follows.
GET https://www.googleapis.com/drive/v3/files?q=name%3D%27{filename}%27
The search query is name='{filename}'.
In this case, the API key cannot be directly used because the file list tries to be retrieved from the while Google Drive including the file. In this case, the access token is required to be used.
By this, I thought that your goal 2 might be suitable.
Answer for question 2:
In order to retrieve the file IDs from a shared folder, I think that the method of "Files: list" of Drive API can be also used. In this case, at least, the file list is retrieved from the shared folder. By this, the API key can be used.
The endpoint is as follows.
GET https://www.googleapis.com/drive/v3/files?q=%271h1VuPtXfWflgIQw7ecMTwweoLblADscq%27%20in%20parents&key=[YOUR_API_KEY]
The search query is '1h1VuPtXfWflgIQw7ecMTwweoLblADscq' in parents.
In this case, the file list can be retrieved using the API key because the folder is publicly shared and the file list is directly retrieved from the publicly shared.
But, in order to use this, it is required to use the API key. Please be careful this.
Other pattern:
If you want to achieve your goal without the API key and the access token, I would like to propose to use the Web Apps created by Google Apps Script as a wrapper API. When this Web Apps is used, you can achieve above both goals without using the API key and the access token.
The official document of Web Apps is here.
The unofficial document of Web Apps including several sample situations is here.
References:
Files: list
Search for files and folders

How to print documents and spreedsheets from google drive

I can't seem to find information on how to print files through google app script.
I found an answer in this website but it doesn't work, I think it's because it's 3 years old and google script has had some changes.
google app script print button
You can't print directly from GAS (I've searched far and wide), as described in the post that you linked. However it is possible to create a downloadable document with the desired content and then pass that blob off to the client for downloading (depending on wether you're still working in GAS or a web app environment).
Personally, I felt that it was a headache to deal with how each browser deals with downloading blobs. Therefore I usually go one of these two routes: provide a public downloadable link from the drive of the owner of the script and then push it to the user to download, OR just email the document to the user and let their email client handle downloading the blob.
Alternatively, if this is an add-on, you can make it so that the document is downloaded directly to the users own drive and just inform them where the document will be located.

Use Google Drive as an external editor

I have a website that stores, parses and display .docx files. I want to give users a way to edit those files.
I don't want to embed a third-party widget in my website and I thought that I could use Google Drive API. My idea is that I can give user a button “Edit in Google Drive” which back to my server somehow.
The problem is that I haven't found neither API function that allows to do that nor examples of such approach.
Am I on the right way? Can my idea work? If yes, then how can I do it?
Look at the drive api. There is one to upload a new file and optionally convert it to native google format.
To upload it, read about using oauth2 to authorize storing it on the user's drive. User must have Drive of course.
After the user finishes editing, they press a button or something on your site so you use the drive api to either read the gdoc and convert yourself to docx or use the drive api to download as docx.
Both uses of drive api are explained in the docs.

uploading to google drive with paperclip in Rails

Hello there i am new to using the google drive gem and i do not know how to use it with paperclip to upload photos to the google drive. Its a simple rails application that i am developing that uploads photos to google drive, is there any rails application example that i can use to get me started with the gem or can any body present an example here to aid we beginners using the google drive gem. Paperclip should be used to upload the files. Thank You
You'd likely have to implement a storage adapter for Paperclip. Take a look at something like https://github.com/dripster82/paperclipdropbox as an example. In that case, it looks like it only supports uploading to a single pre-authorized account. If that's OK, then modifying that to talk to Google Drive instead of Dropbox should be straightforward. If you need to support multiple users, you'd have to make sure the right credentials are being used for the current user.
Anyway, the paperclip docs are likely the best place to get started :)

Integrating Google Docs API

I'm just beginning with programming, but i wanted to know if it's possible to use google docs api to make documents on another site using the google docs text editor?
Is there some sort of way i can put the google docs text editor onto a website so that we can use that for document creation instead of tiny mce?
Basically the functionality needed would be documents created, openly shared, a postable version of it (take html code) -- so it can go on the document display page, and
Of course there would be google login and everything, but i just wanted to see if this would work.
No, that is not possible, sorry.

Resources