3

I have a script that is used for a previous spreadsheet that adds a row to the table and keeps the formatting. I am trying to use this script on another sheet to do the same thing. The problem is that the row is being added in the incorrect location at the bottom of the page. What can I do to adjust where the new row is being added?

  // addRow.GS 
var ss = SpreadsheetApp.getActive();

function onOpen() {
  var menu = [{name:"Add New Last Row", functionName:"addRow"}];
  ss.addMenu("Extra", menu);
}

function addRow() {
  var s, data, rw;
  s=SpreadsheetApp.getActiveSheet();
  data=s.getDataRange().getValues();
  for (rw in data) {
    if (data[rw][6]=="Condition Description :") break;
  }
  s.insertRowBefore(Number(rw));
  s.getRange(Number(rw), 4, 1, 2).merge();
  s.getRange(Number(rw), 8).setFormula("=f"+Number(rw)+"*g"+Number(rw));
}



function myFunction() //Code.GS {

var ss = SpreadsheetApp.getActive();

function onOpen() {
  var menu = [{name:"Add New Last Row", functionName:"addFirstRow"}];
  ss.addMenu("Extra", menu);
}
function addFirstRow() {
    var firstRow = 1;
    var sh = ss.getActiveSheet();
    var lCol = sh.getLastColumn();
    var range = sh.getRange(firstRow, 1, 1, lCol);
    var formulas = range.getFormulas(firstRow, 1, 1, lCol);
    sh.insertRowsAfter(1, 1);
    newRange = sh.getRange(firstRow, 1, 1, lCol);
    newRange.setFormulas(formulas);

}
}
2
  • 1
    What are you trying to add? there is an appendRow method in the sheet object Commented Apr 12, 2017 at 20:44
  • onOpen is a reserved name in Google Apps Script. It should be used only once by project. Commented Apr 13, 2017 at 0:20

1 Answer 1

3

Here are a few examples:

var ss = SpreadsheetApp.getActive();

function onOpen() {
  var menu = [{name:"Add New Last Row", functionName:"addFirstRow"}];
  ss.addMenu("Extra", menu);
}

function addFirstRow() {
  var sheet = ss.getActiveSheet();
  sheet.appendRow(['This row']);

  // Insert one empty row after the last row in the active sheet.
  sheet.insertRowsAfter(sheet.getMaxRows(), 1);
  sheet.appendRow(['That Row']);
  
  
  // Shifts all rows down by one
  sheet.insertRows(1);
  
  // inserts a row at row 10
  sheet.insertRows(10);
  
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.