Consider the document represented by the next 3 lines.
..some text..
6 7 8 9 10 11
..some text..
Imagine that all the numbers are their respective font sizes(i.e. 10 is font size 10). Now I want to insert an inline image in the space between 8 and 9, and remove that space, but NOT destroy the formatting(sizes in this example) of the unaffected text. The result would be
..some text..
6 7 89 10 11
..some text..
However, when I try
function placeImage() {
var s = DocumentApp.getActiveDocument().getBody();
//Find in Document
var found = s.findText("8");
if(found==null)
return 0;
var foundLocation = found.getStartOffset(); //position of image insertion
//Get all the needed variables
var textAsElement = found.getElement();
var text = textAsElement.getText();
var paragraph = textAsElement.getParent();
var childIndex = paragraph.getChildIndex(textAsElement); //gets index of found text in paragraph
//Problem part - when removing the space, destroys all formatting
var textRemaining = text.substring(foundLocation + 2);
textAsElement.deleteText(foundLocation, text.length-1);
if(textRemaining != "")
paragraph.insertText(childIndex+1, textRemaining);//destroys formatting for the rest of the child index
//Insert image
var imgSource = UrlFetchApp.fetch("https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Red_information_icon_with_gradient_background.svg/48px-Red_information_icon_with_gradient_background.svg.png");
var ingBlob = imgSource.getBlob();
paragraph.getChild(childIndex+1).insertInlineImage(foundLocation, ingBlob);
}
The problem is that when I remove the space and create another child element in the paragraph to insert the image at, the substring also deletes the remaining text formatting. I have tried looking into copy(), but I'm not sure how that would work efficiently.
I have researched many other places, both answers here don't preserve formatting, and position.insertInlineImage() seems to be broken, as asked here.