WpsOffice.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. MWF.xDesktop.requireApp("process.Xform", "$Module", null, false);
  2. /** @class WpsOffice Wps组件。
  3. * @o2cn WpsOffice
  4. * @example
  5. * //可以在脚本中获取该组件
  6. * //方法1:
  7. * var wpsOffice = this.form.get("fieldId"); //获取组件
  8. * //方法2
  9. * var wpsOffice = this.target; //在组件本身的脚本中获取
  10. * @extends MWF.xApplication.process.Xform.$Module
  11. * @o2category FormComponents
  12. * @o2range {Process|CMS|Portal}
  13. * @hideconstructor
  14. */
  15. MWF.xApplication.process.Xform.WpsOffice = MWF.APPWpsOffice = new Class(
  16. /** @lends MWF.xApplication.process.Xform.WpsOffice# */
  17. {
  18. Extends: MWF.APP$Module,
  19. isActive: false,
  20. options:{
  21. "version": "wpsWebOffice",
  22. /**
  23. * 文档打开前事件。
  24. * @since V8.0
  25. * @event MWF.xApplication.process.Xform.WpsOffice#beforeOpen
  26. */
  27. /**
  28. * 文档打开后事件。
  29. * @since V8.0
  30. * @event MWF.xApplication.process.Xform.WpsOffice#afterOpen
  31. */
  32. /**
  33. * 保存后事件。
  34. * @since V8.0
  35. * @event MWF.xApplication.process.Xform.WpsOffice#afterSave
  36. */
  37. "moduleEvents": ["beforeOpen",
  38. "afterOpen",
  39. "afterSave"
  40. ]
  41. },
  42. initialize: function(node, json, form, options){
  43. this.node = $(node);
  44. this.node.store("module", this);
  45. this.json = json;
  46. this.form = form;
  47. this.documentId = "";
  48. this.mode = "write";
  49. this.officeType = {
  50. "docx" : "Writer",
  51. "doc" : "Writer",
  52. "xlsx" : "Spreadsheet",
  53. "xls" : "Spreadsheet",
  54. "pptx" : "Presentation",
  55. "ppt" : "Presentation",
  56. "pdf" : "Pdf",
  57. "ofd" : "Pdf"
  58. };
  59. this.appToken = "x_processplatform_assemble_surface";
  60. this.version = this.options.version;
  61. },
  62. _loadUserInterface: function(){
  63. this.node.empty();
  64. this.node.setStyles({
  65. "min-height": "700px"
  66. });
  67. },
  68. _afterLoaded: function(){
  69. if(!layout.serviceAddressList["x_wpsfile_assemble_control"]){
  70. this.node.set("html","<h3><font color=red>please install wps application</font></h3>");
  71. return false;
  72. }
  73. if(this.mode !== "read" && this.json.allowUpload){
  74. this.createUpload();
  75. }
  76. this.action = o2.Actions.load("x_wpsfile_assemble_control");
  77. if (!this.json.isNotLoadNow){
  78. this.data = this.getData();
  79. if(this.data.documentId === ""){
  80. if (this.json.officeType === "other" && this.json.templateType === "script"){
  81. this.json.template = this.form.Macro.exec(this.json.templeteScript.code, this);
  82. }
  83. this[this.json.officeType === "other"&&this.json.template !== ""? "createDocumentByTemplate":"createDocument"](function (){
  84. this.loadDocument();
  85. }.bind(this));
  86. }else {
  87. this.documentId = this.data.documentId;
  88. this.loadDocument();
  89. }
  90. }
  91. },
  92. createUpload : function (){
  93. this.uploadNode = new Element("div",{"style":"margin:10px;"}).inject(this.node);
  94. var uploadBtn = new Element("button",{"text":MWF.xApplication.process.Xform.LP.ofdview.upload,"style":"margin-left: 15px; color: rgb(255, 255, 255); cursor: pointer; height: 26px; line-height: 26px; padding: 0px 10px; min-width: 40px; background-color: rgb(74, 144, 226); border: 1px solid rgb(82, 139, 204); border-radius: 15px;"}).inject(this.uploadNode);
  95. uploadBtn.addEvent("click",function (){
  96. o2.require("o2.widget.Upload", null, false);
  97. var upload = new o2.widget.Upload(this.content, {
  98. "action": o2.Actions.get(this.appToken).action,
  99. "method": "uploadAttachment",
  100. "accept" : ".docx,.xlsx,.pptx,.pdf,.ofd",
  101. "parameter": {
  102. "id" : this.form.businessData.work.id
  103. },
  104. "data":{
  105. },
  106. "onCompleted": function(data){
  107. o2.Actions.load(this.appToken).AttachmentAction.delete(this.documentId,function( json ){
  108. }.bind(this));
  109. this.documentId = data.id;
  110. this.reload();
  111. }.bind(this)
  112. });
  113. upload.load();
  114. }.bind(this));
  115. },
  116. reload : function (){
  117. this.setData();
  118. this.node.empty();
  119. this.createUpload();
  120. this.loadDocument();
  121. },
  122. createDocumentByTemplate : function (callback){
  123. this.action.CustomAction.getInfo(this.json.template).then(function(json) {
  124. var data = {
  125. "fileName": MWF.xApplication.process.Xform.LP.onlyoffice.filetext + "." + json.data.extension,
  126. "fileType": json.data.extension,
  127. "appToken" : this.appToken,
  128. "workId" : this.form.businessData.work.id,
  129. "site" : "filetext",
  130. "tempId": this.json.template
  131. };
  132. this.action.CustomAction.createForO2(data,
  133. function( json ){
  134. this.documentId = json.data.fileId;
  135. this.setData();
  136. if (callback) callback();
  137. }.bind(this),null, false
  138. );
  139. }.bind(this))
  140. },
  141. createDocument : function (callback){
  142. var data = {
  143. "fileName" : MWF.xApplication.process.Xform.LP.onlyoffice.filetext + "." + this.json.officeType,
  144. "appToken" : this.appToken,
  145. "workId" : this.form.businessData.work.id,
  146. "site" : "filetext"
  147. };
  148. this.action.CustomAction.createForO2(data,
  149. function( json ){
  150. this.documentId = json.data.fileId;
  151. this.setData();
  152. if (callback) callback();
  153. }.bind(this),null, false
  154. );
  155. },
  156. loadDocument: function () {
  157. o2.Actions.load(this.appToken).AttachmentAction.getOnlineInfo(this.documentId, function( json ){
  158. this.documentData = json.data;
  159. this.fileName = this.documentData.name;
  160. this.extension = this.documentData.extension;
  161. this.getEditor(function () {
  162. this.loadApi(function (){
  163. this.loadEditor();
  164. }.bind(this));
  165. }.bind(this));
  166. }.bind(this),null,false);
  167. },
  168. loadApi : function (callback){
  169. if(this.version === "wpsWebOffice"){
  170. o2.load(["../x_component_WpsOfficeEditor/web-office-sdk-solution-v2.0.2.umd.min.js"], {"sequence": true}, function () {
  171. if (callback) callback();
  172. }.bind(this));
  173. }else {
  174. o2.load(["../x_component_WpsOfficeEditor/web-office-sdk-v1.1.19.umd.js"], {"sequence": true}, function () {
  175. if (callback) callback();
  176. }.bind(this));
  177. }
  178. },
  179. getEditor: function (callback) {
  180. if (this.isReadonly()){
  181. this.mode = "read";
  182. }else{
  183. if (this.json.readScript && this.json.readScript.code){
  184. var flag = this.form.Macro.exec(this.json.readScript.code, this);
  185. if (flag){
  186. this.mode = "read";
  187. }
  188. }
  189. }
  190. if(this.action.ConfigAction.getBaseConfig){
  191. this.action.ConfigAction.getBaseConfig(function( json ){
  192. this.appId = json.data.appId;
  193. this.version = json.data.version;
  194. if (callback) callback();
  195. }.bind(this),null,false);
  196. }else {
  197. this.action.ConfigAction.getAppId(function( json ){
  198. this.appId = json.data.value;
  199. if (callback) callback();
  200. }.bind(this),null,false);
  201. }
  202. },
  203. loadEditor : function (){
  204. this.fireEvent("beforeOpen");
  205. if(this.wpsOffice) this.wpsOffice.destroy();
  206. this.officeNode = new Element("div#_" + this.documentId,{"style":"height:100%;overflow:hidden;min-height:700px"}).inject(this.node);
  207. var config = {
  208. mount: this.officeNode,
  209. mode : this.json.showMode,
  210. cooperUserAttribute: {
  211. isCooperUsersAvatarVisible: this.json.isCooperUsersAvatarVisible //是否显示协作用户头像
  212. },
  213. // 通用选项,所有类型文档适用
  214. commonOptions: {
  215. isShowTopArea: this.json.isShowTopArea, // 隐藏顶部区域(头部和工具栏)
  216. isShowHeader: this.json.isShowHeader, // 隐藏头部区域
  217. isBrowserViewFullscreen : this.json.isBrowserViewFullscreen,//是否在浏览器区域全屏
  218. isIframeViewFullscreen : this.json.isIframeViewFullscreen//是否在iframe区域内全屏
  219. },
  220. wordOptions : {
  221. isShowDocMap : this.json.isShowDocMap,//是否开启目录功能,默认开启
  222. isBestScale : this.json.isBestScale,//打开文档时,默认以最佳比例显示(适用于pc)
  223. isShowBottomStatusBar : this.json.isShowBottomStatusBar,//pc-是否展示底部状态栏
  224. "mobile.isOpenIntoEdit" : this.json.isOpenIntoEdit//mobile-要有编辑权限,移动端打开时是否进入编辑
  225. },
  226. commandBars: [
  227. {
  228. cmbId: "HeaderLeft",
  229. attributes: {
  230. visible: this.json.HeaderLeft,
  231. enable: this.json.HeaderLeft
  232. }
  233. },
  234. {
  235. cmbId: "HeaderRight",
  236. attributes: {
  237. visible: this.json.HeaderRight,
  238. enable: this.json.HeaderRight
  239. }
  240. },
  241. {
  242. cmbId: "FloatQuickHelp",
  243. attributes: {
  244. visible: this.json.FloatQuickHelp,
  245. enable: this.json.FloatQuickHelp
  246. }
  247. },
  248. {
  249. cmbId: "HistoryVersion",
  250. attributes: {
  251. visible: this.json.HistoryVersion,
  252. enable: this.json.HistoryVersion
  253. }
  254. },
  255. {
  256. cmbId: "HistoryRecord",
  257. attributes: {
  258. visible: this.json.HistoryRecord,
  259. enable: this.json.HistoryRecord
  260. }
  261. },
  262. {
  263. cmbId: "HistoryVersionDivider",
  264. attributes: {
  265. visible: this.json.HistoryVersionDivider,
  266. enable: this.json.HistoryVersionDivider
  267. }
  268. },
  269. {
  270. cmbId: "Logo",
  271. attributes: {
  272. visible: this.json.Logo,
  273. enable: this.json.Logo
  274. }
  275. },
  276. {
  277. cmbId: "Cooperation",
  278. attributes: {
  279. visible: this.json.Cooperation,
  280. enable: this.json.Cooperation
  281. }
  282. },
  283. {
  284. cmbId: "More",
  285. attributes: {
  286. visible: this.json.More,
  287. enable: this.json.More
  288. }
  289. },
  290. {
  291. cmbId: "SendButton",
  292. attributes: {
  293. visible: this.json.SendButton,
  294. enable: this.json.SendButton
  295. }
  296. },
  297. {
  298. cmbId: "CooperHistoryMenuItem",
  299. attributes: {
  300. visible: this.json.CooperHistoryMenuItem,
  301. enable: this.json.CooperHistoryMenuItem
  302. }
  303. },
  304. {
  305. cmbId: "TabPrintPreview",
  306. attributes: {
  307. visible: this.json.TabPrintPreview,
  308. enable: this.json.TabPrintPreview
  309. }
  310. },
  311. {
  312. cmbId: "MenuPrintPreview",
  313. attributes: {
  314. visible: this.json.MenuPrintPreview,
  315. enable: this.json.MenuPrintPreview
  316. }
  317. },
  318. {
  319. cmbId: "ReviewTrackChanges",
  320. attributes: {
  321. visible: this.json.ReviewTrackChanges,
  322. enable: this.json.ReviewTrackChanges
  323. }
  324. },
  325. {
  326. cmbId: "TrackChanges",
  327. attributes: {
  328. visible: this.json.TrackChanges,
  329. enable: this.json.TrackChanges
  330. }
  331. },
  332. {
  333. cmbId: "ContextMenuConvene",
  334. attributes: {
  335. visible: this.json.ContextMenuConvene,
  336. enable: this.json.ContextMenuConvene
  337. }
  338. },
  339. {
  340. cmbId: "WriterHoverToolbars",
  341. attributes: {
  342. visible: this.json.WriterHoverToolbars,
  343. enable: this.json.WriterHoverToolbars
  344. }
  345. },
  346. {
  347. cmbId: "ReadSetting",
  348. attributes: {
  349. visible: this.json.ReadSetting,
  350. enable: this.json.ReadSetting
  351. }
  352. }
  353. ]
  354. };
  355. if(this.version === "wpsWebOffice"){
  356. config.officeType = WebOfficeSDK.OfficeType[this.officeType[this.extension.toLowerCase()]];
  357. config.appId = this.appId;
  358. config.fileId = this.documentId.replace(/-/g, "_");
  359. config.token = layout.session.token;
  360. config.customArgs = {
  361. "appToken" : this.appToken,
  362. "mode" : this.mode
  363. };
  364. this.wpsOffice = WebOfficeSDK.init(config);
  365. }else {
  366. this.action.CustomAction.getWpsFileUrl(this.documentId,{
  367. "mode" : this.mode,
  368. "appToken" : this.appToken
  369. },function( json ){
  370. this.wpsUrl = json.data.wpsUrl;
  371. config.url = this.wpsUrl;
  372. }.bind(this),null,false);
  373. // console.log(this.wpsUrl)
  374. this.wpsOffice = WebOfficeSDK.config(config);
  375. this.wpsOffice.setToken({
  376. token: layout.session.token,
  377. timeout: 100 * 60 * 1000 // token超时时间, 可配合refreshToken配置函数使用,当超时前将调用refreshToken
  378. });
  379. }
  380. this.wpsOffice.on('fileOpen', function(result) {
  381. this.fireEvent("afterOpen");
  382. }.bind(this));
  383. this.wpsOffice.on('fileStatus', function(result) {
  384. if(result.status === 7){
  385. this.fireEvent("afterSave");
  386. }
  387. }.bind(this));
  388. this.wpsOffice.on('fullscreenChange', function(result) {
  389. console.log(JSON.stringify(result))
  390. });
  391. this.wpsOffice.on('previewLimit', function(result) {
  392. console.log(JSON.stringify(result))
  393. });
  394. this.wpsOffice.on('tabSwitch', function(result) {
  395. console.log(JSON.stringify(result))
  396. });
  397. this.wpsOffice.on('error', function(result) {
  398. console.log(JSON.stringify(result))
  399. });
  400. //是否显示评论
  401. if(this.json.isShowComment){
  402. this.showComments();
  403. }else {
  404. this.hideComments();
  405. }
  406. },
  407. hide: function(){
  408. this.node.hide();
  409. },
  410. show: function(){
  411. this.node.show();
  412. },
  413. isEmpty : function(){
  414. var data = this.getData();
  415. if(data.documentId === ""){
  416. return true;
  417. }else {
  418. return false;
  419. }
  420. },
  421. getData: function(){
  422. var data = {
  423. "documentId" : ""
  424. };
  425. if(this.form.businessData.data[this.json.id]){
  426. data.documentId = this.form.businessData.data[this.json.id].documentId;
  427. }
  428. return data;
  429. },
  430. setData: function() {
  431. var data = {
  432. "documentId": this.documentId,
  433. "appToken": this.appToken
  434. }
  435. this.data = data;
  436. this._setBusinessData(data);
  437. var jsonData = {}
  438. jsonData[this.json.id] = data;
  439. o2.Actions.load(this.appToken).DataAction.updateWithJob(this.form.businessData.work.job, jsonData, function (json) {
  440. data = json.data;
  441. })
  442. },
  443. /**
  444. * @summary 保存wps
  445. * @example
  446. * this.form.get("fieldId").save(callback)
  447. */
  448. save: function(callback){
  449. var promise = this.wpsOffice.save();
  450. promise.then(function(){
  451. console.log("save success");
  452. if(callback) callback();
  453. });
  454. },
  455. /**
  456. * @summary 发送全局广播
  457. * @example
  458. * this.form.get("fieldId").sendBroadcast("测试")
  459. */
  460. sendBroadcast : async function (text){
  461. await this.wpsOffice.ready();
  462. const app = this.wpsOffice.Application;
  463. const Public = await app.Public;
  464. const result = await Public.SendBroadcast({
  465. Data: { message: text }
  466. });
  467. },
  468. /**
  469. * @summary 获取word页面总数,
  470. * @return 返回是个Promise对象
  471. * @example
  472. * this.form.get("fieldId").getTotalPage()
  473. */
  474. getTotalPage : async function (){
  475. await this.wpsOffice.ready();
  476. const app = this.wpsOffice.Application;
  477. const totalPages = await app.ActiveDocument.Range.Information(app.Enum.WdInformation.wdNumberOfPagesInDocument);
  478. console.log(totalPages)
  479. return totalPages;
  480. },
  481. /**
  482. * @summary 获取当前页
  483. * @example
  484. * this.form.get("fieldId").getCurrentPage()
  485. */
  486. getCurrentPage: async function (){
  487. // 获取当前页数
  488. await this.wpsOffice.ready();
  489. const app = this.wpsOffice.Application;
  490. const currentPage = await app.ActiveDocument.ActiveWindow.Selection.Information(
  491. app.Enum.WdInformation.wdActiveEndPageNumber
  492. )
  493. console.log(currentPage)
  494. return currentPage;
  495. },
  496. /**
  497. * @summary 跳转到指定页
  498. * @example
  499. * this.form.get("fieldId").gotoPage(page)
  500. */
  501. gotoPage : async function (page){
  502. await this.wpsOffice.ready();
  503. const app = this.wpsOffice.Application;
  504. await app.ActiveDocument.Selection.GoTo({
  505. What: app.Enum.WdGoToItem.wdGoToPage,
  506. Which: app.Enum.WdGoToDirection.wdGoToAbsolute,
  507. Count: page
  508. });
  509. },
  510. /**
  511. * @summary 获取所有书签
  512. * @return 返回是个Promise对象
  513. * @example
  514. * this.form.get("fieldId").getAllBookmark()
  515. */
  516. getAllBookmark : async function (callback){
  517. this.wpsOffice.ready();
  518. const app = this.wpsOffice.Application;
  519. const bookMarks = await app.ActiveDocument.Bookmarks.Json();
  520. return bookMarks;
  521. },
  522. /**
  523. * @summary 获取书签值
  524. * @return 返回是个Promise对象
  525. * @example
  526. * this.form.get("fieldId").getBookmarkText()
  527. */
  528. getBookmarkText : async function (name) {
  529. this.wpsOffice.ready();
  530. const app = this.wpsOffice.Application;
  531. const text = await app.ActiveDocument.GetBookmarkText(name);
  532. return text;
  533. },
  534. /**
  535. * @summary 跳转到书签
  536. * @example
  537. * this.form.get("fieldId").gotoBookmark()
  538. */
  539. gotoBookmark : async function (name){
  540. this.wpsOffice.ready();
  541. const app = this.wpsOffice.Application;
  542. await app.ActiveDocument.Selection.GoTo({
  543. What: app.Enum.WdGoToItem.wdGoToBookmark,
  544. Which: app.Enum.WdGoToDirection.wdGoToAbsolute,
  545. Name: name
  546. });
  547. },
  548. /**
  549. * @summary 给书签赋值
  550. * @return 返回是个Promise对象
  551. * @example
  552. * this.form.get("fieldId").setBookmarkText(name,value)
  553. */
  554. setBookmarkText : async function (name,value){
  555. await this.wpsOffice.ready();
  556. const app = this.wpsOffice.Application;
  557. // 书签对象
  558. const bookmarks = await app.ActiveDocument.Bookmarks;
  559. // 替换书签内容
  560. const isReplaceSuccess = await bookmarks.ReplaceBookmark([
  561. {
  562. name: name,
  563. type: 'text',
  564. value: value
  565. }
  566. ])
  567. return isReplaceSuccess;
  568. },
  569. /**
  570. * @summary 盖章
  571. * @return 返回是个Promise对象
  572. * @example
  573. * this.form.get("fieldId").setSeal(bookmark,img,left,top,width,height)
  574. */
  575. setSeal : async function (bookmark,img,left,top,width,height){
  576. await this.wpsOffice.ready();
  577. const app = this.wpsOffice.Application;
  578. //获取当前选区
  579. const selection = await app.ActiveDocument.ActiveWindow.Selection;
  580. // 跳转到指定的书签
  581. await app.ActiveDocument.ActiveWindow.Selection.GoTo(
  582. app.Enum.WdGoToItem.wdGoToBookmark, // 类型:Bookmark
  583. app.Enum.WdGoToDirection.wdGoToAbsolute, // 定位
  584. 1, // 数量
  585. bookmark, // 书签名
  586. );
  587. // 获取图形对象
  588. const shapes = await app.ActiveDocument.Shapes;
  589. // 光标插入非嵌入式图片
  590. const shape = await shapes.AddPicture({
  591. FileName: img, // 图片地址
  592. LinkToFile: true,
  593. SaveWithDocument: true,
  594. Left: left, // 图片距离左边位置
  595. Top: top, // 图片距离顶部位置
  596. Width: width, // 图片宽度
  597. Height: height, // 图片高度
  598. });
  599. // 设置文字环绕模式为【衬于文字下方】
  600. shape.ZOrder(app.Enum.ZOrderCmd.sendBehindText);
  601. },
  602. /**
  603. * @summary 查找替换
  604. * @return 返回是个Promise对象
  605. * @example
  606. * this.form.get("fieldId").replaceText(key,value)
  607. */
  608. replaceText : async function (key,value,options){
  609. await this.wpsOffice.ready();
  610. const app = this.wpsOffice.Application;
  611. const isReplaceSuccess = app.ActiveDocument.ReplaceText([{key: key, value: value}])
  612. return isReplaceSuccess;
  613. },
  614. /**
  615. * @summary 开启修订模式
  616. * @return 返回是个Promise对象
  617. * @example
  618. * this.form.get("fieldId").startRevisions()
  619. */
  620. startRevisions : function (){
  621. this.wpsOffice.ready().then(function(){
  622. const app = this.wpsOffice.Application;
  623. // 将当前文档的编辑状态切换成修订模式
  624. app.ActiveDocument.TrackRevisions = true;
  625. }.bind(this));
  626. },
  627. /**
  628. * @summary 关闭修订模式
  629. * @return 返回是个Promise对象
  630. * @example
  631. * this.form.get("fieldId").stopRevisions()
  632. */
  633. stopRevisions : function (){
  634. //关闭修订模式
  635. this.wpsOffice.ready().then(function(){
  636. const app = this.wpsOffice.Application;
  637. // 将当前文档的编辑状态切换成修订模式
  638. app.ActiveDocument.TrackRevisions = false;
  639. }.bind(this));
  640. },
  641. /**
  642. * @summary 接受所有修订
  643. * @example
  644. * this.form.get("fieldId").acceptAllRevisions()
  645. */
  646. acceptAllRevisions : async function (){
  647. await this.wpsOffice.ready();
  648. const app = this.wpsOffice.Application;
  649. // 获取修订对象
  650. const revisions = await app.ActiveDocument.Revisions;
  651. // 接受对指定文档的所有修订
  652. await revisions.AcceptAll();
  653. },
  654. /**
  655. * @summary 拒绝所有修订
  656. * @example
  657. * this.form.get("fieldId").rejectAllRevisions()
  658. */
  659. rejectAllRevisions : async function (){
  660. await this.wpsOffice.ready();
  661. const app = this.wpsOffice.Application;
  662. // 获取修订对象
  663. const revisions = await app.ActiveDocument.Revisions;
  664. // 拒绝对指定文档的所有修订
  665. await revisions.RejectAll();
  666. },
  667. /**
  668. * @summary 显示痕迹
  669. * @example
  670. * this.form.get("fieldId").showRevisions()
  671. */
  672. showRevisions : async function (){
  673. await this.wpsOffice.ready();
  674. const app = this.wpsOffice.Application;
  675. // 获取节对象
  676. const View = await app.ActiveDocument.ActiveWindow.View;
  677. View.RevisionsView = 0;
  678. // 设置修订状态为 显示标记的最终状态
  679. View.ShowRevisionsAndComments = true;
  680. },
  681. /**
  682. * @summary 隐藏痕迹
  683. * @example
  684. * this.form.get("fieldId").hideRevisions()
  685. */
  686. hideRevisions : async function (){
  687. await this.wpsOffice.ready();
  688. const app = this.wpsOffice.Application;
  689. // 获取节对象
  690. const View = await app.ActiveDocument.ActiveWindow.View;
  691. View.RevisionsView = 0;
  692. // 设置修订状态为 显示标记的最终状态
  693. View.ShowRevisionsAndComments = false;
  694. },
  695. /**
  696. * @summary 打印
  697. * @example
  698. * this.form.get("fieldId").print()
  699. */
  700. print : function (){
  701. this.wpsOffice.ready().then(function(){
  702. const app = this.wpsOffice.Application;
  703. // 页面定制对象:更多菜单
  704. app.CommandBars('TabPrintPreview').then(function(printMenu){
  705. printMenu.Execute();
  706. });
  707. }.bind(this));
  708. },
  709. /**
  710. * @summary 导出pdf
  711. * @example
  712. * this.form.get("fieldId").exportPDF()
  713. */
  714. exportPDF : async function (){
  715. await this.wpsOffice.ready();
  716. const app = this.wpsOffice.Application;
  717. var pdfUrl;
  718. switch (this.json.officeType){
  719. case "docx":
  720. pdfUrl = await app.ActiveDocument.ExportAsFixedFormat();
  721. break;
  722. case "xlsx":
  723. pdfUrl = await app.ActiveWorkbook.ExportAsFixedFormat()
  724. break;
  725. case "pptx":
  726. pdfUrl = await app.ActivePresentation.ExportAsFixedFormat()
  727. }
  728. window.open(pdfUrl.url);
  729. },
  730. /**
  731. * @summary 获取文档权限信息
  732. * @example
  733. * this.form.get("fieldId").getOperatorsInfo()
  734. */
  735. getOperatorsInfo : async function (){
  736. await this.wpsOffice.ready();
  737. const app = this.wpsOffice.Application;
  738. var operatorsInfo ;
  739. switch (this.json.officeType){
  740. case "docx":
  741. operatorsInfo = await app.ActiveDocument.GetOperatorsInfo();
  742. break;
  743. case "xlsx":
  744. operatorsInfo = await app.ActiveWorkbook.GetOperatorsInfo();
  745. break;
  746. case "pptx":
  747. operatorsInfo = await app.ActivePresentation.GetOperatorsInfo();
  748. }
  749. return operatorsInfo;
  750. },
  751. /**
  752. * @summary 获取内容控件个数
  753. * @return 返回是个Promise对象
  754. * @example
  755. * this.form.get("fieldId").getContentControlsCount()
  756. */
  757. getContentControlsCount : async function (){
  758. //获取内容控件
  759. await this.wpsOffice.ready();
  760. const app = this.wpsOffice.Application;
  761. // 内容控件对象
  762. const contentControls = await app.ActiveDocument.ContentControls;
  763. // 内容控件数量
  764. const count = await contentControls.Count;
  765. return count;
  766. },
  767. /**
  768. * @summary 获取内容控件文本
  769. * @example
  770. * this.form.get("fieldId").getContentControlText()
  771. */
  772. getContentControlText : async function (pos){
  773. await this.wpsOffice.ready();
  774. const app = this.wpsOffice.Application;
  775. const contentControls = await app.ActiveDocument.ContentControls;
  776. const contentControl = await contentControls.Item(pos);
  777. const range = await contentControl.Range;
  778. const text = range.Text;
  779. return text
  780. // range.Text = 'WebOffice'
  781. },
  782. /**
  783. * @summary 设置内容控件文本
  784. * @example
  785. * this.form.get("fieldId").setContentControlText()
  786. */
  787. setContentControlText : async function (pos,text){
  788. await this.wpsOffice.ready();
  789. const app = this.wpsOffice.Application;
  790. const contentControls = await app.ActiveDocument.ContentControls;
  791. const contentControl = await contentControls.Item(pos);
  792. const range = await contentControl.Range;
  793. range.Text = text;
  794. },
  795. /**
  796. * @summary 获取文档缩放
  797. * @example
  798. * this.form.get("fieldId").getZoom()
  799. */
  800. getZoom : async function (){
  801. await this.wpsOffice.ready();
  802. const app = this.wpsOffice.Application;
  803. var zoom;
  804. switch (this.json.officeType){
  805. case "docx":
  806. zoom = await app.ActiveDocument.ActiveWindow.View.Zoom.Percentage
  807. break;
  808. case "xlsx":
  809. zoom = await app.ActiveWorkbook.ActiveSheetView.Zoom
  810. break;
  811. case "pptx":
  812. zoom = await app.ActivePresentation.View.Zoom
  813. }
  814. return zoom;
  815. },
  816. /**
  817. * @summary 设置文档缩放
  818. * @example
  819. * this.form.get("fieldId").setZoom()
  820. */
  821. setZoom : async function (zoom){
  822. //缩放属性值在50%到 300%之间。
  823. await this.wpsOffice.ready();
  824. const app = this.wpsOffice.Application;
  825. switch (this.json.officeType){
  826. case "docx":
  827. app.ActiveDocument.ActiveWindow.View.Zoom.Percentage = zoom;
  828. break;
  829. case "xlsx":
  830. app.ActiveWorkbook.ActiveSheetView.Zoom = zoom;
  831. break;
  832. case "pptx":
  833. app.ActivePresentation.View.Zoom = zoom;
  834. }
  835. },
  836. /**
  837. * @summary 是否有评论
  838. * @example
  839. * this.form.get("fieldId").hasComments()
  840. */
  841. hasComments : async function (){
  842. await this.wpsOffice.ready();
  843. const app = this.wpsOffice.Application;
  844. var hasComments;
  845. switch (this.json.officeType){
  846. case "docx":
  847. hasComments = await app.ActiveDocument.HasComments();
  848. break;
  849. case "xlsx":
  850. hasComments = await app.ActiveWorkbook.HasComments();
  851. break;
  852. case "pptx":
  853. hasComments = await app.ActivePresentation.HasComments();
  854. }
  855. return hasComments;
  856. },
  857. /**
  858. * @summary 显示评论
  859. * @example
  860. * this.form.get("fieldId").showComments()
  861. */
  862. showComments : async function (){
  863. await this.wpsOffice.ready();
  864. const app = this.wpsOffice.Application;
  865. app.ActiveDocument.ActiveWindow.View.ShowComments = true;
  866. },
  867. /**
  868. * @summary 隐藏评论
  869. * @example
  870. * this.form.get("fieldId").hideComments()
  871. */
  872. hideComments : async function (){
  873. await this.wpsOffice.ready();
  874. const app = this.wpsOffice.Application;
  875. app.ActiveDocument.ActiveWindow.View.ShowComments = false;
  876. },
  877. /**
  878. * @summary 获取所有评论
  879. * @example
  880. * this.form.get("fieldId").hideComments()
  881. */
  882. getComments : async function (){
  883. await this.wpsOffice.ready();
  884. const app = this.wpsOffice.Application;
  885. const operatorsInfo = await app.ActiveDocument.GetComments({ Offset: 0, Limit: 2000 });
  886. return operatorsInfo;
  887. },
  888. /**
  889. * @summary 页面模式
  890. * @example
  891. * this.form.get("fieldId").showPageMode()
  892. */
  893. showPageMode : async function (){
  894. await this.wpsOffice.ready();
  895. const app = this.wpsOffice.Application;
  896. app.ActiveDocument.SwitchTypoMode(false);
  897. },
  898. /**
  899. * @summary 连页模式
  900. * @example
  901. * this.form.get("fieldId").showPageMode()
  902. */
  903. showUnionPageMode : async function (){
  904. await this.wpsOffice.ready();
  905. const app = this.wpsOffice.Application;
  906. app.ActiveDocument.SwitchTypoMode(true);
  907. },
  908. /**
  909. * @summary 显示连页模式下的文件名
  910. * @example
  911. * this.form.get("fieldId").showFileName()
  912. */
  913. showFileName : async function (){
  914. await this.wpsOffice.ready();
  915. const app = this.wpsOffice.Application;
  916. await app.ActiveDocument.SwitchTypoMode(true);
  917. await app.ActiveDocument.SwitchFileName(true);
  918. },
  919. /**
  920. * @summary 隐藏连页模式下的文件名
  921. * @example
  922. * this.form.get("fieldId").hideFileName()
  923. */
  924. hideFileName : async function (){
  925. await this.wpsOffice.ready();
  926. const app = this.wpsOffice.Application;
  927. await app.ActiveDocument.SwitchTypoMode(true);
  928. await app.ActiveDocument.SwitchFileName(false);
  929. },
  930. /**
  931. * @summary 显示导航目录
  932. * @example
  933. * this.form.get("fieldId").showDocumentMap()
  934. */
  935. showDocumentMap : async function (callback){
  936. await this.wpsOffice.ready();
  937. const app = this.wpsOffice.Application;
  938. await app.ActiveDocument.SwitchTypoMode(true);
  939. app.ActiveDocument.ActiveWindow.DocumentMap = true;
  940. },
  941. /**
  942. * @summary 隐藏导航目录
  943. * @example
  944. * this.form.get("fieldId").hideDocumentMap()
  945. */
  946. hideDocumentMap : async function (callback){
  947. await this.wpsOffice.ready();
  948. const app = this.wpsOffice.Application;
  949. await app.ActiveDocument.SwitchTypoMode(true);
  950. app.ActiveDocument.ActiveWindow.DocumentMap = false;
  951. },
  952. });