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
Additional Information: Error is showing that it is passing a propertyID as a string to the .findIndex
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.searchResult
using the value of identityNumber
, please modify as follows.var searchResult = columnValues.findIndex(identityNumber);
To
var searchResult = columnValues.findIndex(([r]) => r == identityNumber);
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();