Skip to content

Instantly share code, notes, and snippets.

@bpwebs
Created November 18, 2023 19:37
Show Gist options
  • Save bpwebs/94c1aa7a76ee6b859ca83e072ecb6e72 to your computer and use it in GitHub Desktop.
Save bpwebs/94c1aa7a76ee6b859ca83e072ecb6e72 to your computer and use it in GitHub Desktop.
Extract and combine first row of CSV files with Google Apps Script
function extractFirstRowCsvWithFilename() {
const folder = DriveApp.getFolderById("1MBaI_rGJ6t_loANMAvGpDbr2MeHZwxnn");
const files = folder.getFilesByType(MimeType.CSV);
let combinedData = [];
while (files.hasNext()) {
let file = files.next();
let filename = file.getName();
let csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
// Add the filename to the first column.
csvData[0].unshift(filename);
combinedData.push(csvData[0]);
}
folder.createFile("CombinedFirstRowWithFilename.csv", combinedData.join("\n"), MimeType.CSV);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment