Always know where your towel is.

Google Apps Script: Importing a text file via a form file input

Google Apps Script

One of the organisations I work for makes heavy use of Google Apps such as Drive & Spreadsheet. They wanted the ability to upload/import a CSV file, perform automated operations on the data and finally spit out the result into a Google Spreadsheet.

I built the text/csv file import facility by using the FileReader API. Essentially we read the file locally and pass the data along to Google Script.


<!-- Use a templated HTML printing scriptlet to import common stylesheet. -->
<?!= HtmlService.createHtmlOutputFromFile('Stylesheet').getContent(); ?>

<!-- Below is the HTML code that defines the sidebar element structure. -->
<div class="sidebar branding-below">

  Import the CSV file to automatically format it

  <form id="csvForm">

    <div class="form-element file-input">
      <input type="file" name="myFile" id="myFile">

    <div class="form-element">
      <button type="submit" id="upload-csv" class="action">Upload CSV</button>


  <div id="output"></div>


<!-- Enter sidebar bottom-branding below. -->
<div class="sidebar bottom">
  <span class="gray branding-text">By Trevor Wistaff</span>

<!-- Use a templated HTML printing scriptlet to import JavaScript. -->
<?!= HtmlService.createHtmlOutputFromFile('SidebarJavaScript').getContent(); ?>


<script src="//"></script>
  var form = document.getElementById('csvForm');

  form.addEventListener('submit', function(e) {
    // Prevent the default submit behaviour
    // Reset the output text if any
    document.getElementById('output').innerHTML = '';
    // Read the CSV

  function readCSV() {
    // Grab the first file out of the array
    var myFile = form.myFile.files[0];

    // Very basic file type checking
    if('.').pop().toLowerCase() === 'csv') {
      // Initialize the FileReader
      var reader = new FileReader();

      // Define the onload method belonging to the reader object
      reader.onload = function(e) {
        // Here I'm using Papa Parse to parse the CSV
        var csv = Papa.parse(reader.result);
        // Define 2 handlers that handle the result of processCSV and finally call processCSV()
        // which is defined in my file.;
      // Read the file as text. This will then run reader.onload define above.
    } else {
      document.getElementById('output').innerHTML = "That's just silly, remember you can only import CSV files!";

  function postHandler(status) {
    document.getElementById('output').innerHTML = status;

Note: You must have your script’s sandbox mode set to IFRAME. As of yesterday, all new GA Scripts will default to IFRAME as per their sunset schedule, so unless you’re working on an old script, no need to worry about this.

Post navigation

1 comment for “Google Apps Script: Importing a text file via a form file input

  1. Tiago
    May 30, 2017 at 03:55

    Hello, thanks for your post.
    I’m new to this and trying to import a .csv to googlesheets.
    Your solution appears to be missing the function processCSV, or am I wrong?

Leave a Reply

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