用AutoHotkey解决Excel中修改超链接公式会自动修改文字格式的问题

存在的困难

如果用了excel的超链接,一定会被Excel自动修改格式折磨过。
只要修改了公式中的任意内容,回车后单元格格式就变成蓝字,12号字体。
然后就要手动改回原来的格式,非常繁琐

解决方案:

用脚本在修改前记录当前单元格的格式,设置公式后再自动设置字体格式。 记录单元格格式和恢复单元格格式的函数如下

xl := ComObjActive("Excel.application")
ac := xl.ActiveCell
objFormat := get(ac)
arrFormula := StrSplit(ac.formula, '"')
rng := inputbox("引用区域",,,ltrim(arrFormula[2],"#"))
name := inputbox("显示名称",,,ac.text)
gs := format('=HYPERLINK("#{1}","{2}")', rng,name)
xl.ScreenUpdating := false
ac.formula := gs
set(ac, objFormat)
xl.ScreenUpdating := true
return

get(cell) {
objFormat := {}
objFormat.font := {}
objFormat.interior := {}
objFormat.font.name := cell.font.name,
objFormat.font.Size := cell.font.Size,
objFormat.font.bold := cell.font.bold,
objFormat.font.italic := cell.font.italic,
objFormat.font.Strikethrough := cell.font.Strikethrough,
objFormat.font.underline := cell.font.underline,
objFormat.font.ColorIndex := cell.font.ColorIndex,
objFormat.interior.ColorIndex := cell.interior.ColorIndex,
return objFormat
}
set(cell, objFormat) {
cell.font.name := objFormat.font.name,
cell.font.Size := objFormat.font.Size,
cell.font.bold := objFormat.font.bold,
cell.font.italic := objFormat.font.italic,
cell.font.Strikethrough := objFormat.font.Strikethrough,
cell.font.underline := objFormat.font.underline,
cell.font.ColorIndex := objFormat.font.ColorIndex,
cell.interior.ColorIndex := objFormat.interior.ColorIndex,
}

原文地址:https://www.cnblogs.com/hyaray/p/15042214.html