首页 > 解决方案 > 如何为多值 IMPORTXML 函数添加空白行?

问题描述

这是我的importxml:

=IMPORTXML(CA1,"//div[@class='1a']/div[@class='1b']/div[@class='1c']/img[@class='1d']/@src 
| //div[@class='2a']/span[@class='2b'] 
| //div[@class='3a']/span[@class='3b'] 
| //div[@class='4a']/span[@class='4b'] 
| //div[@class='5a']/span[@class='5b'] 
| //div[@class='6a']/span[6] 
| //div[@class='7a']/span[8] 
| //a/@title")

它显示如下内容:

img src
span1
span2
span3
span4
span5
span6
a title7

现在有时,缺少跨度或 img 之一。

所以它只会制作 7 或 6 或 5 等...行,并且只显示它找到的值。

是否可以添加一个空白行,而不是消除该行?

例如:

img src
span1
BLANK ROW
span3
span4
BLANK ROW
span6
a title7

代替:

img src
span1
span3
span4
span6
a title7

标签: xmlxpathgoogle-sheetsgoogle-sheets-formula

解决方案


您可以做的最简单的解决方案是将单个命令IMPORTXML分成多个IMPORTXML命令,然后IFERROR添加。将它组合成一个将防止您将每个不匹配的模式替换为无。

公式:

=IFERROR(CA1,"//div[@class='1a']/div[@class='1b']/div[@class='1c']/img[@class='1d']/@src]"), "BLANK ROW")
=IFERROR(CA1,"//div[@class='2a']/span[@class='2b']"), "BLANK ROW")
...
=IFERROR(CA1,"//div[@class='7a']/span[8]"), "BLANK ROW")
=IFERROR(CA1,"//a/@title"), "BLANK ROW")

样本:

样本 =IMPORTXML("https://www.xe.com/currencytables/?from=EUR&date=2020-10-31","(//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[1] | (//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[2] | (//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[3] | (//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[4]")

输出:

输出

=IFERROR(IMPORTXML("https://www.xe.com/currencytables/?from=EUR&date=2020-10-31","(//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[1]"), "BLANK ROW")
=IFERROR(IMPORTXML("https://www.xe.com/currencytables/?from=EUR&date=2020-10-31","(//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[2]"), "BLANK ROW")
=IFERROR(IMPORTXML("https://www.xe.com/currencytables/?from=EUR&date=2020-10-31","(//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[3]"), "BLANK ROW")
=IFERROR(IMPORTXML("https://www.xe.com/currencytables/?from=EUR&date=2020-10-31","(//h2[@class='heading__Heading1-n07sti-0 heading__Heading2-n07sti-1 iXbZUl'])[4]"), "BLANK ROW")

推荐阅读