arraysgoogle-apps-scriptgoogle-sheetsgoogle-sheets-formula

How to union ranges in google spreadsheets


I want to union ranges from any Google spreadsheets.

The example

Sheet1!A:A

{12, 131, 45}

Sheet2!A:A

{12, 131, 46}

The unknown function

=formula_for_union_range(Sheet1!A:A; Sheet2!:A:A)

should return

{12, 131, 45, 12, 131, 46}

The question

How is it possible?


Solution

  • Google Apps Script

    And yet the question was about the script. I'm still successfully using the following code:

    function unionRanges(e) {
      var result = [];
      var length = 0;
      var i = 0;
      try {
        for (i = 0; i < arguments.length; i++)
          length += arguments[i].length;
        if (length > 3000) return '#BIGRANGE';
        for (var i = 0; i < arguments.length; i++)
          result = result.concat(arguments[i].filter(function (el) {
            return el.join('').length > 0
          }));
        return result;
      } catch (err) {
        return JSON.stringify(err);
      }
    }
    

    Spreadsheets feature

    But, as noted above, it is easier to use {}-notation.

    ={ Sheet1!A1:C10 ; Sheet2!A1:C34 }
    

    Vertical concatenation

    ={ Range(Cols=N) ; Range(Cols=N) }
    

    Horizontal concatenation

    ={ Range(Rows=M) , Range(Rows=M) }
    

    It's possible to combine

    ={ { , , } ; { , , } }
    

    Or something more hard

    ={{{;;},{;;}};{{;;},{;;}};{{;;},{;;}}}
    

    Try something like this

    ={
       {{ 1; 2; 3},{ 4; 5; 6}};
       {{ 7; 8; 9},{10;11;12}};
       {{13;14;15},{16;17;18}}
     }
    

    The internal horizontal concatenation is not required

    ={
        { 1; 2; 3},{ 4; 5; 6};
        { 7; 8; 9},{10;11;12};
        {13;14;15},{16;17;18}
    }
    

    Locale dependencies of argument delimiters

    If your current locale supports , as an argument delimiter thnen you should use ; for a vertical concatenation and , for a horizontal concatenation.

    Otherwise your argument delimiter is ; and you have to use ; and \ (without spaces), respectively.

    Sheet 'Data 1'!A1:C20

    |   Name  |    Date   | Sum |
    | Ethan   |  3/4/2017 |  31 |
    | Logan   |  3/6/2017 |  62 |
    | Brian   | 3/26/2017 |  61 |
    |   ...   |     ...   | ... |
    

    Sheet 'Data 2'!A1:C20

    |  Name   |    Date   | Sum |
    | Nathan  | 3/30/2017 |  53 |
    | Alyssa  | 3/13/2017 |  72 |
    | John    | 3/24/2017 |  79 |
    | Megan   | 3/16/2017 |  10 |
    |   ...   |     ...   | ... |
    

    Concatenation

    Vertical concatenation

    ={'Data 1'!A1:C20;'Data 2'!A2:C20}
    

    Result

    |  Name  |    Date   | Sum |
    | Ethan  |  3/4/2017 |  31 |
    | Logan  |  3/6/2017 |  62 |
    | Brian  | 3/26/2017 |  61 |
    | ...    |       ... | ... |
    | Nathan | 3/30/2017 |  53 |
    | Alyssa | 3/13/2017 |  72 |
    | John   | 3/24/2017 |  79 |
    | ...    |       ... | ... |
    

    Horizontal concatenation

    ={TRANSPOSE('Data 1'!A1:C20),TRANSPOSE('Data 2'!A2:C20)}
    

    Result

    | Name |   Ethan  |   Logan  |   Brian   | ... |   Nathan  |   Alyssa  |    John   |
    | Date | 3/4/2017 | 3/6/2017 | 3/26/2017 | ... | 3/30/2017 | 3/13/2017 | 3/24/2017 |
    | Sum  |       31 |       62 |        61 | ... |        53 |        72 |        79 |
    

    More about this How to concatenate ranges in Google spreadsheets