Warm tip: This article is reproduced from serverfault.com, please click

.findIndex()

发布于 2020-11-29 02:15:46

The Problem

.findIndex() is breaking. I'm providing it with a string, but it's saying "It's not a function"

Objective / Context

Auto reply to property inquiry

Function does the following:

  • Scans the inbox to identify inquiry.

  • Breaks down the email, storing the data in variables.

  • Scans a spreadsheet with information about all of our homes for sale. Matches the property ID in the email against the spreadsheet looking for the match.

  • Replys to the email with data stored in the spreadsheet that matches that property.

Where I am up to

I am able to identify the email. I'm then able to scan through the email and pull the relevant information.

The Code

function autoReply() {
var queryInbox = "is:unread from:(example@gmail.com) to:(example@gmail.com) Example Text:"; // Email Identification
var locatedEmail = GmailApp.search(queryInbox); // Search Inbox for Identification
locatedEmail.forEach(thread => {
  var messages = thread.getMessages();
  if (messages.length === 1) {
    // BREAKING DOWN THE EMAIL
    var msgBody = messages[0].getPlainBody();
    var identityNumber = msgBody.split("\n")[1].replace('Property ID: ','');
  
    // SPREADSHEET      
    var SS = SpreadsheetApp.openById('exampleId').getSheetByName("Sheet1");
    var column = 1; // column Index
    var columnValues = SS.getRange(2, column, SS.getLastRow()).getValues(); //1st is header row
    var searchResult = columnValues.findIndex(identityNumber); //Row Index - 2
    var rowID = searchResult + 2;
  
    var houseAddress = SS.getRange(rowID, 2).getValue();
  
    // EMAIL BODY
    var body = "<p>Hi, thank you for emailing us about " + houseAddress + ".</p>";
    var options = { name: "Property Enquiry", htmlBody: body };
    thread.reply(body, options);
    thread.markRead();
    thread.moveToArchive();
  }
});
}

Example Email

enter image description here

Example Spreadsheet enter image description here

Additional Information: Error is showing that it is passing a propertyID as a string to the .findIndex

enter image description here

Questioner
Bjaeg
Viewed
0
Tanaike 2020-11-29 10:35:08

Modification points:

  • In your script, columnValues of var columnValues = SS.getRange(2, column, SS.getLastRow()).getValues() is 2 dimensional array like [["sample1"],["sample2"],["sample3"],,,]. And the argument of findIndex is a function. I think that the reason of your issue is due to this.
  • When you want to retrieve the index of searchResult using the value of identityNumber, please modify as follows.

Modified script:

From
var searchResult = columnValues.findIndex(identityNumber);
To
var searchResult = columnValues.findIndex(([r]) => r == identityNumber);
  • In your script, it seems that the column "B" of the searched row is retrieved.

Note:

  • In your situation, when TextFinder is used, your script can be also modified as follows.

    • From

        var columnValues = SS.getRange(2, column, SS.getLastRow()).getValues(); //1st is header row
        var searchResult = columnValues.findIndex(identityNumber); //Row Index - 2
        var rowID = searchResult + 2;
      
        var houseAddress = SS.getRange(rowID, 2).getValue();
      
    • To

        var houseAddress = SS.getRange(2, column, SS.getLastRow()).createTextFinder(identityNumber).findNext().offset(0, 1).getValue();
      

Reference: