Google Apps 脚本比较范围以查找匹配项 [英] Google Apps Script compare ranges to find matches

本文介绍了Google Apps 脚本比较范围以查找匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 张客户(下表 1)";和颜色描述(下表2)".我想用 GAS 做以下事情:

I have 2 Sheets "Client (Table1 below)" and "Color Description (table2 below)." I want to use GAS to do the following:

  1. onEdit 我想比较 2 个范围然后...
  2. 找到Client Sheet - Col2"所在的每个实例匹配颜色描述表 - Col1"
  3. 获取实例所在单元格的地址
  4. 在客户表 - Col3(单元格地址)"中设置该值

所需的输出如下所示:[约翰|红色 |颜色说明 A2] 或 [Beth |粉红色|颜色说明A7]

The desired output would look something like this: [John | red | Color Description A2] or [Beth | pink | Color Description A7]

e 是由电子表格上的多项内容触发的

<头>
客户颜色单元地址
乔恩红色
贝丝粉色
汤姆蓝色
Cj红色
奥马尔绿色
丽莎紫色

<头>
颜色说明
红色关于红色
绿色关于绿色
蓝色关于蓝色
紫色关于紫色
白色关于白色
粉红色关于粉红色的事情

我希望这是有道理的.TIA 寻求帮助.

I hope this makes sense. TIA for your help.

推荐答案

这是我想到的.抱歉,这有点笨拙,因为我是新手.

This is what I came up with. Sorry if this is kind of clunky as I'm new to it all.

function myFunction() {
    var clientColors = SpreadsheetApp.getActiveSpreadsheet();
    var clientSheet = clientColors.getSheetByName("Client");
    var colorSheet = clientColors.getSheetByName("Color Description");
    var lastRownumberClient = clientSheet.getLastRow();
    var lastRownumberColor = colorSheet.getLastRow();
    var clientSheetVals = clientSheet.getRange(2,2,lastRownumberClient-1,1).getValues();//-1 to remove header row
    var colorSheetVals = colorSheet.getRange(2,1,lastRownumberColor-1,1).getValues();//-1 to remove header row

    var colorColA = clientSheetVals.map(function(row){//This maps out ColB in the client sheet and returns the matches from ColA in the color sheet
    var theItem = row[0];
    var  lookupColorRangeValues = colorSheetVals.map(function(row){return row[0];});            
    var index = lookupColorRangeValues.indexOf(theItem)+2;
      
    return ['A'+[index]]});//'A' concatenates the the column letter to the returned index number
 
 
 
 clientSheet.getRange(2,3,clientSheet.getLastRow()-1,1).setValues(colorColA);//Sets the returned values in ColC of the client sheet -1 to remove header row
}

这篇关于Google Apps 脚本比较范围以查找匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆