From 5b00bd10d068831e82d6cdb45bb4ab02818ca953 Mon Sep 17 00:00:00 2001 From: woni <320753691@qq.com> Date: Fri, 1 Nov 2024 09:03:27 +0800 Subject: [PATCH] init --- images/1.svg | 1 + images/2.svg | 1 + images/3.svg | 19 ++++++ images/newFromTemp.svg | 22 ++++++ index.html | 6 ++ js/dialog.js | 69 +++++++++++++++++++ js/ribbon.js | 152 +++++++++++++++++++++++++++++++++++++++++ js/systemdemo.js | 37 ++++++++++ js/taskpane.js | 66 ++++++++++++++++++ js/util.js | 19 ++++++ main.js | 4 ++ package.json | 9 +++ ribbon.xml | 16 +++++ ui/dialog.html | 53 ++++++++++++++ ui/taskpane.html | 50 ++++++++++++++ 15 files changed, 524 insertions(+) create mode 100644 images/1.svg create mode 100644 images/2.svg create mode 100644 images/3.svg create mode 100644 images/newFromTemp.svg create mode 100644 index.html create mode 100644 js/dialog.js create mode 100644 js/ribbon.js create mode 100644 js/systemdemo.js create mode 100644 js/taskpane.js create mode 100644 js/util.js create mode 100644 main.js create mode 100644 package.json create mode 100644 ribbon.xml create mode 100644 ui/dialog.html create mode 100644 ui/taskpane.html diff --git a/images/1.svg b/images/1.svg new file mode 100644 index 0000000..e114763 --- /dev/null +++ b/images/1.svg @@ -0,0 +1 @@ +编辑文字 \ No newline at end of file diff --git a/images/2.svg b/images/2.svg new file mode 100644 index 0000000..a0214d4 --- /dev/null +++ b/images/2.svg @@ -0,0 +1 @@ +艺术字大小 \ No newline at end of file diff --git a/images/3.svg b/images/3.svg new file mode 100644 index 0000000..f779740 --- /dev/null +++ b/images/3.svg @@ -0,0 +1,19 @@ + + + + + + + diff --git a/images/newFromTemp.svg b/images/newFromTemp.svg new file mode 100644 index 0000000..f6c70b3 --- /dev/null +++ b/images/newFromTemp.svg @@ -0,0 +1,22 @@ + + + + 从模版新建 + Created with Sketch. + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..2efb298 --- /dev/null +++ b/index.html @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/js/dialog.js b/js/dialog.js new file mode 100644 index 0000000..6959807 --- /dev/null +++ b/js/dialog.js @@ -0,0 +1,69 @@ +function onbuttonclick(idStr) +{ + switch(idStr) + { + case "getDocName":{ + let doc = window.Application.ActiveDocument + let textValue = "" + if (!doc){ + textValue = textValue + "当前没有打开任何文档" + return + } + textValue = textValue + doc.Name + document.getElementById("text_p").innerHTML = textValue + break + } + case "createTaskPane":{ + let tsId = window.Application.PluginStorage.getItem("taskpane_id") + if (!tsId){ + let tskpane = window.Application.CreateTaskPane(GetUrlPath() + "/taskpane.html") + let id = tskpane.ID + window.Application.PluginStorage.setItem("taskpane_id", id) + tskpane.Visible = true + }else{ + let tskpane = window.Application.GetTaskPane(tsId) + tskpane.Visible = true + } + break + } + case "newDoc":{ + window.Application.Documents.Add() + break + } + case "addString":{ + let doc = window.Application.ActiveDocument + if (doc){ + doc.Range(0, 0).Text="Hello, wps加载项!" + //好像是wps的bug, 这两句话触发wps重绘 + let rgSel = window.Application.Selection.Range + if (rgSel) + rgSel.Select() + } + break; + } + case "closeDoc":{ + if (window.Application.Documents.Count < 2) + { + alert("当前只有一个文档,别关了。") + break + } + + let doc = window.Application.ActiveDocument + if (doc) + doc.Close() + break + } + } + +} + +window.onload = function () { + var xmlReq = WpsInvoke.CreateXHR(); + var url = location.origin + "/.debugTemp/NotifyDemoUrl" + xmlReq.open("GET", url); + xmlReq.onload = function (res) { + var node = document.getElementById("DemoSpan"); + node.innerText = res.target.responseText; + }; + xmlReq.send(); +} \ No newline at end of file diff --git a/js/ribbon.js b/js/ribbon.js new file mode 100644 index 0000000..be4c751 --- /dev/null +++ b/js/ribbon.js @@ -0,0 +1,152 @@ + +//这个函数在整个wps加载项中是第一个执行的 +function OnAddinLoad(ribbonUI){ + if (typeof (window.Application.ribbonUI) != "object"){ + window.Application.ribbonUI = ribbonUI + } + + if (typeof (window.Application.Enum) != "object") { // 如果没有内置枚举值 + window.Application.Enum = WPS_Enum + } + + window.Application.PluginStorage.setItem("EnableFlag", false) //往PluginStorage中设置一个标记,用于控制两个按钮的置灰 + window.Application.PluginStorage.setItem("ApiEventFlag", false) //往PluginStorage中设置一个标记,用于控制ApiEvent的按钮label + return true +} + +var WebNotifycount = 0; +function OnAction(control) { + const eleId = control.Id + switch (eleId) { + case "btnShowMsg": + { + const doc = window.Application.ActiveDocument + if (!doc) { + alert("当前没有打开任何文档") + return + } + alert(doc.Name) + } + break; + case "btnIsEnbable": + { + let bFlag = window.Application.PluginStorage.getItem("EnableFlag") + window.Application.PluginStorage.setItem("EnableFlag", !bFlag) + + //通知wps刷新以下几个按饰的状态 + window.Application.ribbonUI.InvalidateControl("btnIsEnbable") + window.Application.ribbonUI.InvalidateControl("btnShowDialog") + window.Application.ribbonUI.InvalidateControl("btnShowTaskPane") + //window.Application.ribbonUI.Invalidate(); 这行代码打开则是刷新所有的按钮状态 + break + } + case "btnShowDialog": + window.Application.ShowDialog(GetUrlPath() + "/ui/dialog.html", "这是一个对话框网页", 400 * window.devicePixelRatio, 400 * window.devicePixelRatio, false) + break + case "btnShowTaskPane": + { + let tsId = window.Application.PluginStorage.getItem("taskpane_id") + if (!tsId) { + let tskpane = window.Application.CreateTaskPane(GetUrlPath() + "/ui/taskpane.html") + let id = tskpane.ID + window.Application.PluginStorage.setItem("taskpane_id", id) + tskpane.Visible = true + } else { + let tskpane = window.Application.GetTaskPane(tsId) + tskpane.Visible = !tskpane.Visible + } + } + break + case "btnApiEvent": + { + let bFlag = window.Application.PluginStorage.getItem("ApiEventFlag") + let bRegister = bFlag ? false : true + window.Application.PluginStorage.setItem("ApiEventFlag", bRegister) + if (bRegister){ + window.Application.ApiEvent.AddApiEventListener('DocumentNew', OnNewDocumentApiEvent) + } + else{ + window.Application.ApiEvent.RemoveApiEventListener('DocumentNew', OnNewDocumentApiEvent) + } + + window.Application.ribbonUI.InvalidateControl("btnApiEvent") + } + break + case "btnWebNotify": + { + let currentTime = new Date() + let timeStr = currentTime.getHours() + ':' + currentTime.getMinutes() + ":" + currentTime.getSeconds() + window.Application.OAAssist.WebNotify("这行内容由wps加载项主动送达给业务系统,可以任意自定义, 比如时间值:" + timeStr + ",次数:" + (++WebNotifycount), true) + } + break + default: + break + } + return true +} + +function GetImage(control) { + const eleId = control.Id + switch (eleId) { + case "btnShowMsg": + return "images/1.svg" + case "btnShowDialog": + return "images/2.svg" + case "btnShowTaskPane": + return "images/3.svg" + default: + ; + } + return "images/newFromTemp.svg" +} + +function OnGetEnabled(control) { + const eleId = control.Id + switch (eleId) { + case "btnShowMsg": + return true + break + case "btnShowDialog": + { + let bFlag = window.Application.PluginStorage.getItem("EnableFlag") + return bFlag + break + } + case "btnShowTaskPane": + { + let bFlag = window.Application.PluginStorage.getItem("EnableFlag") + return bFlag + break + } + default: + break + } + return true +} + +function OnGetVisible(control){ + return true +} + +function OnGetLabel(control){ + const eleId = control.Id + switch (eleId) { + case "btnIsEnbable": + { + let bFlag = window.Application.PluginStorage.getItem("EnableFlag") + return bFlag ? "按钮Disable" : "按钮Enable" + break + } + case "btnApiEvent": + { + let bFlag = window.Application.PluginStorage.getItem("ApiEventFlag") + return bFlag ? "清除新建文件事件" : "注册新建文件事件" + break + } + } + return "" +} + +function OnNewDocumentApiEvent(doc){ + alert("新建文件事件响应,取文件名: " + doc.Name) +} diff --git a/js/systemdemo.js b/js/systemdemo.js new file mode 100644 index 0000000..aec7548 --- /dev/null +++ b/js/systemdemo.js @@ -0,0 +1,37 @@ + +function openOfficeFileFromSystemDemo(param){ + let jsonObj = (typeof(param)=='string' ? JSON.parse(param) : param) + alert("从业务系统传过来的参数为:" + JSON.stringify(jsonObj)) + return {wps加载项项返回: jsonObj.filepath + ", 这个地址给的不正确"} +} + +function InvokeFromSystemDemo(param){ + let jsonObj = (typeof(param)=='string' ? JSON.parse(param) : param) + let handleInfo = jsonObj.Index + switch (handleInfo){ + case "getDocumentName":{ + let docName = "" + if (window.Application.ActiveDocument){ + docName = window.Application.ActiveDocument.Name + } + + return {当前打开的文件名为:docName} + } + + case "newDocument":{ + let newDocName="" + let doc = window.Application.Documents.Add() + newDocName = doc.Name + + return {操作结果:"新建文档成功,文档名为:" + newDocName} + } + + case "OpenFile":{ + let filePath = jsonObj.filepath + window.Application.Documents.OpenFromUrl(filePath) + return {操作结果:"打开文件成功"} + } + } + + return {其它xxx:""} +} diff --git a/js/taskpane.js b/js/taskpane.js new file mode 100644 index 0000000..41c618b --- /dev/null +++ b/js/taskpane.js @@ -0,0 +1,66 @@ +function onbuttonclick(idStr) +{ + if (typeof (window.Application.Enum) != "object") { // 如果没有内置枚举值 + window.Application.Enum = WPS_Enum + } + switch(idStr) + { + case "dockLeft":{ + let tsId = window.Application.PluginStorage.getItem("taskpane_id") + if (tsId){ + let tskpane = window.Application.GetTaskPane(tsId) + tskpane.DockPosition = window.Application.Enum.msoCTPDockPositionLeft + } + break + } + case "dockRight":{ + let tsId = window.Application.PluginStorage.getItem("taskpane_id") + if (tsId){ + let tskpane = window.Application.GetTaskPane(tsId) + tskpane.DockPosition = window.Application.Enum.msoCTPDockPositionRight + } + break + } + case "hideTaskPane":{ + let tsId = window.Application.PluginStorage.getItem("taskpane_id") + if (tsId){ + let tskpane = window.Application.GetTaskPane(tsId) + tskpane.Visible = false + } + break + } + case "addString":{ + let doc = window.Application.ActiveDocument + if (doc){ + doc.Range(0, 0).Text="Hello, wps加载项!" + //好像是wps的bug, 这两句话触发wps重绘 + let rgSel = window.Application.Selection.Range + if (rgSel) + rgSel.Select() + } + break; + } + case "getDocName":{ + let doc = window.Application.ActiveDocument + let textValue = ""; + if (!doc){ + textValue = textValue + "当前没有打开任何文档" + return + } + textValue = textValue + doc.Name + document.getElementById("text_p").innerHTML = textValue + break + } + } +} + +window.onload = function () { + var xmlReq = WpsInvoke.CreateXHR(); + var url = location.origin + "/.debugTemp/NotifyDemoUrl" + xmlReq.open("GET", url); + xmlReq.onload = function (res) { + var node = document.getElementById("DemoSpan"); + node.innerText = res.target.responseText; + }; + xmlReq.send(); +} \ No newline at end of file diff --git a/js/util.js b/js/util.js new file mode 100644 index 0000000..8c991a1 --- /dev/null +++ b/js/util.js @@ -0,0 +1,19 @@ +//在后续的wps版本中,wps的所有枚举值都会通过wps.Enum对象来自动支持,现阶段先人工定义 +var WPS_Enum = { + msoCTPDockPositionLeft: 0, + msoCTPDockPositionRight: 2 +} + +function GetUrlPath() { + let e = document.location.toString() + return -1 != (e = decodeURI(e)).indexOf("/") && (e = e.substring(0, e.lastIndexOf("/"))), e +} +/** + * 通过wps提供的接口执行一段脚本 + * @param {*} param 需要执行的脚本 + */ +function shellExecuteByOAAssist(param) { + if (wps != null) { + wps.OAAssist.ShellExecute(param) + } +} \ No newline at end of file diff --git a/main.js b/main.js new file mode 100644 index 0000000..ccf6cd5 --- /dev/null +++ b/main.js @@ -0,0 +1,4 @@ +//这个文件由index.html包含 +document.write(""); +document.write(""); +document.write(""); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..ad11a3f --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "XbotWps", + "addonType": "wps", + "version": "1.0.0", + "description": "wps加载项示例, 执行npm install后,提供代码联想功能", + "devDependencies": { + "wps-jsapi-declare": "latest" + } +} \ No newline at end of file diff --git a/ribbon.xml b/ribbon.xml new file mode 100644 index 0000000..8d98e3e --- /dev/null +++ b/ribbon.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + +
+
文档文件名为:
+ + + + + + + \ No newline at end of file diff --git a/ui/taskpane.html b/ui/taskpane.html new file mode 100644 index 0000000..bd0dd1a --- /dev/null +++ b/ui/taskpane.html @@ -0,0 +1,50 @@ + + + + + + + + + + + +
+
+ 这是一个网页,按"F12"可以打开调试器。 +
+
+ 这个示例展示了wps加载项的相关基础能力,与B/S业务系统的交互,请用浏览器打开: + +
+
开发文档: https://open.wps.cn/docs/office
+
+
+ + + + + +
+
+
文档文件名为:
+
+ + + + + \ No newline at end of file