From d15e8185bbbadf8718e20e8bf554c6138fd4d39a Mon Sep 17 00:00:00 2001 From: woni <320753691@qq.com> Date: Tue, 5 Nov 2024 21:23:22 +0800 Subject: [PATCH] first commit --- README.md | 18 +++++++++ js/dialog.js | 69 ------------------------------- js/ribbon.js | 98 ++++++--------------------------------------- js/systemdemo.js | 37 ----------------- js/taskpane.js | 66 ------------------------------ js/xbot.js | 36 +++++++++++++++++ main.js | 2 +- package.json | 4 +- python/README.md | 18 +++++++++ python/invoke_js.py | 83 ++++++++++++++++++++++++++++++++++++++ python/wps.js | 29 ++++++++++++++ ribbon.xml | 11 ++--- ui/dialog.html | 53 ------------------------ ui/taskpane.html | 50 ----------------------- 14 files changed, 203 insertions(+), 371 deletions(-) create mode 100644 README.md delete mode 100644 js/dialog.js delete mode 100644 js/systemdemo.js delete mode 100644 js/taskpane.js create mode 100644 js/xbot.js create mode 100644 python/README.md create mode 100644 python/invoke_js.py create mode 100644 python/wps.js delete mode 100644 ui/dialog.html delete mode 100644 ui/taskpane.html diff --git a/README.md b/README.md new file mode 100644 index 0000000..ecffe9e --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# XBot 文字插件 + + + +## 配置相关 + +%appdata%\kingsoft\wps\jsaddons + + + +## 发版相关 + + + +## 资料相关 + +- https://open.wps.cn/previous/docs/client/wpsload +- https://gitee.com/wpsoa/wpsoa diff --git a/js/dialog.js b/js/dialog.js deleted file mode 100644 index 6959807..0000000 --- a/js/dialog.js +++ /dev/null @@ -1,69 +0,0 @@ -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 index be4c751..3f51c0d 100644 --- a/js/ribbon.js +++ b/js/ribbon.js @@ -1,10 +1,10 @@ //这个函数在整个wps加载项中是第一个执行的 -function OnAddinLoad(ribbonUI){ - if (typeof (window.Application.ribbonUI) != "object"){ - window.Application.ribbonUI = ribbonUI +function OnAddinLoad(ribbonUI) { + if (typeof (window.Application.ribbonUI) != "object") { + window.Application.ribbonUI = ribbonUI } - + if (typeof (window.Application.Enum) != "object") { // 如果没有内置枚举值 window.Application.Enum = WPS_Enum } @@ -15,6 +15,7 @@ function OnAddinLoad(ribbonUI){ } var WebNotifycount = 0; + function OnAction(control) { const eleId = control.Id switch (eleId) { @@ -28,57 +29,6 @@ function OnAction(control) { 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 } @@ -90,10 +40,6 @@ function GetImage(control) { switch (eleId) { case "btnShowMsg": return "images/1.svg" - case "btnShowDialog": - return "images/2.svg" - case "btnShowTaskPane": - return "images/3.svg" default: ; } @@ -106,47 +52,29 @@ function OnGetEnabled(control) { 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){ +function OnGetVisible(control) { return true } -function OnGetLabel(control){ +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 - } + { + let bFlag = window.Application.PluginStorage.getItem("EnableFlag") + return bFlag ? "按钮Disable" : "按钮Enable" + break + } } return "" } -function OnNewDocumentApiEvent(doc){ +function OnNewDocumentApiEvent(doc) { alert("新建文件事件响应,取文件名: " + doc.Name) } diff --git a/js/systemdemo.js b/js/systemdemo.js deleted file mode 100644 index aec7548..0000000 --- a/js/systemdemo.js +++ /dev/null @@ -1,37 +0,0 @@ - -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 deleted file mode 100644 index 41c618b..0000000 --- a/js/taskpane.js +++ /dev/null @@ -1,66 +0,0 @@ -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/xbot.js b/js/xbot.js new file mode 100644 index 0000000..003fa61 --- /dev/null +++ b/js/xbot.js @@ -0,0 +1,36 @@ + + +function InvokeFromXbot(param) { + let jsonObj = (typeof (param) == 'string' ? JSON.parse(param) : param) + let func = jsonObj.Func; + let args = jsonObj.Args; + args = JSON.parse(args); + console.log(func); + console.log(args); + switch (func) { + case "getDocumentName": { + const doc = wps.WpsApplication().ActiveDocument; + alert(doc.Name); + if (!doc) { + return { currentDoc: null } + } + return { currentDoc: doc.Name } + } + + case "evalCode": { + // 1. 注入 WPS 对象 + // 2. 执行函数, 获取返回值 + let code = args?.code; + console.log("start eval code"); + console.log(code); + // var result = eval(code); + // const tmpCode = `return ${code}`; // 为了获取返回值,需要在代码前面加上 return + const result = new Function(code)(); // 执行并获取返回值 + console.log(result); + console.log("success eval code"); + return { message: "ok", data: result } + } + } + + return { message: "ok" } +} diff --git a/main.js b/main.js index ccf6cd5..fc183d6 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,4 @@ //这个文件由index.html包含 document.write(""); document.write(""); -document.write(""); \ No newline at end of file +document.write(""); diff --git a/package.json b/package.json index ad11a3f..20e775b 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { - "name": "XbotWps", + "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/python/README.md b/python/README.md new file mode 100644 index 0000000..8f6787e --- /dev/null +++ b/python/README.md @@ -0,0 +1,18 @@ +# XbotWPS + +## 介绍 + + +## 功能 + +``` +启动Word +Word导出PDF +读取Word文本 +写入文本至Word中 +定位Word光标 +移动Word光标 +替换Word文本 +读取Word表格 +插入Word表格, 插入图片, 插入超链接, 保存Word, 关闭Word +``` diff --git a/python/invoke_js.py b/python/invoke_js.py new file mode 100644 index 0000000..d2129ad --- /dev/null +++ b/python/invoke_js.py @@ -0,0 +1,83 @@ +import base64 +import uuid +import json +import http.client +import webbrowser + + +def start_http_server(): + """start debug""" + webbrowser.open("ksowpscloudsvr://start=RelayHttpServer") + + +def get_version(): + """check http status by get version""" + try: + conn = http.client.HTTPConnection("127.0.0.1", 58890) + conn.request("POST", "/version", body="{}", headers={}) + response = conn.getresponse() + data = response.read() + conn.close() + return response.status == 200 + except: + return False + + +def invoke_wps_func(func, args): + + # 可能触发是否允许访问 + invoke_param = { + "Func": func, + "Args": json.dumps(args), + "AppType": "wps", + } + + req_uui = str(uuid.uuid4()) + + info = f"{json.dumps(invoke_param)});\ + var xhr = new XMLHttpRequest();\ + xhr.open('POST', 'http://127.0.0.1:58890/transferEcho/runParams');\ + xhr.send(JSON.stringify({{id: '{req_uui}', response: res}}));void(0" + + data = { + "name": "xbotwps", + "function": "var res = InvokeFromXbot", + "info": info, + "showToFront": True, + "jsPluginsXml": "", + } + + payload = { + "id": req_uui, + "app": "wps", + "data": "ksowebstartupwps://" + + base64.b64encode(json.dumps(data).encode()).decode(), + "mode": False, + } + + conn = http.client.HTTPConnection("127.0.0.1", 58890) + payload = json.dumps(payload) + + conn.request("POST", "/transfer/runParams", payload) + + http_response = conn.getresponse() + data = http_response.read() + conn.close() + + try: + return json.loads(data.decode("utf-8")) + except Exception as e: + print(data.decode("utf-8")) + return {"message": "failed to parse response"} + + +if __name__ == "__main__": + start_http_server() + + print(get_version()) + + func = "getDocumentName" + func = "evalCode" + args = {"code": "wps.WpsApplication().ActiveDocument.Name"} + res = invoke_wps_func(func, args) + print(res) diff --git a/python/wps.js b/python/wps.js new file mode 100644 index 0000000..ff6f553 --- /dev/null +++ b/python/wps.js @@ -0,0 +1,29 @@ +const wps = { + app: wps.WpsApplication(), + replace: () => { + Selection.SetRange(16, 16); + Selection.Find.Wrap = wdFindContinue; + (obj => { + obj.Text = "1"; + obj.Forward = true; + obj.Wrap = wdFindContinue; + obj.MatchCase = false; + obj.MatchByte = true; + obj.MatchWildcards = false; + obj.MatchWholeWord = false; + obj.MatchFuzzy = false; + obj.Replacement.Text = "2"; + })(Selection.Find); + (obj => { + obj.Style = ""; + obj.Highlight = wdUndefined; + (obj => { + obj.Style = ""; + obj.Highlight = wdUndefined; + })(obj.Replacement); + })(Selection.Find); + Selection.Find.Execute(undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, wdReplaceAll, undefined, undefined, undefined, undefined); + Selection.Find.Replacement.Text = "2"; + } + +} diff --git a/ribbon.xml b/ribbon.xml index 8d98e3e..05ee89f 100644 --- a/ribbon.xml +++ b/ribbon.xml @@ -1,16 +1,11 @@ - + - - - - - - -
-
文档文件名为:
- - - - - - - \ No newline at end of file diff --git a/ui/taskpane.html b/ui/taskpane.html deleted file mode 100644 index bd0dd1a..0000000 --- a/ui/taskpane.html +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - -
-
- 这是一个网页,按"F12"可以打开调试器。 -
-
- 这个示例展示了wps加载项的相关基础能力,与B/S业务系统的交互,请用浏览器打开: - -
-
开发文档: https://open.wps.cn/docs/office
-
-
- - - - - -
-
-
文档文件名为:
-
- - - - - \ No newline at end of file