Yes. In Google Sheets, use Google Apps Script’s Range.merge() method to merge a selected range; for example, sheet.getRange('A1:C1').merge(); merges cells A1 through C1. Use mergeAcross() or mergeVertically() when you want separate merges across rows or down columns.
What merging cells does
Merging combines a rectangular group of cells into one larger cell. It can make a report title span several columns or make a category label cover several rows. The merged cell behaves as one cell for purposes such as selecting and entering a value, even though it occupies the space of multiple grid cells.
In Apps Script, you work with a Range object: first identify the sheet and range, then call the method that matches the layout you need. A merge changes the sheet itself, not just the way it looks. It can affect sorting, filtering, pasting, formulas, and later scripts that expect a regular grid. For that reason, merges are usually most suitable for presentation areas, such as report headings, rather than the main data table.
For a one-time manual merge in the Sheets browser, select the cells and choose Format > Merge cells, then choose a merge option. Apps Script is useful when you need to repeat the same layout, apply it to generated reports, or run the operation on a range selected by a script.
Merge a specific range with a short script
Open the spreadsheet in a browser, select Extensions > Apps Script, and add a function like this in the script editor:
function mergeHeader() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange('A1:C1').merge();
}
Save the project, choose mergeHeader in the function selector, and click Run. The code gets the active sheet, identifies the range from A1 to C1, and merges that range into one cell. Change the range reference to suit your sheet, such as B2:D2 for a different heading.
The example uses the active sheet, so confirm that the correct tab is active before running it. For a script that should always affect a particular tab, use getSheetByName() instead. For instance, replace the active-sheet line with const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Summary');. The tab name must match, including spaces and punctuation; if the name is wrong, the lookup returns no sheet and a later range operation will fail.
Choose the right merge method
The three merge methods have different results. Calling merge() on a multirow, multicolumn range combines the entire selection into a single cell. The other two methods make a series of smaller merges:
merge()merges the selected rectangle as one cell. A range such as A1:C3 becomes one merged block.mergeAcross()merges each row of the selected range separately. On A1:C3, it creates three merged cells: A1:C1, A2:C2, and A3:C3.mergeVertically()merges each column separately. On A1:C3, it creates one merged range in each column: A1:A3, B1:B3, and C1:C3.
Use mergeAcross() when each row has its own label or heading spanning several columns. Use mergeVertically() when a label should span multiple rows within each column. Choose merge() only when the whole rectangle is meant to represent one item. These methods do not decide which cells should be merged based on their contents; your script must identify the ranges correctly.
Merge rows in a range that can grow
If your sheet gets new rows over time, you can use getLastRow() to calculate the range size rather than hard-coding the final row. This example merges columns A through C separately on each populated row, starting at row 2:
function mergeRowsInUse() {
const sheet = SpreadsheetApp.getActiveSheet();
const lastRow = sheet.getLastRow();
if (lastRow >= 2) {
sheet.getRange(2, 1, lastRow - 1, 3).mergeAcross();
}
}
The four numeric arguments to getRange() mean starting row, starting column, number of rows, and number of columns. Here, the range starts at row 2 and column 1 (A), covers rows 2 through the last used row, and includes three columns (A:C). The condition prevents the script from requesting a range with zero rows when the sheet has no data below the first row.
Before using this pattern, check what “last used row” means for your sheet. getLastRow() returns the last row with content, but it does not determine whether every row in the range is intended for merging. If a notes section or unrelated table appears lower on the same tab, this script will include it. A fixed data boundary, a dedicated report tab, or an explicit list of rows may be safer.
Run the script and grant access
The first time you run a script that accesses a spreadsheet, Google may ask you to authorize it. Review the account and requested access before approving. A simple script created from the spreadsheet generally operates in the context of that spreadsheet, but authorization prompts depend on what the project does and which services it uses.
To run the function from the editor, save the project, select its name in the toolbar, and click Run. Check the spreadsheet after execution to confirm that the intended cells changed. If you want a repeatable command for other users, you can add a custom menu with an onOpen() function, but that is optional; it does not change how the merge method works. A custom menu is most useful when people need to run a known operation without opening the script editor.
Apps Script is Google’s scripting environment for Sheets. This code does not run as-is in Excel: Excel uses different automation options, such as Office Scripts or VBA, with different APIs and syntax.
Protect data before merging
A key risk is merging cells that contain multiple values. In a merge, the upper-left cell supplies the surviving value; content in the other cells of the merged range is discarded. For example, merging A1:C1 when A1 contains “Quarter” and B1 contains “2026” does not preserve both pieces as separate cell values. Combine the text into one cell first if both parts matter, or move the data elsewhere before merging.
It is also wise to avoid merging cells in a raw data table. Tables work best when each row is a record and each column is a field, with one value per cell. Merged cells can make it harder to sort or filter consistently, copy rectangular blocks, fill formulas, and process the data with another script. A merged visual heading above the table is less disruptive than merging cells inside its records.
- Make a copy of the sheet or spreadsheet before applying a script to valuable data.
- Test the code on a small range and inspect every affected row.
- Check whether the selected range already contains values or existing merges.
- Keep the script’s target sheet and row limits explicit when possible.
Unmerge cells and inspect existing merges
To reverse a merge in Apps Script, call breakApart() on the relevant range. For example, sheet.getRange('A1:C1').breakApart(); breaks apart merged cells that overlap that selected range. The command restores the grid structure, but it cannot bring back values that were discarded when the cells were originally merged. Unmerging is not a substitute for a backup.
To find merged ranges that overlap a particular area, use getMergedRanges(). It returns an array of merged ranges, which you can inspect or loop through. For example:
function listMergesInArea() {
const sheet = SpreadsheetApp.getActiveSheet();
const ranges = sheet.getRange('A1:F20').getMergedRanges();
ranges.forEach(function(range) {
Logger.log(range.getA1Notation());
});
}
In this example, the script logs the A1 notation of each merged range overlapping A1:F20. View the execution details or logs in the Apps Script editor to review the results. This can help diagnose a script that fails because a target area includes an unexpected merged cell, or help you decide which ranges to unmerge before rebuilding a report.
Common problems and practical alternatives
If a script reports an error, first check that the sheet lookup succeeded, the range is valid, and row and column counts are positive. Then inspect the target for existing merged cells and confirm that the range does not cross a section boundary. Scripts that calculate ranges from the last used row can accidentally include unrelated content, so log the target range before changing a large sheet.
Another common misconception is that merging combines the contents of cells. It does not concatenate values; it creates one cell from a range and keeps only the upper-left cell’s content. If the goal is simply to show a title centered over several columns, consider whether a normal cell with horizontal alignment and a suitable column width will work. In Google Sheets, there is no Excel-style “Center Across Selection” command, so alignment alone will not make the text span the selected columns in the same way as a merge.
When you do want the merged appearance, use the Google Sheets browser menu Format > Merge cells for a one-off task, or use Apps Script for repeatable operations. The menu is easier for a single title; a script is better for a recurring report or a carefully defined set of ranges. Neither method makes merging a good fit for data that people need to sort, filter, or analyze row by row.
Frequently Asked Questions
What is the Apps Script command to merge cells?
Use getRange('A1:C1').merge() on a sheet object. Replace the range with the cells you want to merge.
How do I merge each row separately?
Call mergeAcross() on a range covering the desired rows and columns. It makes one merge per row, not one large merge across the whole selection.
Does merging cells combine their text?
No. The upper-left cell’s content is retained, while content in the other cells of the merged range is discarded. Combine or copy values first if you need to preserve them.
How can I unmerge cells with Apps Script?
Call breakApart() on a range that overlaps the merged cells. This restores separate cells but does not restore content removed by the original merge.
Can I merge cells in Excel with this script?
No. Google Apps Script is for Google Workspace apps. Excel automation uses different tools and APIs, so this code must be rewritten for Excel.
Related
- How to Insert a Table of Contents in Microsoft Word
- Inserting Citations in Word: A Step-by-Step Guide
- Inserting a Line in Word: Rules, Borders, and Shapes
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Insert a Dynamic Checkbox in Microsoft WordTo insert a dynamic, clickable checkbox in Word, show the Developer tab, then choose Developer > Check Box…
- Excel Cell Margins: Add Top and Bottom SpaceExcel does not have a separate top or bottom cell-margin setting. To create space inside a cell, adjust…
- Change Currency Format in Google Sheets: A Practical GuideTo change a cell’s currency format in Google Sheets, select the cell or range, then choose Format >…
- Excel Freezing? Causes, Fixes, and PreventionIf an Excel file keeps freezing, first wait briefly to see whether Excel is calculating or loading; if…
- Excel Print Selected Area on One Page: Steps & TipsTo print a selected area on one page in Excel for Microsoft 365 on Windows, select the cells,…
- How to Double Space in Word: Steps, Tips, and FixesTo double-space text in Word, select it and choose Home > Line and Paragraph Spacing > 2.0. To…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.