How to create a grid from instances?

I’m coding a placement system, grid-based. To actually code some of the functionality, i want this scripted grid, to be turned into a table again. It should function, no matter the orientation of the grid, but i have no idea where to start.

Generation code:

Thanks in advance!