文档服务会创建并打开可编辑的 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');
属性
| 属性 | 类型 | 说明 | 
|---|---|---|
| Attribute | Attribute | Attribute枚举。 | 
| Element | Element | Element枚举。 | 
| Font |  | 枚举。 | 
| Glyph | Glyph | Glyph枚举。 | 
| Horizontal | Horizontal | Horizontal枚举。 | 
| Paragraph | Paragraph | Paragraph枚举。 | 
| Positioned | Positioned | Positioned枚举。 | 
| Text | Text | Text枚举。 | 
| Vertical | Vertical | Vertical枚举。 | 
方法
| 方法 | 返回类型 | 简介 | 
|---|---|---|
| create(name) | Document | 创建并返回新的文档。 | 
| get | Document | 返回脚本绑定到容器的文档。 | 
| get | Ui | 返回文档界面环境的实例,以便脚本添加菜单、对话框和边栏等功能。 | 
| open | Document | 返回具有指定 ID 的文档。 | 
| open | Document | 打开并返回指定网址的文档。 | 
详细文档
create(name)
get
返回脚本绑定到容器的文档。如需与脚本未绑定的文档互动,请改用 open 或 open。
// Get the document to which this script is bound. const doc = DocumentApp.getActiveDocument();
返回
Document - 文档实例
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents.currentonly
- 
https://www.googleapis.com/auth/documents
get
返回文档界面环境的实例,以便脚本添加菜单、对话框和边栏等功能。脚本只能与打开文档的当前实例的界面互动,并且只有在脚本绑定到文档的情况下才能执行此操作。如需了解详情,请参阅菜单和对话框和边栏指南。
// 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 - 本文档界面环境的实例
open
返回具有指定 ID 的文档。如果脚本与文档绑定到容器,请改用 get。
// Open a document by ID. // TODO(developer): Replace the ID with your own. const doc = DocumentApp.openById('DOCUMENT_ID');
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| id | String | 要打开的文档的 ID。 | 
返回
Document - 文档实例。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents
open
打开并返回指定网址的文档。如果脚本与文档绑定到容器,请改用 get。
// Open a document by URL. const doc = DocumentApp.openByUrl( 'https://docs.google.com/document/d/1234567890abcdefghijklmnopqrstuvwxyz_a1b2c3/edit', );
参数
| 名称 | 类型 | 说明 | 
|---|---|---|
| url | String | 要打开的文档的网址 | 
返回
Document - 文档实例
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
- 
https://www.googleapis.com/auth/documents