wps点保存为什么变成另存为(WPS加载项另存为本地)

案例需求:

实现wps的个性化另存为

  1. 在保存时执行一系列客开动作
  2. 对文件选择器的默认值进行修改Title 标题(下图TestPDF)FilterIndex 欲保存的文件类型(下图:文件类型(T))InitialFileName 默认文件名称(下图:文件名(N))

wps点保存为什么变成另存为(WPS加载项另存为本地)(1)

场景:
  • 适用于老版本OA助手无法另存为的情况
  • 自定义另存为弹窗
  • 实现文件格式的转换,可转为/html/docx/doc/dot/pdf/ofd等...
案例可能遇到的坑:

暂无

代码流程

1. 弹出文件选择器FileDialog(2) 2. 定义文件选择器相关属性 3. 识别传入的文件名、文件类型,获取文件选择器中的路径 4. 使用SaveAs2另存为文件至对应格式

代码实现

// 01. 更新func_tabcontrol.js文件中OnBtnSaveAsLocalFile函数 /** * 执行另存为本地文件操作 * @param {*} defaultType 另存为时指定默认后缀 docx/doc/wps/html/pdf等... 可空 * @param {*} defaultName 另存为时指定默认文件名 可指定路径 可空 * @param {*} title 另存为弹窗的标题Title 可空 * @param {*} doc 欲另存的文档 默认为当前文档 可空 */ function OnBtnSaveAsLocalFile(defaultType, defaultName, title, doc) { //初始化临时状态值 wps.PluginStorage.setItem(constStrEnum.OADocUserSave, false); wps.PluginStorage.setItem(constStrEnum.IsInCurrOADocSaveAs, false); if (!WPS_Enum.FileExtTypes) init_FileExtTypes(); //检测是否有文档正在处理 var l_doc = doc ? doc : wps.WpsApplication().ActiveDocument; if (!l_doc) { alert("WPS当前没有可操作文档!"); return; } // 设置WPS文档对话框 2 FileDialogType:=msoFileDialogSaveAs var l_ksoFileDialog = wps.WpsApplication().FileDialog(2); l_ksoFileDialog.Title = title ? title : "另存为"; l_ksoFileDialog.FilterIndex = defaultType ? WPS_Enum.FileExtTypes[defaultType] : WPS_Enum.FileExtTypes.docx; l_ksoFileDialog.InitialFileName = defaultName ? defaultName : l_doc.Name; //默认为文档名称 if (l_ksoFileDialog.Show() == -1) { // -1 代表确认按钮 wps.PluginStorage.setItem(constStrEnum.OADocUserSave, true); //设置保存为临时状态,在Save事件中避免OA禁止另存为对话框 wps.Application.ActiveDocument.SaveAs2(l_ksoFileDialog.SelectedItems.Item(1), returnFormatType(l_ksoFileDialog.SelectedItems.Item(1))) pSetNoneOADocFlag(l_doc); wps.PluginStorage.setItem(constStrEnum.OADocUserSave, false); wps.RibbonUI.Invalidate(); //刷新Ribbon的状态 }; } // 02. 在func_tabcontrol.js文件中添加init_FileExtTypes、returnFormatType函数 /** * init FileExtTypes * 初始化枚举 考虑到每台机器的Filters可能不同,在调用前先动态遍历一次 * @author Li Zuxian */ function init_FileExtTypes() { WPS_Enum.FileExtTypes = {} const filters = wps.WpsApplication().FileDialog(2).Filters; for (let i = 1; i <= filters.Count; i ) { let items = filters.Item(i) items = items.Extensions.matchAll(/\*\.([\w]*)/g) for (const item of items) { WPS_Enum.FileExtTypes[item[1]] = i } } } //返回文件对应的类型 function returnFormatType(newName) { var typeList = [{ type: "ofd", value: 102 }, { type: "dot", value: 1 }, { type: "txt", value: 2 }, { type: "rtf", value: 6 }, { type: "html", value: 8 }, { type: "mht", value: 9 }, { type: "htm", value: 10 }, { type: "xml", value: 11 }, { type: "docx", value: 12 }, { type: "docm", value: 13 }, { type: "dotx", value: 14 }, { type: "dotm", value: 15 }, { type: "doc", value: 16 }, { type: "pdf", value: 17 }, { type: "uot", value: 1 }, { type: "uof", value: 111 }] let splitArr = newName.split(".") let newType = splitArr[splitArr.length - 1]; let formatType = 12; typeList.map((item) => { if (item.type == newType) { formatType = item.value } }) return formatType; } //例: OnBtnSaveAsLocalFile('pdf', 'D:/Tools/123.pdf', 'TestPDF') OnBtnSaveAsLocalFile('html', 'D:/123.html') OnBtnSaveAsLocalFile()

wps点保存为什么变成另存为(WPS加载项另存为本地)(2)

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页