Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

google apps script - Copy filtered data to another sheet but creates a blank row if it met a condition

For example, I have this data,

enter image description here

I want to filter the data which is "USA" and copy it to another sheet but I have to creates a blank row if it met a condition. For example like this

enter image description here

Is it possible? I have also tried

IF(AND(F2=FALSE, NOT(ISBLANK(F2))), "", INDEX(QUERY('Sheet 1'!A2:E, "Select A where A contains '"&"USA"&"'"),COUNTIF($F$2:F2,TRUE),1))

But it didn't work as I expected

question from:https://stackoverflow.com/questions/65847970/copy-filtered-data-to-another-sheet-but-creates-a-blank-row-if-it-met-a-conditio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a formula version:

=ArrayFormula(
     array_constrain(
        sortn(
          filter(
              {if(A:A="USA",A:A,),if(A:A="USA",B:B,),A:A="USA",
               if(A:A="USA",row(A:A),iferror(vlookup(row(A:A),if(A:A="USA",row(A:A),),1,true),0)+1)},
          A:A<>""),
        1000,2,4,1),
     1000,3)
)

The reason for the long formula is mainly finding a way to get just one row to replace one or more rows that don't start with USA. The basis of the formula is to do a lookup for non-USA rows to get the row number of the most recent USA row. All of the non-USA rows in the same block then have the same row number and can be discarded (apart from the first) using Sortn.

enter image description here

I have added an extra non-USA row at the beginning to check that this edge case works and falls through to the Iferror clause.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...