QUERY IMPORTRANGE中如何通过命名范围或标题引用列?

huangapple go评论65阅读模式
英文:

QUERY IMPORTRANGE where Columns are referenced by Named Range or Header?

问题

我正在从其他电子表格导入范围并使用QUERY进行筛选。

但我试图使我的QUERY更具弹性,以适应更改,如添加或删除列,因为当添加新列或删除旧列时,它可能会出错。我想参考列时不是按其编号(例如Col1、Col2等),而是按标题或命名范围(首选)来引用它。

基本上,我想引用第18列(R)而不是使用"Col18",这样做的方式不会在添加/删除任何其他列时中断。我想通过将列命名为命名范围并在QUERY中引用它来实现这一点。

所以,不是:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col18 is not null";0)

而是:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col"&COLUMN(namedRange)&" is not null";0)

不幸的是,这不起作用。尽管根据这里的回答应该可以。

错误是

> 未知的范围名称:'namedRange'。

如果我尝试类似以下的内容,会出现相同的错误:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where "&namedRange&" is not null";0)

作为测试的一部分,唯一成功的方法是,如果导入的范围(因此命名范围)位于导入它们的同一电子表格中,但对我来说这不起作用(因为我正在从其他电子表格导入范围),而且这也违背了使用IMPORTRANGE的初衷。似乎“COLUMN”公式在与来自另一电子表格的IMPORTRANGE一起使用时无法看到命名范围。

我是做错了什么吗,还是有其他方法可以做到这一点?

英文:

I am importing ranges from other spreadsheets and filtering them using QUERY.

But I am trying to make my QUERY more resilient to changes, such as addition or removal of columns, as it can break when new columns are added or old removed. Instead of referencing the column by its number (e.g. Col1, Col2, etc.), I would like to reference it either by the header or by the named range (preferred).

Essentially, instead of referencing, for example, the Column 18 (R) as "Col18", I would reference it in a way that won't break if any other columns are removed/added. Which, I figured, could be done by making the column a named range and referencing it as such in QUERY.

So, instead of:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col18 is not null";0)

It would be:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col"&COLUMN(namedRange)&" is not null";0)

This, unfortunately, does not work. Despite supposedly working according to the answer here.

The error is

> Unknown range name: 'namedRange'.

The same error appears if I try something like:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where "&namedRange&" is not null";0)

As part of testing, the only way I managed to get this to "work" is if the imported range (and thus named range) are in the same spreadsheet that they are being imported in, but that will not work for me (since I am importing ranges from other spreadsheets) and also beats the purpose of using IMPORTRANGE. It seems like the "COLUMN" formula does not see the named range when used with IMPORTRANGE from another Spreadsheet.

Am I doing something wrong, or is there an alternative way of doing this?

答案1

得分: 1

请问您能尝试在Column函数内添加一个新的IMPORTRANGE吗?:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col"&COLUMN(IMPORTRANGE ("https://docs.google.com/spreadsheets/d/.."),namedRange))&" is not null";0)

如果这不起作用,我认为您应该尝试添加标题。然后您可以使用Match找到列号:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col"&MATCH("HeaderExample",IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A1:W1"),0)&" is not null";0)

或者,为了避免重复IMPORTRANGE:

=LET(imp,IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W"),

QUERY({imp}; "Select Col1 where Col"&MATCH("HeaderExample", INDEX (imp,1),0)&" is not null";0))

更新

两种方法的结合: 找到命名范围中的第一个值,以便与标题匹配

=LET(imp,IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W"),

QUERY({imp}; "Select Col1 where Col"&MATCH(INDEX(IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; namedRange),1), INDEX (imp,1),0)&" is not null";0))

英文:

Could you please try adding a new IMPORTRANGE inside the Column function?:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col"&COLUMN(IMPORTRANGE ("https://docs.google.com/spreadsheets/d/.."),namedRange))&" is not null";0)

If this doesn't work, then I think you should try with the headers. Then you could use Match to find the column numbers:

=QUERY({IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W")}; "Select Col1 where Col"&MATCH("HeaderExample",IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A1:W1"),0)&" is not null";0)

Or, in order not to repeat the IMPORTRANGE:

    =LET(imp,IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W"),
QUERY({imp}; "Select Col1 where Col"&MATCH("HeaderExample", INDEX (imp,1),0)&" is not null";0))

UPDATE

Combination of both methods: Finding first value of named range in order to match it with the headers

    =LET(imp,IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; "Sheet1!A:W"),
QUERY({imp}; "Select Col1 where Col"&MATCH(INDEX(IMPORTRANGE("https://docs.google.com/spreadsheets/d/..."; namedRange),1), INDEX (imp,1),0)&" is not null";0))

huangapple
  • 本文由 发表于 2023年4月4日 06:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定