Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda