1入门
WPS window版本才支持JSA宏的功能。 可以自动化的操作文档中的一些内容。
参考文档:
WPS API 参考文档:https://open.wps.cn/previous/docs/client/wpsLoad
微软的Word API文档:Microsoft.Office.Interop.Word 命名空间 | Microsoft Learn
有问题,基本查看上面的两个文档,再加上百度搜索,就差不多了。
2 样例
function 选择所有图片()
{
var arr = [...ActiveDocument.InlineShapes];
// 关闭屏幕更新,提升执行效率
Application.ScreenUpdating = false;
ActiveDocument.DeleteAllEditableRanges(wdEditorEveryone);
arr.forEach(shape=>{
shape.Range.Editors.Add(wdEditorEveryone);
});
ActiveDocument.SelectAllEditableRanges(wdEditorEveryone);
ActiveDocument.DeleteAllEditableRanges(wdEditorEveryone);
// 开启屏幕更新
Application.ScreenUpdating = true;
Console.log('批量设置【图片】完成。');
MsgBox("选择完毕!");
}
function 选择所有表格()
{
var countT =0;
var i = 0;
countT = ActiveDocument.Tables.Count;//计算文档中一共包含的表格数量。
Application.ScreenUpdating = 0;
ActiveDocument.DeleteAllEditableRanges(wdEditorEveryone);
for(i=1;i<=countT;i++)
{
ActiveDocument.Tables.Item(i).Range.Editors.Add(wdEditorEveryone);
}
ActiveDocument.SelectAllEditableRanges(wdEditorEveryone);
ActiveDocument.DeleteAllEditableRanges(wdEditorEveryone);
Application.ScreenUpdating = 1;
MsgBox("表格完成批量调整,共计"+countT+"个!");
}
function 选中有底纹表的Cell(){
Application.ScreenUpdating = 0;
ActiveDocument.DeleteAllEditableRanges(wdEditorEveryone);
var countT =0;
var i = 0;
countT = ActiveDocument.Tables.Count;//计算文档中一共包含的表格数量。
console.log("处理到表格=" + 0 + ", 总共" + countT);
for(i=1;i<=countT;i++)
{
var tableObj = ActiveDocument.Tables.Item(i);
var rowNum = tableObj.Rows.Count;
var colNum = tableObj.Columns.Count;
// console.log("aaa" + rowNum + " " + colNum);
for(var j=1;j<=rowNum;j++){
for(var k=1; k<= colNum; k++){
try {
var cell = tableObj.Cell(j,k);
if(cell){
console.log(cell.Shading.Texture);
if(cell.Shading.BackgroundPatternColor != -16777216 || cell.Shading.Texture != 0){
cell.Range.Editors.Add(wdEditorEveryone);
}
}
} catch(e) {
// console.log("bbb");
}
}
}
if(i%2000 == 0){
console.log("处理到表格=" + i + ", 总共" + countT);
}
}
ActiveDocument.SelectAllEditableRanges(wdEditorEveryone);
ActiveDocument.DeleteAllEditableRanges(wdEditorEveryone);
Application.ScreenUpdating = 1;
MsgBox("选择完毕!");
}
/**
* 修改标题和正文样式
*/
function 修改标题和正文样式_标题1到标题5和正文(){
var countT = 0;
var paragraphT = 0;
var totalT = ActiveDocument.Paragraphs.Count;
// 循环获取段落信息
for(var i = 1; i <= totalT; i++){
paragraphT ++;
// 获取每个段落范围的内容
var paragraphObj = ActiveDocument.Paragraphs.Item(i);
// 获取范围样式
var styleName = paragraphObj.Range.Style;
// 如果样式返回结果为空,则继续循环下一个
if(!styleName) continue;
styleName = styleName.toString();
console.log(styleName);
// 过滤出标题段落
switch(styleName){
case '标题 1':
countT++;
editStyle1(paragraphObj);
break;
case '标题 2':
countT++;
editStyle2(paragraphObj);
break;
case '标题 3':
countT++;
editStyle3(paragraphObj);
break;
case '标题 4':
countT++;
editStyle4(paragraphObj);
break;
case '标题 5':
countT++;
editStyle5(paragraphObj);
break;
case '标题 6':
countT++;
editStyle5(paragraphObj);
break;
case '标题 7':
countT++;
editStyle5(paragraphObj);
break;
case '标题 8':
countT++;
editStyle5(paragraphObj);
break;
case '标题 9':
countT++;
editStyle5(paragraphObj);
break;
case '正文':
countT++;
editStyle(paragraphObj);
break;
case '文档正文':
countT++;
editStyle(paragraphObj);
break;
case '正文':
countT++;
editStyle(paragraphObj);
break;
case '标书正文':
countT++;
editStyle(paragraphObj);
break;
case '正文缩进':
countT++;
editStyle(paragraphObj);
break;
}
if(paragraphT%5000 == 0){
console.log("处理进度," + paragraphT + "个, 总共:"+ totalT)
}
}
MsgBox("正文修改完毕!"+countT+"个!被修改了");
}
/**
* 标题1 格式
*/
function editStyle1(paragraphObj){
// console.log('标题1-' + paragraphObj.Range);
// 设置字段样式 字体:小二宋体加粗;
(font => {
font.Blod = true;
font.Size = 18;
font.Name = "宋体";
font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
//设置标题段落的缩进
setBiaotiSJ(paragraphObj);
/*间距:段前段后:6磅;行距:1.5倍。*/
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 6;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 6;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
}
/**
* 标题2 格式
*/
function editStyle2(paragraphObj){
console.log('111' + paragraphObj.Range);
paragraphObj.Range.Select();
// 三号宋体加粗;
(font => {
font.Blod = true;
font.Size = 16;
font.Name = "宋体";
font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
/*段落的对齐方式 对齐方式:左对齐;*/
paragraphObj.Alignment = wdAlignParagraphLeft;
/*大纲级别:2级;*/
paragraphObj.OutlineLevel = wdOutlineLevel2;
//设置标题段落的缩进
setBiaotiSJ(paragraphObj);
// paragraphObj.CharacterUnitFirstLineIndent = 0;
// paragraphObj.IndentCharWidth(0);
/*间距:段前段后:6磅;行距:1.5倍。*/
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 6;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 6;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
}
/**
* 设置标题段落的缩进
*/
function setBiaotiSJ(paragraphObj){
/*左侧、右侧:0字符;特殊格式:首行缩进,2字符*/
/* 该属性返回或设置指定段落的左缩进量(以字符为单位) */
if(paragraphObj.LeftIndent != 0){
paragraphObj.LeftIndent = 0;
}
if(paragraphObj.CharacterUnitLeftIndent != 0){
paragraphObj.CharacterUnitLeftIndent = 0
}
//
/* 该属性返回或设置指定段落的右缩进量(以字符为单位) */
if(paragraphObj.RightIndent != 0){
paragraphObj.RightIndent = 0;
}
if(paragraphObj.CharacterUnitRightIndent != 0){
paragraphObj.CharacterUnitRightIndent = 0
}
/*设置首行缩进*/
if(paragraphObj.FirstLineIndent != 0){
paragraphObj.FirstLineIndent = 0;
}
if(paragraphObj.CharacterUnitFirstLineIndent != 0){
paragraphObj.CharacterUnitFirstLineIndent = 0;
}
if(paragraphObj.LeftIndent != 0){
paragraphObj.LeftIndent = 0;
}
if(paragraphObj.CharacterUnitLeftIndent != 0){
paragraphObj.CharacterUnitLeftIndent = 0
}
//
/* 该属性返回或设置指定段落的右缩进量(以字符为单位) */
if(paragraphObj.RightIndent != 0){
paragraphObj.RightIndent = 0;
}
if(paragraphObj.CharacterUnitRightIndent != 0){
paragraphObj.CharacterUnitRightIndent = 0
}
/*设置首行缩进*/
if(paragraphObj.FirstLineIndent != 0){
paragraphObj.FirstLineIndent = 0;
}
if(paragraphObj.CharacterUnitFirstLineIndent != 0){
paragraphObj.CharacterUnitFirstLineIndent = 0;
}
}
/**
* 标题3 格式
*/
function editStyle3(paragraphObj){
// console.log('111' + paragraphObj.Range);
// 小三宋体加粗;
(font => {
font.Blod = true;
font.Size = 15;
font.Name = "宋体";
font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
/*段落的对齐方式 对齐方式:左对齐;*/
paragraphObj.Alignment = wdAlignParagraphLeft;
/*大纲级别:3级;*/
paragraphObj.OutlineLevel = wdOutlineLevel3;
//设置标题段落的缩进
setBiaotiSJ(paragraphObj);
/*间距:段前段后:6磅;行距:1.5倍。*/
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 6;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 6;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
}
/**
* 标题4 格式
*/
function editStyle4(paragraphObj){
// console.log('111' + paragraphObj.Range);
// 四号宋体加粗;
(font => {
font.Blod = true;
font.Size = 14;
font.Name = "宋体";
font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
/*段落的对齐方式 对齐方式:左对齐;*/
paragraphObj.Alignment = wdAlignParagraphLeft;
/*大纲级别:4级;*/
paragraphObj.OutlineLevel = wdOutlineLevel4;
//设置标题段落的缩进
setBiaotiSJ(paragraphObj);
/*间距:段前段后:6磅;行距:1.5倍。*/
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 6;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 6;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
}
/**
* 标题5 格式
*/
function editStyle5(paragraphObj){
// console.log('111' + paragraphObj.Range);
// 小四宋体加粗;
(font => {
font.Blod = true;
font.Size = 12;
font.Name = "宋体";
font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
/*段落的对齐方式 对齐方式:左对齐;*/
paragraphObj.Alignment = wdAlignParagraphLeft;
/*大纲级别:5级;注释掉, 5、6、7、8、9 公用这个函数*/
// paragraphObj.OutlineLevel = wdOutlineLevel5;
//设置标题段落的缩进
setBiaotiSJ(paragraphObj);
/*间距:段前段后:6磅;行距:1.5倍。*/
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 6;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 6;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
}
/**
* 修改样式
*/
function editStyle(paragraphObj){
// console.log('333' + paragraphObj.Range);
// 中文:宋体 小四号, 西文:Times New Roman 小四号
(font => {
// font.Blod = false;
font.Size = 12;
font.Name = "宋体";
font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
/*段落的对齐方式 对齐方式:左对齐;*/
paragraphObj.Alignment = wdAlignParagraphLeft;
/*大纲级别:正文*/
paragraphObj.OutlineLevel = wdOutlineLevelBodyText;
/*左侧、右侧:0字符;特殊格式:首行缩进,2字符*/
/* 该属性返回或设置指定段落的左缩进量(以字符为单位) */
paragraphObj.LeftIndent = 0;
/* 该属性返回或设置指定段落的右缩进量(以字符为单位) */
paragraphObj.RightIndent = 0;
/*设置首行缩进*/
paragraphObj.CharacterUnitFirstLineIndent = 2;
/*段前段后:0行;行距:1.5倍;*/
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 0;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 0;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
}
function 所有表格上面添加表标题(){
var countT =0;
var i = 0;
countT = ActiveDocument.Tables.Count;//计算文档中一共包含的表格数量。
console.log("处理到表格=" + 0 + ", 总共" + countT);
var firstEmpty = false;
for(i=1;i<=countT;i++)
{
var tableObj = ActiveDocument.Tables.Item(i);
// tableObj.Range.InsertParagraphBefore()
// tableObj.Range.InsertBefore("Introduction")
var rangeObj = tableObj.Range;
var beforeRangeObj = rangeObj.Previous(wdParagraph, 1);
if (!beforeRangeObj) {
firstEmpty = true;
//beforeRangeObj = Application.ActiveDocument.Paragraphs.Add(ActiveDocument.Paragraphs.Item(1).Range).Range
beforeRangeObj = Application.ActiveDocument.Range(0, 0)
beforeRangeObj.InsertParagraph()
}
var paA = beforeRangeObj.Paragraphs.Item(1);
console.log("000-" + paA.Alignment + "-" +wdAlignParagraphCenter) ;
console.log(paA.Range.Text.length);
console.log("222=" + paA.Range.Text);
paA.Range.Select();
//如果上段有居中,且长度大于2 就表名不处理
if(paA.Alignment == wdAlignParagraphCenter && paA.Range.Text.length > 2){
console.log("44444");
} else {
console.log("33333");
// beforeRangeObj.Select();
// rangeObj.Collapse(wdCollapseStart)
// beforeRangeObj.InsertParagraphAfter()
beforeRangeObj.Collapse(wdCollapseEnd)
//网上移动一个字符的位置
beforeRangeObj.MoveEnd(wdCharacter, -1);
// beforeRangeObj.InsertParagraphAfter()
if(firstEmpty){
firstEmpty = false;
} else {
beforeRangeObj.InsertParagraph()
}
beforeRangeObj.InsertAfter("表 xxx");
}
var lastParagraph = beforeRangeObj.Paragraphs.Item(beforeRangeObj.Paragraphs.Count);
// lastParagraph.Range.Select();
changeStyleBBT(lastParagraph);
if(i%2000 == 0){
console.log("处理到表格=" + i + ", 总共" + countT);
}
}
MsgBox("选择完毕!");
}
/**
* 表标题样式设置
**/
function changeStyleBBT(paragraphObj){
/*段落的对齐方式 对齐方式:居中;*/
paragraphObj.Alignment = wdAlignParagraphCenter;
// 宋体 五号 加粗
(font => {
font.Blod = true;
font.Size = 10.5;//5号字
font.Name = "宋体";
// font.NameAscii = 'Times New Roman';
})(paragraphObj.Range.Font);
//段前段后:0行;行距:1.5行距;
(obj=>{
/*本示例将活动文档中所有段落的段前间距设置为 6 磅。*/
obj.SpaceBefore = 0;
/*本示例将活动文档中所有段落的段后间距设置为 6 磅。*/
obj.SpaceAfter = 0;
/*为指定段落设置 1.5 倍行距。*/
obj.LineSpacingRule = wdLineSpace1pt5;
})(paragraphObj.Range.ParagraphFormat);
changeStyleBBT_CJ(paragraphObj);
}
/**
* 表标题样式缩进
*/
function changeStyleBBT_CJ(paragraphObj){
/*左侧、右侧:0字符;特殊格式:首行缩进,2字符*/
/* 该属性返回或设置指定段落的左缩进量(以字符为单位) */
if(paragraphObj.LeftIndent != 0){
paragraphObj.LeftIndent = 0;
}
if(paragraphObj.CharacterUnitLeftIndent != 0){
paragraphObj.CharacterUnitLeftIndent = 0
}
//
/* 该属性返回或设置指定段落的右缩进量(以字符为单位) */
if(paragraphObj.RightIndent != 0){
paragraphObj.RightIndent = 0;
}
if(paragraphObj.CharacterUnitRightIndent != 0){
paragraphObj.CharacterUnitRightIndent = 0
}
/*设置首行缩进*/
if(paragraphObj.FirstLineIndent != 0){
paragraphObj.FirstLineIndent = 0;
}
if(paragraphObj.CharacterUnitFirstLineIndent != 0){
paragraphObj.CharacterUnitFirstLineIndent = 0;
}
if(paragraphObj.LeftIndent != 0){
paragraphObj.LeftIndent = 0;
}
if(paragraphObj.CharacterUnitLeftIndent != 0){
paragraphObj.CharacterUnitLeftIndent = 0
}
//
/* 该属性返回或设置指定段落的右缩进量(以字符为单位) */
if(paragraphObj.RightIndent != 0){
paragraphObj.RightIndent = 0;
}
if(paragraphObj.CharacterUnitRightIndent != 0){
paragraphObj.CharacterUnitRightIndent = 0
}
/*设置首行缩进*/
if(paragraphObj.FirstLineIndent != 0){
paragraphObj.FirstLineIndent = 0;
}
if(paragraphObj.CharacterUnitFirstLineIndent != 0){
paragraphObj.CharacterUnitFirstLineIndent = 0;
}
}
3 录制脚本
如果有些API搜索不到,可以通过录制脚本,看看WPS生成的脚本是怎么写的。