Unmerge spreadsheet cells and fill ex-merged empty cells with data

Viewed 518

breakApart() works fine, but I'd like to fill the empty cells with merged cell's data.

var sheet = SpreadsheetApp.getActiveSheet();
var sheetRange = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()); 
sheetRange.breakApart();  

I got the value I need, but how can I get the cells to fill?

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

If you already have the value, You can setValue() the desired value over the entire range and all the range will be filled with the same value.

const sheet = SpreadsheetApp.getActiveSheet();
const sheetRange = sheet.getDataRange(); 
sheetRange
  .getMergedRanges()
  .forEach(range => range.setValue(range.breakApart().getValue()));

getValue() already returns only the value in the top left cell. So the above chaining works.

Related