1

I want to check if a substring is in a string.

The string come from the 12th row of a Google sheet. The substring come from an HTML form where the user inserts data. (It's a simple string in the example below and it still doesn't work. The problem is not the data inserted.)

I want to display the row number of the string when the substring is detected.

for (var i=0; i < sheetDataRange.getValues().length; i++) {
  if (sheetDataRange.getValues()[i][11].includes("I14")) { // The string value is okay
    SpreadsheetApp.getUi().alert(i+5); // The data starts at the 5th row
  }
}

I tried to resolve the issue with the includes() function. The issue is that some rows without "I14" are displayed and other with it are not. I tried to use the match() function too but it doesn't work.

Thanks for your help.

3
  • Are you sure that sheetDataRange starts from row 5? Commented Aug 1, 2021 at 17:40
  • The string come from the 12th row but getValues()[i][11 is 12th column not a row. And actually you're using terribly wrong the method getValues(). Try @Cooper's solution. Commented Aug 2, 2021 at 9:51
  • funkizer, Yes, sure about it. The issue is in the condition. Yuri, I used the wrong word. The string comes from somewhere in the 12th column. Not row Commented Aug 2, 2021 at 10:52

1 Answer 1

1

You only need to getValues() once not everytime you go through the loop

const values = sheetDataRange.getValues();
const ui = SpreadsheetApp.getUi();
for (var i=0; i < values.length; i++) {
  if (values[i][11].toString().includes("I14")) { 
    ui.alert(i+5); // The data starts at the 5th row
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.