google-sheets - 将 IMPORTXML Xpath 查询格式化为 Google 表格的可读数据

标签 google-sheets xpath split google-query-language textjoin

我正在将 XML 数据导入到具有以下结构的 Google 工作表中: 编辑:网址:https://sonicstate.com/news/tools/revive_stats/client_camp_3785.xml

<data>
    <campaignId>15802</campaignId>
    <campaignName>Some name</campaignName>
      <startDate>
       <year>2021</year>
       <month>12</month>
       <day>02</day>
     </startDate>
     <endDate>
       <year>2021</year>
       <month>12</month>
       <day>13</day>
     </endDate>
</data>
<data>
.... another record
</data>

我希望导入多行结果并连接日期值,以便它们可以显示在工作表中可管理的行中,然后我可以查询,而其他值将显示在它们自己的单元格中,每条记录对应一行。 例如

15802 | Some name | 2021/12/02 | 2021/12/13

15803 | Another name | 2021/11/30 | 2021/12/04

我已经尝试过:

IMPORTXML("myurl" , "//data/campaignId | //data/campaignName | //data/startDate/year | //data/startDate/month|//data/startDate/day")

但每个值都会返回一个单独的行,其中包含年、月、日的单元格 例如:

15802
Some Name
Year | Month | Day
15802
Another Name
Year | Month | Day
etc

我也尝试过:


IMPORTXML("myurl" , "concat(//data/campaignId , //data/campaignName , //data/startDate/year,'/', //data/startDate/month,'/',//data/startDate/day")

但是这只返回一条记录。我正在努力寻找合适的术语来搜索我想要实现的目标。此外,Sheets XMLIMPORT 使用 XPath 1.0,这限制了可用的功能

最佳答案

尝试:

=INDEX(SUBSTITUTE(TRIM(SPLIT(FLATTEN(SPLIT(QUERY(FLATTEN(QUERY(TRANSPOSE(
 IFERROR(IF(0=MOD(ROW(A:A)-1, 5)-{0, 1, 4, 3, 2}, {"♦","","","",""}&TEXT(
 IMPORTXML(A1, "//data/campaignId|//data/campaignName|//data/startDate/year|//data/startDate/month|//data/startDate/day"), 
 {"@","@","@","@","@"})&{"♠","♠","","♣","♣"}, ))),,9^9)),,9^9), "♦")), "♠")), "♣ ", "/"))

enter image description here

formula explanation


较短的效果:

=INDEX(SUBSTITUTE(TRIM(SPLIT(FLATTEN(SPLIT(QUERY(IFNA(
 CHOOSE(MATCH(MOD(SEQUENCE(999)-1, 5), {0, 1}), "♦", )&
 IMPORTXML(A1, "//data/campaignId|//data/campaignName|//data/startDate/year|//data/startDate/month|//data/startDate/day")&
 CHOOSE(MATCH(MOD(SEQUENCE(999)-1, 5), {0, 1, 2, 3, 4}), "♠","♠","♣","♣","")),,9^9), "♦")), "♠")), "♣ ", "/"))

enter image description here

关于google-sheets - 将 IMPORTXML Xpath 查询格式化为 Google 表格的可读数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69786049/

相关文章:

正则表达式匹配句子中的数字字符串(带有可选文本)

google-apps-script - GAS 脚本是否可以锁定 Google 表格,以便在脚本完成之前没有其他人可以更改它

wordpress - 网页抓取 - XPath - 查找 div 中特定标签的所有元素

xml - 为什么简单的 XPATH 不起作用

java - xpath 错误 : null on jaxb external bindings file

go - 返回 Golang 中 strings.Split() slice 的最后一项

javascript - 使用 for 循环增量打印数组项

javascript - 代码运行速度太慢

c# - 在 C# 中拆分字符串

string - 在空格上拆分字符串但忽略双引号之间的部分