0

I have some data pulling from a sheet. I have created an array that is sorted by the 2nd column (SKU column) below is what is returned in the spreadsheet:

enter image description here

In this array would like to add a blank row when the SKU changes to make it easier to read. I wrote the following code:

function update(){

  var mfgSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Order Info");
  var purchaseLog = SpreadsheetApp.
    openById("1KkR4jE1c00WuQpPrexW8f9BIq5vxl0fWaUGxbeYERsE").
    getSheetByName("Order Details");

    var orderSort = purchaseLog.getRange(3, 1, purchaseLog.getLastRow(), 15).getValues().
    sort(function(r1,r2){
      var a = r1[1];
      var b = r2[1];
      if(a>b){
        return 1;
      }else if (a<b){
        return -1;}
      return 0;
    }).filter(function (item){if(item[1]!=""&&item[1]!="Purchase Order Link"){return true}})
    .map(function(r){return [r[0],r[1],r[2],r[5],r[6],r[11],r[13],r[12],r[8]]});


  var SKUs = orderSort.map(function(r){return r[1]})

  var SKUList = [];
  for (var i in SKUs) {
    var SKU = SKUs[i];
    var duplicate = false;
    for (var j in SKUList) {
      if (SKU === SKUList[j]) {
        duplicate = true;
      }
    }
    if (!duplicate) {
      SKUList.push(SKU);
    }
  }

  var finalArr = []
  for(var i = 0; i <= SKUList.length-1; i++){
    var element = orderSort.filter(function(item){if(item[1]===SKUList[i]){return true}});
    finalArr.push(element);
    finalArr.push([,,,,,,,,,]);
  }

  Logger.log(finalArr);

}

The code almost works, but I'm getting a weird 3d Array, and I'm afraid that my logic is very wrong. Photo of the log is also included. Anyone that could help me solve this problem, it would be greatly appreciated.

enter image description here

0

1 Answer 1

2

element is already a 2D array. By pushing it to another array, a 3D array is created. Use Array.concat instead:

finalArr = finalArr.concat(element);//instead of finalArr.push(element);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.