Home » excel » How can I get the current Excel file in Office JavaScript API?

How can I get the current Excel file in Office JavaScript API?

Posted by: admin April 23, 2020 Leave a comment


I am developing a tab pane app in Excel which needs to read the current document. In Word, the Office JavaScript API has the method Office.context.document.getFileAsync(), but this is not available in Excel.

I can get the URL of the document with Office.context.document.getFileProperties(), and then I thought I could read the file with this.

I tried using the HTML5 FileReader() object, but this only works for files selected from the file input control. I tried manipulating a hidden file input control so it automatically uses the current document, but JavaScript understandably prevents you from doing this for security reasons. I could ask the user to browse to the document they are currently using but that would be a poor user experience.

So I tried using ActiveXObject(‘Scripting.FileSystemObject’) but ActiveX is not allowed in tab pane apps at all, whatever the current security setting are in IE.

What other options do I have?

How to&Answers:

According to the API road map, Office.context.document.getFileAsync() is not available in Excel at this moment.

I don’t think it’s feasible by using getFilePropertiesAsync(). It only returns the URL. Usually browser forbids developer touching any content in the file system. Therefore, it’s hard to access the local file system in JavaScript code.

Besides, the file may not be in local file system. For example, it could be hosted in Onedrive or SharePoint. getFilePropertiesAsync() should return its real URL in Onedrive/SharePoint, instead of local file system.

I guess Microsoft will support getFileAsync() in the future.