Home > Enterprise >  count of elements concatenated in apps script
count of elements concatenated in apps script

Time:11-28

I have a concatenated range of values. I am trying to get the number of elements concatenated by looping over the range B2:B to identify the count of elements if are 3.

The loop should check if there are 3 elements then push those values into an array, if encountered 1 element then should break out.

function test(){
   d1= SpreadsheetApp.getActiveSpreadsheet();
   var P_Details = d1.getSheetByName("test1");
   var P_range = P_Details.getRange("B2:B");
   var P_val = P_range.getValues();
   
   P_dump = [];
   for (var i in P_val){
     if(// (code here to split and count)=3){
         P_dump.push(P_val[i]);
     }
     else{ 
     break;
     }
   }
}


[![image of the range][1]][1]


  [1]: https://i.stack.imgur.com/1NfOp.jpg

|          | Concatenated values|
| -------- | -------------------|
|          | a-b-c              |
|          | a-d-e              |
|          | w-x-y              |
|          | d                  |
|          | d                  |
|          | e                  |

CodePudding user response:

Try this,

   P_dump = [];
   for (var i in P_val){
     if(P_val[i][0].split('-').length == 3){
         P_dump.push(P_val[i][0]);
     }
     else{ 
      break;
     }
   }
   console.log(P_dump);
  • Related