How do you get the formula from a cell instead of the value?

Use getFormula() or getFormulaR1C1() methods to get the formula of a cell.

Example adaptated from https://webapps.stackexchange.com/a/92156/88163

The following custom function will return the formula of the referenced cell but if the reference is not valid, it will return an error message.

function CELLFORMULA(reference) {
  var ss = SpreadsheetApp;
  var sheet = ss.getActiveSheet();
  var formula = ss.getActiveRange().getFormula();
  var args = formula.match(/=\w+\((.*)\)/i);
  try {
    var range = sheet.getRange(args[1]);
  }
  catch(e) {
    throw new Error(args[1] + ' is not a valid range');
  }
  return range.getFormula();
}

Example of use of the above custom function

A2: FOO
B3: Formula =A2, value FOO
C3: Formula =CELLFORMULA(B3), value =A2


Use this:function =FORMULATEXT("cell")