简体   繁体   中英

Google Docs - create image at bookmark with Google Apps Script

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?

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);
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM