patu
patu

Reputation: 27

Fetch text string from specific column and select range of that string

I have shared below link of my sheet.

I tried every possible script to accomplish below task in the end in frustration i wipe out my whole script.

I would like to match text from column A and return or getValue of corresponding B column.

So I can use that getValue from its corresponding B column for further arithmetic operations.

Thank you.

sheet link - https://docs.google.com/spreadsheets/d/1SwYYacz9A9s6ZXrL44KtRN7gqnwXkhu4cDILdUA1iJQ/edit?usp=sharing

Upvotes: 0

Views: 40

Answers (1)

ziganotschka
ziganotschka

Reputation: 26806

Basic steps:

  • Retrieve your data range
  • Form an 1D array out of your column A entries, e.g. with map()
  • Check either the search string is contained in the array - and if yes retrieve its position - e.g. with indexOf()
  • Retrieve the value with the respective row index in column B

Sample:

function myFunction() {
  var matchText = "C";
  var values = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getDataRange().getValues();
  var columnA =values.map(function(e){return e[0]}); 
  var row = columnA.indexOf(matchText);
  if (row >= 0){
    var Bvalue = values[row][1];
    Logger.log(Bvalue);
  } 
}

I encourage you to take some time to study Apps Script, so you cannot only understand this code and adapt to your needs, but also write your own scripts.

Upvotes: 1

Related Questions