13

Is there a way to export all sheets in a google spreadsheet to csv as individual files ?

0

1 Answer 1

9

Couldn't find any built in way to do it so I wrote a script to use in Google Sheets

Just paste it in script editor and run

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var csvMenuEntries = [{name: "export as csv files", functionName: "saveAsCSV"}];
  ss.addMenu("csv", csvMenuEntries);
};

function saveAsCSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheets();
  // create a folder from the name of the spreadsheet
  var folder = DriveApp.createFolder(ss.getName().toLowerCase().replace(/ /g,'_') + '_csv_' + new Date().getTime());
  for (var i = 0 ; i < sheets.length ; i++) {
    var sheet = sheets[i];
    // append ".csv" extension to the sheet name
    fileName = sheet.getName() + ".csv";
    // convert all available sheet data to csv format
    var csvFile = convertRangeToCsvFile_(fileName, sheet);
    // create a file in the Docs List with the given name and the csv data
    folder.createFile(fileName, csvFile);
  }
  Browser.msgBox('Files are waitig in a folder named ' + folder.getName());
}

function convertRangeToCsvFile_(csvFileName, sheet) {
  // get available data range in the spreadsheet
  var activeRange = sheet.getDataRange();
  try {
    var data = activeRange.getValues();
    var csvFile = undefined;

    // loop through the data in the range and build a string with the csv data
    if (data.length > 1) {
      var csv = "";
      for (var row = 0; row < data.length; row++) {
        for (var col = 0; col < data[row].length; col++) {
          if (data[row][col].toString().indexOf(",") != -1) {
            data[row][col] = "\"" + data[row][col] + "\"";
          }
        }

        // join each row's columns
        // add a carriage return to end of each row, except for the last one
        if (row < data.length-1) {
          csv += data[row].join(",") + "\r\n";
        }
        else {
          csv += data[row];
        }
      }
      csvFile = csv;
    }
    return csvFile;
  }
  catch(err) {
    Logger.log(err);
    Browser.msgBox(err);
  }
}

See more here: http://drzon.net/export-all-google-sheets-to-csv/

5
  • This worked from my personal Gmail account as of 6/2018. May want to confirm and revise your answer accordingly. Thanks for this script -- it worked like a charm to dump out 30 tabs from Google Sheets!
    – Corey S.
    Jun 23, 2018 at 21:33
  • Michael unlike the Google Sheets export function, this script exports with empty rows of data. How can I remove them?
    – user3871
    Apr 28, 2019 at 1:21
  • 1
    DocsList in line 11 has to be replaced with DriveApp for the new version, but otherwise this worked great for a sheet with dozens of tabs that I had.
    – Sixhobbits
    Nov 12, 2019 at 17:44
  • 1
    @Sixhobbits thanks, I actually updated the gist source, but not the answer. Updated now
    – Michael
    Nov 14, 2019 at 12:59
  • Excellent! Thank you for this... Made my life so much easier!
    – rimkashox
    Feb 6, 2021 at 0:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.