Class 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');
เอกสารประกอบโดยละเอียด
create(name)
สร้างและแสดงเอกสารใหม่
// Create and open a new document.
const doc = DocumentApp.create('Document Name');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
name | String | ชื่อของเอกสารใหม่ |
รีเทิร์น
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');
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
id | String | รหัสของเอกสารที่จะเปิด |
รีเทิร์น
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',
);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
url | String | URL ของเอกสารที่จะเปิด |
รีเทิร์น
Document
— อินสแตนซ์เอกสาร
การให้สิทธิ์
สคริปต์ที่ใช้วิธีการนี้ต้องได้รับอนุญาตด้วยขอบเขตต่อไปนี้อย่างน้อย 1 รายการ
-
https://www.googleapis.com/auth/documents
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe \u003ccode\u003eDocumentApp\u003c/code\u003e service in Google Apps Script allows you to programmatically create, open, and edit Google Documents.\u003c/p\u003e\n"],["\u003cp\u003eYou can open existing documents using their ID or URL, or create new documents with a specified name.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003eDocumentApp\u003c/code\u003e provides access to a document's user interface, enabling features like custom menus, dialogs, and sidebars.\u003c/p\u003e\n"],["\u003cp\u003eIt offers a variety of properties and methods to manipulate document content, formatting, and elements.\u003c/p\u003e\n"],["\u003cp\u003eUsing this service requires specific authorization scopes related to document access and modification.\u003c/p\u003e\n"]]],[],null,["# Class DocumentApp\n\nDocumentApp\n\nThe document service creates and opens [Documents](/apps-script/reference/document/document) that can be edited.\n\n```javascript\n// Open a document by ID.\n// TODO(developer): Replace the ID with your own.\nlet doc = DocumentApp.openById('DOCUMENT_ID');\n\n// Create and open a document.\ndoc = DocumentApp.create('Document Name');\n``` \n\n### Properties\n\n| Property | Type | Description |\n|-------------------------|-----------------------------------------------------------------------------|----------------------------------------------------------------------------------------------|\n| `Attribute` | [Attribute](/apps-script/reference/document/attribute) | The [Attribute](/apps-script/reference/document/attribute) enumeration. |\n| `Element``Type` | [ElementType](/apps-script/reference/document/element-type) | The [ElementType](/apps-script/reference/document/element-type) enumeration. |\n| `Font``Family` | [FontFamily](/apps-script/reference/document/font-family) | The [FontFamily](/apps-script/reference/document/font-family) enumeration. |\n| `Glyph``Type` | [GlyphType](/apps-script/reference/document/glyph-type) | The [GlyphType](/apps-script/reference/document/glyph-type) enumeration. |\n| `Horizontal``Alignment` | [HorizontalAlignment](/apps-script/reference/document/horizontal-alignment) | The [HorizontalAlignment](/apps-script/reference/document/horizontal-alignment) enumeration. |\n| `Paragraph``Heading` | [ParagraphHeading](/apps-script/reference/document/paragraph-heading) | The [ParagraphHeading](/apps-script/reference/document/paragraph-heading) enumeration. |\n| `Positioned``Layout` | [PositionedLayout](/apps-script/reference/document/positioned-layout) | The [PositionedLayout](/apps-script/reference/document/positioned-layout) enumeration. |\n| `Text``Alignment` | [TextAlignment](/apps-script/reference/document/text-alignment) | The [TextAlignment](/apps-script/reference/document/text-alignment) enumeration. |\n| `Vertical``Alignment` | [VerticalAlignment](/apps-script/reference/document/vertical-alignment) | The [VerticalAlignment](/apps-script/reference/document/vertical-alignment) enumeration. |\n\n### Methods\n\n| Method | Return type | Brief description |\n|---------------------------------------------|------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|\n| [create(name)](#create(String)) | [Document](/apps-script/reference/document/document) | Creates and returns a new document. |\n| [getActiveDocument()](#getActiveDocument()) | [Document](/apps-script/reference/document/document) | Returns the document to which the script is [container-bound](/apps-script/scripts_containers). |\n| [getUi()](#getUi()) | [Ui](../base/ui.html) | Returns an instance of the document's user-interface environment that allows the script to add features like menus, dialogs, and sidebars. |\n| [openById(id)](#openById(String)) | [Document](/apps-script/reference/document/document) | Returns the document with the specified ID. |\n| [openByUrl(url)](#openByUrl(String)) | [Document](/apps-script/reference/document/document) | Opens and returns the document with the specified URL. |\n\nDetailed documentation\n----------------------\n\n### `create(name)`\n\nCreates and returns a new document.\n\n```javascript\n// Create and open a new document.\nconst doc = DocumentApp.create('Document Name');\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|----------|--------------------------|\n| `name` | `String` | The new document's name. |\n\n#### Return\n\n\n[Document](/apps-script/reference/document/document) --- The new document instance.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents`\n\n*** ** * ** ***\n\n### `get``Active``Document()`\n\nReturns the document to which the script is [container-bound](/apps-script/scripts_containers). To interact with document to which\nthe script is not container-bound, use [openById(id)](#openById(String)) or [openByUrl(url)](#openByUrl(String))\ninstead.\n\n```javascript\n// Get the document to which this script is bound.\nconst doc = DocumentApp.getActiveDocument();\n```\n\n#### Return\n\n\n[Document](/apps-script/reference/document/document) --- the document instance\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents.currentonly`\n- `https://www.googleapis.com/auth/documents`\n\n*** ** * ** ***\n\n### `get``Ui()`\n\nReturns an instance of the document's user-interface environment that allows the script to add\nfeatures like menus, dialogs, and sidebars. A script can only interact with the UI for the\ncurrent instance of an open document, and only if the script is [bound](/apps-script/scripts_containers) to the document. For more information, see the\nguides to [menus](/apps-script/guides/menus) and [dialogs and sidebars](/apps-script/guides/dialogs).\n\n```javascript\n// Add a custom menu to the active document, including a separator and a\n// sub-menu.\nfunction onOpen(e) {\n DocumentApp.getUi()\n .createMenu('My Menu')\n .addItem('My menu item', 'myFunction')\n .addSeparator()\n .addSubMenu(\n DocumentApp.getUi()\n .createMenu('My sub-menu')\n .addItem('One sub-menu item', 'mySecondFunction')\n .addItem('Another sub-menu item', 'myThirdFunction'),\n )\n .addToUi();\n}\n```\n\n#### Return\n\n\n[Ui](../base/ui.html) --- an instance of this document's user-interface environment\n\n*** ** * ** ***\n\n### `open``By``Id(id)`\n\nReturns the document with the specified ID. If the script is container-bound to the document,\nuse [getActiveDocument()](#getActiveDocument()) instead.\n\n```javascript\n// Open a document by ID.\n// TODO(developer): Replace the ID with your own.\nconst doc = DocumentApp.openById('DOCUMENT_ID');\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|------|----------|---------------------------------|\n| `id` | `String` | The ID of the document to open. |\n\n#### Return\n\n\n[Document](/apps-script/reference/document/document) --- The document instance.\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents`\n\n*** ** * ** ***\n\n### `open``By``Url(url)`\n\nOpens and returns the document with the specified URL. If the script is container-bound to the\ndocument, use [getActiveDocument()](#getActiveDocument()) instead.\n\n```javascript\n// Open a document by URL.\nconst doc = DocumentApp.openByUrl(\n 'https://docs.google.com/document/d/1234567890abcdefghijklmnopqrstuvwxyz_a1b2c3/edit',\n);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|-------|----------|---------------------------------|\n| `url` | `String` | the URL of the document to open |\n\n#### Return\n\n\n[Document](/apps-script/reference/document/document) --- the document instance\n\n#### Authorization\n\nScripts that use this method require authorization with one or more of the following [scopes](/apps-script/concepts/scopes#setting_explicit_scopes):\n\n- `https://www.googleapis.com/auth/documents`"]]