如果行中的另一个单元格包含匹配值

时间:2015-08-20 08:01:18

标签: google-apps-script google-sheets

我有两张Google Speadsheet表。我想交叉引用两个工作表,并使用SheetB中的cellA值更新SheetA中的行,如果SheetB中的CellC包含SheetA中的CellA值。

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看起来像这样:

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

现在,如果是cellC,"单词列表"包含来自cellA的单词," Word"在SheetA中,然后使用SheetB中的值("代码")更新SheetA中的那一行。

我该怎么做?

我想最好的方法是创建一个脚本,在SheetB-CellC中查找值以查找匹配然后更新。但是虽然我已经完成了一些脚本编写,但我通常更容易在电子表格单元格中直接使用公式。但是,是否有一个函数可以将一个单元格的值与另一个单元格中的部分值匹配?

谢谢!

1 个答案:

答案 0 :(得分:0)

我实际上在这里找到了部分解决方案:https://stackoverflow.com/a/11443715/1688190

然而,我必须做的是添加一个检查,以便每个目标行只输出一个值。否则,源的每次迭代,试图从目标中寻找值,输出另一行。

我的工作代码:

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);
//

}