An element representing an embedded image. An InlineImage
can be contained within a
ListItem
or Paragraph
, unless the ListItem
or Paragraph
is within
a FootnoteSection
. An InlineImage
cannot itself contain any other element. For
more information on document structure, see the
guide to extending Google Docs.
Methods
Method | Return type | Brief description |
---|---|---|
copy() | InlineImage | Returns a detached, deep copy of the current element. |
getAs(contentType) | Blob | Return the data inside this object as a blob converted to the specified content type. |
getAttributes() | Object | Retrieves the element's attributes. |
getBlob() | Blob | Return the data inside this object as a blob. |
getHeight() | Integer | Retrieves the image's height, in pixels. |
getLinkUrl() | String | Retrieves the link URL. |
getNextSibling() | Element | Retrieves the element's next sibling element. |
getParent() | ContainerElement | Retrieves the element's parent element. |
getPreviousSibling() | Element | Retrieves the element's previous sibling element. |
getType() | ElementType | Retrieves the element's ElementType . |
getWidth() | Integer | Retrieves the image's width, in pixels. |
isAtDocumentEnd() | Boolean | Determines whether the element is at the end of the
Document . |
merge() | InlineImage | Merges the element with the preceding sibling of the same type. |
removeFromParent() | InlineImage | Removes the element from its parent. |
setAttributes(attributes) | InlineImage | Sets the element's attributes. |
setHeight(height) | InlineImage | Sets the image's height, in pixels. |
setLinkUrl(url) | InlineImage | Sets the link URL. |
setWidth(width) | InlineImage | Sets the image's width, in pixels. |
Detailed documentation
copy()
Returns a detached, deep copy of the current element.
Any child elements present in the element are also copied. The new element will not have a parent.
Return
InlineImage
— the new copy
getAs(contentType)
Return the data inside this object as a blob converted to the specified content type. This method adds the appropriate extension to the filename — for example, "myfile.pdf". However, it assumes that the part of the filename that follows the last period (if any) is an existing extension that should be replaced. Consequently, "ChristmasList.12.25.2014" will become "ChristmasList.12.25.pdf".
Parameters
Name | Type | Description |
---|---|---|
contentType | String | the MIME type to convert to. For most blobs,
'application/pdf' is the only valid option. For images in BMP, GIF, JPEG,
or PNG format, any of 'image/bmp' , 'image/gif' ,
'image/jpeg' , or 'image/png' are also valid. |
Return
Blob
— the data as a blob
getAttributes()
Retrieves the element's attributes.
The result is an object containing a property for each valid element
attribute where each property name corresponds to an item in the
DocumentApp.Attribute
enumeration.
var body = DocumentApp.getActiveDocument().getBody();
// Append a styled paragraph.
var par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);
// Retrieve the paragraph's attributes.
var atts = par.getAttributes();
// Log the paragraph attributes.
for (var att in atts) {
Logger.log(att + ":" + atts[att]);
}
Return
Object
— the element's attributes
getHeight()
Retrieves the image's height, in pixels.
Return
Integer
— the image's height, in pixels
getLinkUrl()
Retrieves the link URL.
Return
String
— the link URL, or null
if the element contains multiple values for this
attribute.
getNextSibling()
Retrieves the element's next sibling element.
The next sibling has the same parent and follows the current element.
Return
Element
— the next sibling element
getParent()
Retrieves the element's parent element.
The parent element contains the current element.
Return
ContainerElement
— the parent element
getPreviousSibling()
Retrieves the element's previous sibling element.
The previous sibling has the same parent and precedes the current element.
Return
Element
— the previous sibling element
getType()
Retrieves the element's ElementType
.
Use getType()
to determine the exact type of a given element.
var body = DocumentApp.getActiveDocument().getBody();
// Obtain the first element in the document body.
var firstChild = body.getChild(0);
// Use getType() to determine the element's type.
if (firstChild.getType() == DocumentApp.ElementType.PARAGRAPH) {
Logger.log('The first element is a paragraph.');
} else {
Logger.log('The first element is not a paragraph.');
}
Return
ElementType
— the element type
getWidth()
Retrieves the image's width, in pixels.
Return
Integer
— the image's width, in pixels
isAtDocumentEnd()
Determines whether the element is at the end of the
Document
.
Return
Boolean
— whether the element is at the end of the document
merge()
Merges the element with the preceding sibling of the same type.
Only elements of the same ElementType
may be merged. Any
child elements contained in the current element are moved to the preceding
sibling element.
The current element is removed from the document.
var body = DocumentApp.getActiveDocument().getBody();
// Append two paragraphs to the document.
var par1 = body.appendParagraph('Paragraph 1.');
var par2 = body.appendParagraph('Paragraph 2.');
// Merge the newly added paragraphs into a single paragraph.
par2.merge();
Return
InlineImage
— the merged element
removeFromParent()
Removes the element from its parent.
var body = DocumentApp.getActiveDocument().getBody();
// Remove all images in the document body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
imgs[i].removeFromParent();
}
Return
InlineImage
— the removed element
setAttributes(attributes)
Sets the element's attributes.
The specified attributes parameter must be an object where each
property name is an item in the DocumentApp.Attribute
enumeration
and each property value is the new value to be applied.
var body = DocumentApp.getActiveDocument().getBody();
// Define a custom paragraph style.
var style = {};
style[DocumentApp.Attribute.HORIZONTAL_ALIGNMENT] =
DocumentApp.HorizontalAlignment.RIGHT;
style[DocumentApp.Attribute.FONT_FAMILY] =
DocumentApp.FontFamily.CALIBRI;
style[DocumentApp.Attribute.FONT_SIZE] = 18;
style[DocumentApp.Attribute.BOLD] = true;
// Append a plain paragraph.
var par = body.appendParagraph('A paragraph with custom style.');
// Apply the custom style.
par.setAttributes(style);
Parameters
Name | Type | Description |
---|---|---|
attributes | Object | the element's attributes |
Return
InlineImage
— the current element
setHeight(height)
Sets the image's height, in pixels.
Parameters
Name | Type | Description |
---|---|---|
height | Integer | the image's height, in pixels |
Return
InlineImage
— the current element
setLinkUrl(url)
Sets the link URL. If the given URL is null
or an empty string, this method will
create a link with an empty URL that may display as "Invalid link" in Google Docs.
Parameters
Name | Type | Description |
---|---|---|
url | String | the link URL |
Return
InlineImage
— the current element
setWidth(width)
Sets the image's width, in pixels.
Parameters
Name | Type | Description |
---|---|---|
width | Integer | the image's width, in pixels |
Return
InlineImage
— the current element