office这个体系分为四个大教程
2、【连接器(connector)或者jsApi调用操作office】-进阶
3、【document builder文档构造器使用】-进阶
如果需要连接器,可以查看【镜像需要付费购买嘿嘿】:onlyofficeV7.5.1 jsApi调用 进阶开发 二次开发 连接器(connector)开发 - 知乎 (zhihu.com)
一、首先部署基础环境
比如:
http://47.94.91.67:10100/web-apps/apps/api/documents/api.js
二、Document Builder 文档生成器介绍
文档生成器我们可以理解为【后端】操作,类似于java的poi、easyExcel等等,由代码级别操作office文档。
只是说onlyoffice的生成效果可能会比poi等框架好一些,这个需要开发者自行实验查看效果。
Api文档:ONLYOFFICE Api Documentation - GetRangeBySelect
文档目录说明:
图1
三、PostMan效果测试
图2
四、服务URL说明
官方是这样写的:https://documentserver/docbuilder
也就是说,http://office部署地址/docbuilder,比如:http://47.94.91.67:10100/docbuilder,实际上就是第一步中提到的基础环境部署地址了。
五、参数展示
参数说明:ONLYOFFICE Api Documentation - Web Document Builder API
比如,目前的请求url为:http://47.94.91.67:10100/docbuilder,需要使用post方法的json格式进行参数传递。参照【图2】
{
"async": false,是否异步非常重要,将决定获取生成文件的逻辑
"url": "http://192.168.71.162:8081/caorui_7.3.3/demo/doc_builder/demo.docbuilder"
}
首先说一下【url】,这个就是生成office的脚本文件了,比如:demo.docbuilder,官方说的是需要使用【.docbuilder】后缀,但是我测试后发现用【.txt】也可以,对于文件格式的需求,就由各位开发者自行测试了。
或者,直接使用【.docbuilder】更方便些。
其次说一下【async】,是否开启异步决定了获取生成好文件的方法不同,其实所有异步的本质都一样:
async:false,直接就返回生成好的文件了【锚点1】。
async:true,需要个key做标识,然后不断通过key轮询获取文件的生成情况【锚点2】。
【锚点1】当 async:false时:直接等待就可以获取文件了。
请求参数
{
"async": false,
"url": "http://192.168.71.162:8081/caorui_7.3.3/demo/doc_builder/demo.docbuilder"
}
返回参数
{
"key": "bld_c9f97878f0879467",
"urls": {
"demo.docx": "http://192.168.70.10:10100/cache/files/data/bld_c9f97878f0879467/output/demo.docx/demo.docx?md5=9SKQIWKqhH5em6qA1qVAXQ&expires=1708399869&filename=demo.docx"
},
"end": true
}
【锚点2】当async:true时:会返回当前【async task】的key,并且返回【end】状态,接下来就是不断通过key去轮询任务了。
请求参数
{
"async": true,
"url": "http://192.168.71.162:8081/caorui_7.3.3/demo/doc_builder/demo.docbuilder"
}
返回参数
{
"key": "bld_1f713a5ce29e5a65",
"end": false
}
--------------------------------------------------
第一次得到key以后,重新使用key发送请求
{
"key": "bld_1f713a5ce29e5a65",
"end": false
}
直到监听end:true
{
"key": "bld_1f713a5ce29e5a65",
"urls": {
"demo.docx": "http://192.168.70.10:10100/cache/files/data/bld_1f713a5ce29e5a65/output/demo.docx/demo.docx?md5=z7ue3-McCrvlWRgxSvxClQ&expires=1708400013&filename=demo.docx"
},
"end": true
}
六、demo.docbuilder脚本测试
builder.CreateFile("docx");
var oDocument = Api.GetDocument();
var oHeading1Style = oDocument.CreateStyle("Heading 1", "paragraph");
var oParaPr = oHeading1Style.GetParaPr();
oParaPr.SetKeepNext(true);
oParaPr.SetKeepLines(true);
oParaPr.SetSpacingAfter(240);
var oTextPr = oHeading1Style.GetTextPr();
oTextPr.SetColor(0xff, 0x68, 0x00, false);
oTextPr.SetFontSize(70);
oTextPr.SetFontFamily("Calibri Light");
var oParagraph = oDocument.GetElement(0);
oParagraph.SetStyle(oHeading1Style);
oParagraph.AddText("Weekly Report | QTR 3 Week 12");
var oDrawing =Api.CreateImage("https://api.onlyoffice.com/content/img/docbuilder/examples/api-word.jpg", 220 * 36000, 350 * 36000);
oDrawing.SetWrappingStyle("behind");
oDrawing.SetHorAlign("page", "left");
oDrawing.SetVerAlign("page", "top");
oParagraph.AddDrawing(oDrawing);
oParagraph = Api.CreateParagraph();
var oChart = Api.CreateChart("bar3D", [ [200, 140, 180, 160, 240, 240], [250, 190, 200, 180, 260, 260] ], ["Projected Revenue", "Estimated Costs"], ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], 80 * 36000, 55 * 36000, 24);
var oFill = Api.CreateSolidFill(Api.CreateRGBColor(51, 51, 51));
oChart.SetSeriesFill(oFill, 0, false);
oFill = Api.CreateSolidFill(Api.CreateRGBColor(255, 111, 61));
oChart.SetSeriesFill(oFill, 1, false);
oChart.SetVerAxisTitle("USD In Thousands", 10);
oChart.SetLegendPos("bottom");
oChart.SetShowDataLabels(false, false, true, false);
oChart.SetTitle("Financial Overview", 13);
oChart.SetDistances(5 * 36000, 5 * 36000, 5 * 36000, 36000);
oChart.SetWrappingStyle("square");
oChart.SetHorAlign("page", "left");
oChart.SetHorPosition("leftMargin", 30 * 36000);
oParagraph.AddDrawing(oChart);
oParagraph.AddText("Regardless of your sector or industry, it’s likely that your finances department is the beating heart of your entire operation. Without financial fluency, it’s difficult for an organization to thrive, which means that keeping your monetary affairs in order is essential.");
oDocument.Push(oParagraph);
oParagraph = Api.CreateParagraph();
oParagraph.AddText("As a business, you need the reliability of frequent financial reports to better understand the state of your finances, both current and future. Financial reports give you the ability to take a proactive approach to managing your company's economy and improve long-term profitability with short-term company financial reports.");
oDocument.Push(oParagraph);
oParagraph = Api.CreateParagraph();
oParagraph.AddText("A robust finance report communicates crucial accounting information that covers a specified period through daily, weekly, and monthly financial reports. These are powerful tools that you can apply to increase internal business performance. A data-driven finance report is also an effective means of remaining updated with any significant progress or changes in the status of your finances and helps you measure your results, cash flow, and financial position.");
oDocument.Push(oParagraph);
builder.SaveFile("docx", "demo.docx");
builder.CloseFile();