简体   繁体   English

用于匹配和复制基于另一个相邻单元格的 Google 表格脚本

[英]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.我已经手动完成了这个例子。

主单

更简单的例子

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 : ABC列:

在此处输入图像描述

  • Columns Q , R , S , T , U , V :QRSTUV

在此处输入图像描述

Sample Demonstration示例演示

  • After running the script from the Apps Script editor:从 Apps 脚本编辑器运行脚本后:

在此处输入图像描述

  • Apps Script editor log results for review: Apps 脚本编辑器日志结果供审核:

在此处输入图像描述

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:结果:

在此处输入图像描述

Sheet 床单

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.

相关问题 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 用于复制和重命名工作表和名称的Google脚本基于单元格引用 - Google script to copy and rename a sheet and name is based on a cell reference 检查日期并使用Google脚本编辑Google表格中的相邻单元格 - Check date and edit adjacent cell in Google Sheet with Google Script google脚本:根据触发将公式计算出的值从一张纸复制到另一张纸上(在编辑或时间驱动下) - google script: To copy the value calculated by formula from one sheet to another based on TRIGGER (on edit or time driven) 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script Google Apps脚本:将单元格值复制到另一个工作表中,条件是要复制并放置该值 - Google Apps Script: Copy cell value to another sheet with condition to copy and position the value 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] Google脚本将单元格的一部分复制到另一个工作表 - Google Script Copying part of a cell to another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM