首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果行中的另一个单元格包含匹配值,则查找单元格中的值

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

Stack Overflow用户
提问于 2015-08-20 16:01:18
回答 1查看 784关注 0票数 0

我有两个谷歌速查表。如果SheetA中的cellA包含CellA在SheetA中的值,我想交叉引用这两个表,并使用SheetB中的CellA的值更新SheetA中的行。

SheetA看起来像这样:

代码语言:javascript
复制
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看起来像这样:

代码语言:javascript
复制
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,"List of words“包含来自cellA的单词,SheetA中的" word”,则使用来自SheetB的CellA ("Code")中的值更新SheetA中的该行。

我该怎么做呢?

我认为最好的方法是创建一个脚本,该脚本在SheetB-CellC中查找值以查找匹配项,然后进行更新。尽管我已经编写了一些脚本,但我通常更习惯于直接在电子表格单元格中使用公式。但是,有没有一个函数可以将一个单元格的值与另一个单元格的部分值进行匹配?

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-08-20 20:42:28

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

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

我的工作代码:

代码语言:javascript
复制
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);
//

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32112700

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档