1

I'm trying to compare the values from 2 google sheets to get the difference. I'm loosely trying to follow Javascript / Google Apps Script compare 2 arrays, return items with NO match.

to do this I'm trying to stringify rows of my 2d value array which I created using:

var values = range.getValues();

I have created a function:

function toArrayOfJson(myArray){
  newArray = [];
  for(i=0; i<(myArray.length-1); i++){

    x=    myArray[i]
    Logger.log(x);
    Logger.log(myArray);
    y =JSON.stringify(myArray[i][0])

    newArray.push(JSON.stringify(myArray[i]));
  }
  return newArray;
}

and while debugging I see that

 JSON.stringify(myArray[i])

takes a array that looks like:

 ["none", "none", "Tyler", "TX", "none" ]

to:

"["none", "none", "Tyler", "TX", "none" ]"

Does this look right? Is this the expected appearance for a json stringified array?

user1592380
  • 34,265
  • 92
  • 284
  • 515
  • Yes, it's the expected appearance. But why are you stringifying each element, instead of just the final result? – Barmar Jun 19 '18 at 20:08
  • Each element is supposed to represent 1 row and based on the link above I want to get the diff between 2 sheets that have a lot but not all rows in common . Is there a better way? – user1592380 Jun 19 '18 at 20:13
  • Why does that require stringifying anything? Just compare the array elements directly. – Barmar Jun 19 '18 at 20:14
  • See https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript – Barmar Jun 19 '18 at 20:15
  • The accepted answer in the link involved doing that, so I thought maybe that would be simpler in apps script, I will check out your link .. – user1592380 Jun 19 '18 at 20:19
  • `JSON.stringify` doesn't appear anywhere in the question you linked to. Are you sure that's the one you meant? – Barmar Jun 19 '18 at 20:20
  • I'm not that familiar with the reduce function but thought that turning each row to JSON might be an acceptable substitute . – user1592380 Jun 19 '18 at 20:37
  • A simple `for` loop should work fine. Loop over the two arrays, return `false` if the corresponding elements don't match. If you get to the end of the loop, return `true`. – Barmar Jun 19 '18 at 20:39
  • Your approach should also work, though. – Barmar Jun 19 '18 at 20:40

0 Answers0