【吐槽:机器人也忒多了,大半夜刚发出来两分钟100个机器人浏览,3个人点赞】
参考别人的修改的,原版来源于Zotero在word中插入带超链接的参考文献/交叉引用/跳转参考文献_zotero交叉引用-CSDN博客文章浏览阅读9.8k次,点赞9次,收藏43次。Zotero以其丰富的插件而闻名,使用起来十分的带劲,最重要的是它是免费的、不卡顿,不像某专业软件。然而Zotero在word插入参考文献时,无法为参考文献添加超链接,这是一个不得不提的遗憾。不过,有大佬已经写出了基于Word VBA语言的代码,可以为Zotero在Word中的参考文献一键增加超链接!不过,这个代码在运行的时候,我遇到了一些严重的问题:_zotero交叉引用https://blog.csdn.net/weixin_42413559/article/details/134494771#comments_33771824基于修改(ChatGPT调教的)过的GB/T 7714-2015标准的引用样式,其他样式没试过,文末贴上相应的引用样式css
解决的问题1
数字开头的文献引用不能建立正确位置的超链接,因为超链接的书签(作为唯一标识符)不能以数字开头,原版是直接以题目作为书签,所以会报错矛盾,改进是在每个参考目录的书签加上了前缀"zotero_",总之加个前缀就能解决问题
解决的问题2
在多重引用[x—x]的情况下,原版会出错(点zotero插件的refresh按钮也会把自己好不容易输入的多个连号引用合并成一个多重引用)。以[2,20-25]为例子,现在实现的功能是给"2"、"20”和“25"分别增添上指向2、20和25的超链接
只认[]里的数字,为里面的数字增加对应超链接
解决的问题3
引用中如果有引号会出现问题,已经修复
引用英文论文题目中带英文的双引号"",但是插入引用列表的时候会自动变成中文的双引号“”,这个小问题好像是国标要求的,我看一些大牛的中文论文引用都是这样的,就没有改css
Public Sub ZoteroLinkCitation()
Dim nStart As Long, nEnd As Long
Dim title As String
Dim titleAnchor As String
Dim fieldCode As String
Dim pos As Long, n1 As Long, n2 As Long
Dim startPosition As Long, commaPosition As Long
Dim commaPositions() As Long
Dim aField As Field
Dim rng As Range
Dim Paper_i As Long
Dim uniqueID As String
Dim docRange As Range
nStart = Selection.Start
nEnd = Selection.End
Application.ScreenUpdating = False
' 显示域代码
ActiveWindow.View.ShowFieldCodes = True
Selection.Find.ClearFormatting
With Selection.Find
.text = "^d ADDIN ZOTERO_BIBL"
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
' 添加书签 "Zotero_Bibliography"
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, name:="Zotero_Bibliography"
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
ActiveWindow.View.ShowFieldCodes = False
' 遍历文档中的所有域
For Each aField In ActiveDocument.Fields
' 检查是否是 Zotero 文内引用
If InStr(aField.Code, "ADDIN ZOTERO_ITEM") > 0 Then
fieldCode = aField.Code
pos = 0
Paper_i = 1
aField.Select
Dim Extra_Result As Variant
Extra_Result = ExtractNumbersFromSelection
Dim Part_Index_Citation As Long
Dim filedCode_tmp As String
' 循环遍历文本数组
For Part_Index_Citation = LBound(Extra_Result) To UBound(Extra_Result)
filedCode_tmp = fieldCode
' 打印当前处理的文本
Debug.Print "Processing: " & Extra_Result(Part_Index_Citation)
'Debug.Print fieldCode
' 查找第 Part_Index_Citation 个 "title":
If Left(Extra_Result(Part_Index_Citation), 1) = "–" Then
n = (CInt(Mid(Extra_Result(Part_Index_Citation), 2)) - CInt(Extra_Result(Part_Index_Citation - 1))) - 1
For i = 1 To n
n1 = InStr(filedCode_tmp, """title"":""") + Len("""title"":""")
n2 = InStr(n1, filedCode_tmp, """,""")
' 移动到 "title": 后面的双引号
filedCode_tmp = Mid(filedCode_tmp, n2 + 1)
Next i
'Extra_Result(Part_Index_Citation) = Mid(Extra_Result(Part_Index_Citation), 2)
End If
n1 = InStr(filedCode_tmp, """title"":""")
If n1 > 0 Then
n1 = n1 + Len("""title"":""")
n2 = InStr(n1, filedCode_tmp, """,""")
If n2 > n1 Then
' 提取双引号内的文字
title = Mid(filedCode_tmp, n1, n2 - n1)
Title_tmp = title
If Mid(title, 1, 1) = "\" Then
title = Mid(title, 3)
title = "“" & Mid(title, 1, InStr(title, "\""") - 1) & "”" & Mid(title, InStr(title, "\""") + 2)
End If
Title_tmp = title
' 打印提取到的 title
Debug.Print "Title: " & title
' 生成带有 zotero_ 前缀的唯一书签名称
uniqueID = CStr(Rnd() * 1000000) ' 通过随机数确保唯一性
titleAnchor = "zotero_" & Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(title, " ", "_"), "#", "_"), "&", "_"), ":", "_"), ",", "_"), "-", "_"), "‐", "_"), "'", "_"), ".", "_"), "(", "_"), ")", "_"), "?", "_"), "!", "_")
titleAnchor = Left(titleAnchor, 40)
' 移动到 Zotero Bibliography 书签
Selection.GoTo What:=wdGoToBookmark, name:="Zotero_Bibliography"
Selection.Find.ClearFormatting
With Selection.Find
.text = Left(Title_tmp, 255)
.Replacement.text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' 在参考文献中查找引文
Selection.Find.Execute
' 选择对应的引用段落
Selection.Paragraphs(1).Range.Select
If Right(Selection.Range.text, 1) = vbCr Or Right(Selection.Range.text, 1) = vbCrLf Then
' 将选中范围的结束位置向前移动一位,排除最后的回车符号
Selection.MoveEnd wdCharacter, -1
End If
' 为找到的引文添加书签
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, name:=titleAnchor
.DefaultSorting = wdSortByName
.ShowHidden = True
End With
aField.Select
Selection.Find.ClearFormatting
' 查找并选择特定数字
' 查找第 Part_Index_Citation 个 "title":
tmp = Extra_Result(Part_Index_Citation)
If Left(Extra_Result(Part_Index_Citation), 1) = "–" Then
tmp = Mid(Extra_Result(Part_Index_Citation), 2)
End If
Set rng = Selection.Range
With rng.Find
.text = tmp
.Replacement.text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = True ' 确保完全匹配
.MatchWildcards = False ' 禁用通配符匹配
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
rng.Find.Execute
rng.Select
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", SubAddress:=titleAnchor, ScreenTip:=""
style = Selection.style
aField.Select
End If
' 从当前位置继续查找下一个 "title":
fieldCode = Mid(filedCode_tmp, n2 + 1)
Else
' 如果没有找到 "title": 则退出循环
Exit For
End If
Next Part_Index_Citation
End If
NextField:
Next aField
' 确保 nStart 和 nEnd 在文档范围内
Set docRange = ActiveDocument.Content
If nStart < docRange.Start Then nStart = docRange.Start
If nEnd > docRange.End Then nEnd = docRange.End
ActiveDocument.Range(nStart, nEnd).Select
Application.ScreenUpdating = True
End Sub
Function ExtractNumbersFromSelection() As Variant
Dim selectedText As String
Dim cleanedText As String
Dim elements() As String
Dim i As Long
Dim j As Long
Dim numArray() As String
Dim tempArray() As String
' 获取选中的文本
selectedText = Selection.text
' 去掉文本两端的中括号
cleanedText = Replace(Replace(selectedText, "[", ""), "]", "")
' 以逗号分割文本
elements = Split(cleanedText, ",")
' 初始化结果数组
ReDim numArray(0)
' 遍历分割的元素
For i = LBound(elements) To UBound(elements)
' 去掉元素两端的空格
elements(i) = Trim(elements(i))
' 检查元素是否包含范围符号(–)
If InStr(elements(i), "–") > 0 Then
' 以范围符号分割元素
tempArray = Split(elements(i), "–")
' 将范围内的数字加入结果数组
For j = LBound(tempArray) To UBound(tempArray)
tempArray(j) = Trim(tempArray(j))
If IsNumeric(tempArray(j)) Then
tmp = ""
If j = 1 Then
tmp = "–"
End If
numArray(UBound(numArray)) = tmp & tempArray(j)
ReDim Preserve numArray(UBound(numArray) + 1)
End If
Next j
Else
' 直接加入单个数字
If IsNumeric(elements(i)) Then
numArray(UBound(numArray)) = elements(i)
ReDim Preserve numArray(UBound(numArray) + 1)
End If
End If
Next i
' 去掉多余的最后一个空元素
If UBound(numArray) >= 0 And numArray(UBound(numArray)) = "" Then
ReDim Preserve numArray(UBound(numArray) - 1)
End If
' 返回结果数组
ExtractNumbersFromSelection = numArray
End Function
<?xml version="1.0" encoding="utf-8"?>
<style xmlns="http://purl.org/net/xbiblio/csl" version="1.0" class="in-text" names-delimiter=". " name-as-sort-order="all" sort-separator=" " demote-non-dropping-particle="never" initialize-with=" " initialize-with-hyphen="false" page-range-format="expanded" default-locale="zh-CN">
<info>
<title>China National Standard GB/T 7714-2015 (numeric, 中文)-sjx</title>
<id>http://www.zotero.org/styles/china-national-standard-gb-t-7714-2015-numeric</id>
<link href="http://www.zotero.org/styles/china-national-standard-gb-t-7714-2015-numeric" rel="self"/>
<link href="http://std.samr.gov.cn/gb/search/gbDetailed?id=71F772D8055ED3A7E05397BE0A0AB82A" rel="documentation"/>
<author>
<name>牛耕田</name>
<email>buffalo_d@163.com</email>
</author>
<contributor>
<name>Zeping Lee</name>
<email>zepinglee@gmail.com</email>
</contributor>
<category citation-format="numeric"/>
<category field="generic-base"/>
<summary>The Chinese GB/T 7714-2015 numeric style</summary>
<updated>2022-02-23T10:44:01+00:00</updated>
<rights license="http://creativecommons.org/licenses/by-sa/3.0/">This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 License</rights>
</info>
<locale xml:lang="zh-CN">
<date form="text">
<date-part name="year" suffix="年" range-delimiter="—"/>
<date-part name="month" form="numeric" suffix="月" range-delimiter="—"/>
<date-part name="day" suffix="日" range-delimiter="—"/>
</date>
<terms>
<term name="edition" form="short">版</term>
<term name="open-quote">“</term>
<term name="close-quote">”</term>
<term name="open-inner-quote">‘</term>
<term name="close-inner-quote">’</term>
</terms>
</locale>
<locale>
<date form="numeric">
<date-part name="year" range-delimiter="/"/>
<date-part name="month" form="numeric-leading-zeros" prefix="-" range-delimiter="/"/>
<date-part name="day" form="numeric-leading-zeros" prefix="-" range-delimiter="/"/>
</date>
<terms>
<term name="page-range-delimiter">-</term>
</terms>
</locale>
<!-- 引用日期 -->
<!-- 自己修改的,修改后只针对网页添加引用日期 -->
<macro name="accessed-date">
<choose>
<if type="post post-weblog webpage" match="any">
<date variable="accessed" form="numeric" prefix="[" suffix="]"/>
</if>
</choose>
</macro>
<!-- 主要责任者 -->
<macro name="author">
<names variable="author">
<name>
<name-part name="family"/>
<name-part name="given"/>
</name>
<substitute>
<names variable="composer"/>
<names variable="illustrator"/>
<names variable="director"/>
<choose>
<if variable="container-title" match="none">
<names variable="editor"/>
</if>
</choose>
</substitute>
</names>
</macro>
<!-- 书籍的卷号(“第 x 卷”或“第 x 册”) -->
<macro name="book-volume">
<choose>
<if type="article article-journal article-magazine article-newspaper periodical" match="none">
<choose>
<if is-numeric="volume">
<group delimiter=" ">
<label variable="volume" form="short" text-case="capitalize-first"/>
<text variable="volume"/>
</group>
</if>
<else>
<text variable="volume"/>
</else>
</choose>
</if>
</choose>
</macro>
<!-- 专著主要责任者 -->
<macro name="container-author">
<names variable="editor">
<name>
<name-part name="family" text-case="uppercase"/>
<name-part name="given"/>
</name>
<substitute>
<names variable="editorial-director"/>
<names variable="collection-editor"/>
<names variable="container-author"/>
</substitute>
</names>
</macro>
<!-- 专著题名 -->
<macro name="container-title">
<group delimiter=", ">
<group delimiter=": ">
<choose>
<if variable="container-title">
<text variable="container-title"/>
</if>
<else>
<text variable="event"/>
</else>
</choose>
<text macro="book-volume"/>
</group>
<choose>
<if variable="event-date">
<date variable="event-date" form="text"/>
<text variable="event-place"/>
</if>
</choose>
</group>
</macro>
<!-- 版本项 -->
<macro name="edition">
<choose>
<if is-numeric="edition">
<group delimiter=" ">
<number variable="edition" form="ordinal"/>
<text term="edition" form="short"/>
</group>
</if>
<else>
<text variable="edition"/>
</else>
</choose>
</macro>
<!-- 电子资源的更新或修改日期 -->
<macro name="issued-date">
<date variable="issued" form="numeric"/>
</macro>
<!-- 出版年 -->
<macro name="issued-year">
<choose>
<if is-uncertain-date="issued">
<date variable="issued" prefix="[" suffix="]">
<date-part name="year" range-delimiter="-"/>
</date>
</if>
<else>
<date variable="issued">
<date-part name="year" range-delimiter="-"/>
</date>
</else>
</choose>
</macro>
<!-- 专著的出版项 -->
<macro name="publishing">
<group delimiter=": ">
<group delimiter=", ">
<group delimiter=": ">
<text variable="publisher-place"/>
<text variable="publisher"/>
</group>
<!-- 非电子资源显示“出版年” -->
<choose>
<if variable="publisher page" type="book chapter paper-conference thesis" match="any">
<text macro="issued-year"/>
</if>
<else-if variable="URL DOI" match="none">
<text macro="issued-year"/>
</else-if>
</choose>
</group>
<text variable="page"/>
</group>
<choose>
<!-- 纯电子资源显示“更新或修改日期” -->
<if variable="publisher page" type="book chapter paper-conference thesis" match="none">
<choose>
<if variable="URL DOI" match="any">
<text macro="issued-date" prefix="(" suffix=")"/>
</if>
</choose>
</if>
</choose>
<text macro="accessed-date"/>
</macro>
<!-- 其他责任者 -->
<macro name="secondary-contributor">
<names variable="translator">
<name>
<name-part name="family" text-case="uppercase"/>
<name-part name="given"/>
</name>
<label form="short" prefix=", "/>
</names>
</macro>
<!-- 连续出版物中的析出文献的出处项(年、卷、期等信息) -->
<macro name="periodical-publishing">
<group>
<group delimiter=": ">
<group>
<group delimiter=", ">
<text macro="container-title" text-case="title"/>
<choose>
<if type="article-newspaper">
<text macro="issued-date"/>
</if>
<else>
<text macro="issued-year"/>
</else>
</choose>
<text variable="volume"/>
</group>
<text variable="issue" prefix="(" suffix=")"/>
</group>
<text variable="page"/>
</group>
<text macro="accessed-date"/>
</group>
</macro>
<!-- 题名 -->
<macro name="title">
<group delimiter=", ">
<group delimiter=": ">
<text variable="title"/>
<group delimiter=" ">
<choose>
<if variable="container-title" type="paper-conference" match="none">
<text macro="book-volume"/>
</if>
</choose>
<choose>
<if type="bill legal_case legislation patent regulation report standard" match="any">
<text variable="number"/>
</if>
</choose>
</group>
</group>
<choose>
<if variable="container-title" type="paper-conference" match="none">
<choose>
<if variable="event-date">
<text variable="event-place"/>
<date variable="event-date" form="text"/>
</if>
</choose>
</if>
</choose>
</group>
<text macro="type-code" prefix="[" suffix="]"/>
</macro>
<!-- 文献类型标识 -->
<macro name="type-code">
<group delimiter="/">
<choose>
<if type="article">
<choose>
<if variable="archive">
<text value="A"/>
</if>
<else>
<text value="M"/>
</else>
</choose>
</if>
<else-if type="article-journal article-magazine periodical" match="any">
<text value="J"/>
</else-if>
<else-if type="article-newspaper">
<text value="N"/>
</else-if>
<else-if type="bill collection legal_case legislation regulation" match="any">
<text value="A"/>
</else-if>
<else-if type="book chapter" match="any">
<text value="M"/>
</else-if>
<else-if type="dataset">
<text value="DS"/>
</else-if>
<else-if type="map">
<text value="CM"/>
</else-if>
<else-if type="paper-conference">
<text value="C"/>
</else-if>
<else-if type="patent">
<text value="P"/>
</else-if>
<else-if type="post post-weblog webpage" match="any">
<text value="EB"/>
</else-if>
<else-if type="report">
<text value="R"/>
</else-if>
<else-if type="software">
<text value="CP"/>
</else-if>
<else-if type="standard">
<text value="S"/>
</else-if>
<else-if type="thesis">
<text value="D"/>
</else-if>
<else>
<text value="Z"/>
</else>
</choose>
<!-- <choose>
<if variable="URL DOI" match="any">
<text value="OL"/>
</if>
</choose> -->
<!-- 自行补充的内容 -->
<choose>
<if type="post post-weblog webpage" match="any">
<text value="OL"/>
</if>
</choose>
</group>
</macro>
<!-- 获取和访问路径以及 DOI -->
<!-- 自己修改的,修改后只针对网页链接添加DOI -->
<macro name="url-doi">
<choose>
<if type="post post-weblog webpage" match="any">
<group delimiter=". ">
<text variable="URL"/>
<text variable="DOI" prefix="DOI:"/>
</group>
</if>
</choose>
</macro>
<!-- 连续出版物的年卷期 -->
<macro name="year-volume-issue">
<group>
<group delimiter=", ">
<text macro="issued-year"/>
<text variable="volume"/>
</group>
<text variable="issue" prefix="(" suffix=")"/>
</group>
</macro>
<!-- 专著和电子资源 -->
<macro name="monograph-layout">
<group delimiter=". " suffix=".">
<text macro="author"/>
<text macro="title"/>
<text macro="secondary-contributor"/>
<text macro="edition"/>
<text macro="publishing"/>
<text macro="url-doi"/>
</group>
</macro>
<!-- 专著中的析出文献 -->
<macro name="chapter-in-book-layout">
<group delimiter=". " suffix=".">
<text macro="author"/>
<group delimiter="//">
<group delimiter=". ">
<text macro="title"/>
<text macro="secondary-contributor"/>
</group>
<group delimiter=". ">
<text macro="container-author"/>
<text macro="container-title"/>
</group>
</group>
<text macro="edition"/>
<text macro="publishing"/>
<text macro="url-doi"/>
</group>
</macro>
<!-- 连续出版物 -->
<macro name="serial-layout">
<group delimiter=". " suffix=".">
<text macro="author"/>
<text macro="title"/>
<text macro="year-volume-issue"/>
<text macro="publishing"/>
<text variable="URL"/>
<text variable="DOI" prefix="DOI:"/>
</group>
</macro>
<!-- 连续出版物中的析出文献 -->
<macro name="article-in-periodical-layout">
<group delimiter=". " suffix=".">
<text macro="author"/>
<text macro="title"/>
<text macro="periodical-publishing"/>
<text macro="url-doi"/>
</group>
</macro>
<!-- 专利文献 -->
<macro name="patent-layout">
<group delimiter=". " suffix=".">
<text macro="author"/>
<text macro="title"/>
<group>
<text macro="issued-date"/>
<text macro="accessed-date"/>
</group>
<text macro="url-doi"/>
</group>
</macro>
<!-- 正文中引用的文献标注格式 -->
<macro name="citation-layout">
<group>
<text variable="citation-number"/>
</group>
</macro>
<!-- 参考文献表格式 -->
<macro name="entry-layout">
<choose>
<if type="article-journal article-magazine article-newspaper" match="any">
<text macro="article-in-periodical-layout"/>
</if>
<else-if type="periodical">
<text macro="serial-layout"/>
</else-if>
<else-if type="patent">
<text macro="patent-layout"/>
</else-if>
<else-if type="paper-conference" variable="container-title" match="any">
<text macro="chapter-in-book-layout"/>
</else-if>
<else>
<text macro="monograph-layout"/>
</else>
</choose>
</macro>
<citation collapse="citation-number" after-collapse-delimiter=",">
<layout vertical-align="sup" delimiter="," prefix="[" suffix="]">
<text macro="citation-layout"/>
</layout>
</citation>
<!--bibliography entry-spacing="0" et-al-min="4" et-al-use-first="3" second-field-align="flush"-->
<bibliography entry-spacing="0" et-al-min="4" et-al-use-first="3" line-spacing="1" second-field-align="flush">
<!-- 取消这部分注释可以使用 CSL-M 的功能支持双语 -->
<layout locale="en"><text variable="citation-number" prefix="[" suffix="]"/><text macro="entry-layout"/></layout>
<layout>
<text variable="citation-number" prefix="[" suffix="]"/>
<text macro="entry-layout"/>
</layout>
</bibliography>
</style>