Cannot load local office.js

2019-03-02 15:44发布

问题:

As this answer suggested, I want to modify a little bit office.js.

So I copied the content of https://appsforoffice.microsoft.com/lib/1/hosted/office.js to local, such that https://localhost:3000/static/office.js shows well the content.

Then, I make a very simple file test.html:

<html>
    <head>
        <!--<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>-->
        <script type="text/javascript" src="https://localhost:3000/static/office.js"></script>
    </head>
    <body>
        haha
    </body>
</html>

Loading https://localhost:3000/static/test.html with remote office.js in a browser shows Warning: Office.js is loaded outside of Office client o15apptofilemappingtable.js:11, and loading it in an Office add-in works well.

However, loading https://localhost:3000/static/test.html with local office.js in a browser shows Uncaught SyntaxError: Unexpected token < o15apptofilemappingtable.js:1, and loading it in an Office add-in gives an error Add-in Error Something went wrong and we couldn't start this add-in. Please try again later or contact your system administrator.

So does anyone know what's wrong here? Is it possible to make our own local version of office.js?

回答1:

See my answer in the linked question. Essentially, no, you should not be making your own local copy of "office.js". But if you did want a local copy -- which we already offer on NuGet and, more recently, on NPM (https://www.npmjs.com/package/@microsoft/office-js) -- you would need all the associated files in that directory as well, not just Office.js itself.

See https://unpkg.com/@microsoft/office-js@1.1.1/dist/ for a listing of the files and folders that make up Office.js. Not all (in fact, not most) are used in any given session, but you do want the whole set of files since different ones are loaded in different circumstances.