操纵PositionedImage并将文字环绕在Google文档中 [英] Manipulate PositionedImage and wrap text around image in Google Docs

查看:76
本文介绍了操纵PositionedImage并将文字环绕在Google文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下代码段将图像插入Google文档:

I used the following snippet to insert an image into a Google Document:

// Source: http://stackoverflow.com/a/18859986/1536038
var doc = DocumentApp.openById('Google Drive Id');
var img = DriveApp.getFileById('Google Drive Id').getBlob(); 
doc.getBody().insertImage(0, img);

结果是一个In line图像:

The result is an In line image:

但是,我希望有一个Wrap text图像,如下所示:

I want, however, to have a Wrap text image, like so:

可以通过Google Apps脚本(即时)实现吗?

Is that possible via Google Apps Script (on the fly)?

推荐答案

问题1529 已修复.自2015年12月起,Google Apps脚本可以操纵 PositionedImage Google文档中的对象.

Issue 1529 has been fixed. As of December 2015, Google Apps Script can manipulate PositionedImage objects in Google Docs.

它们的行为与 InlineImage 元素略有不同,因为它们需要锚定到ListItem或Paragraph元素,而InlineImages只能添加到BodyFooterSectionHeaderSectionTableCell元素.

They behave a little differently than InlineImage elements, as they need to be anchored to a ListItem or Paragraph element, while InlineImages can be added only to Body, FooterSection, HeaderSection or TableCell elements.

PositionedImage是锚定在元素中的对象,而InlineImage本身是文档的元素. 这意味着您不能将一种图像直接转换为另一种图像.(使用UI将图像从自动换行"切换为内联"时,PositionedImage将从其锚点中移除段落,然后插入该段落之外的文档正文中.如有必要,您可以通过脚本进行模拟.)

A PositionedImage is an object anchored in an element, while an InlineImage is itself an element of a document. This implies that you cannot convert one type of image directly to the other. (When you switch an image from "Wrap text" to "Inline" using the UI, the PositionedImage is removed from its anchor paragraph, then inserted into the body of the document outside of that paragraph. You could emulate that via script if necessary.)

这是以下脚本插入的PositionedImage的示例:

Here's an example of a PositionedImage inserted by the following script:

// http://stackoverflow.com/a/20661113/1677912
function DemoPositionedImage() {
  // Get handle on active document
  var doc = DocumentApp.getActiveDocument();

  // Find desired image file
  var matchedFiles = DriveApp.getFilesByName('apple-touch-icon.png');
  if (matchedFiles.hasNext()) {
    // Get image object from file
    var image = matchedFiles.next().getBlob(); 

    // Add image as a PositionedImage.
    var positionedImage = doc.getBody().getParagraphs()[0].addPositionedImage(image);

    // Adjust layout, etc. here

    // Log the ID of the new image
    Logger.log( positionedImage.getId() );
  }
}

日志显示新图像的ID,如下所示:

The log shows the ID of the new image, like this:

[15-12-11 20:35:03:706 EST] kix.9dwnzjfapdy8

请注意-如果您将多个图像添加到同一元素(例如,段落)中,并且使用默认布局,则最新图像将覆盖现有图像.因此,当实际上有一堆图像时,看起来好像只有一张图像.

Be careful - if you add multiple images to the same element (e.g. Paragraph), with default layout, the newest image will overlay existing ones. Therefore, it may look like you have a single image when there are actually a pile of them.

由于PositionedImage不是文档的元素,因此它不会出现在具有诸如段落,表格或InlineImages之类的元素的元素层次结构中,并且无法通过文档方法getChild()getNextSibling()找到,等等.同样,没有Body.getPositionedImages()可以与Body.getImages()并行.

Since a PositionedImage is not an element of a document, it does not appear in the element hierarchy with elements like paragraphs, tables, or InlineImages, and cannot be found through the document methods getChild(), getNextSibling(), and so on. Likewise, there is no Body.getPositionedImages() to parallel Body.getImages().

相反,您可以使用其唯一ID获得PositionedImage,例如kix.9dwnzjfapdy8来自先前的示例.

Instead, you can get a PositionedImage using its unique ID, e.g. kix.9dwnzjfapdy8 from the earlier example.

var positionedImage = getPositionedImage(storedId);

或者,您可以将包含元素中的所有PositionedImage对象作为数组获取.

Alternatively, you can get all the PositionedImage objects in a containing element as an array.

var positionedImages = getPositionedImages();
for (var i=0; i<positionedImages.length; i++) {
  Logger.log( positionedImages[i].getId() );
}

要检索文档中的所有PositionedImage,需要遍历所有可能的锚点元素.以下实用程序可以做到这一点.

Retrieving all the PositionedImages in a document requires traversing all the possible anchor elements. The following utility does just that.

/**
 * Get a list of all PositionedImages in a document.
 * See stackoverflow.com/a/20661113/1677912.
 *
 * @param {String} docId         (optional) ID of document to scan
 *
 * @returns {PositionedImage[]}  Array of PositionedImages in document
 */
function getAllPositionedImages( docId ) {
  // Open document if given ID, otherwise use active document.
  if (docId) {
    var doc = DocumentApp.openById(docId);
  }
  else {
    doc = DocumentApp.getActiveDocument();
  }

  // Get handle on document's body
  var body = doc.getBody();

  // array to hold all images in document
  var allPositionedImages = [];

  var numElems = body.getNumChildren();

  for (var childIndex=0; childIndex<numElems; childIndex++) {
    var child = body.getChild(childIndex);
    switch ( child.getType() ) {
      case DocumentApp.ElementType.PARAGRAPH:
        var container = child.asParagraph();
        break;
      case DocumentApp.ElementType.LIST_ITEM:
        container = child.asListItem();
        break;

      default:
        // Skip elements that can't contain PositionedImages.
        continue;
    }
    // Collect images from current container
    var imagesHere = container.getPositionedImages();
    allPositionedImages = allPositionedImages.concat(imagesHere);        
  }
  return allPositionedImages;
}

布局控制

PositionedImages的大多数布局控件在文档中都有详细描述:

Layout control

Most of the layout controls for PositionedImages are well described in the documentation:

  • Height: setHeight(), getHeight()
  • Width: setWidth(), getWidth()
  • LeftOffset: setLeftOffset(), getLeftOffset()
  • TopOffset: setTopOffset(), getTopOffset()
  • Layout: setLayout(), getLayout()

一起使用的 PositionedLayout枚举 > Layout 方法对于PositionedImages是唯一的.但是,在启动PositionedImage支持时,它不包含在编辑器自动完成中,并且文档中没有使用它的示例.让我们填补这个空白.

The PositionedLayout enum used with the Layout methods is unique to PositionedImages. At the time of launch of PositionedImage support however, it was not included in editor autocompletion, and the documentation contained no examples of its use. Let's fill that gap.

这是您设置布局的方法PositionedImage,以便用文本将其包裹起来:

Here's how you can set the layout of a PositionedImage so that it is wrapped by text:

positionedImage.setLayout( DocumentApp.PositionedLayout.WRAP_TEXT );

以下实用程序函数获得与PositionedLayout枚举等效的英语版本.

The following utility function gets the English equivalent of a PositionedLayout enum.

/**
 * Get the string representing the given PositionedLayout enum.
 * Ref: https://developers.google.com/apps-script/reference/document/positioned-layout
 *
 * See stackoverflow.com/a/20661113/1677912.
 *
 * @param {PositionedLayout} PositionedLayout  Enum value.
 *
 * @returns {String}         English text matching enum.
 */
function getLayoutString( PositionedLayout ) {
  var layout;
  switch ( PositionedLayout ) {
    case DocumentApp.PositionedLayout.ABOVE_TEXT:
      layout = "ABOVE_TEXT";
      break;
    case DocumentApp.PositionedLayout.BREAK_BOTH:
      layout = "BREAK_BOTH";
      break;
    case DocumentApp.PositionedLayout.BREAK_LEFT:
      layout = "BREAK_LEFT";
      break;
    case DocumentApp.PositionedLayout.BREAK_RIGHT:
      layout = "BREAK_RIGHT";
      break;
    case DocumentApp.PositionedLayout.WRAP_TEXT:
      layout = "WRAP_TEXT";
      break;
    default:
      layout = "";
      break;
  }
  return layout;
}

注意:该消息已同时发布

Note: This has been concurrently posted on my blog.

这篇关于操纵PositionedImage并将文字环绕在Google文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆