Class RichLink

RichLink

表示指向 Google 资源(例如云端硬盘文件或 YouTube 视频)的链接的元素。

方法

方法返回类型简介
copy()RichLink返回当前元素的已分离的深层副本。
getAttributes()Object检索元素的属性。
getMimeType()String返回链接的 MIME 类型(当该项是指向云端硬盘文件的链接时可用,否则为 null 时)。
getNextSibling()Element检索元素的下一个同级元素。
getParent()ContainerElement获取元素的父元素。
getPreviousSibling()Element检索元素的上一个同级元素。
getTitle()String返回链接的显示标题。
getType()ElementType检索元素的 ElementType
getUrl()String返回资源的网址。
isAtDocumentEnd()Boolean确定元素是否位于 Document 的末尾。
merge()RichLink将元素与同一类型的上一个同级元素合并。
removeFromParent()RichLink将元素从其父项中移除。
setAttributes(attributes)RichLink设置元素的属性。

详细文档

copy()

返回当前元素的已分离深层副本。

元素中存在的所有子元素也会被复制。新元素没有父元素。

弃踢回攻

RichLink - 新副本。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAttributes()

检索元素的属性。

结果会得到一个包含每个有效元素属性的属性的对象,其中每个属性名称对应 DocumentApp.Attribute 枚举中的一项。

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

弃踢回攻

Object - 元素的属性。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getMimeType()

返回链接的 MIME 类型(当该项是指向云端硬盘文件的链接时可用,否则为 null 时可用)。

例如,如果链接指向 Google 文档文件,则返回字符串 application/vnd.google-apps.document

弃踢回攻

String - 链接的 MIME 类型(如果有)。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNextSibling()

检索元素的下一个同级元素。

下一个同级元素具有相同的父元素,并跟随当前元素。

弃踢回攻

Element - 下一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getParent()

获取元素的父元素。

父元素包含当前元素。

弃踢回攻

ContainerElement - 父元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getPreviousSibling()

检索元素的上一个同级元素。

上一个同级元素具有相同的父元素,并且位于当前元素之前。

弃踢回攻

Element - 上一个同级元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getTitle()

返回链接的显示标题。此标题与插入链接或上次更新链接时关联资源的标题一致。例如,如果此链接指向标题为“Analysis”的 Google 文档文档,则会返回 Analysis

弃踢回攻

String - 链接的显示标题。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getType()

检索元素的 ElementType

使用 getType() 可确定给定元素的确切类型。

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

弃踢回攻

ElementType - 元素类型。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getUrl()

返回资源的网址。

弃踢回攻

String - 资源的网址。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

isAtDocumentEnd()

确定元素是否位于 Document 的末尾。

弃踢回攻

Boolean - 元素是否位于文档末尾。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

merge()

将元素与同一类型的前面的同级元素合并。

只能合并同一 ElementType 的元素。当前元素中包含的任何子元素都将移动到前面的同级元素。

当前元素会从文档中移除。

var body = DocumentApp.getActiveDocument().getBody();
// Example 1: Merge paragraphs
// 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();

// Example 2: Merge table cells
// Create a two-dimensional array containing the table's cell contents.
var cells = [
['Row 1, Cell 1', 'Row 1, Cell 2'],
['Row 2, Cell 1', 'Row 2, Cell 2']
];
// Build a table from the array.
var table = body.appendTable(cells);
// Get the first row in the table.
 var row = table.getRow(0);
// Get the two cells in this row.
var cell1 = row.getCell(0);
var cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
var merged = cell2.merge();

弃踢回攻

RichLink - 已合并的元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeFromParent()

将元素从其父项中移除。

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

弃踢回攻

RichLink - 已移除的元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setAttributes(attributes)

设置元素的属性。

指定的属性参数必须是一个对象,其中每个属性名称都是 DocumentApp.Attribute 枚举中的一个项,每个属性值都是要应用的新值。

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] = '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);

参数

名称类型说明
attributesObject元素的属性。

弃踢回攻

RichLink - 当前元素。

授权

使用此方法的脚本需要获得以下一个或多个范围的授权:

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents