Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

How to insert several cells (not an entire row/column) using google script?


Say I have a sheet with values in all visible cells. I want to insert cells above, say, C3:D3 so that the existing values in C3:D3 and below are moved downwards. I want the columns next to C3:D3 to remain unchanged. Is this at all possible?

Thank you.


Solution

  • Possible. You'd use insertCells():

    function testInsertCells(){
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheets()[0];
      var range = sheet.getRange("C3:D3");
      range.insertCells(SpreadsheetApp.Dimension.ROWS);
    }