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

时间:2019-03-29 19:15:31

标签: javascript arrays google-apps-script

试图找到一种适用于Google Apps脚本的方法,以比较两个数组并查找第二个数组中缺少的值。

我尝试了几种方法,但是找不到适用于GAS的方法。当前正在尝试使用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]
   }

源表有两列要比较,第三列应存储新的“缺失”数组。

orders  products    missing
a       a           c
b       b           d
c       
d       

我尝试了其他几篇文章中的方法,但是所有方法都使用了Google Apps脚本中不提供的功能。

2 个答案:

答案 0 :(得分:1)

查找缺少的订单:

<?php
if( $_POST['name'] != '' && is_email($_POST['email']) && wp_verify_nonce( $_POST['aspirante_nonce'], 'graba_aspirante')) {
    $table = 'Liquor_Type';
    $nombre = sanitize_text_field($_POST['nombre']);
    $correo = $_POST['correo'];
    $web = esc_url($_POST['web']);
    $created_at = date('Y-m-d H:i:s');

    $wpdb->insert(
        $table,
        array(
            'name' => $name,
            'email' => $email,
            'web' => $web,
            'created_at' => $created_at,
        )
    );
    echo "<p>Your data has been recorded. Thanks!<p>";   
} else {
    echo "<p>There was an error. Try again, please!<p>";   
}
ob_start();
?>
<form action="<?php get_the_permalink(); ?>" method="post" id="my_form">
    <?php wp_nonce_field('save_it', 'form_nonce'); ?>
    <div class="form-input">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" required>
    </div>
    <div class="form-input">
        <label for='email'>Email</label>
        <input type="email" name="email" id="email" required>
    </div>
    <div class="form-input">
        <label for="web">Web</label>
        <input type="text" name="web" id="web">
    </div>
    <div class="form-input">
        <input type="submit" value="Submit">
    </div>
</form>
<?php

return ob_get_clean();

这是getColumnHeight()函数:

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);
  }
}

之前的电子表格:

enter image description here

之后的电子表格:

enter image description here

答案 1 :(得分:0)

您是否尝试在.filter()变量上使用orders?这样的事情应该可以解决问题:

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
}