0x800a138f - JavaScript runtime error: Unable to set property 'CustomFunctions' of undefined or null reference

0

I'm newly developing office Add-Ins. I was able to create an Excel add-in using visual studio 2019 with Office / SharePoint development workload (JavaScrip APIs). On the other hand, I'm trying to execute this example:https://github.com/lindalu-MSFT/Excel-Custom-Functions#prerequisites

The point is: Executing the solution, the excel runs but there is an error on this line of Home.js:

Excel.Script.CustomFunctions = {};

The error:

0x800a138f - JavaScript runtime error: Unable to set property 'CustomFunctions' of undefined or null reference

This is my HTML code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Expires" content="0" />
    <title>Excel Add-In with Commands Sample</title>
    <script src="Scripts/jquery-3.5.0.js" type="text/javascript"></script>
    <script src="https://appsforoffice.edog.officeapps.live.com/lib/beta/hosted/office.js" type="text/javascript"></script>
    <script src="Home.js" type="text/javascript"></script>
</head>
<body>
</body>
</html>

This is my XML code:

<?xml version="1.0" encoding="UTF-8"?>
<OfficeApp 
          xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" 
          xmlns:ov="http://schemas.microsoft.com/office/taskpaneappversionoverrides"
          xsi:type="TaskPaneApp">
  <Id>a8ec7572-9a78-4d27-9232-e72765183d11</Id>
  <Version>1.0.0.0</Version>
  <ProviderName>Contoso</ProviderName>
  <DefaultLocale>en-US</DefaultLocale>
  <DisplayName DefaultValue="Custom functions sample" />
  <Description DefaultValue="A variety of sample custom functions."/>
  <Hosts>
    <Host Name="Workbook" />
  </Hosts>
  <DefaultSettings>                                             
    <SourceLocation DefaultValue="~remoteAppUrl/Home.html" />          
  </DefaultSettings>
  <Permissions>ReadWriteDocument</Permissions>
  <VersionOverrides xmlns="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="VersionOverridesV1_0">
    <Hosts>
      <Host xsi:type="Workbook">
        <DesktopFormFactor>
          <ExtensionPoint xsi:type="CustomFunctions">    
            <Script>
              <SourceLocation resid="functionsjs" />     
            </Script>
            <Page>
              <SourceLocation resid="functionshtml"/>   
            </Page>
          </ExtensionPoint>
        </DesktopFormFactor>
      </Host>
    </Hosts>
    <Resources>
      <bt:Urls>
        <bt:Url id="functionsjs" DefaultValue="~remoteAppUrl/Home.js" />
        <bt:Url id="functionshtml" DefaultValue="~remoteAppUrl/Home.html" />
      </bt:Urls>      
    </Resources>
  </VersionOverrides>  
</OfficeApp>

and finally the js code:

Office.initialize = function (reason) {
    // Define the Contoso prefix.
    Excel.Script.CustomFunctions = {};
    Excel.Script.CustomFunctions["CONTOSO"] = {};

    // add42 is an example of a synchronous function.
    function add42(a, b) {
        return a + b + 42;
    }
    Excel.Script.CustomFunctions["CONTOSO"]["ADD42"] = {
        call: add42,
        description: "Finds the sum of two numbers and 42.",
        helpUrl: "https://www.contoso.com/help.html",
        result: {
            resultType: Excel.CustomFunctionValueType.number,
            resultDimensionality: Excel.CustomFunctionDimensionality.scalar,
        },
        parameters: [
            {
                name: "num 1",
                description: "The first number",
                valueType: Excel.CustomFunctionValueType.number,
                valueDimensionality: Excel.CustomFunctionDimensionality.scalar,
            },
            {
                name: "num 2",
                description: "The second number",
                valueType: Excel.CustomFunctionValueType.number,
                valueDimensionality: Excel.CustomFunctionDimensionality.scalar,
            }
        ],
        options: { batch: false, stream: false }
    };

    Excel.run(function (context) {
        context.workbook.customFunctions.addAll();
        return context.sync();
    }).catch(function (error) { });
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
};

Can you guys help me with a direction to solve this problem?

I checked these topics:

Stack question

excel package

create custom functions

javascript
api
office-js
excel-addins
custom-functions-excel
asked on Stack Overflow Mar 5, 2021 by DeSanterra • edited Mar 5, 2021 by Rick Kirkham

1 Answer

0

I think that is some one's old fork for custom functions, so you're likely trying to access a version of this that was in preview and doesn't work any longer as our interfaces have changed.

Please refer to the official guidence on custom functions here: https://aka.ms/customfunctions

answered on Stack Overflow Mar 5, 2021 by Keyur Patel - MSFT

User contributions licensed under CC BY-SA 3.0