vsto - 使用自己的浏览器控件扩展 Office JavaScript API
问题描述
我正在尝试使用 System.Windows.Forms.WebBrowser-Control 编写一个 VSTO-Add-In,以启用类似于 Office-JS-Add-In 模型的功能。
WebBrowser 控件将显示一些 HTML/JS 页面,并能够通过 window.external 和 WebBrowser 对象的 ObjectForScripting 属性从 JavaScript 调用 VSTO-Add-In 中的 C# 函数。
那就是在JS中调用将是
window.external.DoFancyStuffToMyDocument(withTheseParams)
虽然必须有一些
class MyFunctionProxy() {
public void DoFancyStuffToMyDocument(string theParam) {
//code here
}
}
在 C#-Code 中,这将附加到 WebBrowser
myWebBrowser.ObjectForScripting = new MyFunctionProxy();
到目前为止,一切都很好。现在来了。我希望我的 HTML/JS 代码也能够使用 office.js 代码和功能,例如
Word.run(function (context) {
var thisDocument = context.document;
var range = thisDocument.getSelection();
range.insertText('"Hitch your wagon to a star."\n', Word.InsertLocation.replace);
//...
}
有没有人看到让这个工作的方法?
我最初的猜测是,Word on-prem 中的 OfficeJS-taskpane-add-ins 使用了一些与上面类似的方法,以及从 WebBrowser 和适当的 ObjectForScripting 派生的类。这表明必须有一个(希望可访问的)类分配给处理来自 office.js 的函数调用的 ObjectForScripting-property。然后我可以代理这个 ObjectForScripting 类并添加我自己的函数,如“DoFancyStuffToMyDocument()”。
解决方案
推荐阅读
- json - 使用 BigQuery 从 JSON 中提取动态参数
- r - 替换数据帧列表中的所有 NA R
- python - 如何使用 python exchangelib 在 Outlook 电子邮件中发送 html 表格内容
- angularjs - 如何更改 Angular Formly 描述的颜色?
- go - 可以使用切片作为共享内存吗?
- notepad++ - 在记事本++中,我可以禁用多行制表符缩进(使用自动换行)吗?
- c - 如何排列 .txt 文件的数据以与 C 中其他数据的每个值相对应
- react-native - react-native-azure-auth 退出 Web 视图不会自动关闭并返回移动应用程序
- python - 按字母顺序对熊猫数据框进行排序不起作用
- mysql - Mysql 8.0 使用低 CPU/RAM 和慢查询