0

How to merge cell in a column when cells are having same value

Script 1: Merge cells in column B:B only (Ver/Hor = center/center)

Script 2: Merge cells in column B:B and column E:E (Ver/Hor = center/center)

Sample Here

Regards,

2
  • You're probably better off not to merge those cells if they represent data because then only the first cell in the merged range will contain any data. Commented Mar 14, 2021 at 6:14
  • @Cooper: Thank you for your comment. Commented Mar 14, 2021 at 20:02

2 Answers 2

4

I believe your goal as follows.

  • When the same values are continuously existing in the column direction, you want to merge the cells to the vertical direction in the columns "B" and "E". The following image of sample situation is from your question.

  • You want to achieve this using Google Apps Script.

In this case, I would like to propose the following sample script.

Sample script:

Before you use this script, please set the variables of columns and sheetName. The following sample script merge the columns "B" and "E" of "Shet1".

function myFunction() { const columns = [2, 5]; // These column numbers are the columns "B" and "E". This is from your question. const sheetName = "Sheet1"; // Please set the sheetname. const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); const [,...values] = sheet.getDataRange().getValues(); values[0] .map((_, i) => values.map(e => e[i])) .forEach((col, i) => { if (!columns.includes(i + 1)) return; let temp = {}; col.forEach((row, j) => { if (row === col[j + 1] && !(row in temp)) { temp[row] = j; } else if (row != col[j + 1] && row in temp) { sheet.getRange(temp[row] + 2, i + 1, (j - temp[row]) + 1, 1).merge(); temp = {}; } }); }); } 

References:

Added:

About your following additional question,

The script is perfect. How to modify the script if I want to start at row# 7?

When above script is modified, it becomes as follows.

Sample script:

function myFunction() { const columns = [2, 5]; // These column numbers are the columns "B" and "E". This is from your question. const sheetName = "Sheet1"; // Please set the sheetname. const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); const [,,,,,,...values] = sheet.getDataRange().getValues(); values[0] .map((_, i) => values.map(e => e[i])) .forEach((col, i) => { if (!columns.includes(i + 1)) return; let temp = {}; col.forEach((row, j) => { if (row === col[j + 1] && !(row in temp)) { temp[row] = j; } else if (row != col[j + 1] && row in temp) { sheet.getRange(temp[row] + 7, i + 1, (j - temp[row]) + 1, 1).merge(); temp = {}; } }); }); } 
Sign up to request clarification or add additional context in comments.

4 Comments

The script is perfect. How to modify the script if I want to start at row# 7?
@tt3 Thank you for replying. I'm glad your issue was resolved. About your additional request of How to modify the script if I want to start at row# 7?, I added one more sample script. Could you please confirm it?
Your modified script worked and thank you very much for your help.
@tt3 Thank you for replying. I'm glad your issues were resolved.
0

This is so helpful! How would I extend this to a 3rd column. I would like to use Option 1 and affect columns A, B and E.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.