简体   繁体   中英

How to Merge rows in Google Sheets

I tried digging this stuff and have no solution so I'm hoping someone can assist. I have a sheet with the following:

Data

123|456|789        
111|222|333        

etc...

Result Needed

|123 456 789|        
|111 222 333|        

etc...

I'm trying to avoid formulas ( =Concat ) and ( =A2&" "&B2&" "&C2 ) etc...

I tried sheet.getRange(2,1,1,2).mergeAcross() ; and it merged the cells and kept he left-most value. Google searches point to the formula solution.

You can try Array.join() for each row:

Snippet:

var jointRowsArray = sheet
  .getRange(2, 1, 2, 3) //A2:C4
  .getValues()
  .map(function(row) { 
    return [row.join(' ')];//join Each row
  });
sheet.getRange(2, 4, jointRowsArray.length, 1).setValues(jointRowsArray);

To Read:

var range  = sheet.getRange(2,1,1,2)  
var values = range.getValues()

range.clearContent()

sheet.getRange(2,1).setValue(values.join(' '));

You can pull the values into JS and then join and insert them into a single cell. You can also place this inside an iterator and do something akin to getRange(i,1,1,2) . This can be triggered manually or by an edit hook.

However, this seems like the perfect fit for a single formula.

=TRANSPOSE(QUERY(TRANSPOSE(A:C),,COLUMNS(A:C)))

The formula would go on the first row in perhaps column D. JOIN functions cannot usually be arrayed in google sheets, and you would normally have to put a formula in for every row. However, here we are tricking the sheet into thinking our data is the header and displaying it accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM