2017-12-10 1 views
1

Gmail 메일을 스프레드 시트로 가져 오는 스크립트를 작성했습니다. 받은 편지함 규칙은 Gmail 라벨 "가져 오기 대기 중"으로 이동하고 스크립트는 메일을 처리 한 다음이 라벨을 제거하고 가져온 라벨에 추가합니다.스레드의 모든 메일 가져 오기 방지하기

"가져 오기 대기 중"레이블로 전자 메일을 받으면 레이블이 전체 스레드에 적용된 것처럼 보입니다. 메시지는 이전에 가져온 메시지와 동일한 제목을 가지므로 스레드에서 이미 가져온 다른 모든 메시지를 가져옵니다.

여기 내 스크립트입니다 :

var importedCount = 0; 
    var label = GmailApp.getUserLabelByName('Monitor/AwaitingImport'); 
    var threads = label.getThreads(0,50);  // An array of threads (limit set to 50, this could be increased) 

    if (threads.length != 0) { // If a thread exists 
    for (var i = 0; i < threads.length; i++) { // for each thread in the label 
     var threadsubject = threads[i].getFirstMessageSubject();  
     if (threadsubject.match(/(Check In)/)) { // if the subject contains "Check In" 
     var messages = threads[i].getMessages(); // get ALL the messages 
     for (var j = 0; j < messages.length; j++) { // for each message 
      var body = messages[j].getPlainBody(); 
      // Get key:value pairs from email into an array 
      var array = body.trim().split("\n"); 
      var newArray = []; 
      for (k = 0; k < array.length; k++) { 
      newArray = newArray.concat(array[k].toString().split("=")); 
      } 

      // Push other data to array that isn't in body of email 
      newArray.push("GmailMessageID", messages[j].getId()); 
      newArray.push("MessageDate", messages[j].getDate()); 

      // Create object 
      var newObj = {}; 
      for (var l =0 ; l< newArray.length ; l++) { 
      if (l%2 == 0){ 
       newObj[newArray[l]] = newArray[l+1]; 
      } 
      } 

      // Now insert object as record into the spreadsheet 
      insertRecordWithAnyFieldsIntoAnySheet('Incoming Logs', newObj, true); 
     } 
     // Finally remove the label from this message, mark it as read, and update the importedCount (for logging) 
     threads[i].removeLabel(GmailApp.getUserLabelByName('Monitor/AwaitingImport')); 
     threads[i].addLabel(GmailApp.getUserLabelByName('Monitor/Imported')); 
     importedCount++; 
     } 
    }  
    } 
    SpreadsheetApp.getActiveSpreadsheet().toast(importedCount + ' messages imported'); 

어떻게 수입되는 스레드의 모든 메시지를 방지하고, 방금 수신 된 하나를 가져올 수 있습니까?

  • 나는이 이미 시트로 가져올 메시지의 수천의 리소스를 많이 검색을 포함하는 것처럼, 이미 Gmail의 메시지 ID를 사용하여 수입 메시지에 대해 수신 메시지를 확인하지 않으 .

  • 이 잠재적으로 다른를 추가하여, 마지막으로 내가 읽지 않은 메시지를 가져올 시도

  • 수입되지 않은 다른 메시지를 그리워 때문에, 스레드에서 마지막 메시지를 가져 오지 않는다 스크립트에 대한 조건 (즉, if (messages[j].isRead())을), (위의 코드에서 도시하지 않음) 읽을 수 있지만이 여전히 기본 Gmail이 작품으로 스레드

답변

0

의 모든 메시지를 가져 듯 나의 스크립트가 메시지를 표시 할 것입니다 기초 스레드 및 GmailApp에서만 스레드와 함께 작동합니다. 가능한 해결 방법이 있습니다. 먼저 Gmail 설정으로 이동하여 대화 형식으로보기에서 까지를으로 설정해야합니다. 이렇게하면 각 이메일이 별도의 개체가됩니다. 이렇게하면 레이블이 메시지에 적용됩니다. 처음이 작업을 수행하면 스레드의 모든 메시지에 레이블이 있지만 이후에는 각 메시지에 자체 레이블이있을 수 있습니다.

GmailApp은 여전히 ​​스레드와 호환됩니다. 대신 Gmail API를 사용해야합니다. 사용을 시작하려면 here을 읽으십시오. 그럼 당신이하고 싶은 것은 이것의 라인을 따라 뭔가가 있습니다 :

queriedMessages = 
     Gmail.Users.Messages.list(userInfo.mail, { 
     'q': queryString, 
     'pageToken': execProperties.nextPageId 
     }); 

이것은 내 스크립트 예제입니다. UserInfo.mail은 간단한 이메일 'me'으로 바꿀 수 있습니다. queryString은 중요한 부분입니다. 이는 Gmail의 고급 검색 문자열과 동일합니다. 예를 들어 'in:all newer_than:2d -in:chats -in:trash -in:draft'은 2 일 이내에 행 아웃 채팅이 아닌 휴지통이나 초안이 아닌 모든 메일을 검색합니다. 이 쿼리를 작성하는 방법은 here을 참조하십시오. pageToken은 실제로 queriedMessages.nextPageToken에 있습니다. 다시 검색하고 페이지 1 대신 검색 결과 페이지 2를 시작하려는 경우에만 필요합니다.

6 분 이상 걸릴 것으로 생각되면 유용합니다. 사용자 또는 스크립트 속성 안에 pageToken을 저장하고 스크립트를 다시 시작하고 현재 실행을 종료 할 트리거를 만듭니다. 그런 다음 다시 시작하면 속성을 가져 와서 중단 한 페이지에서 시작합니다.

관련 문제