Exporting big google sheets file as multiple csv with batch size 500

Viewed 629

I would like to download a 10000 row long singular sheet from Google Sheets split into 20 separate csv files with batch size of 500 rows each. Could this be possibly done using Google Sheets?

1 Answers

I wasn't able to find a native support for sheets to download and separate the files into multiple parts/fragments. But I was able to find a Google Apps Script code that saves the sheet into a csv.

So what I did was modify the code and then make it turn a sheet into multiple csv's.

The catch is, it will upload all those files into a folder in the logged in google drive and then you can download them in a zip file. (I'll show this later below)

Here is the source code.

And here is the modified code I did:

function saveAsCSV() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  // Update numFiles into how many files you want is outputted
  // The script will calculate how many rows it can place in a file equally
  // Excess will be placed into an additional file
  var numFiles = 5;
  var rowsPerFile = Math.floor(sheet.getDataRange().getLastRow() / numFiles);
  // create a folder from the name of the spreadsheet
  var folder = DriveApp.createFolder(ss.getName().toLowerCase().replace(/ /g,'_') + '_csv_' + new Date().getTime());
  // append ".csv" extension to the sheet name
  fileName = sheet.getName();
  // convert all available sheet data to csv format
  var csvFiles = convertRangeToCsvFile_(sheet, rowsPerFile);
  // create a file in the Docs List with the given name and the csv data
  csvFiles.forEach(function(csvFile, i){
    var csvName = fileName + '_' + (i + 1) + '.csv';
    folder.createFile(csvName, csvFile);
  });
}

function convertRangeToCsvFile_(sheet, rowsPerFile) {
  // get available data range in the spreadsheet
  var activeRange = sheet.getDataRange();
  try {
    var data = activeRange.getValues();
    var csvFiles = [];

    // 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];
        }

        if ((row + 1) % rowsPerFile === 0) {
          csvFiles.push(csv);
          csv = "";
        }
      }
    }
    // If there are remainders that doesnt reach the number of files needed.
    // It will add another file, containing all remainder rows there.
    // 10010 rows / 20 files = 10 rows excess
    // 21st file will contain those 10 rows excess
    // No excess means no 21st file
    if(csv) {
      csvFiles.push(csv);
    }
    return csvFiles;
  }
  catch(err) {
    Logger.log(err);
  }
}

Sample Data:

sample data

Sample Output:

output 1

Upon right clicking the folder created in Google Drive, you will have the option to download the whole folder as zip file containing all the csv files.

Here is the downloaded zip file containing all csv files:

downloaded file

Since there are 22 rows and the sample code wants 5 files, a 6th file was created containing the excess 2 rows

output 2

Related