Search code examples
google-apps-scriptgoogle-docs

Google apps script - Bookmark a selected image


How can I addBookmark() a selected image from Goole docs. enter image description here

enter image description here


Solution

  • In your situation, how about the following sample script?

    Sample script:

    function myFunction() {
      const doc = DocumentApp.getActiveDocument();
      const selection = doc.getSelection();
      if (!selection) return;
      selection.getRangeElements().forEach(e => {
        const ele = e.getElement();
        const type = ele.getType();
        if (type == DocumentApp.ElementType.TEXT) {
          doc.addBookmark(doc.newPosition(ele, e.getStartOffset()));
        } else if (type == DocumentApp.ElementType.INLINE_IMAGE) {
          doc.addBookmark(doc.newPosition(ele.asInlineImage().getParent(), 1));
        }
      });
    }
    
    • When you use this script, please select an inline image on Google Drive and run the script. By this, the bookmark is added to the inline image. And, when you select a text, the bookmark is added to the text.

    Reference: