Home > Net >  Create a list with values not contained in a Google Sheets column via Google Apps Script
Create a list with values not contained in a Google Sheets column via Google Apps Script

Time:06-23

My attempt was to work with filter includes:

function not_contains() {
  var new_opt = [['a'],['b'],['d']];

  var col_g = SpreadsheetApp.getActive().getSheetByName('test_stack').getRange('G1:G').getValues();
  // [['a'],['b'],['c'],[''],['']]

  var not_match = new_opt.filter(x => !col_g.includes(x));
}

Actual Result:

[ [ 'a' ], [ 'b' ], [ 'd' ] ]

Expected Result:

[ [ 'd' ] ]

CodePudding user response:

Since it's a 2D array, and includes only compares primitives#strings directly, but not array objects, use Array.flat on one array:

const col_g_flat = col_g.flat();//// ['a','b','c',..]
const not_match = new_opt.filter(x => !col_g_flat.includes(x/*access the string inside*/[0]));
  • Related