【问题标题】:Repeat country name for each of the leagues为每个联赛重复国家名称
【发布时间】:2023-03-08 10:35:01
【问题描述】:

当使用IMPORTXML解析值时,它会创建一个包含国家名称和联赛名称的列表,我想为每个联赛指定国家名称

当前公式:

=IMPORTXML("XXXXXXXXXX",
           "//country/title/text() | //country/leagues/title/text()")

导入结果:

Argentina
Liga Profesional Argentina
Austria
2. Liga
Bundesliga
Belarus
Premier League
Belgium
First Division B
First Division A

分成两列后(我将使用另一个公式分成两列):

=ArrayFormula(IFERROR(HLOOKUP(1,{1;IMPORTXML("XXXXXXXXXX","//country/title/text() | //country/leagues/title/text()")},(ROW(A1:A)+1)*2-TRANSPOSE(sort(ROW($A$1:$A$2)+0,1,0)))))
COUNTRIES           LEAGUES

Argentina           Liga Profesional Argentina
Austria             2. Liga
Bundesliga          Belarus
Premier League      Belgium
First Division B    First Division A

我尝试创建的公式:

=IMPORTXML("XXXXXXXXXX",
           "//country/title/text() and ..//country/leagues/title/text() | //country/leagues/title/text()")

预期的导入结果:

Argentina
Liga Profesional Argentina
Austria
2. Liga
Austria
Bundesliga
Belarus
Premier League
Belgium
First Division B
Belgium
First Division A

最终预期目标(我将使用另一个公式分为两列):

COUNTRIES     LEAGUES

Argentina     Liga Profesional Argentina
Austria       2. Liga
Austria       Bundesliga
Belarus       Premier League
Belgium       First Division B
Belgium       First Division A

总之,当每个国家/地区有多个联赛时,我需要一种方法来复制国家/地区名称。

【问题讨论】:

  • 例如,<title>Austria</title> 在 XML 数据中只能出现一次。但在您的预期结果中,有 2 个Austria。我无法理解实现目标的逻辑。可以问一下具体情况吗?
  • 感谢您回复和更新您的问题。请问Expected Result:Final Expected Goal的区别?在你目前的情况下,你已经可以获取到Expected Result:
  • 感谢您的回复。我不得不再次为我糟糕的英语水平道歉。在您的问题中,首先,您想要实现Expected Result:,下一步,您想要实现Final Expected Goa。我的理解正确吗?
  • 感谢您的回复。从Dividing the data into two columns I can do,你能提供你的公式检索Expected Import Result:吗?
  • 感谢您的回复和补充信息。当我测试你的公式时,似乎“国家”的标题包括“联盟”的标题。例如,“Bundesliga”包含在“COUNTRIES”中。并且“比利时”包含在“LEAGUES”中。我认为这与您的预期结果不同。我的理解正确吗?

标签: google-apps-script google-sheets google-sheets-formula


【解决方案1】:

我相信你的目标如下。

  • 您想在问题的底部实现Final Expected Goal
  • 在您的公式中,“COUNTRIES”的标题似乎包含“LEAGUES”的标题。例如,“Bundesliga”包含在“COUNTRIES”中。并且“比利时”包含在“LEAGUES”中。我认为这可能与您的预期结果不同。

模式一:

在此模式中,使用了电子表格中的内置函数。

  • 可以使用//country/title 的xpath 检索“COUNTRIES”的值。
  • 可以使用//leagues/title 的xpath 检索“LEAGUES”的值。
    • 在您的情况下,“COUNTRIES”中有多个“LEAGUES”值。
  • 为了检索每个“LEAGUES”的“COUNTRIES”,我使用了//leagues[position()>1]/id | //country/title,并引用了this site 中的示例公式。

从上面的 xpath 和公式,我想提出以下示例公式。

示例公式:

={
ARRAYFORMULA(LOOKUP(ROW(A2:A64),ROW(A2:A64)/IF(IMPORTXML(A1,"//leagues[position()>1]/id | //country/title")<>0,TRUE,FALSE),
IMPORTXML(A1,"//leagues[position()>1]/id | //country/title"))),IMPORTXML(A1,"//leagues/title")
}
  • 在此公式中,http://lineuptoday.com/api/matches/bydate?date=01&amp;month=11&amp;year=2020 的 URL 被放入单元格“A1”中。
  • 作为重要的一点,在这种情况下,需要设置最后一个行号(在你的情况下,它是64。)当最后一个行号没有设置时,公式变得有点复杂,过程成本变得非常高。因此,在这个答案中,作为另一种方法,我还想提出一个使用 Google Apps 脚本创建的自定义公式。在这种情况下,流程成本可能会有所降低。

结果:

模式 2:

在此模式中,使用使用 Google Apps 脚本创建的自定义函数。

示例脚本:

请把=SAMPLE("http://lineuptoday.com/api/matches/bydate?date=01&amp;month=11&amp;year=2020")放到一个单元格中。

function SAMPLE(url) {
  const res = UrlFetchApp.fetch(url);
  const root = XmlService.parse(res.getContentText()).getRootElement();
  const n = root.getNamespace();
  return root.getChildren().reduce((ar, c) => {
    const titleCountry = c.getChild("title", n).getText();
    c.getChildren("leagues", n).forEach(l => {
      ar.push([titleCountry, l.getChild("title", n).getText()])
    });
    return ar;
  }, []);
}
  • 在这种情况下,输出结果与上面的示例公式相同。但不需要设置最后一行。

注意:

  • 这些示例公式和脚本适用于 http://lineuptoday.com/api/matches/bydate?date=01&amp;month=11&amp;year=2020 的 URL。使用其他 URL 时,可能无法检索到您预期的结果。请注意这一点。

参考资料:

【讨论】:

    【解决方案2】:

    @Tanaike 的出色答案没有什么可补充的,但我想您可以过滤而不是固定范围:

    =ArrayFormula({vlookup(
            filter(row(A:A),A:A<>"",isna(vlookup(A:A,C:C,1,0))),
            {if(ISTEXT(vlookup(A:A,C:C,1,0)),row(A:A)),A:A},2),
         filter(A:A,A:A<>"",isna(vlookup(A:A,C:C,1,0)))})
    

    假设你有

    =IMPORTXML("http://lineuptoday.com/api/matches/bydate?date=01&month=11&year=2020","//country/title/text() | //country/leagues/title/text()")
    

    在 A 列和

    =IMPORTXML("http://lineuptoday.com/api/matches/bydate?date=01&month=11&year=2020","//country/title/text()")
    

    在 C 列中。

    如果需要,可以使用扁平化将两列合并为一列。

    顺便说一句,什么是英格兰超级联赛二级联赛 2?没听说过。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多