Search code examples
office-jsscriptlab

ScriptLab message error randomly


I randomly get this error in the console of then 'Running' pane of my Excel 2016 when using ScriptLab:

[
   "Script error.",
   "https://appsforoffice.microsoft.com/lib/1/hosted/office.js", 
   0, 
   0, 
   undefined
]

This console error message appears not immediately after 'deploying' my project, but a few seconds later.

My project runs normally with no console errors and works as expected, tough.

Is this a bug?


EDIT: I was able to create a very small YAML snippet that shows the problem. Try running this example. I figured out that if you uncomment the line Office.initialize, the error doesn't happen anymore.

name: Test
description: Reproduce bug
author: FelipeCostaGualberto
host: EXCEL
api_set: {}
script:
    content: |-
        //Office.initialize = function (reason) {
            $(function () {
                console.log("Test");
            });
        //};
    language: typescript
template:
    content: "<!DOCTYPE html>\n<html>\n<head>\n\t<script src=\"https://appsforoffice.microsoft.com/lib/1/hosted/office.js\" type=\"text/javascript\"></script>\n</head>\n<body>\n</body>\n</html>"
    language: html
style:
    content: ''
    language: css
libraries: |
    [email protected]/client/core.min.js
    @types/core-js

    [email protected]
    @types/jquery

Solution

  • When using Script Lab, you don't need a full HTML or an Office.initialized. That's the beauty of it! You can focus on just the relevant code, which essentially is just console.log("test") in your case, and an empty HTML. (For a more real use-case, you might have a button defined in the HTML, and a click-handler with some sort of API manipulation in the JS).

    Note that you need to have the Office.js reference as part of the libraries tab.

    Take a look at some of the other Script Lab samples (or even the basic "new snippet" sample) to see what it could look like.