Home>

I am a beginner.
With reference to the following URL, we have created a system that extracts email forms into spreadsheets.
https://www.virment.com/google-apps-script-gmail-spreadsheet/
I made it according to the site, but the following error message occurred from the second email.

Error message
The number of data lines does not match the number of lines in the range. The data is X rows, but the range is 1. (Line 55, file "code")


The number of X is in the line of var thread, it was the number of threads that includedreadof the corresponding mail in GmailApp.search.

GAS
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, length, insertCol) .setValues ​​(resultArr);// save data
// ######## Above is the line where the error occurred. The error message says that the number of data lines is more than one line ... ##########
// The contents of getRange are (final line + 1,1, number of threads, 6)
   }
}
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);
     }
    }
}

Only the first email worked, but it didn't work after the second one.
It may not be enough, but thank you.

Supplemental information (FW/tool version etc.)

chrome latest version

  • Answer # 1

    I just read it briefly, but it pushesresultArrbymsgs.lengthnumber of times in thereturnDatafunction
    If the email thread contains multiple emails, or if an error occurs inreturnData

    sheet.getRange (row, 1, length, insertCol) .setValues ​​(resultArr);// Save data


    So,lengthandresultArr.lengthmay not match.

Related articles