簡體   English   中英

從 Office 加載項按鈕操作工作表

[英]Manipulate Worksheet from Office Add-In Button

我剛剛進入 Office.JS,我正在嘗試執行單擊功能區 UI 中的按鈕並在電子表格上執行某些操作的基本操作。 我發現的所有示例都與單擊單元格並使用=CONTOSO...執行返回變量的函數有關。 我的宏沒有一個以這種方式工作,它們要么有一個用戶窗體用於輸入,要么只是“做”一些事情。

我設法在功能區中創建了一個新選項卡,帶有一個應該調用getdata的按鈕,這實際上不是 function,而是更多我只想在單元格A1中輸入FOO的模塊/子。

在此處輸入圖像描述

這是我對運行yo office的默認設置所做的更改。

清單.xml:

          <ExtensionPoint xsi:type="PrimaryCommandSurface">
            <CustomTab id="TabCustom1">
            <!-- <OfficeTab id="TabData"> -->
            <Label resid="TabGroup.Label"/>
              <Group id="CommandsGroup">
                <Label resid="CommandsGroup.Label"/>
                <!-- Can only use 1, or default = Far Right
                <InsertAfter>TabReview</InsertAfter>
                <InsertBefore>TabReview</InsertBefore>
                -->
                <Icon>
                  <bt:Image size="16" resid="Icon.16x16"/>
                  <bt:Image size="32" resid="Icon.32x32"/>
                  <bt:Image size="80" resid="Icon.80x80"/>
                </Icon>
                <Control xsi:type="Button" id="TaskpaneButton">
                  <Label resid="TaskpaneButton.Label"/>
                  <Supertip>
                    <Title resid="TaskpaneButton.Label"/>
                    <Description resid="TaskpaneButton.Tooltip"/>
                  </Supertip>
                  <Icon>
                    <bt:Image size="16" resid="Icon.16x16"/>
                    <bt:Image size="32" resid="Icon.32x32"/>
                    <bt:Image size="80" resid="Icon.80x80"/>
                  </Icon>
                  <Action xsi:type="ShowTaskpane">
                    <TaskpaneId>ButtonId1</TaskpaneId>
                    <SourceLocation resid="Taskpane.Url"/>
                  </Action>
                </Control>
                <Control xsi:type="Button" id="DoButton">
                  <Label resid="DoButton.Label"/>
                  <Supertip>
                    <Title resid="DoButton.Label"/>
                    <Description resid="DoButton.Tooltip"/>
                  </Supertip>
                  <Icon>
                    <bt:Image size="16" resid="Icon.16x16"/>
                    <bt:Image size="32" resid="Icon.32x32"/>
                    <bt:Image size="80" resid="Icon.80x80"/>
                  </Icon>
                  <Action xsi:type="ExecuteFunction">
                    <FunctionName>getData</FunctionName>
                  </Action>
                </Control>
              </Group>
              <!-- </OfficeTab> -->
            </CustomTab>
          </ExtensionPoint>

...

      <bt:ShortStrings>
        <bt:String id="Functions.Namespace" DefaultValue="CONTOSO"/>
        <bt:String id="CommandsGroup.Label" DefaultValue="Group Label"/>
        <bt:String id="GetStarted.Title" DefaultValue="Get started with your sample add-in!"/>
        <bt:String id="TaskpaneButton.Label" DefaultValue="TaskPane Button label"/>
        <bt:String id="DoButton.Label" DefaultValue="Do Button label"/>
        <bt:String id="TabGroup.Label" DefaultValue="Custom Tab"/>
      </bt:ShortStrings>
      <bt:LongStrings>
        <bt:String id="GetStarted.Description" DefaultValue="Your sample add-in loaded succesfully. Go to the Custom Tab and click the 'Button label' button to get started."/>
        <bt:String id="TaskpaneButton.Tooltip" DefaultValue="Click to Show a Taskpane"/>
        <bt:String id="DoButton.Tooltip" DefaultValue="Click to Run A Function"/>
      </bt:LongStrings>

我不確定在哪里添加這個 function,我在functions.js中搞砸了,但同樣,這一切似乎都是為了在單元格中輸入=FUNCNAME 誰能指出我正確的方向?

謝謝

FunctionFile元素指定包含 JavaScript 代碼的文件,以在加載項命令使用ExecuteFunction操作時運行。 FunctionFile元素的resid屬性設置為 HTML 文件,其中包括您的加載項命令所需的所有 JavaScript 文件。 您不能直接鏈接到 JavaScript 文件。 您只能鏈接到 HTML 文件。 文件名在Resources元素中指定為 Url 元素。 例如:

<DesktopFormFactor>
    <FunctionFile resid="residDesktopFuncUrl" />
    <ExtensionPoint xsi:type="PrimaryCommandSurface">
      <!-- information about this extension point -->
    </ExtensionPoint>

    <!-- You can define more than one ExtensionPoint element as needed -->
</DesktopFormFactor>

終於明白了,只花了幾個小時的閱讀,我不明白為什么他們不從像這樣簡單的東西開始,但是文檔的開始是通過任務窗格、過濾表、凍結 header 行然后最后在在底部,我發現了一個名為“保護工作表”的部分,它與通過“任務窗格”運行無關,而只是單擊按鈕。

我調試的另外幾個問題是,如果我選擇Excel Custom Functions Add-in project (這是我不需要的=CONTOSO命名空間的來源),我無法讓它工作,它必須是Office Add-in Task Pane project中。 我還學會了使用npm start vs npm run start:desktop因為我注意到有時它會使用production vs development

最后,學習如何清除所有內容很重要,例如進入“信任中心”並單擊“下次辦公啟動清除緩存”以及刪除 --> %LOCALAPPDATA%\Microsoft\Office\16.0\Wef\下的所有內容

在此處輸入圖像描述

這是我通過 Office.JS 單擊功能區中的按鈕將“hello world”插入單元格 A1 的基本說明。

yo office

選擇項目類型:Office 加載項任務窗格項目

選擇腳本類型:JavaScript

你想為你的加載項命名什么? 我的 Office 加載項

您希望支持哪個 Office 客戶端應用程序? Excel

清單.xml

在現有<Control></Control>末尾插入

                <Control xsi:type="Button" id="HelloWorldButton">
                  <Label resid="HelloWorld.Label"/>
                  <Supertip>
                    <Title resid="HelloWorld.Label"/>
                    <Description resid="HelloWorld.Tooltip"/>
                  </Supertip>
                  <Icon>
                    <bt:Image size="16" resid="Icon.16x16"/>
                    <bt:Image size="32" resid="Icon.32x32"/>
                    <bt:Image size="80" resid="Icon.80x80"/>
                  </Icon>
                  <Action xsi:type="ExecuteFunction">
                    <FunctionName>helloworld</FunctionName>
                  </Action>
                </Control>
....

  <bt:String id="HelloWorld.Label" DefaultValue="Ribbon helloworld Func" />

...

  <bt:String id="HelloWorld.Tooltip" DefaultValue="Click to run helloworld func" />

命令.js

在 function 動作(事件)關閉之后插入 }

function helloworld(args) {
  Excel.run(function (context) {
    var ws = context.workbook.worksheets.getActiveWorksheet();
    var range = ws.getRange("A1");
    range.values = "Hello World!";
    range.select();

    return context.sync();
  }).catch(function (error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
      console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
  });
  args.completed();
}

在文檔底部:

g.helloworld = helloworld;

測試:

npm start

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM