简体   繁体   English

如果行中的另一个单元格包含匹配值,则在单元格中查找值

[英]Lookup value in cell if another cell in the row contains a matching value

I have two Google Speadsheet sheets. 我有两个Google Speadsheet工作表。 I want to cross-reference the two sheets, and update the rows in SheetA with the value of cellA in SheetB, if CellC in SheetB contains the value of CellA in SheetA. 我想交叉引用两个工作表,并且如果SheetB中的CellC包含SheetA中的CellA值,则用SheetB中的cellA值更新SheetA中的行。

SheetA looks like this: SheetA看起来像这样:

A. Word   | B. Other value | C. Another value
---------------------------------------------
word 1    | 1234           | 5678
word 2    | 3921           | 73643
word 3    | 4020           | 43985
word 4    | 32323          | 32325

SheetB looks like this: SheetB看起来像这样:

A. Code  | B. Category | C. List of words
-----------------------------------------------
1.1.1    | Cat1a       | word 1, word 5, word 7
1.1.2    | Cat1b       | word 3, word 2, word 9
1.2      | Cat2        | word 5, word 6, word 4
1.2.1    | Cat2a       | word 8, word 10

Now, if cellC, "List of words" contains the word from cellA, "Word" in SheetA, then update that row in SheetA with the value in CellA ("Code") from SheetB. 现在,如果cellC的“单词列表”包含cellA中的单词,SheetA中的单词“ Word”,则用SheetA中CellA中的值(“代码”)更新SheetA中的该行。

How can I do that? 我怎样才能做到这一点?

I suppose the best way is to create a script that looks up the values in SheetB-CellC to find matches and then update. 我想最好的方法是创建一个脚本,该脚本在SheetB-CellC中查找值以查找匹配项,然后进行更新。 But although I have done some scripting, I'm usually more comfortable with using formulas directly in the spreadsheet cells. 但是,尽管我已经完成了一些脚本编写工作,但是通常我更愿意直接在电子表格单元格中使用公式。 But is there a function that can match the value of one cell with part of value in another cell? 但是,是否存在可以将一个单元格的值与另一单元格的一部分值匹配的函数?

Thanks! 谢谢!

I actually found part of the solution here: https://stackoverflow.com/a/11443715/1688190 我实际上在这里找到了解决方案的一部分: https : //stackoverflow.com/a/11443715/1688190

What I had to do, however, was to add a check so that only one value for each target row was being output. 但是,我要做的是添加一个检查,以便仅输出每个目标行的一个值。 Otherwise each iteration of the source, trying to look for the value from the target, output another line. 否则,源的每次迭代都会尝试从目标中查找值,并输出另一行。

My working code: 我的工作代码:

function updatecategory() {
/* let us say source array with name(columnA) & ID(columnB) is array 'source'
and target array with only IDs is array 'target', you get these with something like*/
var source = SpreadsheetApp.getActiveSpreadsheet().getSheets()[4].getDataRange().getValues();
// and
  var target = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getRange("A:A").getValues();// if other columns, change index values in the arrays : 0=A, 1=B ...
//   then let's create a 3 rd array that will be the new target with ID + names, and call it 'newtarget' 
var newtarget=new Array()
// the iteration could be like this :
      for(i=0;i<target.length;++i){ // don't miss any ID
        var found=""
       for(j=0;j<source.length;++j){ // iterate through source to find the name

         if(source[j][2].toString().indexOf(target[i][0].toString())>-1){
           var newtargetrow=[target[i][0], source[j][0]] // if match found, store it with name (idx0) and ID (idx 1)
           //Logger.log(target[i][0].toString().match(source[j][0].toString()) + " " + source[j][0].toString())
           //newtarget.push(newtargetrow);// store result in new array with 2 columns
           found="found"
           Logger.log(found)
           //Logger.log(newtarget)
         } else if (found != "found") {
           var newtargetrow=[target[i][0], 'not found'] // if no match, show it in name column
           //Logger.log(found)
         }

       }
        Logger.log(newtarget)
         newtarget.push(newtargetrow);// store result in new array with 2 columns
       //loop source
       /* now you have a newtarget array that can directly overwrite the old target 
    using setValues() */
    var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// assuming the target sheet is sheet nr2
    sh.getRange(3,1,newtarget.length,newtarget[0].length).setValues(newtarget);


     } // loop target

    /* now you have a newtarget array that can directly overwrite the old target 
    using setValues() */
    var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];// assuming the target sheet is sheet nr2
    sh.getRange(3,1,newtarget.length,newtarget[0].length).setValues(newtarget);
//

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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