I am a beginner.
Modified the system to extract and fill in unread emails coming from email forms in a spreadsheet,
Based on the following advice, I thought it worked, but now I have extracted not only unread but also read (extracted/filled).
I know that the problem is the part that I modified, but I have a clue how to fix it.
Can someone teach me? . .

Problem i am experiencing
Gmail wants to extract only unread, but already read (extracted/filled) ones are now inserted.
(Send BBBBB by email form)
(Send more CCCCC via email form)
Ideal form
Corrections marked around

>>>>>><<<< Since "push to resultArr as many times as msgs.length in the returnData function", is it ok if we can associate thread or length (variable) with msgs in the returnData function? Both ...
I ’m a beginner, so I ’m sorry.

function getMailandInsert () {
  var sheet = SpreadsheetApp.getActiveSheet ();
  // start potision
  var start = 0;
  // Maximum number of emails to retrieve
  var max = 5;
  // number of data columns to insert into the spreadsheet
  var insertCol = 6;
  // Get incoming email that matches the conditions
  var threads = GmailApp.search ('is: unread from: ([email protected]) subject: (wakaran)', start, max);

  // number of threads matching the condition
  var length = threads.length;
  // The last row of the sheet where the data will be saved. That is, the insertion start position
  var row = sheet.getLastRow () + 1;
  // Array for storing the acquired mail contents
  var resultArr = new Array ();
   for (var n in threads) {
      var the = threads [n];
      // get mail in thread
      var msgs = the.getMessages ();
      // Analyze the mail in the thread and store it in resultArr
      returnData (msgs, resultArr);
      // Mark the acquired thread as read
      the.markRead ();
      Utilities.sleep (1000);
   if (length! = 0) {
      sheet.getRange (row, 1, msgs.length, insertCol) .setValues ​​(resultArr);// save data
// The contents of getRange are (final line + 1,1, number of threads, 6)
//>>>>>>>>>>length->msgs.length modified<<<<<<This is a problem. The cause is ignoring unread. The length value must match the resultArr value as it is.
function returnData (msgs, resArray) {

  for (m in msgs) {
    try {
      var tempArray = new Array ();
      var msg = msgs [m];
      // email date
      var date = msg.getDate ();
      // E-mail sender address
      var from = msg.getFrom ();
      // Email subject
      var subject = msg.getSubject ();
      // Get email body with PlainBody
      var body = msg.getPlainBody ();
      // Parse the body with an XML parser
      var xml = XmlService.parse (body);
      // Get the root element of the XML analysis result
      var root = xml.getRootElement ();
      // Specify each child element in XML and get its value.
      var hinichi = root.getChild ("hinichi"). getText ();
      var zzz = root.getChild ("zzz"). getText ();
      var xxx = root.getChild ("xxx"). getText ();
      var ddd = root.getChild ("ddd"). getText ();
      var vvv = root.getChild ("vvv"). getText ();
      var etc = root.getChild ("etc"). getText ();
      // store each value in an array
      tempArray [0] = hinichi;
      tempArray [1] = zzz;
      tempArray [2] = ddd;
      tempArray [3] = eisei;
      tempArray [4] = vvv;
      tempArray [5] = etc;
      // add array to the end with push
      resArray.push (tempArray);
     } catch (e) {
       Logger.log ("Error:" + e);
Supplemental information (FW/tool version etc.)

chrome latest version

  • Answer # 1

    A thread that is already read will be unread if a new message is received.
    CheckGmailMessage # isUnreadand ignore the read.

    function returnData (msgs, resArray) {
      for (m in msgs) {
        if (! msgs [m] .isUnread ())
        // (omitted)

Related articles