Google使用VBA转换单元格值

问题描述:

我正在尝试通过触发以下代码,使用自动检测功能将其他语言转换为英语.

I'm trying to convert other languages to English using auto detect by triggering the below code.

Sub transalte_using_vba()

Dim ie As Object, i As Long
Dim inputstring As String, outputstring As String, text_to_convert As String, result_data As String, CLEAN_DATA

Set ie = CreateObject("InternetExplorer.application")
        
        inputstring = "auto"
    
        outputstring = "en"
        
        text_to_convert = Sheet3.Range("A2")

 'open website

    ie.Visible = False
    ie.navigate "http://translate.google.com/#" & inputstring & "/" & outputstring & "/" & text_to_convert
   
    Do Until ie.ReadyState = 4
        DoEvents
    Loop
  
    Application.Wait (Now + TimeValue("0:00:5"))
    
    Do Until ie.ReadyState = 4
        DoEvents
    Loop

    CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(ie.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")

    For i = LBound(CLEAN_DATA) To UBound(CLEAN_DATA)
        result_data = result_data & Right(CLEAN_DATA(i), Len(CLEAN_DATA(i)) - InStr(CLEAN_DATA(i), ">"))
    Next
    Sheet3.Range("B2") = result_data
    ie.Quit
    MsgBox "Done", vbOKOnly
    
End Sub

但是我在CLEAN_DATA = Split(Application.WorksheetFunction.Substitute(ie.Document.getElementById("result_box").innerHTML, "</SPAN>", ""), "<")

代码有什么问题?

这段代码的工作速度有点慢.因为我需要处理超过70K的批量数据,有什么快速的方法吗?

This code is working bit slow.. as I need to work on bulk data more than 70K is there any quick way to do this?

在我的系统中,我将google chrome作为默认浏览器,我们可以使用它进行翻译,这可能有助于更快地运行脚本吗?

In my system I have google chrome as default browser and can we use it for translation, which may help to run the script faster?

基于Internet Explorer的解决方案从定义上讲非常慢.请尝试下一个功能:

A solution based on Internet Explorer is very slow by definition. Please, try the next function:

Private Function GTranslate(strInput As String, strFromLang As String, strToLang As String) As String
    Dim strURL As String, objHTTP As Object, objHTML As Object, objDivs As Object, objDiv As Variant
    
    strInput = WorksheetFunction.EncodeURL(strInput)
    strURL = "https://translate.google.com/m?hl=" & strFromLang & _
        "&sl=" & strFromLang & _
        "&tl=" & strToLang & _
        "&ie=UTF-8&prev=_m&q=" & strInput
        
    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    objHTTP.Open "GET", strURL, False
    objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.Send ""
    
    Set objHTML = CreateObject("htmlfile")
    With objHTML
        .Open
        .Write objHTTP.responseText
        .Close
    End With
    
    Set objDivs = objHTML.getElementsByTagName("div")
    For Each objDiv In objDivs
        If objDiv.className = "t0" Then
            GTranslate = objDiv.innerText: Exit For
        End If
    Next objDiv
    
    Set objHTML = Nothing: Set objHTTP = Nothing
End Function

可以通过以下简单方式对其进行测试:

It can be tested in this simple way:

Sub testTranslateG()
  Debug.Print GTranslate("Libro muy grande", "auto", "en")
End Sub

或转换范围内的单元格值:

Or to translate the cells value in a range:

Private Sub Google_translate()
  Dim thisWbs As Worksheet
  Dim i As Long, lastRow As Long
  
  Set thisWbs = ActiveSheet
  lastRow = thisWbs.Range("B" & rows.count).End(xlUp).row
  thisWbs.Range("C2:C" & lastRow).Clear
  
  For i = 2 To lastRow
    thisWbs.Range("C" & i).Value = GTranslate(thisWbs.Range("B" & i).Value, "auto", "en")
  Next i
  MsgBox "Ready..."
End Sub

为了获得更准确的翻译,您可以使用(而不是"auto"):对于西班牙语,"ru"对于俄语,"ro"表示对于罗马尼亚语,"nl"对于"Duch"等等.您可以通过查看Google翻译源并搜索英语"来找到语言的缩写.您会发现一个区域,其中所有可能出现的语言及其缩写都将被使用...

In order to obtain a more accurate translation, you can use (instead of "auto"): "es" for Spanish, "ru" for Russian, "ro" for Romanian, "nl" for "Duch" etc. You can find a language abbreviation by looking into Google Translate source and searching for 'English'. You will find an area where all possible language appear with their abbreviation to be used...