1

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.

2 Answers 2

1

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:

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks TheMaster my Logger is showing that the data merged however when the last line executes I'm receiving an error "Cannot convert Array to Object[][]" This looks positive, when I change the range to 2,1,1,3 and change setValues to setValue it works.
0
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.

3 Comments

Hi Tom, i appreciate your response. The reason why I'm avoiding formulas because when I choose to write the information on a new column or sheet I get errors because it's including the formula cells so its throwing off the row length. so when I log the data it shows [123, 456, 789], [],[],[],[] <-- blanks represent my formulas. I tried the formula and I have an error of: "Result was not automatically expanded, please insert more rows (1)." Js code snippet shows error : TypeError: Cannot find function flat in object 123,456,789. (file Code.gs, line 41)
To avoid that error the formula must be on the top row of the data. If you are doing a full column A:C it needs to be on row 1. A2:C can go on row 2 and so on.
re: TypeError I removed the call to flat(). It was causing the error and is not needed. Sorry about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.