WPS JS宏代码操作表格

5个月前 (12-08) 0 点赞 0 收藏 0 评论 15 已阅读

文件操作

function doFileOperation() {
    // 创建新文件wb
    let wb = Workbooks.Add();
    // 保存指定文件wb
    wb.SaveAs("C:\\Users\\Administrator\\Desktop\\wps\\temp-1.xlsx");
    // 关闭指定文件web
    wb.Close();
    // 删除指定文件
    Kill("C:\\Users\\Administrator\\Desktop\\wps\\temp-1.xlsx");
}

操作工作簿

function test3() {
    // 当前工作簿
    Console.log(ThisWorkbook.Name);
    // 活跃工作簿
    Console.log(ActiveWorkbook.Path);
    // 指定名称的工作簿
    Console.log(Workbooks.Item("456").FullName);
    // 第几个工作簿
    Console.log(Workbooks.Item(1).Name);
}

操作工作表

function test4() {
    let wb = ThisWorkbook;
    // 活动工作簿中的活动工作表
    Console.log(wb.ActiveSheet.Name);
    // 用工作表名获取工作表
    Console.log(wb.Worksheets("Sheet2").Name);
    // 用下标序号获取工作表
    Console.log(wb.Worksheets(3).Name);
    // 用下标序号获取工作表,包含chart
    Console.log(wb.Sheets.Item(3).Name);
    // 用工作表名获取工作表,包含chart
    Console.log(wb.Sheets.Item("Sheet1").Name); 
}

操作单元格

function test5(){
    // 表格内范围查找
    let arr = ThisWorkbook.Sheets.Item("Sheet1").Range("A1:C3").Value2;
    // 查找指定的表格
    let arr1 = ThisWorkbook.Sheets.Item("Sheet1").Cells(1,1).Value2;
    
    // 查找表格范围内的公式和数
    let arr2 = ThisWorkbook.Sheets.Item("Sheet1").Range("A1:C3").Formula;
    Console.log(JSON.stringify(arr));
    Console.log(arr1);
    Console.log(JSON.stringify(arr2));
}


WPS JS宏代码操作表格

本文收录在
0评论

登录

忘记密码 ?

切换登录

注册