WPS加载项为当前节插入页码

案例需求:

在文档的当前节左边插入页码,当前节页面横向,页码编码需要随前节,且奇偶页要不同

案例可能遇到的坑:

1. 首页不同,每一个节都会有首页不同属性,需要确保当前节的首页不同是否有设置;
2. 当前节原有页码影响,如果原来有页码,那么会出现无法添加页码成功,所以需要清空当前节页码;
3. shape的宽度和高度以及页面中的位置,宽高不够可能会出现页码显示不全;

代码流程

1. 获取并进入当前节
2. 清空当前节页码
3. 设置前后节页眉页脚不同前节
4. 设置首页不同和页面横向,页面宽高及页边距也可以在这里设置
5. 为当前节插入页码,并调整页码位置
6. 设置奇偶页不同
7. 为偶数页插入页码,并调整页码位置
8. 设置页码随前节

WPS加载项为当前节插入页码

代码实现


/**
 * g2055 Macro
 * 宏由 jsdkkj 录制,时间: 2022/06/06
 */
function 横排表()
{
	var selectionIndex=Selection.Sections.Item(1).Index;
	ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader;//进入当前页页眉
	var footer=ActiveDocument.Sections.Item(selectionIndex).Footers.Item(1)
	footer.Range.Text=""
	var footer1=ActiveDocument.Sections.Item(selectionIndex).Footers.Item(3);
	footer1.Range.Text=""
	if(selectionIndex!=1){//设置当前节不同前节
		Selection.HeaderFooter.LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex).Footers.Item(1).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex).Headers.Item(2).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex).Footers.Item(2).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex).Headers.Item(3).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex).Footers.Item(3).LinkToPrevious=false;
	}
	if(selectionIndex!=ActiveDocument.Sections.Count){//设置下一节不同前节
		ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument;
		Selection.GoTo(0,1,selectionIndex+1);//光标移动到下一节
		ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader;
		Selection.HeaderFooter?Selection.HeaderFooter.LinkToPrevious=false:"";
		ActiveDocument.Sections.Item(selectionIndex+1).Headers.Item(1).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex+1).Footers.Item(1).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex+1).Headers.Item(2).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex+1).Footers.Item(2).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex+1).Headers.Item(3).LinkToPrevious=false;
		ActiveDocument.Sections.Item(selectionIndex+1).Footers.Item(3).LinkToPrevious=false;
		ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument;
		Selection.GoTo(0,1,selectionIndex);//光标回到当前节
		ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader;
		
	}
	ActiveDocument.Sections.Item(selectionIndex).PageSetup.DifferentFirstPageHeaderFooter=0;//当前节首页不同
	ActiveDocument.Sections.Item(selectionIndex).PageSetup.Orientation = wdOrientLandscape;//页面横向
	
	//为当前节奇数页添加页码
	var shape=ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 92.150002, 774.299988, 78.750000, 32.250000, ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).Range);
	shape.TextFrame.Orientation = msoTextOrientationVertical;
	ActiveWindow.ActivePane.Activate();
	shape.Select();
	(obj=>{
		obj.Range.LanguageID = wdSimplifiedChinese;
		obj.Fields.Add(Selection.Range, wdFieldPage, "", true);
	})(Selection);
        //页码前后加横线
	Selection.MoveLeft(wdCharacter, 1, wdMove);
	Selection.Font.Name = "宋体";
	Selection.Font.Size = 10.500000;
	Selection.TypeText("— ");
	Selection.MoveRight(wdCharacter, 1, wdMove);
	Selection.Font.Name = "宋体";
	Selection.Font.Size = 10.500000;
	Selection.TypeText(" —");
	Selection.ParagraphFormat.Alignment=1;
	Selection.ParagraphFormat.FirstLineIndent=0;
	shape.Rotation = 90;
	shape.Select(false);
	shape.Line.Visible = msoFalse;
	shape.RelativeHorizontalPosition=wdRelativeHorizontalPositionPage
	shape.RelativeVerticalPosition=wdRelativeVerticalPositionPage
	shape.Left = 9;
	shape.Top = 110;
//	设置奇偶页不同
	ActiveDocument.Sections.Item(selectionIndex).PageSetup.OddAndEvenPagesHeaderFooter=1
	//为当前节偶数页添加页码
	var shape1=ActiveDocument.Sections.Item(selectionIndex).Headers.Item(1).Shapes.AddTextbox(msoTextOrientationHorizontal, 92.150002, 774.299988, 78.750000, 32.250000, ActiveDocument.Sections.Item(selectionIndex).Headers.Item(3).Range);
	shape1.TextFrame.Orientation = msoTextOrientationVertical;
	ActiveWindow.ActivePane.Activate();
	shape1.Select();
	(obj=>{
		obj.Range.LanguageID = wdSimplifiedChinese;
		obj.Fields.Add(Selection.Range, wdFieldPage, "", true);
	})(Selection);
        //页码前后加横线
	Selection.MoveLeft(wdCharacter, 1, wdMove);
	Selection.Font.Name = "宋体";
	Selection.Font.Size = 10.500000;
	Selection.TypeText("— ");
	Selection.MoveRight(wdCharacter, 1, wdMove);
	Selection.Font.Name = "宋体";
	Selection.Font.Size = 10.500000;
	Selection.TypeText(" —");
	Selection.ParagraphFormat.Alignment=1;
	Selection.ParagraphFormat.FirstLineIndent=0;
	shape1.Rotation = 90;
	shape1.Select(false);
	shape1.Line.Visible = msoFalse;
	shape1.RelativeHorizontalPosition=wdRelativeHorizontalPositionPage
	shape1.RelativeVerticalPosition=wdRelativeVerticalPositionPage
	shape1.Left = 9;
	shape1.Top = 500;
	(obj=>{//单独为本节设置页码
		obj.RestartNumberingAtSection = true;
		obj.StartingNumber = Selection.Information(wdActiveEndPageNumber);
	})(ActiveDocument.Sections.Item(selectionIndex).Headers.Item(wdHeaderFooterPrimary).PageNumbers);
	//页码续前节
	//(obj=>{
	//	obj.RestartNumberingAtSection = false;
	//	obj.StartingNumber = 1;
	//})(ActiveDocument.Sections.Item(selectionIndex).Headers.Item(wdHeaderFooterPrimary).PageNumbers);
//	回到主文档
	ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument;
	Selection.SetRange(0, 0);
	
}