我剛剛進入 Office.JS,我正在嘗試執行單擊功能區 UI 中的按鈕并在電子表格上執行某些操作的基本操作。我發現的所有示例都與單擊單元格和使用=CONTOSO...來執行回傳變數的函式有關。我的宏沒有一個以這種方式作業,它們要么有一個用于輸入的用戶表單,要么只是“做”一些事情。
我設法在功能區中創建了一個新選項卡,其中有一個按鈕應該呼叫一個函式getdata,而該函式實際上不是一個函式,而是一個我只想FOO在 cell 中輸入的模塊/子A1。

這是我對運行的默認設定所做的更改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>
我什至不確定在哪里添加這個功能,我搞砸了,functions.js但同樣,這一切似乎都是為了進入=FUNCNAME一個單元格。任何人都可以指出我正確的方向嗎?
謝謝
uj5u.com熱心網友回復:
終于拿到了!只花了幾個小時的閱讀時間,我不明白為什么他們不從這樣簡單的事情開始,但是檔案開始是通過任務窗格制作表格,過濾表格,凍結標題行,最后在底部我找到了名為
這是我通過 Office.JS 單擊功能區中的按鈕將“hello world”插入單元格 A1 的基本說明。
yo office
選擇專案型別:Office 加載項任務窗格專案
選擇腳本型別:JavaScript
你想為你的加載項命名什么?我的辦公室插件
您希望支持哪個 Office 客戶端應用程式?電子表格
清單檔案
在現有結束后插入 <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
在函式 action(event) 關閉后插入 }
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
uj5u.com熱心網友回復:
該FunctionFile元素指定一個檔案,該檔案包含在加載項命令使用該ExecuteFunction操作時運行的 JavaScript 代碼。該FunctionFile元素的resid屬性設定為一個 HTML 檔案,其中包含加載項命令所需的所有 JavaScript 檔案。您不能直接鏈接到 JavaScript 檔案。您只能鏈接到 HTML 檔案。檔案名在元素中指定為 UrlResources元素。例如:
<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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/399056.html
標籤:javascript 擅长 office-js excel插件
上一篇:2022屆安防展觀展感悟
