Google Sheets – Copying Cell Formulas with Dynamic Ranges

google sheetsgoogle-apps-script

Trying to figure out how copy and paste a formula using Google Script in google sheets. When I use the getFormula and setFormula it does copy the formula but it keeps the same range, it does not dynamically update the range like it normally does when you use the spreadsheet without a script. Wondering what I am doing wrong… here is my code

     var source = formulaSheet.getRange(row+1,col-1).getFormula();
     var target = formulaSheet.getRange(row+1,col);
     target.setFormula(source);

Best Answer

Use R1C1 notation instead of A1 as the first allows to use relative coordinates. For this you could use getformular1c1 and/or getformulasr1c1 and the corresponding "set" versions.