需求:
将word中所有excel表格的格式进行统一化,修改其中的数字类型为“宋体, 五号,右对齐, 不加粗,不倾斜”,其中的中文为“宋体, 五号, 不加粗,不倾斜”
数字类型有:范围型的比如 10-20, 百分比类型的 6%, 整型的 1, 5, 浮点型的 -124,430.00, 124,430.00
宏实现:
启用宏设置: 文件-》选项-》新建选项卡-》新建分组-》搜索开发工具命令,添加到新建分组
在界面中即可看到 开发工具按钮,点击,即可看到宏的相关按钮:
输入一个宏的名字,点击创建宏,然后,点击编辑,输入如下宏代码,点击运行即可
wps中默认使用JavaScript作为宏的默认语言,示例如下
function FormatExcel() {// 匹配一个数值范围字符串 "10-12" "10.3 -20.3 " "-10.4 - 20" "-10.4--20.4" // const rangeRegex = /^-?\d+(\.\d+)?( *)?-( *)?(-?\d+(\.\d+)?)( *)?.[\s\S]*$/;const rangeRegex = /^-?\d+(\.\d+)?( *)?-( *)?(-?\d+(\.\d+)?)( *)?([\s\S]*)?$/;var doc = Application.ActiveDocument;for (var tableIndex = 1; tableIndex <= doc.Tables.Count; tableIndex++) {var table = doc.Tables.Item(tableIndex);var rows = table.Rows.Count;var cols = table.Columns.Count;// 遍历表格for (var r = 1; r <= rows; r++) {for (var c = 1; c <= cols; c++) {if (r > table.Rows.Count || c > table.Columns.Count) {continue;}//可选,跳过所有表格的第一列 //需求:属性列不需要修改if(c === 1){continue;}try {// console.log( "r,c = " + r +","+ c)var cell = table.Cell(r, c); //合并单元格不能被这样访问所以try-catch下var cellRange = cell.Range;var textContent_src = cellRange.Text.replace(/[\r\n]/g, ""); // 去掉段落标记textContent_src = textContent_src.replace(/[\x00-\x1F\x7F]/g, ''); // 去除BEL字符或其他不可见控制字符console.log("textContent_src: [" + textContent_src +"]" + ", Chin?: " + containsChinese(textContent_src)) if (!containsChinese(textContent_src)){ // 是数字:处理带逗号的数字,格式化并右对齐textContent = textContent_src.replace(/,/g, ""); // 去掉逗号textContent = parseFloat(textContent)//console.log("toFloat:[" + parseFloat(textContent)+"]")if (!isNaN(textContent)) {// 去除前后的空格textContent_src_trimmed = textContent_src.trim().trimEnd();// such as "3% ", "5%、7% "if (textContent_src_trimmed.includes('%')){textContent_out = textContent_src_trimmed; }// 范围数值,such as "12-20 ", "4.50-10.00 ", "-4.50 - -10.00"else if (rangeRegex.test(textContent_src_trimmed)){textContent_out = textContent_src_trimmed;}// 整型数值,such as " -5", "1 ", " 66 " //可以去除前后的空格else if (!textContent_src.includes('.')){textContent_out = textContent;}// 浮点数值,such as " 5.00 ", " -915,507.02 ", "915,507.02 "else {textContent_out =textContent.toLocaleString("en-US", {minimumFractionDigits: 2,maximumFractionDigits: 2});}cellRange.Text = textContent_out;cellRange.Font.Name = "宋体";cellRange.Font.Size = 10.5; //五号cellRange.ParagraphFormat.Alignment = wdAlignParagraphRight; //右对齐cellRange.Font.Italic = false; //不倾斜cellRange.Font.Bold = false; //不加粗}} else {// 含有中文的单元格,移除前面空格并居中//不支持移除后面的空格// textContent_src = textContent_src.trim(); //TBDcellRange.Text = textContent_src;cellRange.Font.Name = "宋体";cellRange.Font.Size = 10.5; //五号cellRange.Font.Italic = false; //不倾斜cellRange.Font.Bold = false; //不加粗// cellRange.ParagraphFormat.Alignment = wdAlignParagraphCenter; //居中对齐}} catch (error) {// console.log('获取单元格时出错(可能是合并单元格/不处理):', error.message);}}}}
}function containsChinese(str) {const chineseRegex = /[\u4e00-\u9fff]/;return chineseRegex.test(str);
}
效果:
右侧为原始数据,左侧为修改后的数据
进行空行的删除
接下来,进行空行的删除,若是没有合并行的单元格比较容易删除,使用
table.Rows.Item(rowsToDelete[l]).Delete();
先找到满足条件的需要删除的行,然后删除行,即可。
若是合并的行,则不能使用table.Rows.Item(rowsToDelete[l]).Delete();进行删除,需要使用cell一个一个删除:
table.Cell(rowsToDelete[l], c).Delete();
整体代码如下:
function FormatExcelLZ() {// 匹配一个数值范围字符串 "10-12" "10.3 -20.3 " "-10.4 - 20" "-10.4--20.4" // const rangeRegex = /^-?\d+(\.\d+)?( *)?-( *)?(-?\d+(\.\d+)?)( *)?.[\s\S]*$/;const rangeRegex = /^-?\d+(\.\d+)?( *)?-( *)?(-?\d+(\.\d+)?)( *)?([\s\S]*)?$/;var doc = Application.ActiveDocument;for (var tableIndex = 1; tableIndex <= doc.Tables.Count; tableIndex++) {var table = doc.Tables.Item(tableIndex);var rows = table.Rows.Count;var cols = table.Columns.Count;var rowsToDelete = [];// 遍历表格for (var r = 1; r <= rows; r++) {var shouldDeleteCounter = 0;for (var c = 1; c <= cols; c++) {if (r > table.Rows.Count || c > table.Columns.Count) {continue;}//可选,跳过所有表格的第一列 //需求:属性列不需要修改if(c === 1){continue;}try {// console.log( "r,c = " + r +","+ c)var cell = table.Cell(r, c); //合并单元格不能被这样访问所以try-catch下var cellRange = cell.Range;var textContent_src = cellRange.Text.replace(/[\r\n]/g, ""); // 去掉段落标记textContent_src = textContent_src.replace(/[\x00-\x1F\x7F]/g, ''); // 去除BEL字符或其他不可见控制字符console.log("textContent_src: [" + textContent_src +"]" + ", Chin?: " + containsChinese(textContent_src)) cellText = textContent_src.trim().trimEnd();console.log("cellText: [" + cellText + "]");// 如果单元格内容不为空、不为0.00、不为"--",则该行不应删除if (cellText === ""|| cellText === "0.00" || cellText === "0.0" || cellText === "0" || cellText === "--"|| cellText === "-"|| cellText === "——" || cellText === "—"|| cellText === "-") // 不同与字符 "-"{shouldDeleteCounter += 1;} if (!containsChinese(textContent_src)){ // 是数字:处理带逗号的数字,格式化并右对齐textContent = textContent_src.replace(/,/g, ""); // 去掉逗号textContent = parseFloat(textContent)//console.log("toFloat:[" + parseFloat(textContent)+"]")if (!isNaN(textContent)) {// 去除前后的空格textContent_src_trimmed = textContent_src.trim().trimEnd();// such as "3% ", "5%、7% "if (textContent_src_trimmed.includes('%')){textContent_out = textContent_src_trimmed; }// 范围数值,such as "12-20 ", "4.50-10.00 ", "-4.50 - -10.00"else if (rangeRegex.test(textContent_src_trimmed)){textContent_out = textContent_src_trimmed;}// 整型数值,such as " -5", "1 ", " 66 " //可以去除前后的空格else if (!textContent_src.includes('.')){textContent_out = textContent;}// 浮点数值,such as " 5.00 ", " -915,507.02 ", "915,507.02 "else {textContent_out =textContent.toLocaleString("en-US", {minimumFractionDigits: 2,maximumFractionDigits: 2});}cellRange.Text = textContent_out;cellRange.Font.Name = "宋体";cellRange.Font.Size = 10.5; //五号cellRange.ParagraphFormat.Alignment = wdAlignParagraphRight; //右对齐cellRange.Font.Italic = false; //不倾斜cellRange.Font.Bold = false; //不加粗}} else {// 含有中文的单元格,移除前面空格并居中//不支持移除后面的空格// textContent_src = textContent_src.trim(); //TBDcellRange.Text = textContent_src;cellRange.Font.Name = "宋体";cellRange.Font.Size = 10.5; //五号cellRange.Font.Italic = false; //不倾斜cellRange.Font.Bold = false; //不加粗// cellRange.ParagraphFormat.Alignment = wdAlignParagraphCenter; //居中对齐}} catch (error) {console.log('1获取单元格时出错(可能是合并单元格/不处理):', error.message);}}// 跳过了第一列,所以这里-1if (shouldDeleteCounter === cols -1 ) {rowsToDelete.push(r);}}// 从后往前删除行for (var l = rowsToDelete.length - 1; l >= 0; l--) {try{table.Rows.Item(rowsToDelete[l]).Delete();} catch (error){console.log('无法通过table.Rows.Item访问行,可能因为有合并的行:', error.message);// 从后往前删除列元素for (var c = cols; c > 0; c--) {var cell_cur = table.Cell(rowsToDelete[l], c);cell_cur.Delete();}}}}console.log('======done=====');
}function containsChinese(str) {const chineseRegex = /[\u4e00-\u9fff]/;return chineseRegex.test(str);
}
使用时,可以将 console.log行注释掉,提供处理速度,不注释也不影响功能。
删除空行效果:
没有合并行的删除:(为了方便检查,没有删除表头)
有合并行的删除:
相关网GPT网站:WPS 灵犀