问题
Consider the query run from sheet2
=query(Sheet!A7:A, "Select A,B,C where A='Order'")
but I want to put this in columns A,E,F in Sheet2.
I've found the workaround
=query(Sheet!A7:A, "Select A,X,Y,Z,B,C where A='Order'")
Where XYZ are blank columns from the end of the range. This seems clunky.
So far searches in both the query syntax on Google docs, and google product forums have been unproductive.
回答1:
Put the empty columns as literals in the query - cannot really be empty though, must contain a space like this
=query(Sheet!A7:C, "Select A,' ',' ',' ',B,C where A='Order'")
回答2:
I tried a version of ttarchala's response, just giving each blank column I wanted a different "signature", if you will:
=query(Sheet!A7:C, "Select A,' ',' ',' ',B,C where A='Order'")
Note the 1, 2, and 3 spaces in the quotes.
This worked - sort of. It gave me column A, then three blank columns, then B and C. However, it gave me a header row (which in my example I didn't want), and in the blank column headers I got:
| " "() | " "() | " "() |
Which was odd, but understandable. I hid the formula one row up and merged cells across A through C. Which left me with a nice blank cell. I took advantage of the fact that the output from query() can't be shown across merged cells.
This is of course a very specific solution - I had an empty row above my output that I could use and abuse for this purpose. But I thought I would mention anyway, just in case it gives a direction for better ideas.
UPDATE
Adding:
... LABEL ' ' '', ' ' '', ' ' ''
.. to the end of the query zeros out those odd headers that are added and removes the need for that extra row. This tells the query that the label for ' ' (one space) should be nothing, ' ' (two spaces) nothing, etc. This results in no header row.
回答3:
If you need default values such as 0 in my case, you can do:
=query(Sheet!A7:C, "Select A,0,1-1,2-2,B,C where A='Order'")
回答4:
While the answer I checked is indeed the best answer to my original question, I had a problem with it:
It required that the columns skipped be blank. There was no ready way to use a single query to populate intermittent columns. If I put anything in them, the query would break, and refuse to overwrite that cell.
Add to that query's other weaknesses:
- convoluted syntax.
- different syntax between referencing internal and external spreadsheets
- Inability to use range names within the select statement
- brittle about changes in the source structure -- add a column and the query does NOT adjust.
- being dependent on any of the source fields made for frequent calculation of the entire result.
I ended up converting my query to 4 filter statements using the same criteria.
- For each column I wanted results from, I defined a named range.
- for each filter I defined my criteria and source in terms of named ranges.
E.g.
=filter(COcode,COcount > 0,isNumber(COcount))
=filter(COcount,COcount > 0,isNumber(COcount))
The double criteria on each filter is due to sheets having the idea that "foo" is > than 0. Not something I find intuitively obvious.
来源:https://stackoverflow.com/questions/43832024/is-there-a-way-to-insert-blank-columns-in-output-with-google-sheets-query