简体   繁体   中英

google scripts looping cells

After a bit of help looping through data on a google script.

What I want to do is loop through a variable range (Column A) and get the unique values in column B where it matches my "Search Criteria"

Example in column AI have multiple occurances of football teams.

in column B is the player names

My search Criteria is "Liverpool"

The code will then loop through column A and where it finds "Liverpool" it will add the value in the adjacent column to an array (if it already exists in the array it won't add it)

(I will then lastly use this array to be a data validation - Drop down list of player names)

Thanks

I believe you are totally new to appscript. So, here I am suggesting few basics which will help you do the task easily if you have knowledge of looping and arrays.

You can use:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YOUR_SHEET_NAME");
  var data = sheet.getDataRange().getValues();

sheet variable will have whole spreadsheet and data variable will have whole sheet as 2D array.

Now as you got the array, you can do normal for looping. So, you can use:

var dropDownArray = [];
  for(var i=0; i<data.length; i++)  //Loop through non-blank values
  {
    if(data[i][0] == "Liverpool")  //search for liverpool in 1st column
    {
      if(dropDownArray.indexOf(data[i][1])<0)  //check whether 2nd column's corresponding value is not duplicate
      {
        dropDownArray.push(data[i][1]);  //If its unique, push into array
      }
    }
  }

Hence you will get an Array with all Liverpool players. Just print its value after looping is over using: Logger.log(dropDownArray);

You can check logs using ctrl+enter from keyboard or click view->Logs.

You can also print that array on spreadsheet, for that, use this:

sheet.getRange(row, column).setValue(value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM