Google-sheets – Fill-down formula to last used row

google sheetsgoogle-apps-script

This function is to copy-down a formula C2424 to the last used row. Instead of copying it down to the last used formula its copying it to the last existent row within the sheet. How can I do it only to the last row that is being used?

function copiarformulas() {

var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();

var fillDownRange = ss.getRange(2424, 3, lr); 
ss.getRange("C2424").copyTo(fillDownRange);

};

TEST SHEET to reproduce issue:

https://docs.google.com/spreadsheets/d/10JL0rOyDwlFU5_vEPO5bpp8e4p1h5J9H1KJ3SzMKLqo/edit?usp=sharing

Best Answer

There is a misunderstanding regarding the third parameter of getRange on var fillDownRange = ss.getRange(2424, 3, lr); as this parameter is the height of the range but what you expect is that it's the last row.

To fix your code replace

var fillDownRange = ss.getRange(2424, 3, lr); 

by

var fillDownRange = ss.getRange(2424, 3, lr - 2424 + 1);