const table = wps.WpsApplication().ActiveDocument.Bookmarks.Item(bookmark) // 获取书签是否存在
table.Cell(1,1).Range.ParagraphFormat.Alignment = 0 // 设置第一行第一列的文字居左对齐
table.Range.Cells.VerticalAlignment = 1; // 文字垂直居中(全部)
table.Range.ParagraphFormat.Alignment = 1; // 文字左右居中
table.Cell(1,1).Range.Font.Bold = true // 设置第一行第一列的文字加粗
Cell 第一个参数为: 行 第二个为 列 (都是从1开始的)
table.Cell(1, 1).Range.Text = '一、资本化预算' // 设置第一行第一列的文字
table.Cell(1, 1).Range.Text 获取第一行第一列的文字
table.Rows.Count // 获取表格的行数
table.Columns.Count // 获取表格的列数
table.Columns.Item(count + 1 - i).Delete() // 删除
table.Cell(1, 1).Merge(table.Cell(3, 1)) // 合并单元格
wps.WpsApplication().ActiveDocument.Bookmarks.Tables.Add(Range, rowsNum - 1, colsNum) // 创建表格
table.Columns.Width = 35 // 设置表格表头宽度
table.Columns.Item(1).Width = 35 // 设置单个表格表头宽度 第一列的宽度
table.Rows.Item(2).Width = 75 // 设置单个表格表头宽度 第二行
table.Cell(i, 1).Range.Text.replace('', '') // 取代 为 '' // 表格 都会有 '',
table.Range.Cells.VerticalAlignment = 1 // 文字水平垂直居中
table.Range.ParagraphFormat.Alignment = 1 // 文字水平垂直居中
创建书签
const doc = wps.WpsApplication().ActiveDocument const Bookmarks = doc.Bookmarks const prevBook = Bookmarks.Item(prevName) // 创建书签 Bookmark = Bookmarks.Add(bookmarkName, prevBook.Range) const st = prevBook.End + 1 Bookmark.Start = st Range = Bookmark.Range let Tables = Range.Tables // 创建表格 const currentTable = Tables.Add(Range, 1, 4) // 换行 doc.Range(Bookmark.End, Bookmark.End).Select() // 换行 doc.ActiveWindow.Selection.TypeText('\r\n')
Range.ParagraphFormat.CharacterUnitFirstLineIndent = 0
Range.ParagraphFormat.CharacterUnitLeftIndent = 0
Range.ParagraphFormat.LeftIndent = 0
Range.ParagraphFormat.FirstLineIndent = 0
以上为去除缩进
const projectNum = wps.WpsApplication().ActiveDocument.Bookmarks.Item('projectNum')
wps.WpsApplication().ActiveDocument.Range(projectNum.Start, projectNum.Start).Select()
设置窗口其实位置 (定位到某个书签位置)
设置内边框为实线
table.Borders.InsideLineStyle = true
设置外边框为实线
for (let index = 1; index < 5; index++) {
const border = table.Borders.Item(index)
border.LineStyle = 1
border.LineWidth = 4
border.Color = 0
}
table.Borders.InsideLineStyle = true
设置外边框为实线
for (let index = 1; index < 5; index++) {
const border = table.Borders.Item(index)
border.LineStyle = 1
border.LineWidth = 4
border.Color = 0
}
table.Borders.Item(1).LineStyle = 0 // 去除上边框线 Item 1是上边框 2是左边框 3是下边框 4是右 5是去掉内横边框 6是去掉内纵边框
table.Cell(1, 1).Borders.Item(1).LineStyle = 0 去除第一行第一列的 上边框
table.TopPadding = 2.8 // 设置整个表格的 头部padding
table.BottomPadding = 2.8 设置整个表格的底部padding
table.BottomPadding = 2.8 设置整个表格的底部padding
table.Cell(1, 1).BottomPadding = 0 // 设置第一行第一列底部padding
table.Cell(1, 1).TopPadding = 25 // 设置第一行第一列头部padding
table.Cell(1, 1).TopPadding = 25 // 设置第一行第一列头部padding
示例:
const arr = [ [ "合计", "2021", "2022", "2023", "2024", "2025", "备注"], ["中国石油集团科学技术研究院有限公司", "测井有限公司"], [ 2021, "中国石油集团科学技术研究院有限公司", 1938.48, "",4296.40], [ 2022, "中国石油集团科学技术研究院有限公司", 779.52, "",4296.40], [ 2023, "中国石油集团科学技术研究院有限公司", 660.89, "",4296.40], [ 2024, "中国石油集团科学技术研究院有限公司", 625.25, "",4296.40], [ 2025, "中国石油集团科学技术研究院有限公司", 292.26, "",4296.40], [ 2021, "测井有限公司", 90.00, "",451.00], [ 2022, "测井有限公司", 114.00, "",451.00], [ 2023, "测井有限公司", 115.00, "",451.00], [ 2024, "测井有限公司", 99.00, "",451.00], [ 2025, "测井有限公司", 33.0, "",451.00] ]
fundingArrangement(arr, bookmark, fileId) {
if (wps.WpsApplication().ActiveDocument.Bookmarks.Item(bookmark) && !wps.WpsApplication().ActiveDocument.Bookmarks.Item(bookmark).Empty) {
console.log(arr, '表二到表四数据')
// 获取数据位置
let table = wps.WpsApplication().ActiveDocument.Bookmarks.Item(bookmark).Range.Tables.Item(1)
// 计算表格有多少行列
// const rows = arr[1].length / 2
const rows = arr[1].length
const cols = arr[0].length
// table.Rows.Count // 获取表格的行数
// table.Columns.Count // 获取表格列数
// 重置行
// for (let i = 1; i <= table.Rows.Count + 2; i++) {
// table.Rows.Item(2) && table.Rows.Item(2).Delete()
//
if (!fileId) {
for (let i = table.Rows.Count; i >= 2; i--) {
table.Rows.Item(2) && table.Rows.Item(2).Delete()
}
// 重置列
for (let i = table.Columns.Count; i >= 3; i--) {
table.Columns.Item(i) && table.Columns.Item(i).Delete()
}
// 根据数据添加行数
for (let i = 1; i < rows+1; i++) {
table.Rows.Add(table.Rows.Item(2));
}
// 重置序号
for (let i = 2; i < rows + 2; i++) {
if (table.Cell(i, 1)) {
// 写入序号
table.Cell(i, 1).Range.Text = i - 1
}
if (table.Cell(i, 2)) {
// 写入单位
table.Cell(i, 2).Range.Text = arr[1][i - 2]
}
}
// 根据数据添加列数
for (let i = 1; i < cols + 1; i++) {
table.Columns.Add(table.Columns.Item(3));
}
for (let i = 3; i < arr[0].length +3; i++) {
table.Cell(1, i).Range.Text = arr[0][i-3]
}
// 写入前两列数据
// let k = 0
// for (let i = 2; i < arr[1].length / 2 + 2; i++) {
// for (let j = 1; j < 3; j++) {
// if (k < arr[1].length) {
// try {
// table.Cell(i, j).Range.Text = arr[1][k++]
// } catch (error) {
// console.log(i, j, arr[1][k++])
// }
// }
// }
// }
// 设置表格列宽
for (let i = 1; i <= table.Columns.Count; i++) {
if (i == 1) {
table.Columns.Item(i).Width = 35 // 设置序号列宽度
} else if (i == 2) {
table.Columns.Item(i).Width = 115 // 设置单个表格表头宽度
} else if (i == table.Columns.Count) {
table.Columns.Item(i).Width = 75 // 设置单个表格表头宽度
} else {
table.Columns.Item(i).Width = 65 // 设置单个表格表头宽度
}
}
table.Rows.Add(table.Rows.Item(table.Rows.Count + 1)); // 在最后一行添加一行空行
table.Rows.Item(table.Rows.Count).Width = 150 // 设置合并后的宽度
table.Cell(table.Rows.Count, 1).Range.Text = '总计'
let yearIndex
let orgIndex
arr.forEach((element, index) => {
if (index > 1) {
yearIndex = arr[0].findIndex((yearItem) => element[0] === yearItem)
orgIndex = arr[1].findIndex((orgItem) => element[1] === orgItem)
if (yearIndex !== -1 && orgIndex !== -1) {
table.Cell(orgIndex + 2, yearIndex + 3).Range.Text = parseFloat(element[2])
}
if (element[3]) { // 合计
orgIndex = arr[1].findIndex((orgItem) => element[1] === orgItem)
if (yearIndex !== -1 && orgIndex !== -1) {
table.Cell(orgIndex + 2, 3).Range.Text = parseFloat(element[3])
}
}
if (element[4]) { // 备注
orgIndex = arr[1].findIndex((orgItem) => element[1] === orgItem)
if (yearIndex !== -1 && orgIndex !== -1) {
table.Cell(orgIndex + 2, 2 + arr[0].length).Range.Text = element[4]
}
}
}
})
for (let col = 3; col < table.Columns.Count; col++) {
let colValueList = []
for (let row = 2; row < table.Rows.Count; row++) {
if (!isNaN(parseInt(table.Cell(row, col).Range.Text))) {
colValueList.push(parseFloat(table.Cell(row, col).Range.Text))
}
}
if (colValueList.length > 0) {
table.Cell(table.Rows.Count, col).Range.Text = sumArr(colValueList)
}
}
table.Cell(table.Rows.Count, 1).Merge(table.Cell(table.Rows.Count, 2)) // 合并最后一行的第一列和第二列
}
}
}
table.Range.Font.NameAscii = '宋体'
table.Range.Font.NameFarEast = '宋体'
table.Range.Font.Size = 12
以上是设置文字字体及字号的