Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 引用错误:未定义Excel_Javascript_Angularjs_Ms Office_Office Js_Office App - Fatal编程技术网

Javascript 引用错误:未定义Excel

Javascript 引用错误:未定义Excel,javascript,angularjs,ms-office,office-js,office-app,Javascript,Angularjs,Ms Office,Office Js,Office App,我想用angularjs和angularjs ui路由器制作一个Office插件: <bt:Urls> <bt:Url id="Contoso.Taskpane3.Url" DefaultValue="https://localhost:3000/addin/test" /> </bt:Urls> 以及控制器: app.controller('TestCtrl', [function () { function loa

我想用angularjs和angularjs ui路由器制作一个Office插件:

<bt:Urls>
    <bt:Url id="Contoso.Taskpane3.Url" DefaultValue="https://localhost:3000/addin/test" />            
</bt:Urls>
以及控制器:

app.controller('TestCtrl', [function () {
    function loadDataAndCreateChart() {
        Excel.run(function (ctx) {
            var sheet = ctx.workbook.worksheets.getActiveWorksheet();
            sheet.getRange("A1").values = "Quarterly Sales Report";
            return ctx.sync()
        })
    }
    loadDataAndCreateChart()
}])
我希望加载加载项会将
季度销售报告写入
A1
。但是,我得到了以下错误:

ReferenceError: Excel is not defined at loadDataAndCreateChart

有人知道怎么了吗?初始化Office并像那样使用angular.bootstrap可以吗?

您需要确保调用

Excel.run(function (ctx) {
        var sheet = ctx.workbook.worksheets.getActiveWorksheet();
        sheet.getRange("A1").values = "Quarterly Sales Report";
        return ctx.sync()
    });
在Office初始化完成后执行

我创建了一个从上面运行代码的小应用程序。实际上,要运行它,只有三个文件

  • manifest.xml
  • index.html
  • app.js
  • 请确保在以下文件中用实际的本地路径替换单词PathToYourLocalFile

    manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <OfficeApp xsi:type="TaskPaneApp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/office/appforoffice/1.0">
         <Id>6ed5a5d8-57e4-4739-9a38-cff59f23e266</Id>
         <Version>1.0.0.0</Version>
         <ProviderName>Test Provider</ProviderName>
         <DefaultLocale>en-US</DefaultLocale>
         <DisplayName DefaultValue="Test" />
         <Description DefaultValue="Angular Test" />
         <Capabilities>
            <Capability Name="Workbook" />
         </Capabilities>
         <DefaultSettings>
             <SourceLocation DefaultValue="PathToYourLocalFile/index.html" />
         </DefaultSettings>
         <Permissions>ReadWriteDocument</Permissions>
    </OfficeApp>
    
    <!DOCTYPE html>
    <html lang="en" ng-app="AngularTest"  ng-controller="MainController">
    <head>
        <meta charset="utf-8">
        <title>Angular Test</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
     </head>
     <body>
         <div ng-view></div>  
         <!-- Load Js Libaries -->
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
         </script>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">
         </script>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-resource.min.js"></script>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js"></script>
    
         <script src="PathToYourLocalFile/app.js"></script>
      </body>
      </html>
    
    您可以在app.js文件中看到,方法loadDataAndCreateChart在Office初始化完成后执行

    在这里,您可以找到如何将清单添加到excel的文档:

    您需要确保

    Excel.run(function (ctx) {
            var sheet = ctx.workbook.worksheets.getActiveWorksheet();
            sheet.getRange("A1").values = "Quarterly Sales Report";
            return ctx.sync()
        });
    
    在Office初始化完成后执行

    我创建了一个从上面运行代码的小应用程序。实际上,要运行它,只有三个文件

  • manifest.xml
  • index.html
  • app.js
  • 请确保在以下文件中用实际的本地路径替换单词PathToYourLocalFile

    manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <OfficeApp xsi:type="TaskPaneApp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/office/appforoffice/1.0">
         <Id>6ed5a5d8-57e4-4739-9a38-cff59f23e266</Id>
         <Version>1.0.0.0</Version>
         <ProviderName>Test Provider</ProviderName>
         <DefaultLocale>en-US</DefaultLocale>
         <DisplayName DefaultValue="Test" />
         <Description DefaultValue="Angular Test" />
         <Capabilities>
            <Capability Name="Workbook" />
         </Capabilities>
         <DefaultSettings>
             <SourceLocation DefaultValue="PathToYourLocalFile/index.html" />
         </DefaultSettings>
         <Permissions>ReadWriteDocument</Permissions>
    </OfficeApp>
    
    <!DOCTYPE html>
    <html lang="en" ng-app="AngularTest"  ng-controller="MainController">
    <head>
        <meta charset="utf-8">
        <title>Angular Test</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>
     </head>
     <body>
         <div ng-view></div>  
         <!-- Load Js Libaries -->
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
         </script>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js">
         </script>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-resource.min.js"></script>
         <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular-route.min.js"></script>
    
         <script src="PathToYourLocalFile/app.js"></script>
      </body>
      </html>
    
    您可以在app.js文件中看到,方法loadDataAndCreateChart在Office初始化完成后执行

    在这里,您可以找到如何将清单添加到excel的文档:

    我尚未测试您的解决方案。。。我认为将
    return Promise.resolve(null)
    放在
    angular.bootstrap(文档,['app'])
    之后,并将
    initOffice
    注入
    TestCtrl
    也会解决问题…我还没有测试您的解决方案。。。我认为将
    return Promise.resolve(null)
    放在
    angular.bootstrap(文档,['app'])
    之后,并将
    initOffice
    注入
    TestCtrl
    也会解决问题。。。