Class Table

ตาราง

องค์ประกอบที่แสดงตาราง Table อาจมีองค์ประกอบ TableRow เท่านั้น สําหรับข้อมูลเพิ่มเติมเกี่ยวกับโครงสร้างเอกสาร โปรดดูคู่มือการขยาย Google เอกสาร

เมื่อสร้าง Table ที่มีแถวหรือเซลล์จํานวนมาก ให้พิจารณาสร้างแถวจากอาร์เรย์สตริง ดังที่แสดงในตัวอย่างต่อไปนี้

var body = DocumentApp.getActiveDocument().getBody();

// Create a two-dimensional array containing the 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.
body.appendTable(cells);

วิธีการ

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
appendTableRow()TableRowสร้างและเพิ่ม TableRow ใหม่
appendTableRow(tableRow)TableRowเพิ่ม TableRow ที่ระบุ
clear()Tableล้างเนื้อหาขององค์ประกอบ
copy()Tableแสดงผลสําเนาเดี่ยวที่แยกองค์ประกอบปัจจุบัน
editAsText()Textรับองค์ประกอบปัจจุบัน Text เพื่อแก้ไข
findElement(elementType)RangeElementค้นหาเนื้อหาขององค์ประกอบจากองค์ประกอบสืบทอดของประเภทที่ระบุ
findElement(elementType, from)RangeElementค้นหาเนื้อหาขององค์ประกอบสืบทอดของประเภทที่ระบุ โดยเริ่มจาก RangeElement ที่ระบุ
findText(searchPattern)RangeElementค้นหาเนื้อหาของรูปแบบข้อความที่ระบุโดยใช้นิพจน์ทั่วไป
findText(searchPattern, from)RangeElementค้นหาเนื้อหาขององค์ประกอบสําหรับรูปแบบข้อความที่ระบุ เริ่มจากผลการค้นหาที่กําหนด
getAttributes()Objectเรียกข้อมูลแอตทริบิวต์ขององค์ประกอบ
getBorderColor()Stringเรียกข้อมูลสีเส้นขอบ
getBorderWidth()Numberเรียกข้อมูลความกว้างของเส้นขอบเป็นจุด
getCell(rowIndex, cellIndex)TableCellเรียกข้อมูล TableCell ที่แถวและดัชนีเซลล์ที่ระบุ
getChild(childIndex)Elementเรียกข้อมูลองค์ประกอบย่อยที่ดัชนีย่อยที่ระบุ
getChildIndex(child)Integerเรียกข้อมูลดัชนีย่อยสําหรับองค์ประกอบย่อยที่ระบุ
getColumnWidth(columnIndex)Numberเรียกข้อมูลความกว้างของคอลัมน์ตารางที่ระบุเป็นจุด
getLinkUrl()Stringเรียก URL ของลิงก์
getNextSibling()Elementเรียกองค์ประกอบข้างเคียงขององค์ประกอบถัดไป
getNumChildren()Integerเรียกข้อมูลจํานวนบุตรหลาน
getNumRows()Integerเรียกข้อมูลจํานวน TableRows
getParent()ContainerElementเรียกองค์ประกอบระดับบนสุดขององค์ประกอบ
getPreviousSibling()Elementเรียกองค์ประกอบระดับข้างก่อนหน้าขององค์ประกอบ
getRow(rowIndex)TableRowเรียกข้อมูล TableRow ที่ดัชนีแถวที่ระบุ
getText()Stringเรียกเนื้อหาขององค์ประกอบเป็นสตริงข้อความ
getTextAlignment()TextAlignmentรับการปรับแนวข้อความ
getType()ElementTypeเรียกข้อมูล ElementType ขององค์ประกอบ
insertTableRow(childIndex)TableRowสร้างและแทรก TableRow ใหม่ที่ดัชนีที่ระบุ
insertTableRow(childIndex, tableRow)TableRowแทรก TableRow ที่ระบุที่ดัชนีที่ระบุ
isAtDocumentEnd()Booleanกําหนดว่าองค์ประกอบอยู่ท้าย Document หรือไม่
removeChild(child)Tableนําองค์ประกอบย่อยที่ระบุออก
removeFromParent()Tableนําองค์ประกอบออกจากองค์ประกอบหลัก
removeRow(rowIndex)TableRowนํา TableRow ที่ดัชนีแถวที่ระบุออก
replaceText(searchPattern, replacement)Elementแทนที่รูปแบบข้อความที่ระบุทั้งหมดด้วยสตริงแทนที่ที่กําหนด โดยใช้นิพจน์ปกติ
setAttributes(attributes)Tableตั้งค่าแอตทริบิวต์ขององค์ประกอบ
setBorderColor(color)Tableตั้งค่าสีเส้นขอบ
setBorderWidth(width)Tableตั้งค่าความกว้างของเส้นขอบเป็นจุด
setColumnWidth(columnIndex, width)Tableตั้งค่าความกว้างของคอลัมน์ที่ระบุเป็นจุด
setLinkUrl(url)Tableตั้งค่า URL ของลิงก์
setTextAlignment(textAlignment)Tableตั้งค่าการจัดข้อความ

เอกสารโดยละเอียด

appendTableRow()

สร้างและเพิ่ม TableRow ใหม่

ขากลับ

TableRow — องค์ประกอบแถวในตารางใหม่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

appendTableRow(tableRow)

เพิ่ม TableRow ที่ระบุ

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table in the document and copies the second row.
const table = body.getTables()[0];
const row = table.getChild(1).copy();

// Adds the copied row to the bottom of the table.
const tableRow = table.appendTableRow(row);

พารามิเตอร์

ชื่อTypeคำอธิบาย
tableRowTableRowแถวของตารางที่จะต่อท้าย

ขากลับ

TableRow — องค์ประกอบแถวในตารางต่อท้าย

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

clear()

ล้างเนื้อหาขององค์ประกอบ

ขากลับ

Table — องค์ประกอบปัจจุบัน


copy()

แสดงผลสําเนาเดี่ยวที่แยกองค์ประกอบปัจจุบัน

ระบบจะคัดลอกองค์ประกอบย่อยที่อยู่ในองค์ประกอบนั้นด้วย องค์ประกอบใหม่ไม่มี ระดับบนสุด

ขากลับ

Table — สําเนาใหม่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

editAsText()

รับองค์ประกอบปัจจุบัน Text เพื่อแก้ไข

ใช้ editAsText เพื่อบิดเบือนองค์ประกอบองค์ประกอบเป็นข้อความริชมีเดีย โหมด editAsText จะละเว้นองค์ประกอบที่ไม่ใช่ข้อความ (เช่น InlineImage และ HorizontalRule)

องค์ประกอบย่อยที่อยู่ในช่วงข้อความที่ถูกลบจะถูกนําออกจากองค์ประกอบ

var body = DocumentApp.getActiveDocument().getBody();

// Insert two paragraphs separated by a paragraph containing an
// horizontal rule.
body.insertParagraph(0, "An editAsText sample.");
body.insertHorizontalRule(0);
body.insertParagraph(0, "An example.");

// Delete " sample.\n\n An" removing the horizontal rule in the process.
body.editAsText().deleteText(14, 25);

ขากลับ

Text — เวอร์ชันข้อความขององค์ประกอบปัจจุบัน


findElement(elementType)

ค้นหาเนื้อหาขององค์ประกอบจากองค์ประกอบสืบทอดของประเภทที่ระบุ

พารามิเตอร์

ชื่อTypeคำอธิบาย
elementTypeElementTypeประเภทขององค์ประกอบที่จะค้นหา

ขากลับ

RangeElement — ผลการค้นหาที่ระบุตําแหน่งขององค์ประกอบการค้นหา

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

findElement(elementType, from)

ค้นหาเนื้อหาขององค์ประกอบสืบทอดของประเภทที่ระบุ โดยเริ่มจาก RangeElement ที่ระบุ

// Get the body section of the active document.
var body = DocumentApp.getActiveDocument().getBody();

// Define the search parameters.
var searchType = DocumentApp.ElementType.PARAGRAPH;
var searchHeading = DocumentApp.ParagraphHeading.HEADING1;
var searchResult = null;

// Search until the paragraph is found.
while (searchResult = body.findElement(searchType, searchResult)) {
  var par = searchResult.getElement().asParagraph();
  if (par.getHeading() == searchHeading) {
    // Found one, update and stop.
    par.setText('This is the first header.');
    return;
  }
}

พารามิเตอร์

ชื่อTypeคำอธิบาย
elementTypeElementTypeประเภทขององค์ประกอบที่จะค้นหา
fromRangeElementผลการค้นหาที่จะค้นหา

ขากลับ

RangeElement — ผลการค้นหาที่ระบุตําแหน่งถัดไปขององค์ประกอบการค้นหา

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

findText(searchPattern)

ค้นหาเนื้อหาของรูปแบบข้อความที่ระบุโดยใช้นิพจน์ทั่วไป

ชุดย่อยของคุณลักษณะนิพจน์ทั่วไปของ JavaScript ไม่ได้รับการสนับสนุนอย่างสมบูรณ์ เช่น แคปเจอร์กรุ๊ป และตัวแก้ไขโหมด

ระบบจะจับคู่รูปแบบนิพจน์ทั่วไปที่มีให้กับบล็อกข้อความแต่ละบล็อกที่มีอยู่ในองค์ประกอบปัจจุบัน

พารามิเตอร์

ชื่อTypeคำอธิบาย
searchPatternStringรูปแบบที่จะค้นหา

ขากลับ

RangeElement — ผลการค้นหาที่ระบุตําแหน่งของข้อความค้นหา หรือค่าว่างหากไม่มีรายการที่ตรงกัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

findText(searchPattern, from)

ค้นหาเนื้อหาขององค์ประกอบสําหรับรูปแบบข้อความที่ระบุ เริ่มจากผลการค้นหาที่กําหนด

ชุดย่อยของคุณลักษณะนิพจน์ทั่วไปของ JavaScript ไม่ได้รับการสนับสนุนอย่างสมบูรณ์ เช่น แคปเจอร์กรุ๊ป และตัวแก้ไขโหมด

ระบบจะจับคู่รูปแบบนิพจน์ทั่วไปที่มีให้กับบล็อกข้อความแต่ละบล็อกที่มีอยู่ในองค์ประกอบปัจจุบัน

พารามิเตอร์

ชื่อTypeคำอธิบาย
searchPatternStringรูปแบบที่จะค้นหา
fromRangeElementผลการค้นหาที่จะค้นหา

ขากลับ

RangeElement — ผลการค้นหาที่ระบุตําแหน่งถัดไปของข้อความค้นหา หรือไม่มีข้อมูลหากไม่มีรายการที่ตรงกัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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 — แอตทริบิวต์ขององค์ประกอบ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getBorderColor()

เรียกข้อมูลสีเส้นขอบ

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border color of the first table.
table.setBorderColor('#00FF00');

// Logs the border color of the first table to the console.
console.log(table.getBorderColor());

ขากลับ

String — สีเส้นขอบในรูปแบบ CSS (เช่น '#ffffff')

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getBorderWidth()

เรียกข้อมูลความกว้างของเส้นขอบเป็นจุด

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border width of the first table.
table.setBorderWidth(20);

// Logs the border width of the first table.
console.log(table.getBorderWidth());

ขากลับ

Number — ความกว้างของเส้นขอบ หน่วยเป็นจุด

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getCell(rowIndex, cellIndex)

เรียกข้อมูล TableCell ที่แถวและดัชนีเซลล์ที่ระบุ

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Gets the cell of the table's third row and second column.
const cell = table.getCell(2, 1);

// Logs the cell text to the console.
console.log(cell.getText());

พารามิเตอร์

ชื่อTypeคำอธิบาย
rowIndexIntegerดัชนีของแถวที่มีเซลล์ที่จะเรียกข้อมูล
cellIndexIntegerดัชนีของเซลล์ที่จะเรียกข้อมูล

ขากลับ

TableCell — เซลล์ของตาราง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getChild(childIndex)

เรียกข้อมูลองค์ประกอบย่อยที่ดัชนีย่อยที่ระบุ

// Get the body section of the active document.
var body = DocumentApp.getActiveDocument().getBody();

// Obtain the first element in the document.
var firstChild = body.getChild(0);

// If it's a paragraph, set its contents.
if (firstChild.getType() == DocumentApp.ElementType.PARAGRAPH) {
  firstChild.asParagraph().setText("This is the first paragraph.");
}

พารามิเตอร์

ชื่อTypeคำอธิบาย
childIndexIntegerดัชนีขององค์ประกอบย่อยเพื่อดึง

ขากลับ

Element — องค์ประกอบย่อยที่ดัชนีที่ระบุ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getChildIndex(child)

เรียกข้อมูลดัชนีย่อยสําหรับองค์ประกอบย่อยที่ระบุ

พารามิเตอร์

ชื่อTypeคำอธิบาย
childElementองค์ประกอบย่อยที่จะดึงข้อมูลดัชนี

ขากลับ

Integer — ดัชนีย่อย

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getColumnWidth(columnIndex)

เรียกข้อมูลความกว้างของคอลัมน์ตารางที่ระบุเป็นจุด

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the width of the second column to 100 points.
const columnWidth = table.setColumnWidth(1, 100);

// Gets the width of the second column and logs it to the console.
console.log(columnWidth.getColumnWidth(1));

พารามิเตอร์

ชื่อTypeคำอธิบาย
columnIndexIntegerดัชนีคอลัมน์

ขากลับ

Number — ความกว้างของคอลัมน์ เป็นจุด

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getLinkUrl()

เรียก URL ของลิงก์

ขากลับ

String — URL ของลิงก์ หรือ Null หากองค์ประกอบมีหลายค่าสําหรับแอตทริบิวต์นี้

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getNextSibling()

เรียกองค์ประกอบข้างเคียงขององค์ประกอบถัดไป

พี่น้องถัดไปมีระดับบนสุดและไปตามองค์ประกอบปัจจุบัน

ขากลับ

Element — องค์ประกอบข้างเคียงถัดไป

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getNumChildren()

เรียกข้อมูลจํานวนบุตรหลาน

// Get the body section of the active document.
var body = DocumentApp.getActiveDocument().getBody();

// Log the number of elements in the document.
Logger.log("There are " + body.getNumChildren() +
    " elements in the document body.");

ขากลับ

Integer — จํานวนเด็ก

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getNumRows()

เรียกข้อมูลจํานวน TableRows

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Logs the number of rows of the first table to the console.
console.log(table.getNumRows());

ขากลับ

Integer — จํานวนแถวในตาราง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getParent()

เรียกองค์ประกอบระดับบนสุดขององค์ประกอบ

องค์ประกอบระดับบนจะมีองค์ประกอบปัจจุบัน

ขากลับ

ContainerElement — องค์ประกอบระดับบน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getPreviousSibling()

เรียกองค์ประกอบระดับข้างก่อนหน้าขององค์ประกอบ

พี่น้องก่อนหน้ามีระดับบนสุดเดียวกันอยู่ก่อนองค์ประกอบปัจจุบัน

ขากลับ

Element — องค์ประกอบข้างเคียงก่อนหน้า

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getRow(rowIndex)

เรียกข้อมูล TableRow ที่ดัชนีแถวที่ระบุ

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table and logs the text of first row to the console.
const table = body.getTables()[0];
console.log(table.getRow(0).getText());

พารามิเตอร์

ชื่อTypeคำอธิบาย
rowIndexIntegerดัชนีของแถวที่จะดึงข้อมูล

ขากลับ

TableRow — แถวของตาราง

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getText()

เรียกเนื้อหาขององค์ประกอบเป็นสตริงข้อความ

ขากลับ

String — เนื้อหาขององค์ประกอบเป็นสตริงข้อความ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

getTextAlignment()

รับการปรับแนวข้อความ ประเภทการจัดข้อความที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT และ DocumentApp.TextAlignment.SUPERSCRIPT

ขากลับ

TextAlignment — ประเภทการจัดข้อความ หรือ null หากข้อความมีการจัดแนวข้อความหลายประเภท หรือหากไม่เคยตั้งค่าการปรับแนวข้อความ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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 — ประเภทองค์ประกอบ

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

insertTableRow(childIndex)

สร้างและแทรก TableRow ใหม่ที่ดัชนีที่ระบุ

พารามิเตอร์

ชื่อTypeคำอธิบาย
childIndexIntegerดัชนีที่จะแทรกองค์ประกอบ

ขากลับ

TableRow — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

insertTableRow(childIndex, tableRow)

แทรก TableRow ที่ระบุที่ดัชนีที่ระบุ

พารามิเตอร์

ชื่อTypeคำอธิบาย
childIndexIntegerดัชนีที่จะแทรกองค์ประกอบ
tableRowTableRowแถวในตารางที่จะแทรก

ขากลับ

TableRow — องค์ประกอบแถวในตารางที่แทรก

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

isAtDocumentEnd()

กําหนดว่าองค์ประกอบอยู่ท้าย Document หรือไม่

ขากลับ

Boolean — ระบุว่าองค์ประกอบอยู่ที่ท้ายเอกสารหรือไม่

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

removeChild(child)

นําองค์ประกอบย่อยที่ระบุออก

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Finds the first table row and removes it.
const element = table.findElement(DocumentApp.ElementType.TABLE_ROW);
table.removeChild(element.getElement());

พารามิเตอร์

ชื่อTypeคำอธิบาย
childElementองค์ประกอบลูกที่จะลบ

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

ขากลับ

Table — องค์ประกอบที่นําออก

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

removeRow(rowIndex)

นํา TableRow ที่ดัชนีแถวที่ระบุออก

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table and removes its second row.
const table = body.getTables()[0];
table.removeRow(1);

พารามิเตอร์

ชื่อTypeคำอธิบาย
rowIndexIntegerดัชนีของแถวที่จะนําออก

ขากลับ

TableRow — แถวที่นําออก

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

replaceText(searchPattern, replacement)

แทนที่รูปแบบข้อความที่ระบุทั้งหมดด้วยสตริงแทนที่ที่กําหนด โดยใช้นิพจน์ปกติ

ระบบจะส่งรูปแบบการค้นหาเป็นสตริง ไม่ใช่ออบเจ็กต์ของนิพจน์ทั่วไป JavaScript ด้วยเหตุนี้ คุณจึงต้องใช้ Escape กับแบ็กสแลชในรูปแบบนี้

เมธอดนี้ใช้ไลบรารีการแสดงผลปกติ RE2 ของ Google ซึ่งจํากัดไวยากรณ์ที่สนับสนุน

ระบบจะจับคู่รูปแบบนิพจน์ทั่วไปที่มีให้กับบล็อกข้อความแต่ละบล็อกที่มีอยู่ในองค์ประกอบปัจจุบัน

var body = DocumentApp.getActiveDocument().getBody();

// Clear the text surrounding "Apps Script", with or without text.
body.replaceText("^.*Apps ?Script.*$", "Apps Script");

พารามิเตอร์

ชื่อTypeคำอธิบาย
searchPatternStringรูปแบบนิพจน์ทั่วไปที่จะค้นหา
replacementStringข้อความที่จะใช้แทนที่

ขากลับ

Element — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

  • 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);

พารามิเตอร์

ชื่อTypeคำอธิบาย
attributesObjectแอตทริบิวต์ขององค์ประกอบ

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setBorderColor(color)

ตั้งค่าสีเส้นขอบ

// Opens the Docs file by its ID. If you created your script from within a
// Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the first table.
const table = body.getTables()[0];

// Sets the border color of the table to green.
table.setBorderColor('#00FF00');

พารามิเตอร์

ชื่อTypeคำอธิบาย
colorStringสีเส้นขอบในรูปแบบ CSS (เช่น '#ffffff')

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setBorderWidth(width)

ตั้งค่าความกว้างของเส้นขอบเป็นจุด

พารามิเตอร์

ชื่อTypeคำอธิบาย
widthNumberความกว้างของเส้นขอบเป็นจุด

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setColumnWidth(columnIndex, width)

ตั้งค่าความกว้างของคอลัมน์ที่ระบุเป็นจุด

พารามิเตอร์

ชื่อTypeคำอธิบาย
columnIndexIntegerดัชนีคอลัมน์
widthNumberความกว้างของเส้นขอบเป็นจุด

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setLinkUrl(url)

ตั้งค่า URL ของลิงก์

พารามิเตอร์

ชื่อTypeคำอธิบาย
urlStringURL ของลิงก์

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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

setTextAlignment(textAlignment)

ตั้งค่าการจัดข้อความ ประเภทการจัดข้อความที่ใช้ได้คือ DocumentApp.TextAlignment.NORMAL, DocumentApp.TextAlignment.SUBSCRIPT และ DocumentApp.TextAlignment.SUPERSCRIPT

// Make the entire first paragraph be superscript.
var text = DocumentApp.getActiveDocument().getBody().getParagraphs()[0].editAsText();
text.setTextAlignment(DocumentApp.TextAlignment.SUPERSCRIPT);

พารามิเตอร์

ชื่อTypeคำอธิบาย
textAlignmentTextAlignmentประเภทของการปรับแนวข้อความที่จะใช้

ขากลับ

Table — องค์ประกอบปัจจุบัน

การให้สิทธิ์

สคริปต์ที่ใช้วิธีนี้ต้องใช้สิทธิ์กับขอบเขตต่อไปนี้อย่างน้อย 1 รายการ

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