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. 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.
I have done the example manually.
CodePudding user response:
Suggestion
Perhaps you can try this sample script:
Script [UPDATED]
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;
}
}
Sample Sheet
- Columns
A,B,C:
- Columns
Q,R,S,T,U,V:
Sample Demonstration
- After running the script from the Apps Script editor:
- Apps Script editor log results for review:
CodePudding user response:
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. It will silently hide many errors in your data. But I can add it if you want.









