【Excel】【VB和JS】表格内容姓名、卡号、身份证敏感信息转换为图片打印

VB代码:

vbscript 复制代码
Function ConvertCellToImageAndPlace(n As Long, m As Long)
    Dim sourceCell As Range
    Set sourceCell = Sheets("Sheet2").Cells(n, m)
    
    sourceCell.CopyPicture Appearance:=xlScreen, Format:=xlPicture
    
    Dim targetCell As Range
    Set targetCell = Sheets("Sheet3").Cells(n, m)
    
    Sheets("Sheet3").Paste targetCell
    
    Dim shp As Shape
    Set shp = Sheets("Sheet3").Shapes(Sheets("Sheet3").Shapes.Count)
    
    shp.Left = targetCell.Left
    shp.Top = targetCell.Top
    shp.Width = targetCell.Width
    shp.Height = targetCell.Height
End Function
Function ClearSheet3Completely()
    Sheets("Sheet3").Cells.ClearContents '清空单元格内容
    Sheets("Sheet3").Cells.ClearFormats '清空单元格格式
    Dim shp As Shape
    For Each shp In Sheets("Sheet3").Shapes
        shp.Delete
    Next shp
End Function
Sub AutoPrintPicture()

    ClearSheet3Completely
    For i = 1 To 10
         Sheets("Sheet2").Cells(10, 3) = i + 201
         ConvertCellToImageAndPlace 10, 3
         ConvertCellToImageAndPlace 10, 2
         ConvertCellToImageAndPlace 10 + 1, 2
         ConvertCellToImageAndPlace 10 + 2, 2
         Sheets("Sheet3").PrintOut

         ClearSheet3Completely
    Next i
    
End Sub

JS代码:

python 复制代码
function convertCellToImageAndPlace(row, col) {
    // 获取源单元格
    let sourceCell = Sheets("Sheet2").Cells(row, col);
    // 复制为图片,明确参数类型为数字,根据 WPS 的文档可能需要使用特定的数值代表外观
    sourceCell.CopyPicture();
    // 获取目标单元格
    let targetCell = Sheets("Sheet3").Cells(row, col);
    // 粘贴到目标单元格
    Sheets("Sheet3").Paste(targetCell);
}

function clearSheet3Completely(){
	// 获取当前Sheet表对象
	var app = Application.Application,
		acSh = app.Sheets("Sheet3");
	// 循环遍历当前Sheet表中的所有图形
	for (let pic of acSh.Shapes){
		// 如果当前为"图片"、"==13",执行删除操作
		if (pic.Type == msoPicture){
            // Shape.TopLeftCell,代表位于对象左上角下方的单元格
			var r = pic.TopLeftCell.Row,
				c = pic.TopLeftCell.Column;
			pic.Delete();
			Console.log(`${r}行${c}列图片删除成功@SMFY`);
		}
	}
}

function autoPrintPicture() {
   //clearSheet3Completely();
    for (let i = 1; i <= 3;i++) {
        Sheets("Sheet2").Cells(10, 3).Value2 = i + 1;
        convertCellToImageAndPlace(10, 3);
        convertCellToImageAndPlace(10, 2);
        convertCellToImageAndPlace(11, 2);
        convertCellToImageAndPlace(12, 2);
        Sheets("Sheet3").PrintOut();
        clearSheet3Completely();
    }
}

总结:

convertCellToImageAndPlace函数:

  • 从 "Sheet2" 工作表的指定单元格(由参数n和m确定)复制内容为图片。
  • 将图片粘贴到 "Sheet3" 工作表的对应单元格。
  • 获取 "Sheet3" 工作表中刚刚粘贴的图片对应的形状对象(即最后一个形状)。
  • 如果形状对象存在,将其位置和大小设置为目标单元格的位置和大小,并返回该形状对象。

clearSheet3Completely函数:

  • 清空 "Sheet3" 工作表的所有单元格内容。
  • 清空 "Sheet3" 工作表的所有单元格格式。
  • 遍历并删除 "Sheet3" 工作表中的所有形状。

autoPrintPicture函数:

  • 首先调用clearSheet3Completely函数清空 "Sheet3" 工作表。
  • 循环 10 次,每次循环: 在 "Sheet2" 工作表的特定单元格(第 10 行第 3 列)设置一个值,该值为当前循环次数加 201。
  • 分别调用convertCellToImageAndPlace函数将 "Sheet2" 工作表的特定四个位置(10,3)、(10,2)、(11,2)、(12,2)的单元格内容复制为图片并放置在 "Sheet3" 工作表的对应位置,同时获取对应的形状对象。 打印 "Sheet3" 工作表。
  • 再次调用clearSheet3Completely函数清空 "Sheet3" 工作表,为下一次循环做准备。
相关推荐
louis.D.3 小时前
excel按开头字符筛选
excel
ms365copilot3 小时前
Copilot Excel真实数据实战
人工智能·excel·copilot
Nontee4 小时前
Excel 导出组件改造实录:从 Full GC 到声明式导出
jvm·excel
Metaphor6924 小时前
使用 Python 设置 Excel 文件属性
开发语言·python·excel
西门子918!5 小时前
Office(Excel)常用记录1
经验分享·excel
reasonsummer1 天前
【办公类110-08】20260807园园通-“小班“户籍地址和居住地址补充完整+外省市所在“省市区”两个按钮手工填写(Python+EXCEL)
python·excel·园园通
CC数分1 天前
如何用Excel数据透视分析业务?3分钟出报表
数据分析·excel
ms365copilot1 天前
iPad Excel Copilot,适合打工人出勤做分析数据
excel·copilot·ipad
码云数智-园园2 天前
Python如何将律师的Excel噩梦变成自动化系统
python·自动化·excel
用户298698530142 天前
将 Excel 表格转换为图片的三种实用方法
人工智能·后端·excel