Skip to content

Instantly share code, notes, and snippets.

@shaleh
Created October 18, 2024 00:36
Show Gist options
  • Save shaleh/3de8f6f1473a7019367a0723f9860cdf to your computer and use it in GitHub Desktop.
Save shaleh/3de8f6f1473a7019367a0723f9860cdf to your computer and use it in GitHub Desktop.
How to import a large batch of CSV into a Google spreadsheet with each CSV file being its own separate sheet. The sheet is named after the CSV file it is sourced from.
function convertCSVFilesToSheets() {
var folderId = "DRIVE_FOLDER_ID"; // Replace with the ID of your folder
var spreadsheetId = "SHEET_ID"; // Replace with the ID of your spreadsheet
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();
var spreadsheet = SpreadsheetApp.openById(spreadsheetId);
while (files.hasNext()) {
var file = files.next();
if (file.getMimeType() == "text/csv") {
var csvData = file.getBlob().getDataAsString();
var csvLines = csvData.split('\n');
var sheet = spreadsheet.insertSheet(file.getName());
for (var i = 0; i < csvLines.length; i++) {
var rowData = csvLines[i].split(',');
sheet.getRange(i + 1, 1, 1, rowData.length).setValues([rowData]);
}
}
}
}
@shaleh
Copy link
Author

shaleh commented Oct 18, 2024

To use.
Upload files to a folder in Google Drive.
Get the ID of the folder. It is in the share link.
Make a new spreadsheet.
Get the ID from the URL.
Go to Extensions. Choose App Script.
Paste the above code.
Edit and replace the drive folder id and sheet id with the IDs you copied earlier.
Save.
Run.
Give it permissions when asked.
Wait.....
Enjoy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment