-1

I'd like to apply a filter to a range in google sheets. The range is input by a function that pulls data from a database. Is it possible to sort this range if the function is managing the data in a certain order (through a 2d array)

Gist of Code in Google App Scripts

function returnData() { 
     return ["DataX", "DataY", "DataZ"]
}

Function Call in Cell:

= returnData()
NightEye
  • 10,634
  • 2
  • 5
  • 24

1 Answers1

0

You can just apply the filter or sort either via formula or the script. Script would be much easier to do though when the data is complicated or you want to sort it a certain way the formula can't do easily.

Let's say we'd like to sort a 2D array based on its 1st column.

Formula:

=sort(returnData(), 1, true)

output1

  • A1 is =returnData()

Script:

function returnData() { 
  return [["4", "DataX", "DataY"],
          ["1", "DataA", "DataB"]].sort(sortFunction);
}
// https://stackoverflow.com/a/16097058/14606045
function sortFunction(a, b) {
    if (a[0] === b[0]) {
        return 0;
    }
    else {
        return (a[0] < b[0]) ? -1 : 1;
    }
}

output2

If you could provide a sample data and the output you'd like to have, then that would enable us able to provide you a more specific answer.

NightEye
  • 10,634
  • 2
  • 5
  • 24