最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin5PV0评论

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()
Post a comment

comment list (0)

  1. No comments so far