I have a legacy database system (not web accessible) on a server which generates CSV or XLS reports to a Google Drive folder. Currently, I am manually opening those files in Drive web interface and converting them to Google Sheets.
I would rather this be automatic so that I can create jobs that append/transform and graph the data in other sheets.
Is it possible to output a native .gsheet file? Or is there a way to convert CSV or XLS to .gsheet programmatically after saving it to Google Drive either in Google Apps or via a Windows based script/utility?
(Mar 2017) The accepted answer is not the best solution. It relies on manual translation using Apps Script, and the code may not be resilient, requiring maintenance. If your legacy system autogenerates CSV files, it's best they go into another folder for temporary processing (importing [uploading to Google Drive & converting] to Google Sheets files).
My thought is to let the Drive API do all the heavy-lifting. The Google Drive API team released v3 at the end of 2015, and in that release,
insert()
changed names tocreate()
so as to better reflect the file operation. There's also no more convert flag -- you just specify MIMEtypes... imagine that!The documentation has also been improved: there's now a special guide devoted to uploads (simple, multipart, and resumable) that comes with sample code in Java, Python, PHP, C#/.NET, Ruby, JavaScript/Node.js, and iOS/Obj-C that imports CSV files into Google Sheets format as desired.
Below is one alternate Python solution for short files ("simple upload") where you don't need the
apiclient.http.MediaFileUpload
class. This snippet assumes your auth code works where your service endpoint isDRIVE
with a minimum auth scope ofhttps://www.googleapis.com/auth/drive.file
.Better yet, rather than uploading to
My Drive
, you'd upload to one (or more) specific folder(s), meaning you'd add the parent folder ID(s) toMETADATA
. (Also see the code sample on this page.) Finally, there's no native .gsheet "file" -- that file just has a link to the online Sheet, so what's above is what you want to do.If not using Python, you can use the snippet above as pseudocode to port to your system language. Regardless, there's much less code to maintain because there's no CSV parsing. The only thing remaining is to blow away the CSV file temp folder your legacy system wrote to.
You can programmatically import data from a csv file in your Drive into an existing Google Sheet using Google Apps Script, replacing/appending data as needed.
Below is some sample code. It assumes that: a) you have a designated folder in your Drive where the CSV file is saved/uploaded to; b) the CSV file is named "report.csv" and the data in it comma-delimited; and c) the CSV data is imported into a designated spreadsheet. See comments in code for further details.
You can then create time-driven trigger in your script project to run
importData()
function on a regular basis (e.g. every night at 1AM), so all you have to do is put new report.csv file into the designated Drive folder, and it will be automatically processed on next scheduled run.If you absolutely MUST work with Excel files instead of CSV, then you can use this code below. For it to work you must enable Drive API in Advanced Google Services in your script and in Developers Console (see How to Enable Advanced Services for details).
The above code is also available as a gist here.
You can get Google Drive to automatically convert csv files to Google Sheets by appending
to the end of the api url you are calling.
EDIT: Here is the documentation on available parameters: https://developers.google.com/drive/v2/reference/files/insert
Also, while searching for the above link, I found this question has already been answered here:
Upload CSV to Google Drive Spreadsheet using Drive v2 API