Last active
November 21, 2023 01:02
-
-
Save bpwebs/90d562b852e0518b73f192f6fbdb2a2e to your computer and use it in GitHub Desktop.
Combine CSV Files with Google Apps Script - Prevent the column headers from being repeated in the merged CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function combineCSVWithoutRepeatedHeaders() { | |
const folder = DriveApp.getFolderById("1MBaI_rGJ6t_loANMAvGpDbr2MeHZwxnn"); | |
const files = folder.getFilesByType(MimeType.CSV); | |
let combinedData = []; | |
let firstFile = true; | |
while (files.hasNext()) { | |
let file = files.next(); | |
let csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); | |
if (firstFile) { | |
firstFile = false; // Skip headers for the first file | |
} else { | |
csvData.shift(); // Remove headers for subsequent files | |
} | |
combinedData = combinedData.concat(csvData); | |
} | |
folder.createFile("Combined1.csv", combinedData.join("\n"), MimeType.CSV); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment