I just need some help, I got this code in youtube that adds rows to the specified sheet, but the code that I got is just adding rows after row 2 and just keeps on adding it non-stop.
here is the code:
function insertRow() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
let sheetToAdd = spreadSheet.getSheetByName('test sheet');
let lastRowEdit = sheetToAdd.getLastRow();
for(var i = 2; i <= lastRowEdit; i )
{
sheetToAdd.insertRowAfter(i);
}
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
What I want is to add row below the last row with data in column A. And the number of the rows to be added is based on cell B2 in another sheet
sample spreadsheet: https://docs.google.com/spreadsheets/d/1sX6wS0ZPeLC1u0nbU2m8jeZ3dMVEXhkFPR_Ply27Z18/edit#gid=746083055
CodePudding user response:
This code may solve your issue: Prerequisite: A google spreadsheet with one sheet called Info containing the amount of rows to add in cell 'B2'. A sheet called 'add here' which will be filled by the script below.
By changing it according to your sheet names you can basically copy pasta it.
Comments for help!
function insertRow() {
let spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); // activates the opened document
let sheetToAdd = spreadSheet.getSheetByName('add here'); // selects the sheet at the bottom of the SpreadSheet (SpreadSheet is a collection of Sheets (!))
let rowsToAdd = spreadSheet.getSheetByName('Info').getRange(2,2).getValue(); // get cell B2
let lastRow = sheetToAdd.getLastRow() 1; // you want to add after the last row with data
for(var i = 0; i <= rowsToAdd; i ){
sheetToAdd.getRange(lastRow i, 1).setValue(i); // get the cell at lastRow counter and column 1; set the value as wished
}
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>