how to insert a row in the middle of a google spreadsheet
Pergunta
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?
Solução
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);
}
- It adds a menu item to the existing menu
- Inserts a row in the middle of the sheet
See example file I've prepared: add row in the middle
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow