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

存在的困难

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

解决方案:

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

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 := map()
    objFormat["font"] := map()
    objFormat["interior"] := map()
    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"]
}
posted @ 2021-07-22 00:05  火冷  阅读(636)  评论(0编辑  收藏  举报