金山在线文档编辑器

2024-09-07 05:20
文章标签 文档 编辑器 在线 金山

本文主要是介绍金山在线文档编辑器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官方文档地址:快速开始-WebOffice 知识库

首先按照文档写的方式将包引入项目了

util.js

import WebOfficeSDK from "../../public/JSEditor/open-jssdk-v0.0.13.umd"
export function WordSDK(url, isEdit, mountDom, isShowTopArea, isShowHeader) {const el = document.querySelector(mountDom)if (!el) return nullconst instance = WebOfficeSDK.config({url,mount: el,commandBars: [{cmbId: 'HeaderMiddle', // 组件 IDattributes: {enable: isEdit, // 禁用组件(组件显示但不响应点击事件)},},],wordOptions: {enableReadOnlyComment: isEdit,isBestScale: false, // 打开文档时,默认以最佳比例显示},commonOptions: {isShowTopArea, // 隐藏顶部区域(头部和工具栏)isShowHeader, // 隐藏头部区域// isBrowserViewFullscreen: false, // 是否在浏览器区域全屏// isIframeViewFullscreen: false, // 是否在 iframe 区域内全屏// acceptVisualViewportResizeEvent: true, // 控制 WebOffice 是否接受外部的 VisualViewport},});instance.setToken({// token: `bearer ${getToken('token')}`,token: '',timeout: 10 * 60 * 1000,});return instance;
}

封装编辑器组件:KSEditor.vue

<template><div ref="editorContainer" class="editor-container"></div>
</template><script>import {WordSDK} from "@/utils/util"export default {name: 'JsEditorComponent',props: {KSfile: {type: String,default: ''},isShowHeader: {type: Boolean,default: true},isShowTopArea: {type: Boolean,default: true},isEditable: {type: Boolean,default: true}},data() {return {editorInstance: null,ksConfig: {// 在这里配置金山在线文档编辑器的初始化参数// 比如 token、文档ID 等}};},mounted() {// 在组件加载完成后初始化编辑器this.initEditor()},methods: {initEditor() {let KSInstance = WordSDK(this.KSfile,this.isEditable,'.editor-container',this.isShowTopArea,this.isShowHeader,)this.$emit('setInstance', KSInstance)},createDocument() {// 在点击按钮时触发新建文档操作// 假设SDK提供了相应的方法来新建文档if (this.editor) {this.editor.createDocument(); // 假设SDK方法名为createDocument} else {console.error('编辑器尚未初始化');}}}};
</script><style scoped>/* 可能需要一些样式来控制编辑器容器的宽度和高度 */.editor-container {width: 100%;height: 600px;}
</style>

页面testKSEditor.vue

<template><div style="width: 100%"><el-uploadclass="compare-uploader"ref="upload":with-credentials="true":show-file-list="false"withCredentials:http-request="httpRequest":on-progress="onProgress":multiple="false"><!-- 文件上传--><el-buttontype="ghost"slot="trigger"size="mini">导入文件</el-button></el-upload>id:<el-input v-model="ksFileInfoCopy.id"/><br/>文件类型:<el-input v-model="ksFileInfoCopy.ext"/><br/>文件名称:<el-input v-model="ksFileInfoCopy.name"/><br/><el-button@click="()=>getFileUrl(this.ksFileInfoCopy.id, this.ksFileInfoCopy.ext)">刷新</el-button><el-button@click="()=>destroyKS()">销毁</el-button><el-button@click="()=>insertBookmark('TEXT')">插入书签</el-button><el-button@click="()=>insertBookmark('TABLE')">插入表格类型书签</el-button><el-button@click="() => this.bookmarks = []">清空</el-button><el-button@click="()=>changeMode()">{{mode === 'edit'?'预览':'编辑'}}</el-button><el-button@click="()=>replaceBookmarkValue()">替换书签值</el-button><JSEditorv-if="showKS"@setInstance="setInstance":KSfile="KSfile":isShowTopArea="isShowTopArea":isShowHeader="isShowHeader":isEditable="isEditableFile"/></div>
</template><script>import axios from "axios";import JSEditor from 'lib@/components/KSEditor'import {userTableMap} from "@/modules/testKSeditor/fieldMap";const userTable = userTableMapexport default {name: "index",components: {JSEditor},data () {// supplementRules:return {userId: this.$store.getters.userId,fileLoading: false,app: null,bookmark: null,KSfile: '',KSurl: '',showKS: false,isShowHeader: true,isShowTopArea: true,isEditableFile: true,ksFileInfo: {ext: 'docx',id: '0af2cc2e1da145f48667a6bd09d30e77',// id: '6170f21b9fe94db78bc2e56d8f543c1e',name: '工治具报修管理.docx',url: ''},ksFileInfoCopy: {ext: 'docx',id: '0af2cc2e1da145f48667a6bd09d30e77',// id: '6170f21b9fe94db78bc2e56d8f543c1e',name: '工治具报修管理.docx',url: ''},tableData: [{number: '1',name: 'yy',sex: 'b',age: '11'},{number: '2',name: 'ee',sex: 'g',age: '22'},{number: '3',name: 'ss',sex: 'b',age: '33'},{index: '4',name: 'ff',sex: 'g',age: '44'},],mode: 'edit',bookmarks: [],KSInstance: null}},methods: {async setInstance(instance){// console.log(instance, '1111')this.KSInstance = instanceawait instance.ready()if(this.mode === 'preview'){const app = instance.Applicationawait app.ActiveDocument.SetReadOnly({Value: true})}// console.log(app, 'this.KSInstance.Application')},httpRequest(fileObj) {const formData = new FormData();formData.append("file", fileObj.file);axios({url: 'https://sdap-dev.sunwoda.com/api/platform-oss/public/upload?bucket=sdap',method: "post",data: formData,}).then(res => {this.ksFileInfo = res.data.datasthis.ksFileInfoCopy = res.data.datasthis.getFileUrl(res.data.datas.id, res.data.datas.ext)}).catch(error => {console.log(error);});},getFileUrl(fileId, ext, isPreview) {this.showKS = falsethis.fileLoading = trueaxios({url: 'https://sdap-dev.sunwoda.com/api/platform-system/public/wps-api/edit-url',method: "get",params: { fileId, ext, previewMode: 'high_definition' }}).then(res => {this.fileLoading = falsethis.KSfile = res.data.datasthis.KSurl = res.data.datasthis.ksFileInfoCopy.url = isPreview ? this.ksFileInfo.url : res.data.datasthis.ksFileInfo.url = res.data.datasthis.showKS = true}).catch(error => {console.log(error);});},destroyKS(){this.KSInstance.destroy()},async insertBookmark(type){const bookmark = await this.KSInstance.Application.ActiveDocument.Bookmarks;const selection = await this.KSInstance.Application.ActiveDocument.ActiveWindow.Selection// 区域对象let mark = 'mark' + Date.now()const range = await selection.Rangeconst start = await range.Startlet content = type + '书签'+ markconst end = start + content.length// 在选区后面插入内容await selection.InsertAfter(content)await bookmark.Add({Name: mark,Range: {Start: start,End: end}})this.bookmarks.push({markName: mark,type: type,value: mark + '的值'})},changeMode(){if(this.mode === 'edit'){this.mode = 'preview'this.isShowHeader = falsethis.isShowTopArea = falsethis.isEditableFile = falselet replaceData = []let modelLines = this.bookmarks // 合同元素相关信息// let allMarks = await this.KSInstance.Application.ActiveDocument.Bookmarks.Json()for (const i of modelLines) {replaceData.push({bookmark: i.markName,type: i.type,text: i.value})}let template_url = 'https://sdap-dev.sunwoda.com/api/platform-oss/public/download/' + this.ksFileInfo.idlet data = {task_id: this.userId,template_url: template_url,template_filename: this.ksFileInfo.name,use_template_section_property:true,flatten_source_style: true,sample_list: replaceData}axios({url: 'https://sdap-dev.sunwoda.com/api/platform-system/public/wps-api/wrap-header',method: 'post',data}).then(res => {if(res.data.resp_code === 200){this.getReviewFile(res.data.datas.download_id)}})} else {this.mode = 'edit'this.ksFileInfoCopy = this.ksFileInfothis.isShowHeader = truethis.isShowTopArea = truethis.isEditableFile = truethis.getFileUrl(this.ksFileInfo.id, this.ksFileInfo.ext)}},getReviewFile(id){let data = {bucket: 'sdap',downloadId: id,fileName: id + '测试文档.docx'}axios({url: ' https://sdap-dev.sunwoda.com/api/platform-system/public/wps-api/upload-file',method: 'post',params: data}).then(res => {this.ksFileInfoCopy = res.data.datasthis.getFileUrl(res.data.datas.id, res.data.datas.ext, true)})},async replaceBookmarkValue(){let modelLines = this.bookmarks // 合同元素相关信息let allMarks = await this.KSInstance.Application.ActiveDocument.Bookmarks.Json()let replaceData = []for (const i of modelLines) {if (i.type === 'TABLE') {let tableData = JSON.parse(JSON.stringify(this.tableData))let header = ["序号", "姓名", "性别", "年龄"]await this.insertTable(header, tableData, userTable, i)} else {replaceData.push({name: i.markName,type: 'text',value: i.value})}}this.KSInstance.Application.ActiveDocument.Bookmarks.ReplaceBookmark(replaceData)},async insertTable(tableHead, tableBodyData, fieldMap, eleInfo){const tables = await this.KSInstance.Application.ActiveDocument.Tables // 编辑器的表格对象let tableData = JSON.parse(JSON.stringify(tableBodyData))tableData.map((i, ind) => {i.number = ind + 1 + ''})let headerData = {}let fields = tableHead.map((elmKey, index) => {if (!!elmKey) {headerData[fieldMap.get(elmKey)] = elmKeyreturn {name: elmKey,field: fieldMap.get(elmKey)}}})tableData.unshift(headerData)let rowCount = tableData.length, colCount = tableHead.length// 插入表格await tables.Add(this.KSInstance.Application.ActiveDocument.Bookmarks.Item(eleInfo.markName).Range, // 位置信息rowCount, // 新增表格的行数colCount, // 新增表格的列数1, // 启用自动调整功能1 // 根据表格中包含的内容自动调整表格的大小)const count = await tables.Count  // 表格数量const curTable = await tables.Item(count)console.log(count, 'count')// console.log(tableData, 'tableData')// console.log(headerData, 'headerData')// console.log(fields, 'fields')for (let j = 1; j < rowCount + 1; j++) { // 遍历行let row = tableData[j-1]for (let f = 1; f < colCount + 1; f++) {  // 遍历列let fieldsInfo = fields[f-1]// console.log(fieldsInfo, 'fieldsInfo')let field = row[fieldsInfo.field] || ''const cell = await curTable.Rows.Item(j).Cells.Item(f).Rangecell.Text = field + ''}}this.KSInstance.Application.ActiveDocument.ReplaceText([ // 将书签的内容替换成空字符串{key: 'TABLE书签' + eleInfo.markName,value: ''}])}}}
</script><style scoped></style>
fieldMap.js
export const userTableMap = new Map();
userTableMap.set('序号', "number");
userTableMap.set('姓名', "name");
userTableMap.set("性别", "sex");
userTableMap.set("年龄", "age");

前端代码实现书签替换页面效果:

两张截图是分开截的,效果差不多是这样。是有bug的,给表格赋值不太稳定。书签替换也不稳定,没截到连续的图就是因为这个,老有问题,不想截了,不稳定的原因不明。

还可以调用在线编辑的接口实现书签替换,这种方式不支持替换成表格,但支持替换成在线文档,就是可以建一个在线文档,里面加表格,然后将这个在线文档引入进来。我用的接口是我们的后端封装过的,具体的找金山的询问,毕竟要用这个肯定得买,他们肯定会派人支持。

调用接口的方式,可以在“编辑”“预览”之前多次跳转,因为这个方法替换书签不是在原文档上修改,是后台会新建一个文件来替换书签,并且这个新文档会缓存24小时,在这时间内可以下载。

这篇关于金山在线文档编辑器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144170

相关文章

Python Pillow 库详解文档(最新推荐)

《PythonPillow库详解文档(最新推荐)》Pillow是Python中最流行的图像处理库,它是PythonImagingLibrary(PIL)的现代分支和继承者,本文给大家介绍Pytho... 目录python Pillow 库详解文档简介安装核心模块架构Image 模块 - 核心图像处理基本导入

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Maven项目中集成数据库文档生成工具的操作步骤

《Maven项目中集成数据库文档生成工具的操作步骤》在Maven项目中,可以通过集成数据库文档生成工具来自动生成数据库文档,本文为大家整理了使用screw-maven-plugin(推荐)的完... 目录1. 添加插件配置到 pom.XML2. 配置数据库信息3. 执行生成命令4. 高级配置选项5. 注意事

Python使用python-docx实现自动化处理Word文档

《Python使用python-docx实现自动化处理Word文档》这篇文章主要为大家展示了Python如何通过代码实现段落样式复制,HTML表格转Word表格以及动态生成可定制化模板的功能,感兴趣的... 目录一、引言二、核心功能模块解析1. 段落样式与图片复制2. html表格转Word表格3. 模板生

Python+wxPython构建图像编辑器

《Python+wxPython构建图像编辑器》图像编辑应用是学习GUI编程和图像处理的绝佳项目,本教程中,我们将使用wxPython,一个跨平台的PythonGUI工具包,构建一个简单的... 目录引言环境设置创建主窗口加载和显示图像实现绘制工具矩形绘制箭头绘制文字绘制临时绘制处理缩放和旋转缩放旋转保存编

浅谈Redis Key 命名规范文档

《浅谈RedisKey命名规范文档》本文介绍了Redis键名命名规范,包括命名格式、具体规范、数据类型扩展命名、时间敏感型键名、规范总结以及实际应用示例,感兴趣的可以了解一下... 目录1. 命名格式格式模板:示例:2. 具体规范2.1 小写命名2.2 使用冒号分隔层级2.3 标识符命名3. 数据类型扩展命

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取