如何修复奇怪的Google Script错误:“丢失;在声明之前。“

时间:2016-12-13 14:09:21

标签: excel excel-vba function vba

我正在尝试使用我发现的“字典”脚本来定义列中的单词。谷歌向我抛出了这个错误,我对如何解决这个问题毫无头绪。我使用谷歌应用程序脚本可以理解的任何东西吗?这是我用来将此功能插入excel的网站:https://script.google.com

代码:

Function DefineWord(wordToDefine As String) As String
' Array to hold the response data.
Dim d() As Byte
Dim r As Research


Dim myDefinition As String
Dim PARSE_PASS_1 As String
Dim PARSE_PASS_2 As String
Dim PARSE_PASS_3 As String
Dim END_OF_DEFINITION As String

'These "constants" are for stripping out just the definitions from the JSON data
PARSE_PASS_1 = Chr(34) & "webDefinitions" & Chr(34) & ":"
PARSE_PASS_2 = Chr(34) & "entries" & Chr(34) & ":"
PARSE_PASS_3 = "{" & Chr(34) & "type" & Chr(34) & ":" & Chr(34) & "text" & Chr(34) & "," & Chr(34) & "text" & Chr(34) & ":"
END_OF_DEFINITION = "," & Chr(34) & "language" & Chr(34) & ":" & Chr(34) & "en" & Chr(34) & "}"
Const SPLIT_DELIMITER = "|"

' Assemble an HTTP Request.
Dim url As String
Dim WinHttpReq As Variant
Set WinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")

'Get the definition from Google's online dictionary:
url = "http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=" & wordToDefine & "&sl=en&tl=en&restrict=pr%2Cde&client=te"
WinHttpReq.Open "GET", url, False

' Send the HTTP Request.
WinHttpReq.Send

'Print status to the immediate window
Debug.Print WinHttpReq.Status & " - " & WinHttpReq.StatusText

'Get the defintion
myDefinition = StrConv(WinHttpReq.ResponseBody, vbUnicode)

'Get to the meat of the definition
myDefinition = Mid$(myDefinition, InStr(1, myDefinition, PARSE_PASS_1, vbTextCompare))
myDefinition = Mid$(myDefinition, InStr(1, myDefinition, PARSE_PASS_2, vbTextCompare))
myDefinition = Replace(myDefinition, PARSE_PASS_3, SPLIT_DELIMITER)

'Split what's left of the string into an array
Dim definitionArray As Variant
definitionArray = Split(myDefinition, SPLIT_DELIMITER)
Dim temp As String
Dim newDefinition As String
Dim iCount As Integer

'Loop through the array, remove unwanted characters and create a single string containing all the definitions
For iCount = 1 To UBound(definitionArray) 'item 0 will not contain the definition
    temp = definitionArray(iCount)
    temp = Replace(temp, END_OF_DEFINITION, SPLIT_DELIMITER)
    temp = Replace(temp, "\x22", "")
    temp = Replace(temp, "\x27", "")
    temp = Replace(temp, Chr$(34), "")
    temp = iCount & ".  " & Trim(temp)
    newDefinition = newDefinition & Mid$(temp, 1, InStr(1, temp, SPLIT_DELIMITER) - 1) & vbLf  'Hmmmm....vbLf doesn't put a carriage return in the cell. Not sure what the deal is there.
Next iCount

'Put list of definitions in the Immeidate window
Debug.Print newDefinition

'Return the value
DefineWord = newDefinition
End Function

1 个答案:

答案 0 :(得分:1)

这看起来像是visual basic,Google使用的是基本上是javascipt的Apps脚本。在Javascript中,您使用分号终止语句,这就是它正在寻找的内容。

链接到这里:Google Help Forum