WeirdGeek

Data Science | Machine Learning | Automation

  • Data Analytics
  • Python
  • Data Science
  • Google Apps Script
  • Machine Learning
  • Artificial Intelligence
  • SQL Server

21/07/2019 By WeirdGeek 2 Comments

Import CSV file data to Google Spreadsheet using Google Apps Script

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.

Import CSV file data to Google Spreadsheet using Google Apps Script

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.

Related posts:

  1. Google Apps Script to search a string in a column in Google Sheet [Part 1]
  2. Google Apps Script to search a string in Google Sheet [Part II]
  3. Getting Spreadsheet creation and modification date using GAS
  4. Find and Replace in Google Apps Script

Filed Under: Google Apps Script Tagged With: code, Google, Google Apps Script, Script

Comments

  1. Fabio says

    07/12/2020 at 7:15 am

    Hi everybody,

    Could you tell me if there is the same solution in Java code?

    Thank you!

    🙂

    Reply
  2. stefano says

    26/01/2021 at 4:28 am

    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

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Subscribe to my Blog !!

Enter your email below to subscribe my blog and get the latest post right in your inbox.

  • Home
  • Terms
  • Privacy
  • Contact Us

Copyright © 2025 · WeirdGeek · All trademarks mentioned are the property of their respective owners.