简体   繁体   English

在Google Apps脚本中查找两个数组(缺少值)之间的差异

[英]Find difference between two arrays (missing values) in Google Apps Script

Trying to find a method that works in Google Apps Scripts, to compare two arrays and find the values missing in the second array. 试图找到一种适用于Google Apps脚本的方法,以比较两个数组并查找第二个数组中缺少的值。

I've tried several approaches but can't find one that works in GAS. 我尝试了几种方法,但找不到在GAS中有效的方法。 Currently attempting with a for() loop and indexOf(): 当前尝试使用for()循环和indexOf():

var ss = SpreadsheetApp.getActiveSpreadsheet();  
var sheet = ss.setActiveSheet(ss.getSheetByName('test'));

function TEST(){
  var lastRow = sheet.getLastRow();
  var orders = sheet.getRange(2,1,lastRow,1).getValues(); //[a,b,c,d]
  var products = sheet.getRange(2, 2,lastRow,1).getValues();  //[a, b]
  var missing = [];
  for ( var i = 0 ; i < Object.keys(orders).length; i++){
    if(products.indexOf(orders[i])<0){
      missing.push(orders[i]);};
  };
  Logger.log(missing); //expect [c, d]
   }

The source table has two columns to compare, and a 3rd column where the new 'missing' array should be stored. 源表有两列要比较,第三列应存储新的“缺失”数组。

orders  products    missing
a       a           c
b       b           d
c       
d       

I tried methods from several other posts but everything is using functions that aren't available in Google Apps Scripts. 我尝试了其他几篇文章中的方法,但所有方法都使用了Google Apps脚本中未提供的功能。

Find Missing Orders: 查找缺失的订单:

function findMissingOrders() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet110');
  var orderA=sh.getRange(2,1,getColumnHeight(1)-1,1).getValues().map(function(r){return r[0];});
  var prodA=sh.getRange(2,2,getColumnHeight(2)-1,1).getValues().map(function(r){return r[0];});
  var missA=[];
  for(var i=0;i<orderA.length;i++) {
    var order=orderA[i];
    if(prodA.indexOf(orderA[i])==-1) {
      missA.push([orderA[i]]);
    }
  }
  if(missA.length>0) {
    sh.getRange(2,3,missA.length,1).setValues(missA);
  }
}

Here's the getColumnHeight() function: 这是getColumnHeight()函数:

function getColumnHeight(col,sh,ss){
  var ss=ss || SpreadsheetApp.getActive();
  var sh=sh || ss.getActiveSheet();
  var col=col || sh.getActiveCell().getColumn();
  var rg=sh.getRange(1,col,sh.getLastRow(),1);
  var vA=rg.getValues();
  while(vA[vA.length-1][0].length==0){
    vA.splice(vA.length-1,1);
  }
  return vA.length;
}

Spreadsheet Before: 电子表格之前:

在此处输入图片说明

Spreadsheet After: 试算表之后:

在此处输入图片说明

Have you tried using .filter() on the orders variable? 您是否尝试过在orders变量上使用.filter() Something like this should do the trick: 这样的事情应该可以解决问题:

var orders = sheet.getRange(2,1,lastRow,1).getValues().map(firstOfArray)
var products = sheet.getRange(2, 2,lastRow,1).getValues().map(firstOfArray)
var missing = orders.filter(missing)

function firstOfArray(array) {
   return array[0]
}
function missing(order) {
    return products.indexOf(order) === -1
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM