Parse and Process CSV Files with Google Apps Script
CSV files are one of the most common data exchange formats. Apps Script can read them from Drive, parse them into arrays, and process or import the data into Sheets — all without any external libraries.
Read a CSV file from Google Drive
functionreadCsvFromDrive(){const fileId ='YOUR_CSV_FILE_ID';// Get this from the file's Drive URLconst file =DriveApp.getFileById(fileId);const content = file.getBlob().getDataAsString();Logger.log(content);// Raw CSV text}
Parse CSV text into a 2D array
Apps Script doesn't have a built-in CSV parser, but Utilities.parseCsv() handles it cleanly:
functionparseCsv(){const fileId ='YOUR_CSV_FILE_ID';const file =DriveApp.getFileById(fileId);const content = file.getBlob().getDataAsString();const rows =Utilities.parseCsv(content);// rows is a 2D array: rows[0] is the header, rows[1..n] are data rows rows.forEach((row, i)=>{Logger.log(`Row ${i}: ${row.join(' | ')}`);});}
Utilities.parseCsv() handles quoted fields and commas within values correctly.
Import CSV data into a Google Sheet
functionimportCsvToSheet(){const fileId ='YOUR_CSV_FILE_ID';const file =DriveApp.getFileById(fileId);const content = file.getBlob().getDataAsString();const rows =Utilities.parseCsv(content);const sheet =SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.clearContents();// Write all rows at once (much faster than appendRow in a loop) sheet.getRange(1,1, rows.length, rows[0].length).setValues(rows);Logger.log(`Imported ${rows.length} rows.`);}
Filter rows during import
functionimportFilteredCsv(){const fileId ='YOUR_CSV_FILE_ID';const content =DriveApp.getFileById(fileId).getBlob().getDataAsString();const rows =Utilities.parseCsv(content);const headers = rows[0];const statusIndex = headers.indexOf('Status');// Find the "Status" column// Only import rows where Status === "Active"const filtered = rows.filter((row, i)=> i ===0|| row[statusIndex]==='Active');const sheet =SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); sheet.clearContents(); sheet.getRange(1,1, filtered.length, filtered[0].length).setValues(filtered);Logger.log(`Imported ${filtered.length-1} active rows.`);}
Process a CSV without writing to a Sheet
functionprocessCsvData(){const fileId ='YOUR_CSV_FILE_ID';const content =DriveApp.getFileById(fileId).getBlob().getDataAsString();const rows =Utilities.parseCsv(content);const headers = rows[0];const dataRows = rows.slice(1);// Convert to array of objectsconst records = dataRows.map(row=>{const obj ={}; headers.forEach((header, i)=>{ obj[header.trim()]= row[i];});return obj;});// Example: sum a numeric columnconst total = records.reduce((sum, r)=> sum +parseFloat(r['Amount']||0),0);Logger.log('Total Amount: '+ total);}