Reputation: 97
I have two arrays, and I am trying to create a third array of the difference between the two arrays. However, I'm having a hard time thinking through it. The code below is obviously wrong. I thought, if I compare the first array with the second, I can capture the difference. However, I realized it doesn't quite work with the script below, because technically, the items in the first array will always be different with at least one record in the second causing it to be added in the third array. What would I have to modify to make the first array pass through the whole 2nd array and if it did not match all items then push it into the third? Thank you in advance for your guidance
function range(){
var ran1 = ["1","2","3"];
var ran2 = ["4","5","3","6"];
var result = new Array();
for(var i = 0; i < 3; i++){
for(var j = 0; j < 4; j++){
if(ran1[i] != ran2[j])
{
result.push(ran1[i]);
}
}
}
Logger.log(result);
}
** EDIT I tried apply the principles of your script to mine. namesInSchSheet is an array from a single row with multiple columns. The values in namesInContactSheet are pulled from a single column with multiple rows. When I display the Logs for both arrays they look different and Im assuming that's why it won't work. Am I not presenting the data that's pulled in from namesInSchSheet (single row w/ values in multiple columns) properly?
var ss = SpreadsheetApp.getActiveSheet();
var ss2 = SpreadsheetApp.getActive();
var ss3 = SpreadsheetApp.openById(ss2.getId());
var ss4 = ss3.getSheetByName('Contacts');
var lastCol = ss.getLastColumn();
var schRange = ss.getRange(4, 3, 1, lastCol-2);
var contactRange = ss4.getRange(2, 4, 8 , 1);
var namesInContactSheet = contactRange.getValues();
var numCols = schRange.getNumColumns();
var conNumRows = contactsheet.getLastRow();
var addnew = new Array();
for(var i = 0; i < conNumRows; i++){
for(var j = 0; j < numCols; j++){
for(var k = 0; k < speakername.length; k++){
if(namesInSchSheet[k][j].indexOf(namesInContactSheet[j]==-1)){
addnew.push(namesInContactSheet[i]);
}
}
}
}
Logger.log(addnew);
Upvotes: 0
Views: 747
Reputation: 46802
for example, using 2 consecutive loops and indexOf array method :
function range(){
var ran1 = ["1","2","3"];
var ran2 = ["4","5","3","6"];
var result = new Array();
for(var i = 0; i < ran1.length; i++){
if(ran2.indexOf(ran1[i])==-1){
result.push(ran1[i]);
}
}
Logger.log('first loop : '+result);
for(var i = 0; i < ran2.length; i++){
if(ran1.indexOf(ran2[i])==-1){
result.push(ran2[i]);
}
}
Logger.log('second (final) loop : '+result);
}
result :
your question was a bit unclear in its first version. The main issue in your case is that arrays you get when reading values in a spreadsheet are 2 dimensions arrays, i.e arrays of arrays, each row in a sheet is an array of column elements structured like this :
[[cell A1],[cell A2],[cell A3], ...]
To be able to use the indexOf
method I suggested you have to convert this to a "normal" array... the easiest way when dealing with single row data is to join it and then split it on the default comma like this :
[[cell A1],[cell A2],[cell A3]].join().split(',')
this will return [cell A1,cell A2,cell A3]
, a simple array of all the elements in the row.
From there use the indexOf
method that will return the position of the argument in the source if the argument is included in the source, else it returns -1.
Be careful to use it the right way :
sourceArray.indexOf(argument) = position of argument in sourceArray
Upvotes: 1