func-tbook-export.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. const { replaceDocx } = require("../../lib/docs");
  2. const Parse = global.Parse;
  3. const path = require("path")
  4. const TemplateDocxPath = path.join(__dirname,"template/模板-推荐申报表.docx")
  5. /**
  6. * 导出流程教材申报文件
  7. */
  8. async function exportProcessReportDocs(processId) {
  9. let query = new Parse.Query("EduTextbook")
  10. query.equalTo("eduProcess",processId);
  11. let textbookList = await query.find();
  12. for (let index = 0; index < textbookList.length; index++) {
  13. let textbook = textbookList[index];
  14. renderReportDocsByTextbook(textbook)
  15. }
  16. console.log(textbookList);
  17. }
  18. module.exports.exportProcessReportDocs = exportProcessReportDocs
  19. async function renderReportDocsByTextbook(textbook){
  20. let json = textbook.toJSON();
  21. let bookData = {
  22. title:padString(json?.title,21),
  23. ISBN:padString(json?.ISBN,21),
  24. }
  25. console.log(bookData)
  26. let bookid = json.code || json?.objectId;
  27. let tempFileName = path.join(`${bookid}${json.title}.docx`)
  28. replaceDocx(TemplateDocxPath,tempFileName,bookData)
  29. }
  30. function padString(str,width) {
  31. width = width || 21 // 目标宽度为21个单位
  32. spaceChar = "&#160;" // 占位符
  33. // 计算字符串的宽度
  34. let strWidth = 0;
  35. for (let char of str) {
  36. // 判断字符是否为中文
  37. if (char.match(/[\u4e00-\u9fa5]/)) {
  38. strWidth += 4; // 中文字符占4个单位
  39. } else {
  40. strWidth += 1; // 英文字符占1个单位
  41. }
  42. }
  43. const totalPadding = width - strWidth;
  44. // 如果已经达到或超过目标宽度,直接返回原字符串
  45. if (totalPadding <= 0) {
  46. return str;
  47. }
  48. // 计算左右两侧的空格数
  49. const leftPadding = Math.floor(totalPadding / 2) * 3;
  50. const rightPadding = Math.ceil(totalPadding / 2) * 3;
  51. // 生成填充空格的字符串
  52. const leftSpaces = spaceChar.repeat(leftPadding);
  53. const rightSpaces = spaceChar.repeat(rightPadding);
  54. // 返回补充后的字符串
  55. return leftSpaces + str + rightSpaces;
  56. }