2

I am trying to search row by column value in spreadsheet (Google Sheets) API Google documentation having API for find & Replace but no API available for find/search.

Some approaches i already tried to achieve searching by value in spreadsheet.

  1. Google Visualization API Query Language: this approach is returning rows matching values.

    Some issues in response:

    a) Not returning row number (No indexing)

    b) search is case sensitive

    Is it possible to get row number in result of this method?

  2. Set column

    =ROW(INDIRECT(ADDRESS(MATCH("search_string",B:B,0),1)))

Got row number eg : 2

And then Get row by get value by range: https://sheets.googleapis.com/v4/spreadsheets/1Y65ZA5mhyImLL00yGR3lqwtbm6MgGLV0nV014fRXhts/values/A2:Z2

This is not a proper way for this i have to create a cell with Formula and then find the row by value in that cell and also clear sell after search done.

*This approach returns only one row.
  1. DeveloperMetadataLookup: i don't know how to use this I created meta data on sheet with metadataKey : key1 metadataValue : value1

    Searching by spreadsheets.values.batchGetByDataFilter

    using dataFilter:DeveloperMetadataLookup{"metadataKey":"key1","metadataValue":"value1"}

This returning all rows in sheet i don't know how to find specific row with this method.

  1. Google Apps Text Finder : i don't know if this is free to use with spreadsheet API.

  2. Basic Filter: i read about this but don't know how to use it for searching.

  3. Some old questions of same topic suggesting to fetch all rows and loop them to get row by value.

    Example: this will hang my server i don't want implement this loop.

    function find(value, range) {
      var data = range.getValues();
      for (var i = 0; i < data.length; i++) {
        for (var j = 0; j < data[i].length; j++) {
          if (data[i][j] == value) {
            return range.getCell(i + 1, j + 1);
          }
        }
      }
      return null;
    }

Conclusion:

  1. Can anyone help me to make a proper way to searching a row by column value in sheets ?

  2. Is it possible to search row by column value with (DeveloperMetadataLookup or Basic Filter) If yes then please suggest me proper way to use it.

  3. Google Visualization API Query Language : is it possible to get row number ?

This is a example sheet

Column-> A, B, C

Row1->B-15, planY, 50
Row2->B-18, planZ, 80
Row3->B-19, planC, 70
Row4->B-21, planZ, 90

My requirement search column:B = value:planZ | (B=planZ)

Result should be similar to this :

Row:{
"2":["B-18","planZ","80"],
"4":["B-21","planZ","90"]
}

Jitendra Pathak
  • 299
  • 1
  • 10
  • Can I ask you about your question? 1. Can I ask you about the language you want to use? 2. In your question, there are 3 questions in one question? – Tanaike Aug 04 '20 at 22:31
  • Hello tanaike actually i tried different ways to get row by column value and these are issues in different approaches for same task. 1.Can I ask you about the language you want to use? Right now I am testing REST API in Postman & after successful testing i will use API with CURL. 2. there are 3 questions in one question it's a one question : What is proper way of searching a row by column value in spreadsheets API But in case we don't have a proper way then i mentioned all the issues i got in different approach i already tried. – Jitendra Pathak Aug 05 '20 at 05:19
  • Thank you for replying. About Q1, I understood you want to achieve your goal using curl command. About Q2, in the current stage, the row number cannot be directly retrieved by Sheets API. So in this case, in order to achieve your goal, as a workaround, I would like to propose to use Web Apps created by Google Apps Script. [This thread](https://stackoverflow.com/q/60763776) might be useful. The maximum concurrent accesses of Web Apps is 30. You can use this with free. When this is not the direction you expect, I apologize. – Tanaike Aug 05 '20 at 05:44
  • Have you looked into the functions. [VLOOKUP](https://support.google.com/docs/answer/3093318?hl=en), [INDEX](https://support.google.com/docs/answer/3098242?hl=en), and [HLOOKUP](https://support.google.com/docs/answer/3093375?hl=en&ref_topic=3105472)? – Aerials Aug 05 '20 at 08:31
  • Yes @Aerials but these function not support REST API can you help to use these function in API with CURL ? – Jitendra Pathak Aug 05 '20 at 08:35
  • @JitendraPathak Can you explain what are your "given data" please differentiate between "column value" and "column index". And what you would like in return from the API call, what do you want to find? Is it the row number, is it the index of the column in the row where the value you are giving exists, is it a value given a "column number" or "index", the whole row values if one of the values matches? – Aerials Aug 05 '20 at 10:19
  • @Aerials : This a example sheet Column-> A, B, C Row1->B-15, planY, 50 Row2->B-18, planZ, 80 Row3->B-19, planC, 70 Row4->B-21, planZ, 90 My requirement search column:B = value:planZ | (B=planZ) Result should be similar to this : Row{"2":["B-18","planZ","80"],"4":["B-21","planZ","90"]} – Jitendra Pathak Aug 05 '20 at 12:43
  • @Aerials Also updated this example in question with proper format. – Jitendra Pathak Aug 05 '20 at 12:50
  • @Tanaike thanks for the App Script thread i have tested the script and i got result like this: [{"range":"'Sheet1'!B4","sheetId":0},{"range":"'Sheet1'!B7","sheetId":0}] is it possible to get result like above example in question. – Jitendra Pathak Aug 05 '20 at 12:53

1 Answers1

0

Tested This thread function with some modification for searching in spreadsheet and i got row number and row values (all cells of that row).

function doGet(e = {
    "parameter": {
        "value": "planc",
        "spreadsheetId": "1Y65ZA5mhyImLL00yGR3lqwtbm6MgGLV0nV014fRXhts",
        "column": "B",
        "headerStatus": true
    }
}) {
    var findText = e.parameter.value;
    var spreadsheetId = e.parameter.spreadsheetId;
    var column = e.parameter.column;
    var ss = SpreadsheetApp.openById(spreadsheetId);
    var ranges = ss.createTextFinder(findText).findAll();
    var headerStatus = e.parameter.headerStatus;
    if (headerStatus == true || headerStatus == "true") {
        var header = ss.getRange("A1:Z1").getValues()[0];
    }
    var res = ranges.map(r => ({
        row: r.getRow(),
        value: r.getSheet().getRange(`A${r.getRow()}:Z${r.getRow()}`).getValues()[0]
    }));
    var result = {};
    var allColumn = {
        "A": 0,
        "B": 1,
        "C": 2,
        "D": 3,
        "E": 4,
        "F": 5,
        "G": 6,
        "H": 7,
        "I": 8,
        "J": 9,
        "K": 10,
        "L": 11,
        "M": 12,
        "N": 13,
        "O": 14,
        "P": 15,
        "Q": 16,
        "R": 17,
        "S": 18,
        "T": 19,
        "U": 20,
        "V": 21,
        "W": 22,
        "X": 23,
        "Y": 24,
        "Z": 25
    };
    var columnIndex = allColumn[column];
    for (var key in res) {
        var getRow = res[key]["row"];
        var getValue = res[key]["value"];
        if (getValue[columnIndex].toLowerCase() == findText.toLowerCase()) {
            if (headerStatus == true || headerStatus == "true") {
                var headerValueObj = {};
                for (var i = 0; i < header.length; i++) {
                    if (header[i] !== "") {
                        var headerName = header[i];
                        headerValueObj[headerName] = getValue[i];
                    }
                }
                result[getRow] = headerValueObj;
            } else {
                result[getRow] = getValue;
            }
        }
    }
    return result;
}

Output result

"result": {
      "4": {
        "Price": 20,
        "heading4": "",
        "Id": "B-16",
        "Plan": "PlanC"
      },
      "7": {
        "Price": 8,
        "heading4": "",
        "Id": "B-24",
        "Plan": "PlanC"
      }
    }

Jitendra Pathak
  • 299
  • 1
  • 10