【问题标题】:Adding custom functions to existing excel add-in (generated by visual studio)将自定义函数添加到现有的 excel 插件(由 Visual Studio 生成)
【发布时间】:2020-06-12 17:45:54
【问题描述】:

所以我正在为 excel 开发一个加载项。我从 Visual Studio 生成的模板开始(不是 yeomon,因为使用 VS 生成使调试设置更容易)。到目前为止,我已经设置了一些任务窗格的东西来对远程服务器进行身份验证,但现在我在尝试运行自定义函数时遇到了问题。文档侧重于 yeomon 生成的项目,我已尽力翻译,但似乎遗漏了一些内容。

所以到目前为止我做了什么:

为了概念验证,我坚持尝试让 OfficeDev 存储库中提供的示例正常工作 (https://github.com/OfficeDev/Excel-Custom-Functions)。我已将示例 (https://github.com/OfficeDev/Excel-Custom-Functions/blob/master/src/functions/functions.ts) 中的 functions.ts 添加到项目中的 MyAddInWeb/Custom 目录中。我手动复制了通过构建示例生成的Functions.jsonFunctions.html 文件,并将清单中的JSON-URLJS-URLHTML-URL 指向各自的文件路径,并添加了一个@ 987654329@ 标记和指向script/page/metadata 标记到相同的文件。我的清单的相关部分如下所示:

<?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">
...    
  <!--Begin TaskPane Mode integration. This section is used if there are no VersionOverrides or if the Office client version does not support add-in commands. -->
  <Hosts>
    <Host Name="Workbook" />
  </Hosts>
  <DefaultSettings>
    <SourceLocation DefaultValue="~remoteAppUrl/Home.html" />
  </DefaultSettings>
  <!-- End TaskPane Mode integration.  -->

  <Permissions>ReadWriteDocument</Permissions>

  <!-- Begin Add-in Commands Mode integration. -->
  <VersionOverrides xmlns="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="VersionOverridesV1_0">

    <!-- The Hosts node is required. -->
    <Hosts>
      <!-- Each host can have a different set of commands. -->
      <!-- Excel host is Workbook, Word host is Document, and PowerPoint host is Presentation. -->
      <!-- Make sure the hosts you override match the hosts declared in the top section of the manifest. -->
      <Host xsi:type="Workbook">
        <!-- Form factor. Currently only DesktopFormFactor is supported. -->
        <AllFormFactors>
          <ExtensionPoint xsi:type="CustomFunctions">
            <Script>
              <SourceLocation resid="Functions.Script.Url"/>
            </Script>
            <Page>
              <SourceLocation resid="Functions.Page.Url"/>
            </Page>
            <Metadata>
              <SourceLocation resid="Functions.Metadata.Url"/>
            </Metadata>
            <Namespace resid="Functions.Namespace"/>
          </ExtensionPoint>
        </AllFormFactors>
        <DesktopFormFactor>
...
        </DesktopFormFactor>
      </Host>
    </Hosts>

    <!-- You can use resources across hosts and form factors. -->
    <Resources>
      <bt:Images>
        <bt:Image id="Contoso.tpicon_16x16" DefaultValue="~remoteAppUrl/Images/Button16x16.png" />
        <bt:Image id="Contoso.tpicon_32x32" DefaultValue="~remoteAppUrl/Images/Button32x32.png" />
        <bt:Image id="Contoso.tpicon_80x80" DefaultValue="~remoteAppUrl/Images/Button80x80.png" />
      </bt:Images>
      <bt:Urls>

        <bt:Url id="JSON-URL" DefaultValue="~remoteAppUrl/Custom/Functions.json"/>
        <bt:Url id="JS-URL" DefaultValue="~remoteAppUrl/Custom/Functions.js"/>
        <bt:Url id="HTML-URL" DefaultValue="~remoteAppUrl/Custom/Functions.html"/>
        <bt:Url id="Functions.Script.Url" DefaultValue="~remoteAppUrl/Custom/Functions.js" />
        <bt:Url id="Functions.Metadata.Url" DefaultValue="~remoteAppUrl/Custom/Functions.json" />
        <bt:Url id="Functions.Page.Url" DefaultValue="~remoteAppUrl/Custom/Functions.html" />
        <bt:Url id="Contoso.DesktopFunctionFile.Url" DefaultValue="~remoteAppUrl/Functions/FunctionFile.html" />
        <bt:Url id="Contoso.Taskpane.Url" DefaultValue="~remoteAppUrl/Home.html" />
        <bt:Url id="Contoso.Callback.Url" DefaultValue="~remoteAppUrl/callback.html" />
        <bt:Url id="Contoso.GetStarted.LearnMoreUrl" DefaultValue="https://go.microsoft.com/fwlink/?LinkId=276812" />
      </bt:Urls>
      <!-- ShortStrings max characters==125. -->
      <bt:ShortStrings>
        <bt:String id="Functions.Namespace" DefaultValue="CONTOSO" />
        <bt:String id="Contoso.TaskpaneButton.Label" DefaultValue="Show Taskpane" />
        <bt:String id="Contoso.Group1Label" DefaultValue="Commands Group" />
        <bt:String id="Contoso.GetStarted.Title" DefaultValue="Get started with your sample add-in!" />
      </bt:ShortStrings>
      <!-- LongStrings max characters==250. -->
      <bt:LongStrings>
        <bt:String id="Contoso.TaskpaneButton.Tooltip" DefaultValue="Click to Show a Taskpane" />
        <bt:String id="Contoso.GetStarted.Description" DefaultValue="Your sample add-in loaded succesfully. Go to the HOME tab and click the 'Show Taskpane' button to get started." />
      </bt:LongStrings>
    </Resources>
  </VersionOverrides>
  <!-- End Add-in Commands Mode integration. -->

</OfficeApp>

所以这是我遇到的第一个障碍。在 Functions.ts 中,我收到一个打字稿错误,抱怨 CustomFunctions 命名空间未定义。不知道样本来自哪里。如果我删除了类型声明(我知道这是个坏主意),加载项构建和显示的自定义函数都已注册,但尝试运行它们中的任何一个都会导致 excel 挂起“我们正在启动加载项运行时,请稍等...” 片刻,然后我在任务窗格中收到错误消息,提示无法启动加载项。不幸的是,据我所知,我没有收到任何可用的错误消息,所以我不确定问题出在哪里。如果我不得不猜测,清单中的某些内容没有正确连接,但我不确定。任何帮助将非常感激。到目前为止,文档似乎相当缺乏。

【问题讨论】:

    标签: excel typescript office-addins


    【解决方案1】:

    如果您是针对 Visual Studio 构建的,我建议您将清单配置为使用共享运行时。这实际上会将您的自定义函数配置为在与您的任务窗格相同的 Web 容器中运行,这将允许您调试您的函数。

    您可以按照以下步骤操作: https://docs.microsoft.com/en-us/office/dev/add-ins/excel/configure-your-add-in-to-use-a-shared-runtime

    #1:添加运行时元素

    <VersionOverrides xmlns="http://schemas.microsoft.com/office/taskpaneappversionoverrides" xsi:type="VersionOverridesV1_0">
      <Hosts>
        <Host xsi:type="Workbook">
        <Runtimes>
          <Runtime resid="ContosoAddin.Url" lifetime="long" />
        </Runtimes>
        <AllFormFactors>
    

    resid 指向您的任务窗格的位置。 #2 确保任务窗格 html 也引用了您的自定义函数 javascript 文件。

    注意:共享运行时功能刚刚达到普遍可用性,并且需要在 2019 年可能还没有的清单架构更改,因此您可能会在 Visual Studio 中遇到构建错误,但您至少可以使用 VS 调试器。

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-12
      • 2021-03-25
      • 2011-03-27
      • 1970-01-01
      相关资源
      最近更新 更多