Class DocumentApp

DocumentApp

บริการเอกสารจะสร้างและเปิด Documents ที่แก้ไขได้

// Open a document by ID.
// TODO(developer): Replace the ID with your own.
let doc = DocumentApp.openById('DOCUMENT_ID');

// Create and open a document.
doc = DocumentApp.create('Document Name');

พร็อพเพอร์ตี้

พร็อพเพอร์ตี้ประเภทคำอธิบาย
AttributeAttributeAttributeการแจงนับ
ElementTypeElementTypeElementTypeการแจงนับ
FontFamilyFontFamilyFontFamilyการแจงนับ
GlyphTypeGlyphTypeGlyphTypeการแจงนับ
HorizontalAlignmentHorizontalAlignmentHorizontalAlignmentการแจงนับ
ParagraphHeadingParagraphHeadingParagraphHeadingการแจงนับ
PositionedLayoutPositionedLayoutPositionedLayoutการแจงนับ
TextAlignmentTextAlignmentTextAlignmentการแจงนับ
VerticalAlignmentVerticalAlignmentVerticalAlignmentการแจงนับ

เมธอด

วิธีการประเภทการแสดงผลรายละเอียดแบบย่อ
create(name)Documentสร้างและแสดงผลเอกสารใหม่
getActiveDocument()Documentแสดงผลเอกสารที่สคริปต์เชื่อมโยงกับคอนเทนเนอร์
getUi()Uiแสดงผลอินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของเอกสารที่อนุญาตให้สคริปต์เพิ่มฟีเจอร์ต่างๆ เช่น เมนู กล่องโต้ตอบ และแถบด้านข้าง
openById(id)Documentแสดงเอกสารที่มีรหัสตามที่ระบุ
openByUrl(url)Documentเปิดและแสดงผลเอกสารที่มี URL ที่ระบุ

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

create(name)

สร้างและแสดงผลเอกสารใหม่

// Create and open a new document.
const doc = DocumentApp.create('Document Name');

พารามิเตอร์

ชื่อประเภทคำอธิบาย
nameStringชื่อของเอกสารใหม่

รีเทิร์น

Document - อินสแตนซ์เอกสารใหม่

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

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

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

getActiveDocument()

แสดงผลเอกสารที่สคริปต์เชื่อมโยงกับคอนเทนเนอร์ หากต้องการโต้ตอบกับเอกสารที่สคริปต์ไม่ได้เชื่อมโยงกับคอนเทนเนอร์ ให้ใช้ openById(id) หรือ openByUrl(url) แทน

// Get the document to which this script is bound.
const doc = DocumentApp.getActiveDocument();

รีเทิร์น

Document - อินสแตนซ์ของเอกสาร

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

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

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

getUi()

แสดงผลอินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของเอกสารที่อนุญาตให้สคริปต์เพิ่มฟีเจอร์ต่างๆ เช่น เมนู กล่องโต้ตอบ และแถบด้านข้าง สคริปต์จะโต้ตอบกับ UI ได้เฉพาะอินสแตนซ์ปัจจุบันของเอกสารที่เปิดอยู่ และเฉพาะในกรณีที่สคริปต์เชื่อมโยงกับเอกสาร ดูข้อมูลเพิ่มเติมได้ที่คำแนะนำเกี่ยวกับเมนูและกล่องโต้ตอบและแถบด้านข้าง

// Add a custom menu to the active document, including a separator and a
// sub-menu.
function onOpen(e) {
  DocumentApp.getUi()
      .createMenu('My Menu')
      .addItem('My menu item', 'myFunction')
      .addSeparator()
      .addSubMenu(
          DocumentApp.getUi()
              .createMenu('My sub-menu')
              .addItem('One sub-menu item', 'mySecondFunction')
              .addItem('Another sub-menu item', 'myThirdFunction'),
          )
      .addToUi();
}

รีเทิร์น

Ui — อินสแตนซ์ของสภาพแวดล้อมอินเทอร์เฟซผู้ใช้ของเอกสารนี้


openById(id)

แสดงเอกสารที่มีรหัสตามที่ระบุ หากสคริปต์เชื่อมโยงกับเอกสารระดับคอนเทนเนอร์ ให้ใช้ getActiveDocument() แทน

// Open a document by ID.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('DOCUMENT_ID');

พารามิเตอร์

ชื่อประเภทคำอธิบาย
idStringรหัสของเอกสารที่จะเปิด

รีเทิร์น

Document — อินสแตนซ์ของเอกสาร

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

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

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

openByUrl(url)

เปิดและแสดงผลเอกสารที่มี URL ที่ระบุ หากสคริปต์เชื่อมโยงกับคอนเทนเนอร์ของเอกสาร ให้ใช้ getActiveDocument() แทน

// Open a document by URL.
const doc = DocumentApp.openByUrl(
    'https://docs.google.com/document/d/1234567890abcdefghijklmnopqrstuvwxyz_a1b2c3/edit',
);

พารามิเตอร์

ชื่อประเภทคำอธิบาย
urlStringURL ของเอกสารที่จะเปิด

รีเทิร์น

Document - อินสแตนซ์ของเอกสาร

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

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

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