Class Person

Person

代表個人連結的元素。人員連結是指電子郵件地址,且可能含有與該地址相關聯的名稱。如果已設定名稱,文件內文會顯示該名稱。

方法

方法傳回類型簡短說明
copy()Person傳回目前元素的深層副本,且該副本已分離。
getAttributes()Object擷取元素的屬性。
getEmail()String傳回對方的電子郵件地址。
getName()String|null傳回使用者的顯示名稱 (如有設定)。
getNextSibling()Element|null擷取元素的下一個同層級元素。
getParent()ContainerElement|null擷取元素的父項元素。
getPreviousSibling()Element|null擷取元素的前一個同層級元素。
getType()ElementType擷取元素的 ElementType
isAtDocumentEnd()Boolean判斷元素是否位於 Document 的結尾。
merge()Person|null將元素與相同類型的前一個同層級元素合併。
removeFromParent()Person|null從父項移除元素。
setAttributes(attributes)Person設定元素的屬性。

內容詳盡的說明文件

copy()

傳回目前元素的深層副本,且該副本已分離。

元素中的任何子項元素也會一併複製。新元素沒有父項。

回攻員

Person:新副本。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getAttributes()

擷取元素的屬性。

結果會是物件,其中包含每個有效元素屬性的屬性,且每個屬性名稱都對應至 DocumentApp.Attribute 列舉中的項目。

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Append a styled paragraph.
const par = body.appendParagraph('A bold, italicized paragraph.');
par.setBold(true);
par.setItalic(true);

// Retrieve the paragraph's attributes.
const atts = par.getAttributes();

// Log the paragraph attributes.
for (const att in atts) {
  Logger.log(`${att}:${atts[att]}`);
}

回攻員

Object:元素的屬性。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getEmail()

傳回對方的電子郵件地址。

回攻員

String:對方的電子郵件地址。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getName()

傳回使用者的顯示名稱 (如有設定)。

回攻員

String|null - 連結對象的顯示名稱,或 null (如果元素顯示的是電子郵件地址)。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getNextSibling()

擷取元素的下一個同層級元素。

下一個同層級元素與目前元素具有相同的父項,且位於目前元素之後。

回攻員

Element|null:下一個同層級元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getParent()

擷取元素的父項元素。

父項元素包含目前的元素。

回攻員

ContainerElement|null:父項元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getPreviousSibling()

擷取元素的前一個同層級元素。

前一個同層級元素與目前元素具有相同的父項,且位於目前元素之前。

回攻員

Element|null:上一個同層級元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

getType()

擷取元素的 ElementType

使用 getType() 判斷指定元素的確切類型。

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Obtain the first element in the active tab's body.

const 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

isAtDocumentEnd()

判斷元素是否位於 Document 的結尾。

回攻員

Boolean:元素是否位於分頁的結尾。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

merge()

將元素與相同類型的前一個同層級元素合併。

只有相同 ElementType 的元素才能合併。目前元素中包含的所有子項元素都會移至前一個同層級元素。

目前元素會從文件中移除。

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Example 1: Merge paragraphs
// Append two paragraphs to the document's active tab.
const par1 = body.appendParagraph('Paragraph 1.');
const 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.
const cells = [
  ['Row 1, Cell 1', 'Row 1, Cell 2'],
  ['Row 2, Cell 1', 'Row 2, Cell 2'],
];
// Build a table from the array.
const table = body.appendTable(cells);
// Get the first row in the table.
const row = table.getRow(0);
// Get the two cells in this row.
const cell1 = row.getCell(0);
const cell2 = row.getCell(1);
// Merge the current cell into its preceding sibling element.
const merged = cell2.merge();

回攻員

Person|null:合併的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

removeFromParent()

從父項移除元素。

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Remove all images in the active tab's body.
const imgs = body.getImages();
for (let i = 0; i < imgs.length; i++) {
  imgs[i].removeFromParent();
}

回攻員

Person|null:已移除的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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

setAttributes(attributes)

設定元素的屬性。

指定的屬性參數必須是物件,其中每個屬性名稱都是 DocumentApp.Attribute 列舉中的項目,每個屬性值都是要套用的新值。

const doc = DocumentApp.getActiveDocument();
const documentTab = doc.getActiveTab().asDocumentTab();
const body = documentTab.getBody();

// Define a custom paragraph style.
const 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.
const par = body.appendParagraph('A paragraph with custom style.');

// Apply the custom style.
par.setAttributes(style);

參數

名稱類型說明
attributesObject元素的屬性。

回攻員

Person:目前的元素。

授權

使用這個方法的指令碼需要一或多個下列範圍的授權:

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