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

148
Views
Javascript: Copy specified Col through last col and paste down to last row

I'm extremely green to javascript and have pieced together what I need through searching online and watching videos. I need to set multiple columns in row4, starting from col 18 through to lastcolumn, as the active cells to copy and then pasteformulas down to the last row (if necessary, based on col 4's data). Code I have below activates (selects) all data from col 18 through last column (as intended) but also through last row (unintended). Anyone know how to correct this?

    function CopyFormulasDown() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var sRA4 = ss.getRange(4,1); //First copy is A4 ..this one works as intended
  var lr = ss.getLastRow();
  var lc = ss.getLastColumn();
  var sRR4lc = ss.getRange('R4:'+ lc +'4'); //Trying to select R4 through last col within row4; also tried (4,18+':'+lc), but throws an error: Exception: Cannot convert '18:37' to int.
  var fillDownRangeA4 = ss.getRange(4,1,lr);
  var fillDownRangeR4lc = ss.getRange(4,18,lr,lc);
  
   // sRA4.activate(); //range cell A4 is copied
    //sRA4.copyTo(fillDownRangeA4);  //copied cell pastes formula down to last row ..WORKS!
    sRR4lc.activate(); //ROW4 COL18 is selected through to last col as intended, but also selects down to last row.... problem is I didn't tell it to select past row 4... not sure how to correct this
   // sRR4lc.copyTo(fillDownRangeR4lc); //commented out for now until I can get just row 4 from col 18 through last col copy to work
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Omgoodness. After typing out the question and posting, I thought of adding the lc to the numofcolumns place instead. Below is the working code for anyone who's interested:

  function CopyFormulasDown() {
    var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var sRA4 = ss.getRange(4,1); //copy A4
    var lr = ss.getLastRow();
    var lc = ss.getLastColumn();
    var sRR4lc = ss.getRange(4,18,1,lc); //select R4 through last col within row4
    var fillDownRangeA4 = ss.getRange(4,1,lr);
    var fillDownRangeR4lc = ss.getRange(4,18,lr,lc);
    
      sRA4.activate(); //range cell A4 is copied
      sRA4.copyTo(fillDownRangeA4);  //copied cell pastes formula down to last row ..WORKS!
      sRR4lc.activate(); //ROW4 COL18 is selected through to last col
      sRR4lc.copyTo(fillDownRangeR4lc);
};
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!