[英]Lookup value in cell if another cell in the row contains a matching value
我有兩個Google Speadsheet工作表。 我想交叉引用兩個工作表,並且如果SheetB中的CellC包含SheetA中的CellA值,則用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 | 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中的單詞,SheetA中的單詞“ Word”,則用SheetA中CellA中的值(“代碼”)更新SheetA中的該行。
我怎樣才能做到這一點?
我想最好的方法是創建一個腳本,該腳本在SheetB-CellC中查找值以查找匹配項,然后進行更新。 但是,盡管我已經完成了一些腳本編寫工作,但是通常我更願意直接在電子表格單元格中使用公式。 但是,是否存在可以將一個單元格的值與另一單元格的一部分值匹配的函數?
謝謝!
我實際上在這里找到了解決方案的一部分: 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);
//
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.