支持打印出的图表与页面展现完全一致;
支持零编码定义“中国式”复杂报表;
支持各种复杂样式、基本计算的报表定义;
实现各项表单“所见即所得”的打印模式,打印出的表单布局美观、内容清晰。
方案一,用代码把每个模板实现一遍;
方案二,线下制作好模板,代码中替换预设好的占位符;
方案三,通过类似“iReport"工具制作不同"JasperReport"模板;
方案四,采用cs端工具辅助实现制作模板。
这些实现方法存在的弊端有:
4 我们需要什么?
我们需要实现的是:
能可视化展示表单的数据集;
能在web页面上制作固定打印内容;
能灵活配置打印输出变量;
能对打印内容做精细化样式调整所见即所得;
能预览打印效果;
yaml
field_plugin
- config.json # 配置文件
- icon.png # 图标
- field.js # 插件逻辑代码
- index.html # 插件布局及入口
- styles.css # 插件样式表
filed.js
javascript
(function (window, undefined) {
window.Asc.plugin.init = function () {
// 插件加载完后的初始化回调,相关逻辑代码编写在此
};
window.Asc.plugin.button = function (id) {
// 插件按钮点击回调,此处关闭插件
this.executeCommand("close", "");
};
})(window, undefined);
6.2 表单字段可视化展示
在插件实现过程中,难点是如何将外部表单数据与onlyoffice编辑器通讯,因为这两块都是各自独立的,经过我们实验,用模板文件名传递标识,能有效的解决这个问题。
javascript
var templateId = window.Asc.plugin.info.documentTitle.split(".")[0];
onlyoffice编辑器内部拿到最关键的标识id后,就能方便的请求后台数据,经过格式化就能以树形结构展示了。
javascript
$.get("http://server/establish-server/free/printingTemplate/" + templateId + "/dataSource", function(res) {
if (res && !res.hasError) {
zNodesRaw.push(filterDataSource(res.result.mainTable));
if (res.result.childTables.length > 0) {
res.result.childTables.forEach(item => {
zNodesRaw.push(filterDataSource(item));
});
}
if (systemFields.hasOwnProperty(res.result.pageType) && systemFields[res.result.pageType]) {
zNodesRaw.push(systemFields[res.result.pageType]);
}
zNodes = JSON.parse(JSON.stringify(zNodesRaw));
$.fn.zTree.init(treeObj, setting, zNodes);
}
});
javascript
function insertString(field) {
window.Asc.plugin.callCommand(function () {
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(field);
oDocument.InsertContent([oParagraph], true);
}, false);
}
function insertString(field) {
// callCommand执行上下文与当前环境不一致,需通过Asc.scope对象传递参数
Asc.scope.field = field;
window.Asc.plugin.callCommand(function () {
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(Asc.scope.field);
oDocument.InsertContent([oParagraph], true);
}, false);
}
function insertString(field) {
var editorType = window.Asc.plugin.info.editorType;
// callCommand执行上下文与当前环境不一致,需通过Asc.scope对象传递参数
Asc.scope.field = field;
if (editorType === "word") {
window.Asc.plugin.callCommand(function () {
var oDocument = Api.GetDocument();
var oParagraph = Api.CreateParagraph();
oParagraph.AddText(Asc.scope.field);
oDocument.InsertContent([oParagraph], true);
}, false);
} else if (editorType === "cell") {
window.Asc.plugin.callCommand(function () {
var oWorksheet = Api.GetActiveSheet();
var oCell = oWorksheet.GetActiveCell();
var value = oCell.GetValue();
oCell.SetValue(value + Asc.scope.field);
}, false);
}
}
实现看似简单的一个功能,其背后所需要的努力,只有亲历者才最清楚。其中遇到的种种问题与仔细思考后解决问题的过程,不仅是经验的积累,也是开发者的乐趣。
End