简体   繁体   中英

Google Sheets looking for a google script like VLOOKUP but combines the found values and joins them into a specific cell

I have a spreadsheet where I'm trying to add an automatic search function whenever a cell on the Sheet "List" Column 1 gets selected.

It's sort of like using a VLOOKUP function of the sheets but I've been unsuccessful trying to come up with an effective code for it.

Test Spreadsheet link

I made a simple test spreadsheet hopefully for people to easily understand how the sheet should work. In the 'Database' sheet, there's a list of people with the fruits they like or dislike. In the 'List' sheet, the list is reversed where the fruits are now the main list but here, when a person clicks on the cells with the fruits, the top cells should automatically update with the combined names of people who like or dislike that certain fruit.

The function should go like this:

If I click a cell on column 1 of the 'List' sheet,

  1. A1 cell updates it's value with the active cell value
  2. B1 cell updates it's value with the combined values based on who likes it from the 'Database' sheet Column 2.
  3. B2 cell updates it's value similar as above but from Dislikes in Column 3

I've attached the code I currently have but I'm not able to get the last 2 steps working.

There is also a guide in the sheets on what the correct answers should look like for each fruit.

I got stuck looking for a way to make the steps 2 and 3 work. I was able to find a code here but it stops after finding one match. stackoverflow.com/questions/10838294/… My current problem is: Since the cells [B2:B] to filter from on the Database sheet has multiple values, is it possible to find all cells with at least a partial match, get the values of the cells on the left of that then list them into the B1 cell on the "List" sheet?

Test Spreadsheet link

Code.gs

function onSelectionChange(e){
var currentsheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var currentcell = currentsheet.getActiveCell();
  var activerow = currentcell.getRow();
  var activecol = currentcell.getColumn();

  var sname = currentsheet.getName(); 

  // Check if current sheet name is correct
  if (
    sname == 'List'
  )
  {
    // Check if this on first column
    if (activecol == 1)
    {
      // ACTION
      var name = currentcell.getValue(); //Get current cell for searching
      var prefsheet = SpreadsheetApp.getActive().getSheetByName("Database");
      var last = prefsheet.getLastRow();
      var data = prefsheet.getRange(1,1,last,2).getValues(); // create an array of data from columns A and B
      for(nn=0;nn<data.length;++nn){
        if (data[nn][1]==name){break} ;
        SpreadsheetApp.getActiveSheet().getRange('A1').setValue('Fruit: ' + name);
        SpreadsheetApp.getActiveSheet().getRange('B1').setValue('Like: ' + data[nn][0]);
      }
    }
  };
};

I was able to find a workaround for this and it turns out I didn't have to use a Google Script code to keep updating the formula on List!B1.

I'm new with scripting and sheets but I'll be sharing this formula here in case someone needs something like this in the future.

This is what I used on List!B1

=JOIN(", ",query(filter(Database!A2:B,REGEXMATCH(Database!B2:B,A1)),"Select Col1"))

Regexmatch does the search for partial value and returns as "TRUE" Filter will then list these rows and query "Select Col1" will only keep the first column of the results and remove the second column. Lastly, Join formula will concatenate them into one cell and add separators.

Which is working very nicely for my purpose. If anyone has any suggestions I'd love to hear about it too.

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