【问题标题】:How to copy attribute of svg graphic (from html to excel)如何复制svg图形的属性(从html到excel)
【发布时间】:2023-03-07 13:23:01
【问题描述】:

我创建了一个宏,可以从网站在线库中查找人员(使用他们的密码),并且我想将一些关于他们的信息复制到 Excel 中(例如他们的姓名、状态等)。 搜索结果是一个包含文本和对象 (svg) 的表,我在将对象复制到 excel 时遇到问题。 搜索结果如下:

<td data-label="Full name">Name</td>
<td data-label="Status" class="results-status">
    <svg role="presentation"><use xlink:href="/static/img/sprite.svg#status-caution"></use></svg>
</td>
<td data-label="Location">UK</td>
<td data-label="Details"></td>

所以我写了这个:

Set elements = doc.getElementsByTagName("td")
            Cells(i, 1).Value = elements(0).innerText
            Cells(i, 3).Value = elements(1).innerText
            Cells(i, 4).Value = elements(2).innerText
            Cells(i, 5).Value = elements(3).innerText

不幸的是,我没有从元素 (1) 中得到任何东西。我猜它不起作用,因为这是一个对象而不是文本。

我必须从第二行 (/static/img/sprite.svg#status-caution) 获取状态信息,但我不知道该怎么做。我也试过img = elements(1).getAttribute() 但 msgbox(img) 只显示了这个:[native code]

【问题讨论】:

  • 可以尝试innerhtml,然后进行字符串解析。
  • innerhtml 给了我 svg 代码,而不是 部分。这是我使用 innerhtml 得到的: w3.org/2000/svg" role="presentation" viewBox="0 0 40 40"> 如果我只能将此图形粘贴到优秀

标签: html excel vba svg explorer


【解决方案1】:

请试试这个:

Cells(i, 3).Value = elements(1).Children(0).Children(0).getAttribute("xlink:href")

解释:

  1. 由于元素设置为doc.getElementsByTagName("td"),且标签名“use”在两级以下,所以添加Children(0).Children(0).firstElementChild.firstElementChild得到href

  2. elements(1).Children(0).Children(0).href 什么也得不到,取而代之的是用.getAttribute("xlink:href") 替换.href

或者另一种解决方案是:

Set elements = doc.getElementsByClassName("results-status")

Cells(i, 3).Value = elements(0).firstElementChild.firstElementChild.getAttribute("xlink:href")

Attached screenshot of working code and tested

【讨论】:

  • 任何让提问者朝着正确方向前进的答案都是有帮助的,但请尝试在您的答案中提及任何限制、假设或简化。简洁是可以接受的,但更全面的解释会更好。
  • 在你的答案中添加一点解释,否则它将被关闭。
猜你喜欢
  • 2019-01-02
  • 2018-06-17
  • 2018-02-12
  • 1970-01-01
  • 1970-01-01
  • 2013-10-11
  • 1970-01-01
  • 1970-01-01
  • 2017-07-28
相关资源
最近更新 更多