Is there a formula function in Google Sheets to count the number of columns in a merge? I could assign a named range to the merge to make it easier....

For instance: see the highlighted merged cells here, gives 13 columns:

enter image description here

  • I have updated my question with an image to better explain – Riccardo May 3 '19 at 12:00

Sure. Use the range object's getMergedRanges() method. Note that it returns an array of Ranges, so if testing a single cell, be sure to [0] -grab the first range returned. Demo with G3 as the cell to test, and getNumColumns() used to detect merge width, per your example. Run it and choose View→Logs to see what it found.

function getCellMergeRange() {
  var testSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(),
      testRange = testSheet.getRange("G3"),
      mergeRange = testRange.getMergedRanges()[0];
  Logger.log("Merged Range is "+mergeRange.getA1Notation()+", containing "+(mergeRange.getNumColumns()*mergeRange.getNumRows())+" cells.");
  • Thanks!................. – Riccardo May 5 '19 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.