[英]Google sheet script to match and copy based from another adjacent cell
I am trying to look for matching cells in column R,T,and V in the reference table with column C in the main sheet.我正在尝试在参考表中的 R、T 和 V 列中查找匹配的单元格,并在主表中的 C 列中查找匹配的单元格。 If there is a match then copy the assigned number in Q,S, and U and past it in column B next to the its match.
如果有匹配项,则复制 Q、S 和 U 中分配的数字,并将其粘贴到匹配项旁边的 B 列中。
I have done the example manually.我已经手动完成了这个例子。
Perhaps you can try this sample script:也许你可以试试这个示例脚本:
function findMatch() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var lastRow = sh.getDataRange().getLastRow();
var reference = [];
var row = 1;
var data = sh.getRange("Q1:V"+lastRow).getDisplayValues().filter(function(x) {
return (x.join('').length !== 0);
});
sh.getRange("C1:C"+lastRow).getDisplayValues().forEach( x => {
reference.push([fixForUsage(x),row]);
row += 1;
});
reference.forEach(aisle => {
if(aisle[0] == "" || aisle[0].toString().toLowerCase().includes("aisle"))return;
data.forEach(match => {
var currentData = match[1] +"-"+match[3]+"-"+match[5];
if(currentData.includes(aisle[0])){
//get its number
if(match[1] == ""){
if(match[3] == ""){
if(match[5] == ""){
}else{
//if not empty
Logger.log("Find \""+aisle[0]+"\" from sheet row#"+aisle[1]+"\nRESULT:"+"\nFound a match on these data!: "+match+"\nHeader: \'"+data[0][4]+"\'\nNumber: "+match[4]);
pasteData(sh,aisle[1], data[0][4], match[4]);
}
}else{
//if not empty
Logger.log(+"Find \""+aisle[0]+"\" from sheet row#"+aisle[1]+"\nRESULT:"+"\nFound a match on these data!: "+match+"\nHeader: \'"+data[0][2]+"\'\nNumber: "+match[2]);
pasteData(sh,aisle[1],data[0][2],match[2]);
}
}else{
//if not empty
Logger.log("Find \""+aisle[0]+"\" from sheet row#"+aisle[1]+"\nRESULT:"+"\nFound a match on these data!: "+match+"\nHeader: \'"+data[0][0]+"\'\nNumber: "+match[0]);
pasteData(sh,aisle[1],data[0][0],match[0]);
}
}
});
});
}
function pasteData(sh,row, colAData, colBData){
sh.getRange("B"+row).setValue(colBData);
sh.getRange("A"+row).setValue(colAData);
}
function fixForUsage(x){ //This is to let the code know that e.q. 53-7 is 53-07
var part = x.toString().split("-");
if(part.length == 2 && part[1].length == 1){
return part[0]+"-0"+part[1];
}else{
return x;
}
}
A
, B
, C
: A
、 B
、 C
列:Q
, R
, S
, T
, U
, V
:Q
, R
, S
, T
, U
, V
:Here is another solution:这是另一个解决方案:
function myFunction() {
var sh = SpreadsheetApp.getActiveSheet();
// get all data
var data = sh.getRange('q2:v16').getDisplayValues();
var cols_QR = data.map(x => ({'crane': 'C5-1', 'seq': x[0], 'aisle': x[1]}));
var cols_ST = data.map(x => ({'crane': 'C5-4', 'seq': x[2], 'aisle': x[3]}));
var cols_UV = data.map(x => ({'crane': 'C5-2', 'seq': x[4], 'aisle': x[5]}));
data = [...cols_QR, ...cols_ST, ...cols_UV];
data = data.filter(x => x.aisle != '');
// create the object 'aisles'
var aisles = {}
for (let obj of data) aisles[obj.aisle] = {'crane': obj.crane, 'seq': obj.seq}
// get target range and target data
var target_range = sh.getRange('a2:c' + sh.getLastRow());
var target_data = target_range.getDisplayValues();
// fill target range with info from the 'aisles' object
for (let row in target_data) {
try {
let key = target_data[row][2];
target_data[row] = [ aisles[key].crane, aisles[key].seq, key ];
} catch(e) {}
}
// fill the target range with updated data
target_range.setValues(target_data);
}
Initial data:初始数据:
Results:结果:
I decided don't add in my variant the function that makes true this: '53-7' == '53-07'
and '11' == '11-0'
, etc, because I think it's a rather bad and error-prone idea.我决定不要在我的变体中添加 function 来实现这一点:
'53-7' == '53-07'
和'11' == '11-0'
等,因为我认为这是一个相当糟糕的容易出错的想法。 It will silently hide many errors in your data.它会默默地隐藏数据中的许多错误。 But I can add it if you want.
但如果你愿意,我可以添加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.