【发布时间】:2011-04-23 15:43:56
【问题描述】:
我有一个条形码阅读器和一堆书。对于每本书,我想在 Excel 电子表格中列出书名和作者。
我的观点是,一些连接到亚马逊网络服务的 VBA 代码会使这更容易。
我的问题是 - 以前没有人这样做过吗?你能指出我最好的例子吗?
【问题讨论】:
标签: vba excel amazon-web-services barcode
我有一个条形码阅读器和一堆书。对于每本书,我想在 Excel 电子表格中列出书名和作者。
我的观点是,一些连接到亚马逊网络服务的 VBA 代码会使这更容易。
我的问题是 - 以前没有人这样做过吗?你能指出我最好的例子吗?
【问题讨论】:
标签: vba excel amazon-web-services barcode
我认为这是一个简单的谷歌搜索,但结果比我预期的要困难。
事实上,我找不到一个基于 VBA ISBN 的程序来从网络上获取图书数据,所以决定做一个。
这是一个使用来自xisbn.worldcat.org 的服务的 VBA 宏。示例here.。这些服务是免费的,不需要身份验证。
为了能够运行它,您应该在工具-> 参考(在 VBE 窗口中)检查“Microsoft xml 6.0”库。
此宏从当前单元格中获取 ISBN(10 位数字),并用作者和标题填充以下两列。您应该能够轻松地遍历整列。
代码已经过测试(嗯,有点),但那里没有错误检查。
Sub xmlbook()
Dim xmlDoc As DOMDocument60
Dim xWords As IXMLDOMNode
Dim xType As IXMLDOMNode
Dim xword As IXMLDOMNodeList
Dim xWordChild As IXMLDOMNode
Dim oAttributes As IXMLDOMNamedNodeMap
Dim oTitle As IXMLDOMNode
Dim oAuthor As IXMLDOMNode
Set xmlDoc = New DOMDocument60
Set xWords = New DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
r = CStr(ActiveCell.Value)
xmlDoc.Load ("http://xisbn.worldcat.org/webservices/xid/isbn/" _
+ r + "?method=getMetadata&format=xml&fl=author,title")
Set xWords = xmlDoc
For Each xType In xWords.ChildNodes
Set xword = xType.ChildNodes
For Each xWordChild In xword
Set oAttributes = xWordChild.Attributes
On Error Resume Next
Set oTitle = oAttributes.getNamedItem("title")
Set oAuthor = oAttributes.getNamedItem("author")
On Error GoTo 0
Next xWordChild
Next xType
ActiveCell.Offset(0, 1).Value = oTitle.Text
ActiveCell.Offset(0, 2).Value = oAuthor.Text
End Sub
我没有通过亚马逊,因为他们新的“直截了当”的身份验证协议......
【讨论】:
这对我很有帮助!
我已经更新了宏,允许它循环通过一列 ISBN 编号,直到它到达一个空单元格。
它还搜索出版商、年份和版本。
如果某些字段不可用,我添加了一些基本的错误检查。
Sub ISBN()
Do
Dim xmlDoc As DOMDocument60
Dim xWords As IXMLDOMNode
Dim xType As IXMLDOMNode
Dim xword As IXMLDOMNodeList
Dim xWordChild As IXMLDOMNode
Dim oAttributes As IXMLDOMNamedNodeMap
Dim oTitle As IXMLDOMNode
Dim oAuthor As IXMLDOMNode
Set xmlDoc = New DOMDocument60
Set xWords = New DOMDocument60
xmlDoc.async = False
xmlDoc.validateOnParse = False
r = CStr(ActiveCell.Value)
xmlDoc.Load ("http://xisbn.worldcat.org/webservices/xid/isbn/" _
+ r + "?method=getMetadata&format=xml&fl=author,title,year,publisher,ed")
Set xWords = xmlDoc
For Each xType In xWords.ChildNodes
Set xword = xType.ChildNodes
For Each xWordChild In xword
Set oAttributes = xWordChild.Attributes
On Error Resume Next
Set oTitle = oAttributes.getNamedItem("title")
Set oAuthor = oAttributes.getNamedItem("author")
Set oPublisher = oAttributes.getNamedItem("publisher")
Set oEd = oAttributes.getNamedItem("ed")
Set oYear = oAttributes.getNamedItem("year")
On Error GoTo 0
Next xWordChild
Next xType
On Error Resume Next
ActiveCell.Offset(0, 1).Value = oTitle.Text
On Error Resume Next
ActiveCell.Offset(0, 2).Value = oAuthor.Text
On Error Resume Next
ActiveCell.Offset(0, 3).Value = oPublisher.Text
On Error Resume Next
ActiveCell.Offset(0, 4).Value = oYear.Text
On Error Resume Next
ActiveCell.Offset(0, 5).Value = oEd.Text
ActiveCell.Offset(1, 0).Select
Loop Until IsEmpty(ActiveCell.Value)
End Sub
【讨论】:
当我试图做同样的事情时,我刚刚发现了这个帖子。不幸的是,我在 MAC 上,所以这些答案无济于事。通过一些研究,我能够通过这个模块让它在 MAC Excel 中工作:
Option Explicit
' execShell() function courtesy of Robert Knight via StackOverflow
' http://stackoverflow.com/questions/6136798/vba-shell-function-in-office- 2011-for-mac
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As Long
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Function HTTPGet(sUrl As String) As String
Dim sCmd As String
Dim sResult As String
Dim lExitCode As Long
Dim sQuery As String
sQuery = "method=getMetadata&format=xml&fl=*"
sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
sCmd = "curl --get -d """ & sQuery & """" & " " & sUrl
sResult = execShell(sCmd, lExitCode)
' ToDo check lExitCode
HTTPGet = sResult
End Function
Function getISBNData(isbn As String) As String
Dim sUrl As String
sUrl = "http://xisbn.worldcat.org/webservices/xid/isbn/" & isbn
getISBNData = HTTPGet(sUrl)
End Function
Function getAttributeForISBN(isbn As String, info As String) As String
Dim data As String
Dim start As Integer
Dim finish As Integer
data = getISBNData(isbn)
start = InStr(data, info) + Len(info) + 2
finish = InStr(start, data, """")
getAttributeForISBN = Mid(data, start, finish - start)
End Function
这不是我的全部原创作品,我从另一个网站将它们粘贴在一起,然后自己完成了。现在您可以执行以下操作:
getAttributeForISBN("1568812019","title")
这将返回该书的标题。当然,您可以将此公式应用于 A 列中的所有 ISBN,以查找多个标题、作者或其他任何内容。
希望这对其他人有帮助!
【讨论】:
如果条形码是 ISBN,这似乎很可能,也许您可以使用:amazon.com/Advanced-Search-Books/b?ie=UTF8&node=241582011
【讨论】: