简体   繁体   中英

how to insert a row in the middle of a google spreadsheet

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM