javascript - .findIndex() - String is not a function. Google Apps Script - Stack Overflow

The Problem.findIndex() is breaking. I'm providing it with a string, but it's saying "I

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:([email protected]) to:([email protected]) 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

Example Spreadsheet

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

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:([email protected]) to:([email protected]) 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

Example Spreadsheet

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

Share Improve this question edited Nov 29, 2020 at 2:26 Bjaeg asked Nov 29, 2020 at 2:15 BjaegBjaeg 3382 silver badges14 bronze badges 3
  • console.log(columnValues) are you sure you have an array? – skyline3000 Commented Nov 29, 2020 at 2:18
  • I set up a small example script outside of my main script to get it working. It seems to work perfectly fine on its own. – Bjaeg Commented Nov 29, 2020 at 2:21
  • 1 A single column is still a two dimensional array try to use flat() – Cooper Commented Nov 29, 2020 at 2:35
Add a ment  | 

1 Answer 1

Reset to default 5

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:

  • findIndex()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664932a4639065.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信