How to extract CSV data from email attachments and populate Google Sheets automatically

Hey everyone, I’m pretty new to Google Apps Script and could really use some guidance.

I’m trying to build a script that can grab CSV files from my email attachments and put the data into different sheets in my Google Spreadsheet. Here’s what I need to do:

  • I get emails with CSV attachments every day at the same time
  • Each email has a specific sender and subject line that I can use to identify them
  • Every email contains exactly one CSV file
  • The CSV files have identical names but come in different emails with unique subjects
  • I want the script to find these emails, extract the CSV data, and completely replace whatever is already in my spreadsheet sheets
  • Each email subject should match up with a specific sheet in my workbook

I found this code online but it keeps throwing an error:

function ImportEmailCSVData() {
 var folderLabel = "Daily Reports";
 var emailSubjects = ["Report A", "Report B", "Report C"];
 var sheetFileId = "98765432100123456789";

 for (var x = 0; x < emailSubjects.length; x++) {
   var currentSubject = emailSubjects[x];
   var targetSheet = currentSubject;
   var worksheet = SpreadsheetApp.openById(sheetFileId).getSheetByName(targetSheet);
   
   worksheet.clear();
   
   var emailThreads = GmailApp.search('label:' + folderLabel + ' subject:' + currentSubject);
   
   for (var y = 0; y < emailThreads.length; y++) {
     var currentThread = emailThreads[y];
     var threadMessages = currentThread.getMessages();
     
     for (var z = 0; z < threadMessages.length; z++) {
       var currentMessage = threadMessages[z];
       var csvFiles = currentMessage.getAttachmentsByType('application/csv');
       
       if (csvFiles.length > 0) {
         var csvFile = csvFiles[0];
         var fileContent = Utilities.newBlob(csvFile.getDataAsString()).getDataAsString();
         var parsedData = Utilities.parseCsv(fileContent);
         worksheet.getRange(worksheet.getLastRow() + 1, 1, parsedData.length, parsedData[0].length).setValues(parsedData);
       }
     }
   }
 }
}

The error I’m getting is:
TypeError: currentMessage.getAttachmentsByType is not a function

Can anyone help me figure out what’s wrong with this approach? I’ve been stuck on this for a while and would really appreciate any tips or corrections.

The issue arises because getAttachmentsByType is not supported in Apps Script. Instead, utilize getAttachments() and manually verify the content type. Additionally, there’s a logic error with getLastRow()—since you clear the sheet before populating it, it will always return 1. Thus, start at row 1 when setting values.

To manage attachments correctly, modify the handling code as follows:

var attachments = currentMessage.getAttachments();
for (var a = 0; a < attachments.length; a++) {
  if (attachments[a].getContentType() === 'text/csv') {
    var csvContent = attachments[a].getDataAsString();
    var parsedData = Utilities.parseCsv(csvContent);
    worksheet.getRange(1, 1, parsedData.length, parsedData[0].length).setValues(parsedData);
    break;
  }
}

Since you receive the emails consistently at the same time daily, adding a date filter like newer_than:1d to your search query can prevent processing old emails, which helps avoid quota issues in the future.

Yeah, that error’s because getAttachmentsByType() doesn’t exist. Use getAttachments() instead, then loop through and check getContentType() on each one. Also, your code might pull multiple emails - throw in some date filtering to only grab today’s emails.

The error happens because getAttachmentsByType doesn’t exist. Use getAttachments() and filter by content type instead.

But honestly, Google Apps Script for email automation is a nightmare. Been there - it gets messy fast with Gmail API limits, parsing headaches, error handling, and code that’s impossible to maintain.

Switched to Latenode last year for this exact thing and it’s night and day. You build the whole process visually - zero code needed.

Here’s what you get:

  • One-click Gmail connection
  • Email triggers based on sender/subject
  • Automatic CSV extraction
  • Direct data push to Google Sheets
  • Replace existing data or append rows
  • Built-in error handling with retry logic

Best part? Everything’s in a visual flowchart. When something breaks, you see exactly where. No more debugging cryptic JavaScript or hitting Gmail quotas.

Built our daily sales report automation this way - runs perfectly every morning. 10 minutes to set up vs hours of coding and testing.

Check it out: https://latenode.com