Google Translate API in Excel 只给出中文音译
Google Translate API in Excel only gives chinese transliteration
我正在使用 Google Translate API in Excel 所以我可以将中文翻译成英文。但是,如果我想把英文翻译成中文,我只能得到中文音译,而不能得到中文翻译letter/alphabet。
我必须改变某事吗?在代码中还是我必须更改 Excel 设置?
谢谢!
Public Function Translate(rng As Range, Optional translateFrom As String = "en", Optional translateTo As String = "zh")
Dim getParam As String, trans As String, objHTTP As Object, URL As String
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
getParam = ConvertToGet(rng.Value)
URL = "https://translate.google.com/m?hl=" & translateFrom & "&sl=" & translateFrom & "&tl=" & translateTo & "&ie=UTF-8&prev=_m&q=" & getParam
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, "div dir=""ltr""") > 0 Then
trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?>(.+?)</div>")
Translate = Clean(trans)
Else
Translate = CVErr(xlErrValue)
End If
End Function
Sub TranslateCell()
Dim getParam As String, trans As String, translateFrom As String, translateTo As String
translateFrom = "zh"
translateTo = "en"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Dim r As Range, cell As Range
Set cell = Selection
For Each cell In Selection.Cells
getParam = ConvertToGet(cell.Value)
URL = "https://translate.google.com/m?hl=" & translateFrom & "&sl=" & translateFrom & "&tl=" & translateTo & "&ie=UTF-8&prev=_m&q=" & getParam
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, "div dir=""ltr""") > 0 Then
trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?>(.+?)</div>")
cell.Value = Clean(trans)
Else
MsgBox ("Error")
End If
Next cell
End Sub
页面 returns 两个 <div>
所以要么选择第一个
trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?""t0"".*?>(.+?)</div>")
或在此处使用以下答案
Extract content of div from Google Translate with VBA
我正在使用 Google Translate API in Excel 所以我可以将中文翻译成英文。但是,如果我想把英文翻译成中文,我只能得到中文音译,而不能得到中文翻译letter/alphabet。
我必须改变某事吗?在代码中还是我必须更改 Excel 设置?
谢谢!
Public Function Translate(rng As Range, Optional translateFrom As String = "en", Optional translateTo As String = "zh")
Dim getParam As String, trans As String, objHTTP As Object, URL As String
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
getParam = ConvertToGet(rng.Value)
URL = "https://translate.google.com/m?hl=" & translateFrom & "&sl=" & translateFrom & "&tl=" & translateTo & "&ie=UTF-8&prev=_m&q=" & getParam
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, "div dir=""ltr""") > 0 Then
trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?>(.+?)</div>")
Translate = Clean(trans)
Else
Translate = CVErr(xlErrValue)
End If
End Function
Sub TranslateCell()
Dim getParam As String, trans As String, translateFrom As String, translateTo As String
translateFrom = "zh"
translateTo = "en"
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
Dim r As Range, cell As Range
Set cell = Selection
For Each cell In Selection.Cells
getParam = ConvertToGet(cell.Value)
URL = "https://translate.google.com/m?hl=" & translateFrom & "&sl=" & translateFrom & "&tl=" & translateTo & "&ie=UTF-8&prev=_m&q=" & getParam
objHTTP.Open "GET", URL, False
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.send ("")
If InStr(objHTTP.responseText, "div dir=""ltr""") > 0 Then
trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?>(.+?)</div>")
cell.Value = Clean(trans)
Else
MsgBox ("Error")
End If
Next cell
End Sub
页面 returns 两个 <div>
所以要么选择第一个
trans = RegexExecute(objHTTP.responseText, "div[^""]*?""ltr"".*?""t0"".*?>(.+?)</div>")
或在此处使用以下答案 Extract content of div from Google Translate with VBA