During the initialization of a WebOffice document application, the aliyun.config method returns a JS-SDK instance that provides some useful properties and methods.
JS-SDK instance properties
Instance property | Type | Description |
version | string | The version of JS-SDK. |
mainVersion | string | The version of WebOffice. |
url | string | The online preview address of the document. |
iframe | HTMLElement | The DOM object of iframe. |
tabs | object | The tab objects. |
tokenData | object | The token data. |
Application | object | The advanced API objects. |
JS-SDK instance methods
Instance method | Description |
save | Saves a document. |
ready | Loads advanced API operations. |
destroy | Destroys the JS-SDK instance. |
ApiEvent | The event to be handled. |
on | Enables event listening. This method is applicable to JS-SDK V1.1.14 and earlier. |
off | Disables event listening. This method is applicable to JS-SDK V1.1.14 and earlier. |
setToken | Sets token-based authentication. |
setCommandBars | Dynamically updates the status of a component. |
setCooperUserColor | Dynamically sets the cursor color for a collaborator. |
executeCommandBar | Executes a component command. |
Save a document
Use the save operation to save a document when necessary.
Sample code
const instance = aliyun.config({
url: 'online document preview address',
});
const result = await instance.save();
Sample response
{
result: 'nochange', // The save status.
size: 15302, // The size of the document. Unit: bytes.
version: 16, // The document version.
}
Save status description
Save status | Description |
ok | The version is saved. You can view the saved version in historical versions of the document. |
nochange | The document has no update. You do not need to save the version. |
SavedEmptyFile | The document is empty and cannot be saved. This status is returned when the document is empty after the kernel is saved. |
SpaceFull | The storage space is used up. |
QueneFull | Frequent saving operations are not allowed when the document is being saved. This status is returned when the saving operation queue is full on the server. |
fail | Failed to save the document. |
Destroy an instance
Use the instance.destroy() method to destroy an instance.
JS-SDK creates an iframe to communicate with WebOffice. Use the instance.destroy() method to destroy the DOM node of iframe. As a result, the JS-SDK instance is destroyed and the WebOffice page stops displaying content.
Operations on tabs
The following sample code describes how to use the operations that are related to tabs.
getTabs
Description: obtains all tabs in the header.
Sample code:
const tabs = await instance.tabs.getTabs();Sample response:
[ {tabKey: 'StartTab', text: 'Start'}, {tabKey: 'InsertTab', text: 'Insert'}, {tabKey: 'ReviewTab', text: 'Review', commandBarId: 'TabReviewWord' }, {tabKey: 'PageTab', text: 'Page'}, ]
switchTab
Description: controls the tab switch in the header.
Sample code:
await instance.tabs.switchTab({ tabKey: 'InsertTab' }); // Switch to the Insert tab.Parameter: tabKey indicates the current tabKey value.
tabSwitch
Description: the callback of a tab switch event.
Sample code:
// Listen to tab switch events. instance.on('tabSwitch', (data) => { console.log('tabSwitch:', data); }); // Automatically call the method to switch to the Insert tab. await instance.tabs.switchTab({ tabKey: 'InsertTab' });Sample response:
{ tabKey: 'InsertTab' }