PageOffice在线打开office文件通过js调用vba可实现的功能

发布时间 2023-07-26 15:33:47作者: qianxi

pageoffice封装的js接口有限,某些比较复杂的设置用到的客户不多,所以没有提供直接的js方法,但是pageoffice提供了Document属性和RunMacro方法,可以调vba或直接运行宏指令实现比较小众的一些需求


image

image

Word相关功能
1、给word表格中的单元格赋值

document.getElementById("PageOfficeCtrl1").Document.Tables(1).Cell(1, 1).Range.Text = "插入文本"

2、弹出word另存为所有格式的对话框

document.getElementById("PageOfficeCtrl1").Document.Application.Dialogs(84).Show();

3、给word文件光标处插入文本(两种方法)

//插入的文本处于光标选中状态
document.getElementById("PageOfficeCtrl1").Document.Application.Selection.Text="测试测试";
//光标在行尾
document.getElementById("PageOfficeCtrl1").Document.Application.Selection.TypeText("测试测试");

4、给word中选中的文本设置颜色(2是WdColorIndex常量,表示设置的颜色)

document.getElementById("PageOfficeCtrl1").Document.Application.Selection.Range.Font.ColorIndex = "2";

5、光标定位到行尾

document.getElementById("PageOfficeCtrl1").Document.Application.Selection.EndKey();

6、 光标定位到word文件结尾(6是 WdUnits参数,表示光标移动的位置)

document.getElementById("PageOfficeCtrl1").Document.Application.Selection.EndKey(6);

7、插入换行符

document.getElementById("PageOfficeCtrl1").Document.Application.Selection.TypeParagraph();

8、拒绝文档中所有的修订(必须在docAdmin模式下使用)

document.getElementById("PageOfficeCtrl1").Document.RejectAllRevisions();

9、显示导航栏窗口

document.getElementById("PageOfficeCtrl1").Document.Application.ActiveWindow.DocumentMap = true;

10、隐藏word中的修订格式(删除、添加内容这些不隐藏。改变字体样式或者大小这种格式会被隐藏)

document.getElementById("PageOfficeCtrl1").Document.Application.ActiveWindow.View.ShowFormatChanges = false;

11、遍历删除当前word文档中所有的键盘批注

function  deleteAllComments(){
    var docObj = document.getElementById("PageOfficeCtrl1").Document;
    for(var i=docObj .Comments.Count; i>=1; i--){
        docObj .Comments(i).Delete();
    }
}

12、设置打开文件的页面的百分比为200%

document.getElementById("PageOfficeCtrl1").Document.Application.ActiveWindow.ActivePane.View.Zoom.Percentage = 200;

13、word中删除光标所在行

document.getElementById("PageOfficeCtrl1").Document.Application.Selection.TypeBackspace();

14、根据用户名显示批注

function Button3_onclick() {
        var name = "Tom";//用户名
        var docObj = document.getElementById("PageOfficeCtrl1").Document;
        for(var i=1;i<=docObj .Comments.Count;i++){
            var author = docObj.Comments.Item(i).Author;
            // alert(author);
            if(author != name){
                document.getElementById("PageOfficeCtrl1").Document.ActiveWindow.View.Reviewers(author).Visible = false;
            }else {
                document.getElementById("PageOfficeCtrl1").Document.ActiveWindow.View.Reviewers(author).Visible = true;
            }
        }
    }

Excel相关功能
1、excel插入批注(注意:此功能只有pageoffice专业版或者企业版才能支持)

var sMac = 'Sub addCom() \r\n ' + 'ActiveSheet.Application.Sheets("Sheet1").Range("A1").AddComment ("批注信息") \r\n ' + 'End Sub';
document.getElementById("PageOfficeCtrl1").RunMacro("addCom", sMac);

2、获取excel当前活动的sheet的名称

var sheetName=document.getElementById("PageOfficeCtrl1").Document.ActiveSheet.Name;

3、excel中光标选中某个单元格

document.getElementById("PageOfficeCtrl1").Document.Application.Sheets("Sheet1").Range("C1").Select();

4、给excel单元格赋值

document.getElementById("PageOfficeCtrl1").Document.Application.Sheets("Sheet1").Range("C1").Value = "测试";

转载:PageOffice在线打开office文件通过js调用vba可实现的功能