Here is code that inserts a table at the current selection. It may need to be modified a little, but the point is; that it finds the child index of the body at the point of the selection.
function insertTableAtSelection() {
// insert table as selection
var theDoc = DocumentApp.getActiveDocument();
var selection = theDoc.getSelection();
Logger.log('selection: ' + selection);
if (selection) {
var elements = selection.getRangeElements();
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
Logger.log('element: ' + element.getElement());
};
var theElmt = element;
var selectedElmt = theElmt.getElement();
Logger.log('selectedElmt: ' + selectedElmt);
var parent = selectedElmt.getParent();
var insertPoint = parent.getChildIndex(selectedElmt);
Logger.log('insertPoint: ' + insertPoint);
var body = theDoc.getBody();
var table = body.insertTable(insertPoint + 1, [['one','two','three'],['yellow', 'green', 'red']]);
};
};
The insertPoint is increased by one:
insertPoint + 1
That gets the table just beyond the selection.
For anyone reading this post who may want to adapt this code, keep in mind that this code only completes if there is a selection; the user needs to have highlighted some amount of content for there to be a selection.