title: Google Sheets - Horizontal merge identical cells in single row
body: <p>I am working towards updating a sample of code seen at:
  <a href="https://stackoverflow.com/questions/49212749/merge-cells-with-same-words">Merge cells with same words</a>
  Provided by @Tanaike</p>
  
  <p>I am essentially trying to do the same thing as the sample code above, but with one difference in that I am hoping to pivot this to work horizontally on like columns, instead of vertically against like rows.</p>
  
  <p>I've done my best to flip the functionality, but I am very much a beginner and have run into an impasse. The below is my edited code snippet, and I've marked the particular line where my understanding goes out the window, so I assume there is something wrong there, or so.</p>
  
  <pre><code>function mergeMonths() {
    var start = 6; // Start row number for values.
    var c = {};
    var k = "";
    var offset = 0;
    var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("BlockingChart1");
  
    // Retrieve values of column B.
    var data = ss
      .getRange(4, start, 1, ss.getLastColumn())
      .getValues()
      .filter(String);
  
    // Retrieve the number of duplicate values. //This is where my eyebrow starts to raise.
    data.forEach(function(e) {
      c[e[0]] = c[e[0]] ? c[e[0]] + 1 : 1;
    });
  
    // Merge cells.
    data.forEach(function(e) {
      if (k != e[0]) {
        ss.getRange(4, start + offset, 1, c[e[0]]).merge();
        offset += c[e[0]];
      }
      k = e[0];
    });
  }
  </code></pre>
  
  <p>Just to be 100% clear:
  I am hoping to check cells in a given (sorted) row for cells with identical contents that are repeated and then merge the repeated cells.
  (I am working on a calendar type chart, at a weekly level per cell, but want to label the top to be months, so merging the repeating "...Dec,Jan, Jan, Jan, Jan, Feb..." into headers.</p>
  
  <p>If anyone wants to point me to some reading which may help me along my journey, or could lend a hand, that would be greatly appreciated.</p>