3

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 8<some image here>9 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.

Community
  • 1
  • 1
John Targaryen
  • 897
  • 1
  • 11
  • 24

1 Answers1

1

It basically depends on the content of your document how it is set with paragraphs. Tried with simple content in the document and insert the image.

It is inserting the image well and also not changing the formatting of rest of the text aswell.

Check the code below:

function placeImage()
 {
 var s = DocumentApp.getActiveDocument().getBody();
 var number = '8'
 //Find in Document
 var found = s.findText(number);
 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.asText().copy();// getText();
 textAsElement.editAsText().deleteText(foundLocation + number.length  ,textAsElement.asText().getText().length -1)

text.asText().editAsText().deleteText(0, foundLocation + 1 );
 //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();

 var children =DocumentApp.getActiveDocument().getBody().getChild(0).asParagraph().appendInlineImage(ingBlob);

 DocumentApp.getActiveDocument().getBody().getChild(0).asParagraph().appendText(text);

 var child = DocumentApp.getActiveDocument().getBody().getNumChildren();


}

The document content tested is:

6 7 8 9 10 11

(The size of the text is similar to as you mentioned. '8' is size 8 and so on)

You have to test using trial and error method as per your document content.

Hope that helps!

KRR
  • 4,023
  • 2
  • 12
  • 14