0
votes

I have created a bookmark in Google Docs. I know the bookmark Id.

function addBookmark() {
 var doc = DocumentApp.getActiveDocument();
 var cursor = doc.getCursor();
 var bookmark = doc.addBookmark(cursor);

 var bookmarkId = bookmark.getId();

 Logger.log(bookmarkId);  
}
  1. Can I create an image from Google Drive at the bookmark?
  2. After creating the image I want to update it regularly with a time-based trigger. Do I have to delete the image before or can I update it without deleting it?
1
What do you mean, "create an image?" Take a screenshot? Insert an image from Drive? You can accomplish the second portion several ways. Use methods in the Paragraph class of DocumentApp. - Brian
Thank you. Yes, I take an image from Drive. I created several paragraphs. I can add the image with f.e. var childIndex = body.getNumChildren() - 1;body.insertImage(childIndex, image); This childIndex changes when adding more paragraps. How can I find the exact childIndex for a specific paragraph? By searching a text string? How works it? - Roberto B
I update the image on child index 0 with body.getChild(0).asParagraph().clear(); body.getChild(0).asParagraph().appendInlineImage(image); - is this the usual way to update a picture? My problem: the picture is at absolute position child index 0. When another user adds a paragraph before this position, the next picture update would append the picture to this position and not to the original paragraph. How can I get a relative child index position, depends on the paragraph text? - Roberto B
You could append a bookmark on the new image and stopped it in the properties of the doc to set it apart from anything else appliances by other users. - Brian
You have convinced me to work with the methods in the paragraph class - no bookmarks :) Summarized. I add a picture with child index 0. body.getChild(0).asParagraph().clear(); body.getChild(0).asParagraph().appendInlineImage(image); After this, I add several paragraphs before it. The picture is at child index X, not 0 now. Yet, I will update exactly this picture. The child index is unknown. How can I find out what child index the picture has now? - Roberto B

1 Answers

0
votes

The bookmark Id is not helpful. Better is to write a paragraph with specific text in the document. Now you can search for it and add or update the image.

function updateImageAtParagraph() {
 var body = DocumentApp.getActiveDocument().getBody();

 // Get an image in Drive from its ID.
 var image = DriveApp.getFileById('...imageId...');

 var searchString = "paragraphText";  //specific text paragraph in the document

 var paragraphSearch = body.findText(searchString);

 if(paragraphSearch !== null) {
   var paragraph = paragraphSearch.getElement().getParent().asParagraph();
   paragraph.clear().appendText(searchString);
   paragraph.appendInlineImage(image);
 }
}