简体   繁体   English

如何在谷歌表格应用程序脚本中对以逗号分隔的多个数据进行 vlookup?

[英]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 G H H
1 1 Categories类别 Category ID类别 ID
2 2 ab, ac, bd ab、ac、bd
3 3 ac交流
4 4 av, ab av, ab

Sheet 2表 2

A一个 B C C
Category类别 Category slug分类蛞蝓 category Id类别 ID
ab抗体 /ab/ /ab/ 1 1
ac交流 /ac /ac 2 2
av影音 /av /av 3 3

I want to do a vlookup as shown below.我想做一个vlookup,如下所示。

G G H H
1 1 Categories类别 Category ID类别 ID
2 2 ab, ac, bd ab、ac、bd 1,2,bd 1,2,bd
3 3 ac交流 2 2
4 4 av, ab av, ab 3,1 3,1

The requirements are:要求是:

  1. Mapping / Create a vlookup for all data in a cell映射/为单元格中的所有数据创建 vlookup
  2. Reversing the same data wherever vlookup data is not available在 vlookup 数据不可用的地方反转相同的数据

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.

相关问题 如何将 Google Apps 脚本应用于多个工作表 - How to apply Google Apps Script to multiple sheets Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 谷歌表格谷歌应用程序脚本为所有行添加 VLOOKUP 函数 - Google Sheets google apps script add a VLOOKUP function for all rows 如何通过 Apps Script 更改 Google Sheets 中的多个单元格值? - How to change multiple cell values in Google Sheets via Apps Script? 如何从表格中计算谷歌应用脚​​本中的持续时间? - How do I calculate duration in google apps script from sheets? 在 Apps Script 和 Google Sheets 中使用 ImportHTML 进行数据抓取 - Data Scraping With ImportHTML in Apps Script & Google Sheets 如何使用单元格的应用程序脚本将 Google 表格数据验证添加到列中,以仅允许输入颜色和十六进制颜色代码 - How do I add Google sheets data vaildation to a column using apps script for cells to only allow colors and hex color codes to be inputted 用于在 Google 表格中进行多次查找和替换的 Google Apps 脚本 - Google Apps Script for Multiple Find and Replace in Google Sheets Google Apps脚本多个在Google表格中查找和替换正则表达式 - Google Apps Script Multiple Find and replace regex in Google Sheets 如何使用Apps脚本将对Google Analytics API的多次调用打印到Google Sheets - How to print multiple calls to Google Analytics API to Google Sheets with Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM