Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Add Row based on cell value, Below the last data in column A

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);
  }
}

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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
  }
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!