[英]How to do a vlookup for multiple data separated by comma in google sheets Apps script?
I have a google sheet to create a vlookup.我有一个谷歌表来创建一个 vlookup。
Sheet 1表 1
G ![]() |
H ![]() |
|
---|---|---|
1 ![]() |
Categories![]() |
Category ID![]() |
2 ![]() |
ab, ac, bd ![]() |
|
3 ![]() |
ac![]() |
|
4 ![]() |
av, ab ![]() |
Sheet 2表 2
A![]() |
B![]() |
C ![]() |
---|---|---|
Category![]() |
Category slug![]() |
category Id![]() |
ab![]() |
/ab/ ![]() |
1 ![]() |
ac![]() |
/ac ![]() |
2 ![]() |
av![]() |
/av ![]() |
3 ![]() |
I want to do a vlookup as shown below.我想做一个vlookup,如下所示。
G ![]() |
H ![]() |
|
---|---|---|
1 ![]() |
Categories![]() |
Category ID![]() |
2 ![]() |
ab, ac, bd ![]() |
1,2,bd ![]() |
3 ![]() |
ac![]() |
2 ![]() |
4 ![]() |
av, ab ![]() |
3,1 ![]() |
The requirements are:要求是:
I tried to create a function in the script editor.我试图在脚本编辑器中创建一个 function。 But things didn't work out the way I expected.
但事情并没有像我预期的那样发展。
function replace_category_with_id(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("T");
var total = sheet.getLastRow();
var range = sheet.getRange(2,7,total,1);
var values = range.getValues();
var asheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("A");
var atotal = asheet.getLastRow();
var arange = asheet.getRange(2,1,atotal-1,3);
var avalues = arange.getValues();
//Create an array variable result
var results =[{}];
//Run a for loop for the values in the T Spreadsheet
for(var i=0; i<values.length-1; i++)
{
//Split multiple data in a cell
var splitdata = values[i].toString().split(",");
//Search if the individual data in splitdata is available
for(var count=0; count<(splitdata.length); count++)
{
//Create a flag to check if the search has found the data
var flag = 0;
for(var n=0; n<avalues.length-1;n++)
{
//Logger.log(avalues[n]);
if(splitdata[count] == avalues[n][0])
{
Logger.log(i);
Logger.log(count);
Logger.log(n);
//results[i][count] = avalues[n][2];
flag = 1;
}
}
if(flag==0)
{
results[i][count] = splitdata[count];
}
}
//Logger.log(values[i]);
}
//Display Results array
for(var i=0; i<=results.length; i++)
{
Logger.log(results[i]);
}
}
I made some modifications to your code and added comments to each process.我对您的代码进行了一些修改,并为每个流程添加了注释。
Try this:尝试这个:
function replace_category_with_id(){
//Retrieve Sheet1 data
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var total = sheet1.getLastRow();
var range = sheet1.getRange(2,7,total-1,1);
var sheet1_values = range.getValues();
//Retrieve Sheet2 data
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var atotal = sheet2.getLastRow();
var arange = sheet2.getRange(2,1,atotal-1,3);
var sheet2_values = arange.getValues();
//Sheet2: Create object as lookup
var lookup = {};
sheet2_values.forEach(element => {
//Push each element and use Category as key and Category ID as value
lookup[element[0]] = element[2]
})
var result = [];
sheet1_values.forEach(element => {
//Sheet1: Split each categories in the first column.
var val = element[0].toString().split(",");
var val_str = [];
val.forEach(element2 => {
//Sheet1: Check if Category exists in lookup, if not use Category
if(lookup.hasOwnProperty(element2)){
val_str.push(lookup[element2])
}else{
val_str.push(element2);
}
})
//Combine results in an Array
result.push([val_str.join(",")])
})
//Set values to sheets using the combined results.
sheet1.getRange(2,8,total-1,1).setValues(result);
}
Here is my sample data: (I added some test data to check if the logic is working) Sheet1这是我的示例数据:(我添加了一些测试数据以检查逻辑是否正常工作) Sheet1
Before:前:
After:后:
Sheet2表 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.