lundi 8 juillet 2019

Dynamic if statement in Google script

I have the function below which sets values in an array to "" if each value in the row is identical and then removes the blank values from the array

If I have

arr = [[[a],[a]],[[a],[b]],[[a],[c]],[[b],[a]],[[b],[b]],[[b],[c]],[[c],[a]],[[c],[b]],[[c],[c]]]

I end up with

 [[[[a],[b]],[[a],[c]],[[b],[a]],[[b],[c]],[[c],[a]],[[c],[b]]]]

But what if my array has n dimensions instead of just two

I can't figure out how to write this part dynamically

for (var i = 0; i < values.length; i++) {
   if (values[i][0] == values[i][1]) {
       values[i][0] = '';
       values[i][1] = '';
   }
 }

So that the if statement was testing values[i][0] == values[i][1]... ==values[i][n]

Thanks

My Function

function removeEmptyRng(shtName, rng, outputRng) {
var sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(shtName);
var lr = sht.getLastRow();

var range = sht.getRange(rng + lr);
var values = range.getValues();

 for (var i = 0; i < values.length; i++) {
   if (values[i][0] == values[i][1]) {
       values[i][0] = '';
       values[i][1] = '';
   }
 }
 //Remove Empty values
 values = values.filter(function(a) {return a.filter(Boolean).length > 0;});

 if (outputRng == true) 
 {sht.getRange(1,2,values.length,values[0].length).setValues(values)};

 return values
}

Aucun commentaire:

Enregistrer un commentaire