|
本帖最后由 976190982 于 2020-8-26 10:26 编辑
事情是这样的,我是搞外贸的,最近搞了一个品牌,需要中文翻译英文,无奈想找一个好点的翻译代码,于是找我朋友,朋友扔过来,他也不懂,实在是没法了,于是向老师们请教。
translateFrom = "zh" 源语言
translateTo = "en" 目标语言 实测定义好后,其余不需要修改- 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.cn/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
- Function ConvertToGet(val As String)
- val = Replace(val, " ", "+")
- val = Replace(val, vbNewLine, "+")
- val = Replace(val, "(", "%28")
- val = Replace(val, ")", "%29")
- ConvertToGet = val
- End Function
- Function Clean(val As String)
- val = Replace(val, """, """")
- val = Replace(val, "%2C", ",")
- val = Replace(val, "'", "'")
- Clean = val
- End Function
- Public Function RegexExecute(str As String, reg As String, _
- Optional matchIndex As Long, _
- Optional subMatchIndex As Long) As String
- On Error GoTo ErrHandl
- Set regex = CreateObject("VBScript.RegExp"): regex.Pattern = reg
- regex.Global = Not (matchIndex = 0 And subMatchIndex = 0) 'For efficiency
- If regex.Test(str) Then
- Set matches = regex.Execute(str)
- RegexExecute = matches(matchIndex).SubMatches(subMatchIndex)
- Exit Function
- End If
- ErrHandl:
- RegexExecute = CVErr(xlErrValue)
- End Function
复制代码 我测试的结果如下
1、会被封ip,太多次就会。
2、使用方式是 先选取要翻译的区域然后运行TranslateCell,即可,但是太多了,会被封掉,求老师们帮忙瞅一下,麻烦了
3、另附一个问题,我需要翻译的数据放在A列,翻译结果放在B列,求老师们帮忙改,对于做外贸的我,这个实在是刚需啊。求老师们帮助。
翻译.zip
(13.85 KB, 下载次数: 33)
在此感谢经常帮助我的老师,@liulang0808 @chxw68 @一把小刀闯天下,还有其余跟多老师,在此就不提名了,谢谢你们,也谢谢这个论坛。
我费劲心思找到了原网站https://analystcave.com/excel-google-translate-functionality/
由于是国外的网站我准备了图片,希望能帮到老师们
图片上传后不清晰,麻烦老师们下载图片附件
Excel Google Translate functionality - Analyst Cav.zip
(598.44 KB, 下载次数: 27)
老师们,我在论坛找到了相关的文档,是自定义公式版本,但是没有VBA版本的,(因实际需要中,是一个宏里面也是之前老师帮助写的,所以想跟那个宏一起同步使用)所以想求老师们帮助改成指定区域循环的,本人功力浅薄,只能求助于老师们。
http://club.excelhome.net/thread-1508743-2-1.html
代码如下:
- Public Function EnToCh(Rng As String)
- Dim xml
- Dim URL$, EngSentence$
- Set xml = CreateObject("MSXML2.XMLHTTP")
- aasc = Asc(Rng)
- If aasc > 64 And aasc < 123 Then
- URL = "enN&tl=zh-CN&ie=UTF-8&prev=_m&q=" & Rng
- Else
- URL = "zh-CN&tl=enN&ie=UTF-8&prev=_m&q=" & GetURL(Rng)
- End If
- URL = "https://translate.google.cn/m?hl=en&sl=" & URL
- With xml
- .Open "GET", URL, False
- .send
- V = .responseText
- ' Debug.Print V '
- If InStr(V, "t0"">") > 0 Then
- EnToCh = Replace(Split(Split(V, "t0"">")(1), "<")(0), "'", "'")
- End If
- End With
- End Function
- Function ChrU(txt)
- txt = "\u6666" & txt
- sr = Split(txt, "\u") '按汉字单位拆分
- For i = 0 To UBound(sr) '遍历
- If Len(sr(i)) > 3 Then sr(i) = ChrW("&H" & Left(sr(i), 4)) & Mid(sr(i), 5)
- '如果4个字符以上 则取前4个字符(汉字的16进制国际双位码值) 添加"&H"转为16进制数
- '然后用ChrW函数即可返回汉字
- Next
- ChrU = Mid(Join(sr, ""), 2) '把转换后的汉字以及其余字符合并起来返回
- End Function
- Function sjc$() '时间戳
- 'sjc = (Now - 25569 - 1 / 3) * 86400
- sjc = (Now - 25570 + 2 / 3) * 86400 + 100
- sjc = "=" & Left(sjc & Rnd * 100000, 13)
- 'Debug.Print sjc
- End Function
- Function GetURL$(txt$)
- 'Dim a() As Byte
- 'a = StrConv(txt, vbFromUnicode, &H804)
- For i = 1 To Len(txt)
- txt1 = Mid(txt, i, 1)
- If Abs(Asc(txt1)) < 128 Then
- GetURL = GetURL & txt1
- Else
- GetURL1 = Application.Hex2Bin(Left(Hex(AscW(txt1)), 2), 8)
- GetURL1 = GetURL1 & Application.Hex2Bin(Right(Hex(AscW(txt1)), 2), 8)
- GetURL1 = Application.Replace(Application.Replace(GetURL1, 11, , 10), 5, , 10)
- GetURL2 = "%E" & Application.Bin2Hex(Left(GetURL1, 4))
- GetURL2 = GetURL2 & "%" & Application.Bin2Hex(Mid$(GetURL1, 5, 8))
- GetURL2 = GetURL2 & "%" & Application.Bin2Hex(Mid$(GetURL1, 13, 8))
- GetURL = GetURL & GetURL2
- End If
- Next
- End Function
- '如果一律转码是: GetURL = GetURL & "%" & Right("0" & Hex(a(i)), 2)
复制代码
|
|