Question

I am trying to insert a row into the middle of a google worksheet, however cannot find a way in the api documentation,

I cannot add a new row to the bottom as the program that consumes the spreadsheet data(not written by myself) simply ignores new rows added to the bottom.

anyone got any Ideas?

Was it helpful?

Solution

The following code adds a row in the middle of the sheet:

function onOpen() {
  // get active spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // create menu
  var menu = [{name: "Insert row ", functionName: "insertRow"}];

  // add to menu
  ss.addMenu("Insert", menu);  
}

function insertRow() {
  // get active spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // get first sheet
  var sh = ss.getSheets()[0];

  // determine number of rows (compensate for header)
  var rows = sh.getMaxRows();

  // insert row in the middle
  var middle = rows/2;
  sh.insertRows(middle);
}
  1. It adds a menu item to the existing menu
  2. Inserts a row in the middle of the sheet

See example file I've prepared: add row in the middle

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top