简体   繁体   中英

Google apps script - Bookmark a selected image

How can I addBookmark() a selected image from Goole docs. 在此处输入图像描述

在此处输入图像描述

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:

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