Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
How to prevent duplicate emails being added to google sheet - App Script

I am new to App Script. My code is below. I have been trying to take data out of an email and put it into different columns in google sheets. I have managed to achieve this and it works but because labels are applied to threads I get duplicates!

I have tried to figure out how to stop this from happening by using the email ID, date etc but I haven't been successful. Any help would be greatly appreciated.

function email_sheet() {
  var ss = SpreadsheetApp.openById("");
  var sheet = ss.getSheetByName("Sheet1");
  var label = GmailApp.getUserLabelByName("ChosenLabel");
  var threads = label.getThreads();

  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var date = messages[j].getDate();
      var body = messages[j].getPlainBody();

      var name = "";
      var accnum = "";
      var paytype ="";
      var amount = "";
      var status = "";

      /** Break Down the Email */
      if(body.indexOf("Recipient : ")>0) {
        var end = body.substring(body.indexOf("Recipient : ")+12,body.length);
        name = end.substring(0, end.indexOf("\n"));
      }

      if(body.indexOf("AN")>0) {
        var end = body.substring(body.indexOf("AN")+2,body.length);
        account = end.substring(0, end.indexOf("\n"));
        var [accnum, paytype] = account.split(" ");
      }

      if(body.indexOf("Amount : ")>0) {
        var end = body.substring(body.indexOf("Amount : ")+9,body.length);
        amount = end.substring(0, end.indexOf("\n"));
      }

      if(body.indexOf("Transaction Status : ")>0) {
        var end = body.substring(body.indexOf("Transaction Status : ")+21,body.length);
        status = end.substring(0, end.indexOf("\n"));
      }

      sheet.appendRow([date, name, accnum, paytype, amount, status]);

    }
  threads[i].removeLabel(label);
  threads[i].addLabel(GmailApp.getUserLabelByName("All Transactions"))
  }
} ```
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can for instance limit to unread mails and at the end mark them as already read, for instance

function mail() {
  var requete ="is:unread {label:ChosenLabel label:OtherLabel}"
  var ss = SpreadsheetApp.getActive().getSheetByName("Mail");
  var threads = GmailApp.search(requete);
  for (var i = 0; i < threads.length; i++) {
    var messages = threads[i].getMessages();
    for (var j = 0; j < messages.length; j++) {
      var msg = messages[j].getPlainBody();
      var sub = messages[j].getSubject();
      var dat = messages[j].getDate();
      ss.appendRow([dat, sub, msg])
    }
  }  
  GmailApp.markThreadsRead(threads);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!