How to extract data from Gmail messages and populate Google Sheets automatically

I get daily emails from my custom application that contain reports with changing numerical values. The email structure stays the same but certain numbers vary each day (like getting 7 one day and 3 the next). I want to create an automated system that pulls these numbers from my emails and updates a Google Sheets document.

Here’s what I tried so far:

function extractEmailData() {
 var emailThread = GmailApp.getUserLabelByName("").getThreads(0,1)[0];
 var emailMessage = emailThread.getMessages()[0];
 Logger.log(emailMessage.getBody());
}

The script runs but nothing happens. Can someone help me figure out what’s going wrong with my approach?

Your script logs the email body but doesn’t extract or write any data. After fixing that empty label name, you’ll need to parse the email content with regular expressions to find your numbers. Try var numbers = emailBody.match(/\d+/g) to grab all numbers from the email. Then use the Sheets API: SpreadsheetApp.openById('your-sheet-id').getRange('A1').setValue(numbers[0]) to write those values to your spreadsheet. I do similar automation for expense reports - creating a Gmail filter to label these emails first makes everything way more reliable.

your code’s missing the label name - there’s empty quotes in getUserLabelByName(""). you need to specify which label you’re searching for, or just use getInboxThreads() instead. also double-check you’ve got emails in that label, otherwise the array comes back empty and crashes everything.

Your script grabs the email but doesn’t do anything with it - there’s no Sheets integration. Plus the empty label issue others mentioned. I built something similar for invoice tracking and getPlainBody() works way better than getBody() since it strips HTML that messes up pattern matching. You’ll need error handling too because emails don’t always show up on time or the format might change. Try GmailApp.search('from:your-app-email newer_than:1d') instead of labels to grab recent emails from specific senders. Check your Logger.log output in the Apps Script editor under Execution Transcript - you need to see what the email content actually looks like before writing extraction patterns.