Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know if spreadsheet cells are merged using google apps script

In a Google docs spreadsheet. If cells A1 & A2 are merged, is there a way to confirm they are merged, using google apps script?

There is a merge function in GAS https://developers.google.com/apps-script/class_range#merge

But there is no function or example that shows how to check if cells are merged.

getValues etc just returns an empty string for the cell. e.g. this does not work.

function testMerge() {

  var spreadsheet = SpreadsheetApp.openById('Z3ppTjxNUE........'); 
  var sheet = spreadsheet.getSheets()[0];
  var range = sheet.getRange("A3:A4");
  var values = range.getValues();
  var formulas = range.getFormulasR1C1();
  var formulasA1 = range.getFormulas();

  range = sheet.getRange("A4");
  range.setValue("a");

  range = sheet.getRange("A3:A4");
  var values2 = range.getValues();
  var formulas2 = range.getFormulasR1C1();
  var formulasA12 = range.getFormulas();


  var count = range.getHeight();


}
like image 930
eddyparkinson Avatar asked Jun 14 '12 01:06

eddyparkinson


People also ask

How do you check if there are merged cells?

Click Alignment > Merge cells > OK. Click Find All to see a list of all merged cells in your worksheet. When you click an item in the list, Excel selects the merged cell in your worksheet. You can now unmerge the cells.

How do I select only merged cells in Google Sheets?

Shortcuts to Unmerge Cells You can select the merged cells and use the Ctrl + \ shortcut that removes formatting to unmerge them. But, keep in mind it will also remove all other formatting.

How do you fix merged cells in Google Sheets?

In order to unmerge them at once, you have to select all cells in the sheet (by clicking the arrow in the left upper corner of the cells, or using the keyboard shortcut CTRL + A). Then in the Ribbon, go to Home > Merge & Center. As a result, all merged cells are now split into their original cells.


1 Answers

Yes, now you can use isPartOfMerge to check.

And for anyone wants to get the value of merged cell:

var value = (cell.isPartOfMerge() ? cell.getMergedRanges()[0].getCell(1,1) : cell).getValue();

Hope it help.

like image 110
eric long Avatar answered Sep 20 '22 15:09

eric long