Return row data from a table with a specified row without looping through all rows

I have a task: I need to return all Row data containing a user-defined string .

One way to achieve this goal is to iterate over all the rows , but this only works when you know which column to search in , as shown, using the code below.

var sheetData = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getValues();
var searchString = "testSearch";

for(var i=0;i<sheetData.getLastRow();i++)
{
  //assuming we know that the search string is going to be in Column 2
  if(sheetData[i][1].search(searchString)!=-1)
  {
    var rowData = sheetData[i];
    return rowData;
  }
}

So, my question: Is there any way that we can achieve , without having all the strings cycle through one ?

, - "" , :

enter image description here

/.

. Google Apps Script.

+4
1

, . . . .

, Put Sheet Tab Name Here , . searchString . , .

function findRowOfSearchString(searchString) {
  var arrIndexOfAllMatches,dataAsStringWithBrackets,dataRange,i,
      isThereA_Match,j,ll,L,L2,matchOfAllInnerBrackets,numberOfColumns,numberOfRows,
      rowCutOff,rowsOfMatchedData,sh,sheetData,ss,thisIndex,thisMatchIndex,thisRow,thisRowData;

  ll = function(a,b) {
    Logger.log(a + ": " + b)
  }

  if (!searchString) {
    searchString = "testSe";
  }

  ss = SpreadsheetApp.getActiveSpreadsheet();
  sh = ss.getSheetByName('Put Sheet Tab Name Here');//

  dataRange = sh.getDataRange();

  numberOfColumns = dataRange.getNumColumns();
  numberOfRows = dataRange.getNumRows(); //changed 'getNumColumns' to 'getNumRows'

  sheetData = dataRange.getValues();//Get a 2D array of all sheet data

  dataAsStringWithBrackets = JSON.stringify(sheetData);
  //ll('dataAsStringWithBrackets: ',dataAsStringWithBrackets)

  isThereA_Match = dataAsStringWithBrackets.indexOf(searchString);
  //ll('isThereA_Match: ',isThereA_Match)

  if (isThereA_Match === -1) {return;}//There is no match - stop

  arrIndexOfAllMatches = [];

  L = dataAsStringWithBrackets.length;
  //ll('L',L)

  thisMatchIndex = 0;

  for (i=0;i<L;i++) {
    //ll('i',i)
    thisMatchIndex = dataAsStringWithBrackets.indexOf(searchString,thisMatchIndex + 1);

    //ll('thisMatchIndex',thisMatchIndex)

    if (thisMatchIndex === -1) {//No more matches were found
      //ll('No more matches found',thisMatchIndex)
      break;
    }

    arrIndexOfAllMatches.push(thisMatchIndex);
  }

  //ll('arrIndexOfAllMatches',arrIndexOfAllMatches)

  matchOfAllInnerBrackets = [];

  thisMatchIndex = 0;

  for (i=0;i<L;i++){
    thisMatchIndex = dataAsStringWithBrackets.indexOf("],[",thisMatchIndex + 1);

    //ll('thisMatchIndex',thisMatchIndex)

    if (thisMatchIndex === -1) {//No more matches were found
      //ll('No more matches found',thisMatchIndex)
      break;
    }

    matchOfAllInnerBrackets.push(thisMatchIndex);
  }

  ll('matchOfAllInnerBrackets',matchOfAllInnerBrackets)

  rowsOfMatchedData = [];
  L = arrIndexOfAllMatches.length;
  L2 = matchOfAllInnerBrackets.length;

  for (i=0;i<L;i++){
    thisIndex = arrIndexOfAllMatches[i];
    ll('thisIndex: ' ,thisIndex)

    for (j=0;j<L2;j++){
      rowCutOff = matchOfAllInnerBrackets[j];
      ll('rowCutOff: ',rowCutOff)

      if (rowCutOff > thisIndex) {
        ll('greater than: ' ,thisIndex > rowCutOff)
        thisRow = j+1;
        ll('thisRow: ', (thisRow))
        rowsOfMatchedData.push(thisRow)
        break;
      }
    }
  }

  ll('rowsOfMatchedData: ',rowsOfMatchedData)

  L = rowsOfMatchedData.length;

  for (i=0;i<L;i++){
    thisRowData = sh.getRange(rowsOfMatchedData[i], 1, 1, numberOfColumns).getValues();
    ll('thisRowData: ',thisRowData)
  }
}
+1

Source: https://habr.com/ru/post/1681871/


All Articles