把表格中N个Sheet页数据合并成一个新的Sheet页
function 合并Sheet页() {
//获取工作表对象
var ws = ThisWorkbook.Worksheets;
//创建名称为 合并页 的Sheet页,如果存在名称叫合并页的Sheet页,这里会报错,没做判断删除
var ws1 = ws.Add();
ws1.Name = "合并页";
//先把表头复制过去
ws.Item(2).Cells(1,1).Rows.EntireRow.Copy(ws1.Cells(1, 1));
//获取所有Sheet页对象
var worksheetObjects = {};
var sheetNum = ws.Count;
//console.log("Sheet页数量:"+sheetNum);
//这里i从2开始,把新创建的合并页抛出去
for(i = 2; i < sheetNum+1; i++){
var sheetName = ws.Item(i).Name;
//console.log("第"+i+"个"+"Sheet页名称:"+sheetName);
worksheetObjects[sheetName] = ws.Item(sheetName);
//每个Sheet页从第二行开始复制
var lastCol = worksheetObjects[sheetName].Cells(1, worksheetObjects[sheetName].Columns.Count).End(xlToLeft).Column;
var lastRow = worksheetObjects[sheetName].Cells(worksheetObjects[sheetName].Rows.Count,1).End(xlUp).Row;
//获取合并页的有多少行
var startRow = ws1.Cells(ws1.Rows.Count,1).End(xlUp).Row;
//开始复制
worksheetObjects[sheetName].Range(Cells(2,1),Cells(lastRow,lastCol)).Copy(ws1.Cells(startRow+1, 1));
}
}