zoteroPane=Zotero.getActiveZoteroPane();items=zoteroPane.getSelectedItems();varresult="";for(itemofitems){vartitle=item.getField('title');result+=" "+title+"\n";varnew_title=title.replace(/\b([A-Z][a-z0-9]+|A)\b/g,function(x){returnx.toLowerCase();});new_title=new_title.replace(/(^|\?\s*)[a-z]/,function(x){returnx.toUpperCase();});result+="-> "+new_title+"\n\n";// Do it at your own risk
item.setField('title',new_title);awaititem.saveTx();}returnresult;
PublicSubZoteroLinkCitation()' get selected area (if applicable)DimnStart&,nEnd&nStart=Selection.StartnEnd=Selection.End' toggle screen updatingApplication.ScreenUpdating=False' define variablesDimtitleAsStringDimtitleAnchorAsStringDimstyleAsStringDimfieldCodeAsStringDimnumOrYearAsStringDimpos&,n1&,n2&,n3&ActiveWindow.View.ShowFieldCodes=TrueSelection.Find.ClearFormatting' find the Zotero bibliographyWithSelection.Find.Text="^d ADDIN ZOTERO_BIBL".Replacement.Text="".Forward=True.Wrap=wdFindContinue.Format=False.MatchCase=False.MatchWholeWord=False.MatchWildcards=False.MatchSoundsLike=False.MatchAllWordForms=FalseEndWithSelection.Find.Execute' add bookmark for the Zotero bibliographyWithActiveDocument.Bookmarks.AddRange:=Selection.Range,name:="Zotero_Bibliography".DefaultSorting=wdSortByName.ShowHidden=TrueEndWith' loop through each field in the documentForEachaFieldInActiveDocument.Fields' check if the field is a Zotero in-text reference'##################################################IfInStr(aField.Code,"ADDIN ZOTERO_ITEM")>0ThenfieldCode=aField.Code'#############' Prepare' Plain citation== Format of Textfield shown' must be in BracketsDimplain_CitAsStringplCitStrBeg="""plainCitation"":""["
plCitStrEnd = "]"""n1=InStr(fieldCode,plCitStrBeg)n1=n1+Len(plCitStrBeg)n2=InStr(Mid(fieldCode,n1,Len(fieldCode)-n1),plCitStrEnd)-1+n1plain_Cit=Mid$(fieldCode,n1-1,n2-n1+2)'Reference 'asshown' in word as a string'Title array in fieldCode (all referenced Titles within this field)Dimarray_RefTitle(32)AsStringi=0DoWhileInStr(fieldCode,"""title"":""")>0n1=InStr(fieldCode,"""title"":""")+Len("""title"":""")n2=InStr(Mid(fieldCode,n1,Len(fieldCode)-n1),""",""")-1+n1Ifn2<n1Then'Exception the type 'Article'n2=InStr(Mid(fieldCode,n1,Len(fieldCode)-n1),"}")-1+n1-1EndIfarray_RefTitle(i)=Mid(fieldCode,n1,n2-n1)fieldCode=Mid(fieldCode,n2+1,Len(fieldCode)-n2-1)i=i+1LoopTitles_in_Cit=i'Number array with References shown in PlainCit'Numer is equal or less than Titels, depending on the type'[3], [8]-[10]; [2]-[4]; [2], [4], [5]' All citations have to be in Brackets each! [3], [8] not [3, 8]' This doesnt work otherwise!' --> treatment of other delimiters could be implemented hereDimRefNumber(32)AsStringi=0DoWhile(InStr(plain_Cit,"]")OrInStr(plain_Cit,"["))>0n1=InStr(plain_Cit,"[")n2=InStr(plain_Cit,"]")RefNumber(i)=Mid(plain_Cit,n1+1,n2-(n1+1))plain_Cit=Mid(plain_Cit,n2+1,Len(plain_Cit)-(n2+1)+1)i=i+1LoopRefs_in_Cit=i'treat only the shown references (skip the rest)'[3], [8]-[10] --> skip [9]'Order of titles given from fieldcode, not checked!IfTitles_in_Cit>Refs_in_CitThenarray_RefTitle(Refs_in_Cit-1)=array_RefTitle(Titles_in_Cit-1)i=1DoWhileRefs_in_Cit+i<=Titles_in_Citarray_RefTitle(Refs_in_Cit+i-1)=""i=i+1LoopEndIf'#############'Make the linksForRefs=0ToRefs_in_Cit-1Step1title=array_RefTitle(Refs)array_RefTitle(Refs)=""' make title a valid bookmark nametitleAnchor=titletitleAnchor=MakeValidBMName(titleAnchor)ActiveWindow.View.ShowFieldCodes=FalseSelection.GoToWhat:=wdGoToBookmark,name:="Zotero_Bibliography"''locatethecorrespondingreferenceinthebibliography''bysearchingforitstitleSelection.Find.ClearFormattingWithSelection.Find.Text=Left(title,255).Replacement.Text="".Forward=True.Wrap=wdFindContinue.Format=False.MatchCase=False.MatchWholeWord=False.MatchWildcards=False.MatchSoundsLike=False.MatchAllWordForms=FalseEndWithSelection.Find.Execute' select the whole caption (for mouseover tooltip)Selection.MoveStartUntil("["),Count:=wdBackwardSelection.MoveEndUntil(vbBack)lnkcap="["&Selection.Textlnkcap=Left(lnkcap,70)' add bookmark for the reference within the bibliographySelection.ShrinkWithActiveDocument.Bookmarks.AddRange:=Selection.Range,name:=titleAnchor.DefaultSorting=wdSortByName.ShowHidden=TrueEndWith' jump back to the fieldaField.Select' find and select the numeric part of the field which will become the hyperlinkSelection.Find.ClearFormattingWithSelection.Find.Text=RefNumber(Refs).Replacement.Text="".Forward=True.Wrap=wdFindContinue.Format=False.MatchCase=False.MatchWholeWord=False.MatchWildcards=False.MatchSoundsLike=False.MatchAllWordForms=FalseEndWithSelection.Find.ExecutenumOrYear=Selection.Range.Text&""' store current style这一行如果不注释可能会存在格式变化' style = Selection.style' Generate the Hyperlink -->Forward!ActiveDocument.Hyperlinks.Addanchor:=Selection.Range,Address:="",SubAddress:=titleAnchor,ScreenTip:=lnkcap,TextToDisplay:=""&numOrYear' reset the style这一行如果不注释可能会存在格式变化' Selection.style = style' comment if you want standard link styleaField.SelectWithSelection.Font.Underline=wdUnderlineNone.Color=wdColorBlackEndWithNextRefs'References in CitEndIf'If Zotero-Field'#########################NextaField' next field' go back to original range selectedActiveWindow.View.ShowFieldCodes=FalseActiveDocument.Range(nStart,nEnd).SelectEndSubFunctionMakeValidBMName(strInAsString)DimpFirstChrAsStringDimiAsLongDimtempStrAsStringstrIn=Trim(strIn)pFirstChr=Left(strIn,1)IfNotpFirstChrLike"[A-Za-z]"ThenstrIn="A_"&strInEndIfFori=1ToLen(strIn)SelectCaseAsc(Mid$(strIn,i,1))Case49To57,65To90,97To122tempStr=tempStr&Mid$(strIn,i,1)CaseElsetempStr=tempStr&"_"EndSelectNextitempStr=Replace(tempStr," "," ")MakeValidBMName=Left(tempStr,40)EndFunction
Ctrl+s 保存,左下角重命名为 ZoteroLinkCitation,关闭页面,并关闭 Word