Google Apps Script is a powerful development platform by Google, where you code in JavaScript and can easily access built-in libraries for your favorite G Suite applications like Gmail, Calendar, Drive, and more to create enhanced option/functionalities which are not available. directly. Here in this post, we’ll see how we can Import CSV file data to Google Spreadsheet using Google Apps Script.
Integration of Google Apps Script:
This is where the best part of the Google Apps Script comes. You can use Google Apps Script to work and build functionalities on any Google Application you can see below.
Reasons why Google Apps Script Rocks :
- You can create custom dialogs, menus, and Sidebars(using HTML and Javascript) to add extra functionality to Google applications
- No Hardware Software deployments, maintenance, etc.
- Write on the web, run on the web
- No need to think about security and data access permissions, since Google has got your back
- Triggers which allows you to schedule your scripts to run automatically depend on time or an event-driven
Code to Import CSV file data to Google Spreadsheet using Google Apps Script:
Below is the code to help you in importing your CSV file data to Google SpreadSheet automatically and the data in spreadsheet changes whenever the data in the CSV file changes.
function importCSVtoGoogleSheet() { #This will select the active Google Spreadsheet along with the range from Column A to AB var sheet = SpreadsheetApp.getActiveSheet().getRange("A:AB"); #This will clear all the contents for the above range sheet.clearContent(); #This will search the drive to find the file with the name Test.csv all the contents for the above range var file = DriveApp.getFilesByName("Test.csv").next(); #This will parse the data from CSV as string var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); #This will again select the active Google Spreadsheet var sheet = SpreadsheetApp.getActiveSheet(); #This will select the range same as CSV file and import the data from CSV to spreadsheet sheet.getRange(1,1,csvData.length, csvData[0].length).setValues(csvData); }
Also if you want to specify the particular CSV file instead of searching it on Google Drive with the name, you can replace the code above and specifically use the file id as shown below.
function importCSVtoGoogleSheet() { #This will select the active Google Spreadsheet along with the range from Column A to AB var sheet = SpreadsheetApp.getActiveSheet().getRange("A:AB"); #This will clear all the contents for the above range sheet.clearContent(); #This will search for the particular file using the file id var file = DriveApp.getFileById("1E-yhM-qrhCfhsjHT5Su0n_vhwgamAs4y9"); #This will parse the data from CSV as string var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); #This will again select the active Google Spreadsheet var sheet = SpreadsheetApp.getActiveSheet(); #This will select the range same as CSV file and import the data from CSV to spreadsheet sheet.getRange(1,1,csvData.length, csvData[0].length).setValues(csvData); }
Hope you have liked our post. If you want to learn more, you can check the Offical Website anytime. Also, please follows us for more content like this and if you need any help please reach out to me using the comment section.
Check out all the Google Apps Script related posts here and for python scripts here.
Fabio says
Hi everybody,
Could you tell me if there is the same solution in Java code?
Thank you!
🙂
stefano says
hello,
there is a way to import with scrypt only column 1, 5, 20 from csv to google scrypt. I use a scrypt but it will import full csv
thank’s a lot