我有两个谷歌速查表。如果SheetA中的cellA包含CellA在SheetA中的值,我想交叉引用这两个表,并使用SheetB中的CellA的值更新SheetA中的行。
SheetA看起来像这样:
A. Word | B. Other value | C. Another value
---------------------------------------------
word 1 | 1234 | 5678
word 2 | 3921 | 73643
word 3 | 4020 | 43985
word 4 | 32323 | 32325SheetB看起来像这样:
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中查找值以查找匹配项,然后进行更新。尽管我已经编写了一些脚本,但我通常更习惯于直接在电子表格单元格中使用公式。但是,有没有一个函数可以将一个单元格的值与另一个单元格的部分值进行匹配?
谢谢!
发布于 2015-08-20 20:42:28
实际上,我在这里找到了部分解决方案: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);
//}
https://stackoverflow.com/questions/32112700
复制相似问题