0.0.4 • Published 3 years ago

string2ooxml v0.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

string2ooxml

NodeJS环境下将字符串转为 Office Open XML,支持插入字符串文字,markdown 字符串片段


支持的功能

  • addSpecialText 将字符串转化为 ooxml,支持文字展示的基本样式设置
  • addMarkDown 将 markdown 片段转化为对应的 ooxml

安装

yarn add string2ooxml

or

npm install --save string2ooxml

快速上手

addSpecialText, addSpecialText 返回的是一个Promise函数

const { addSpecialText, addMarkDown } = require("string2ooxml");

// 转换文字
const textOoxml = addSpecialText({ text: "demo" });
textOoxml
  .then((resp) => {
    console.log("resp:%o", resp);
  })
  .catch((err) => {
    console.log("err:%o", err);
  });

/**
resp:'<w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:t>demo 是的</w:t></w:r></w:p>'
*/

// 转换markdown片段
const mdStr = `
# 你好
## 展示一个demo
`;
const mdOoxml = addMarkDown({ mdStr });
mdOoxml
  .then((resp) => {
    console.log("resp:%o", resp);
  })
  .catch((err) => {
    console.log("err:%o", err);
  });

/**
resp:'<w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" /><w:sz w:val="36"/><w:szCs w:val="36"/></w:rPr><w:t>你好</w:t></w:r></w:p><w:p w:rsidR="00A77427" w:rsidRDefault="007F1D13"><w:pPr><w:ind/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" /><w:sz w:val="32"/><w:szCs w:val="32"/></w:rPr><w:t>展示一个demo</w:t></w:r></w:p>'
*/

string2ooxml API

注意:返回的是一个Promise函数options是一个Object对象

const { addSpecialText, addMarkDown } = require("string2ooxml");

addSpecialText(options);
addMarkDown(options);

参数配置

addSpecialText options 参数

参数说明类型可选值默认值
text字符串stringany''
style字符串的样式Objectstyle option{}

style option

参数说明类型可选值
color字体颜色代码string例如:'ffffff'(白色)或 '000000'(黑色)
back背景颜色代码string例如:'ffffff'(白色)或 '000000'(黑色)
boldtrue 使文本加粗Booleantrueoffalse
border边框string'single'、'dashDotStroked'、'dashed'、'dashSmallGap'、'dotDash'、'dotDotDash'、'dotted'、'double'、'thick'等
italictrue 使文本斜体Booleantrueoffalse
underlinetrue 添加下划线Booleantrueoffalse
font_face要使用的字体string
font_sizept为单位的字体大小number
highlight突出显示string'black', 'blue', 'cyan', 'darkBlue', 'darkCyan', 'darkGray', 'darkGreen', 'darkMagenta', 'darkRed', 'darkYellow', 'green', 'lightGray', 'magenta', 'none', 'red', 'white' or 'yellow'.
strikethroughtrue 添加删除线Booleantrueoffalse
superscript上标true 文本降低到基线以下并将其更改为更小的尺寸Booleantrueoffalse
subscript下标true 文本提高到基线以上并将其更改为较小的尺寸Booleantrueoffalse

style option 其他更多参数, 详见officegen Create Microsoft Office Word Document Reference 文档

addMarkDown options 参数

注意:严重依赖换行空格

参数说明类型可选值默认值
mdStrmarkdown 片段stringany

markdown 目前支持的语法有

# 标题一*em*
## 标题二
### 标题三
#### 标题四
##### 标题五
###### 标题六
####### 文字
_em_
adssad**strong**fdsfdsg
**strong**

--- // 分割线 word 不兼容

// 列表类 wps 兼容有问题
- sadsa
- dfsaaf
- dsaf

1. 大萨达
2. 重复的开始搞
3. 鼓风机宽度是否

// 引用类 wps 兼容有问题
> react

// 链接类 wps 兼容有问题、
[百度](https://www.baidu.com)

案例

docxtemplater的支持插入xml功能结合,插入特殊字体或者markdown片段

设置数据不要使用 doc.setData(data)啦哈,直接使用doc.render(data)

docxtemplaterstring2ooxml结合要做一点小小的改动 修改 doc.render(data)为doc.renderAsync,其他不需要做任何变动

// 因为string2ooxml内部是异步处理,所以要将docxtemplater的render改为异步
doc.renderAsync({
  ...data,
  customText: addSpecialText({ text: "demo" }),
  customMd: addMarkDown({ mdstr: "# demo" }),
});

模板 docx 中使用

{@customText}
{@customMd}

开发和调试

  • npm install
  • npm link
  • 然后到你测试的项目中执行 npm link string2ooxml
  • 引入并使用