首页 > 解决方案 > 从列标题返回列值

问题描述

在回答我的问题时(我将留下下面的链接),使用的公式和结果如下:

https://stackoverflow.com/a/65451225/11462274

=IMPORTXML("https://www.soccerstats247.com/predictions/brazil/serie-a/",
           "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr")

在此处输入图像描述

列可以更改位置,但每个列的标题都遵循新列的位置,所以我怎么能要求一个公式来搜索列所在的title X位置并从中找到值⚽ Matches

在这种情况下,值将是: 通过 VLOOKUP 解释:

=ARRAYFORMULA(IFERROR(VLOOKUP(UNIQUE(B2:B),B1:F,3,FALSE)))

在此处输入图像描述

问题在于带有VLOOKUP的列title X可能并不总是在column D并且⚽ Matches可能并不总是在column B

标签: google-sheetsgoogle-sheets-formula

解决方案


利用:

 =ARRAYFORMULA(IFERROR(VLOOKUP(UNIQUE(B2:B), 
  {FLATTEN(QUERY(TO_TEXT(TRANSPOSE(IMPORTXML(
  "https://www.soccerstats247.com/predictions/brazil/serie-a/",
  "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr"))), 
  "where Col1 contains 'Matches'", 0)), IMPORTXML(
  "https://www.soccerstats247.com/predictions/brazil/serie-a/",
  "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr")}, 5, 0)))

例如。部分是:

=INDEX(FLATTEN(QUERY(TO_TEXT(TRANSPOSE(IMPORTXML(
 "https://www.soccerstats247.com/predictions/brazil/serie-a/",
 "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr"))), 
 "where Col1 contains 'Matches'", 0))

和完整的公式:

 =ARRAYFORMULA(IFERROR(VLOOKUP(UNIQUE(B2:B), 
  {FLATTEN(QUERY(TO_TEXT(TRANSPOSE(IMPORTXML(
  "https://www.soccerstats247.com/predictions/brazil/serie-a/",
  "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr"))), 
  "where Col1 contains 'Matches'", 0)), IMPORTXML(
  "https://www.soccerstats247.com/predictions/brazil/serie-a/",
  "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr")}, 
  MATCH("X", INDEX(IMPORTXML(
  "https://www.soccerstats247.com/predictions/brazil/serie-a/",
  "//table[@id='soccerContentPlaceHolder_gv1X2Predictions']/tbody/tr"), 1), 0)+1, 0)))

推荐阅读