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

后端 未结 9 1269
予麋鹿
予麋鹿 2020-12-01 17:10

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

9条回答
  •  不知归路
    2020-12-01 17:14

    There seems to be a workaround exploiting the fact that merged cells always return a white background. The code below appears to work for me; I will appreciate it if anyone can confirm.

    function testMerged() {
      var WHITE = '#ffffff';
      var NON_WHITE = '#fffffe';
      var range = SpreadsheetApp.getActiveSheet().getDataRange();
      range.setBackground(NON_WHITE);
      range.getBackgrounds().forEach(function (row, rowNum) {
        row.forEach(function (col, colNum) {
          if (col === WHITE) { Logger.log('Cell merged at row ' + rowNum + ' col ' + colNum); }
        });
      });
      range.setBackground(WHITE);  
    }
    

提交回复
热议问题