1
votes

Is there any way to call Google Sheet's File import function in Apps Script? I'm trying to import a csv file from a google drive folder to a google sheet. But I want to be able to set up an automated trigger to import a csv file with the same name regularly from the same google drive folder.

I'm aware that I can write an import function myself. And I actually wrote such function based on someone else's code I found on github. But the import function I have is very slow since it contains loops. Plus, parsing the entire csv file using the script is very slow. So I only want to use Google Sheet's embedded import function. -> I tried it and it's way faster than import a file using my script.

1

1 Answers

0
votes

The menu commands are only available via the user interface. From a script, you can convert CSV files to Google Sheets format using Advanced Drive Service. First, enable the service (v2 of the API) as described on the page. Then in the Apps Script, proceed as follows:

var fileId = '0B.......jQ';  // Id of the CSV file
var newFile = Drive.Files.copy({title: 'Converted File'}, fileId, {convert: true});
var newFileId = newFile.id;

Now you have a converted file in your Google Drive (with the name 'Converted File'), and the variable newFileId contains the Id by which you can access this file using the methods of SpreadsheetApp. In particular, you can get its contents and put them elsewhere.