# - 如何调用ONLYOFFICE

# 使用方式

# 前提条件

确保相应的js文件可以被正常访问

  https://documentserver/web-apps/apps/api/documents/api.js
1

# 快速入门

  1. 新建html文档,引入api.js

    <script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
    
    1
  2. 添加div元素

    <div id="placeholder"></div>
    
    1
  3. 可变参数的传递

    var docEditor = new DocsAPI.DocEditor("placeholder", config);
    
    1
    config = {
        "document": {
            "fileType": "docx",
            "key": "Khirz6zTPdfd7",
            "title": "Example Document Title.docx",
            "url": "https://example.com/url-to-example-document.docx"
        },
        "documentType": "word",
        "editorConfig": {
            "callbackUrl": "https://example.com/url-to-callback.ashx"
        }
    };
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
  4. 完整示例

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>ONLYOFFICE使用方法</title>
    		<!-- 引入外部JavaScript文件 -->
    		<script type="text/javascript" src="https://documentserver/web-apps/apps/api/documents/api.js"></script>
    	</head>
    	<body>
            <div id="placeholder"></div>
            <script language="javascript" type="text/javascript">
                var docEditor = new DocsAPI.DocEditor("placeholder",{
                    "document": {
                        "fileType": "docx",
                        "key": "Khirz6zTPdfd7",
                        "title": "Example Document Title.docx",
                        "url": "https://example.com/url-to-example-document.docx"
                    },
                    "documentType": "word",
                    "editorConfig": {
                        "callbackUrl": "https://example.com/url-to-callback.ashx"
                    }
                });
            </script>
    	</body>
    </html>
    
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27

# 参数说明

ONLYOFFICE 文档服务API文档,大体上可以分为以下几个部分:

config

  • document:文档相关
    • info:文档信息(标题地址类型
    • permissions:文档权限(只读编辑下载
  • editorConfig:编辑器相关
    • customization:自定义
    • embedded:嵌入式
    • plugins:插件
  • events:事件

配置参数进阶示例:

config = {
    "document": {
        "fileType": "docx",		//文档类型
        "info": {
            "favorite": true,
            "folder": "Example Files",
            "owner": "John Smith",	//文件创建者名称
            "sharingSettings": [	//文件对应用户的操作权限配置
                {
                    "permissions": "Full Access",	//完全操作权限-Full Access,只读权限-Read Only 拒绝访问-Deny Access
                    "user": "John Smith"	//有次权限的用户
                },
                {
                    "isLink": true,
                    "permissions": "Read Only",
                    "user": "External link"
                },
                ...
            ],
            "uploaded": "2010-07-07 3:46 PM"	//文件创建时间
        },
        "key": "Khirz6zTPdfd7",		//定义用于服务识别文档的唯一文档标识符。每次编辑和保存文档时,都必须重新生成密钥。长度限制为128个符号。
        "permissions": {
            "chat": true,	//定义“聊天”菜单按钮是显示还是隐藏;请注意,如果您隐藏“聊天”按钮,则相应的聊天功能也将被禁用。默认值为true。
            "comment": true,	//定义是否可以注释文档。如果注释权限设置为“ true”,则文档侧栏将包含“注释”菜单选项;只有将mode参数设置为edit时才生效,默认值与edit参数的值一致。
            "commentGroups": [
                "edit": ["Group2", ""],
                "remove": [""],
                "view": ""
            ],
            "copy": true,	//是否允许您将内容复制到剪贴板。默认值为true。
            "deleteCommentAuthorOnly": false,	//定义用户是否能删除他人的评论。默认值为false。
            "download": true,	/定义是否可以下载文档或仅在线查看或编辑文档。如果下载权限设置为“false”下载为菜单选项将没有。默认值为true"edit": true,	//文件是否可以编辑,false时文件不可编辑
            "editCommentAuthorOnly": false,		//定义用户是否能编辑他人的评论。默认值为false。
            "fillForms": true,	//定义是否能在文档中填充表单
            "modifyContentControl": true,	//定义是否可以更改内容控件设置。仅当mode参数设置为edit时,内容控件修改才可用于文档编辑器。默认值为true。
            "modifyFilter": true,	//定义过滤器是否可以全局应用(true)影响所有其他用户,或局部应用(false),即仅适用于当前用户。如果将mode参数设置为edit,则过滤器修改仅对电子表格编辑器可用。默认值为true。
            "print": true,	//定义文档是否能打印
            "protect": true,
            "review": true,	//是否显示审阅文档菜单
            "reviewGroups": ["Group1", "Group2", ""],
            "userInfoGroups": ["Group1", ""]
        },
        "title": "Example Document Title.docx",		//为查看或编辑的文档定义所需的文件名,该文件名也将在下载文档时用作文件名。长度限制为128个符号。
        "url": "https://example.com/url-to-example-document.docx"	//定义存储原始查看或编辑的文档的绝对URL
    },
    "documentType": "word",
    "editorConfig": {	//编辑配置
        "actionLink": ACTION_DATA,
        "callbackUrl": "https://example.com/url-to-callback.ashx",
        "coEditing": {
            "mode": "fast",
            "change": true
        },
        "createUrl": "https://example.com/url-to-create-document/",	//指定创建文件的页面,添加该配置后文档服务器插件才会显示新建文件按钮
        "customization": {
            "anonymous": {
                "request": true,
                "label": "Guest"
            },
            "autosave": true,	//定义是启用还是禁用“自动保存”菜单选项。请注意,如果您在菜单中更改此选项,它将被保存到浏览器的localStorage中。默认值为true。
            "comments": true,
            "compactHeader": false,
            "compactToolbar": false,
            "compatibleFeatures": false,
            "customer": {
                "address": "My City, 123a-45",
                "info": "Some additional information",
                "logo": "https://example.com/logo-big.png",
                "logoDark": "https://example.com/dark-logo-big.png",
                "mail": "john@example.com",
                "name": "John Smith and Co.",
                "phone": "123456789",
                "www": "example.com"
            },
            "features": {
                "spellcheck": {
                    "mode": true,
                    "change": true
                }
            },
        "feedback": {
                "url": "https://example.com",
                "visible": true
            },
        "forcesave": false,
        "goback": {
                "blank": true,
                "requestClose":
                false, "text": "Open file location",
                "url": "https://example.com"
            },
            "help": true,
            "hideNotes": false,
            "hideRightMenu": false,
            "hideRulers": false,
            "logo": {
                "image": "https://example.com/logo.png",	//图像文件的路径,用于在普通工作模式下显示(即,在所有编辑器的查看和编辑模式下)。图片必须具有以下尺寸:172x40,
                "imageDark": "https://example.com/dark-logo.png",	
                "url": "https://example.com"	//某人单击徽标图像时将使用的绝对URL(可用于转到您的网站等)。保留为空字符串或null以使徽标不可单击
            },
            "macros": true,
            "macrosMode": "warn",
            "mentionShare": true,
            "plugins": true,
            "review": {
                "hideReviewDisplay": false,
        "showReviewChanges": false,
                "reviewDisplay": "original",
                "trackChanges": true,
                "hoverMode": false
                },
        "toolbarHideFileName": false,
        "toolbarNoTabs": false,
        "uiTheme": "theme-dark",
        "unit": "cm",
        "zoom": 100
        },
        "embedded": {
            "embedUrl": "https://example.com/embedded?doc=exampledocument1.docx",
            "fullscreenUrl": "https://example.com/embedded?doc=exampledocument1.docx#fullscreen",
            "saveUrl": "https://example.com/download?doc=exampledocument1.docx",
            "shareUrl": "https://example.com/view?doc=exampledocument1.docx",
            "toolbarDocked": "top"
        },
        "lang": "en",
        "location": "us",
        "mode": "edit",	//文档操作模式 view 视图模式不可编辑  edit 编辑模式可编辑文档
        "plugins": {
             "autostart": [
                 "asc.{0616AE85-5DBE-4B6B-A0A9-455C4F1503AD}",
                 "asc.{FFE1F462-1EA2-4391-990D-4CC84940B754}",
        ...
        ],
             "pluginsData": [
                 "https://example.com/plugin1/config.json",
                 "https://example.com/plugin2/config.json",
                 
             ...]
        },
        "recent": [
            {
                "folder": "Example Files",
                "title": "exampledocument1.docx",
                "url": "https://example.com/exampledocument1.docx"
            },
            {
                "folder": "Example Files",
                "title": "exampledocument2.docx",
                "url": "https://example.com/exampledocument2.docx"
            },
            ...
        ],
        "region": "en-US",
        "templates": [
            {
                "image": "https://example.com/exampletemplate1.png",
                "title": "exampletemplate1.docx",
                "url": "https://example.com/url-to-create-template1"
            },
            {
                "image": "https://example.com/exampletemplate2.png",
                "title": "exampletemplate2.docx",
                "url": "https://example.com/url-to-create-template2"
            },
            ...
        ],
        "user": {	//用户信息
            "group": "Group1",		//组
            "id": "78e1e841",		//用户id
            "name": "John Smith"	//用户全名称
        }
    },
    "events": {
        "onAppReady": onAppReady,							//应用程序被加载到浏览器中。
        "onCollaborativeChanges": onCollaborativeChanges,	//文档由其他用户以 严格 共同编辑模式共同编辑。
        "onDocumentReady": onDocumentReady,					//文档被加载到文档编辑器中。
        "onDocumentStateChange": onDocumentStateChange,		//文档被修改。
        "onDownloadAs": onDownloadAs,						//调用 downloadAs 方法时编辑文件的绝对 URL 。
        "onError": onError,									//发生错误或其他特定事件时调用的函数。错误消息在data参数中发送。
        "onInfo": onInfo,									//应用程序打开了文件。
        "onMetaChange": onMetaChange,						//通过meta命令更改文档的 元 信息。
        "onOutdatedVersion": onOutdatedVersion,				//使用旧的 document.key 值打开文档进行编辑,该值用于编辑以前的文档版本并成功保存。
        "onPluginsReady": onPluginsReady,					//所有插件都已加载并可以使用。
        "onRequestClose": onRequestClose,					//必须结束与编辑器的工作并且必须关闭编辑器。
        "onRequestCompareFile": onRequestCompareFile,		//用户试图通过单击 存储中的文档 按钮来选择要比较的文档。
        "onRequestCreateNew": onRequestCreateNew,			//用户试图通过单击 新建 按钮来创建文档。
        "onRequestEditRights": onRequestEditRights,	        //用户试图通过单击 编辑文档 按钮将文档从查看模式切换到编辑模式。
        "onRequestHistory": onRequestHistory,				//用户试图通过单击 版本历史 按钮来显示文档版本历史。
        "onRequestHistoryClose": onRequestHistoryClose,		//用户试图通过单击 关闭历史记录 按钮从查看文档版本历史记录返回到文档。
        "onRequestHistoryData": onRequestHistoryData,		//用户正在尝试单击文档版本历史记录中的特定文档版本。
        "onRequestInsertImage": onRequestInsertImage,		//用户尝试通过单击 存储中的图像 按钮来插入图像。
        "onRequestMailMergeRecipients": onRequestMailMergeRecipients,	//用户试图通过单击 邮件合并 按钮来选择收件人数据。
        "onRequestRename": onRequestRename,		//用户试图通过单击 重命名... 按钮来重命名文件。
        "onRequestRestore": onRequestRestore,	//用户正在尝试通过单击版本历史记录中的 恢复 按钮来恢复文件版本。
        "onRequestSaveAs": onRequestSaveAs,		//用户尝试通过单击“另存为...”按钮保存文件时调用的函数。文档的标题和要下载的文档的绝对URL在data参数中发送。如果未声明该方法,则不会显示“另存为...”按钮。
        "onRequestSendNotify": onRequestSendNotify,		//用户在评论中被提及。
        "onRequestSharingSettings": onRequestSaveAs,	//用户单击“更改访问权限”按钮来管理文档访问权限时调用的事件。必须调用setSharingSettings方法来更新有关允许与其他用户共享文档的设置的信息。如果未声明该方法,则不会显示“更改访问权限”按钮。
        "onRequestUsers": onRequestUsers,	//评论者可以选择要在评论中提及的其他用户时调用的函数。要设置用户列表,必须调用setUsers方法。
        "onWarning": onWarning	//出现警告。
    },
    "height": "100%",	//打开窗口高度
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M", //令牌
    "type": "desktop",	//平台类型
    "width": "100%"		//打开窗口宽度                      
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
上次更新: 2023/12/27