Skip to main content

Calling SDK api reports error: Cannot read properties of undefined (reading 'rootView')

Answer:

Due to business scenario requirements, environment variable LS_WEBRESOURCE_WRITERAUTOFOCUS is set to 'false', meaning document should not auto-focus after opening. This causes document to have no selection initially after opening, unless user manually clicks on the document. So when SDK api calls need to use selection, cannot get selection will report error, api cannot execute.

Solution:

const app = await ZOfficeSDK.mount(url, selector ,true);
await app.ready();
// After document opens, set cursor to document beginning
const para = await app.ActiveDocument.Paragraphs.item(1);
const range = await para.getRange(0, 0);
await app.ActiveDocument.setSelection(range);