N
N
Nik322020-02-05 15:59:41
Google Apps Script
Nik32, 2020-02-05 15:59:41

How to copy a formula with a script?

Hello, tell me how the script can copy the formula from the "J3" cell to the last not filled cell of the "J: J" column, respectively, so that the cell references change when copying. That is, he performed the usual action, copy the cell and paste it into the last cell that was not filled, respectively, the links will also logically change.
I made this script

function myFunction222() {
// The code below copies only the values of the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A1").copyTo(sheet.getRange(sheet.getLastColumn() + 1,1), SpreadsheetApp.CopyPasteType.PASTE_VALUES);
}
, and where to register so that he searches for the last unfilled cell in the "A: A" column?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor L, 2020-02-05
@Fzero0

Well, I don't know what you wanted or not

function copyFormulas () {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  sheet.getRange("B2:B"+sheet.getLastRow()).setFormulaR1C1(sheet.getRange("B1").getFormulaR1C1());
}

A
Alexander Ivanov, 2020-02-06
@oshliaer

You need

/**
 * User action. Runs the snippet
 */
function run2() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const base = sheet.getRange('J3:J');
  const colFormula = sheet.getRange('J3');
  draggDownOneFormula_(base, colFormula);
}

/**
 * @param {GoogleAppsScript.Spreadsheet.Range} base
 * @param {GoogleAppsScript.Spreadsheet.Range} colFormula
 */
function draggDownOneFormula_(base, colFormula) {
  const baseValues = base.getValues();
  const lastBase =
    baseValues.length - baseValues.reverse().findIndex(row => row[0] !== '');
  const colFormulaFormula = colFormula.getFormula();
  colFormula
    .getSheet()
    .getRange(base.getRow(), colFormula.getColumn(), lastBase + 1)
    .setFormula(colFormulaFormula);
}

snippet

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question