Emanuel Maia
Emanuel Maia

Reputation: 35

Cell is formula? In googlescript

I want to do an action if the selected cell is formula, how do I do?

Some command like isformula() of Google Sheets.

Ex.: if (cell == "formula"?) {make something} else {make something}

Upvotes: 2

Views: 2769

Answers (2)

Adam Glazier
Adam Glazier

Reputation: 85

getFormula return true if there is a formula and false if it doesn't. There is no need to check the string for "=".

if (range.getFormula()) {
  ui.alert('has formula: ' + range.getFormula()); 
} else {
  ui.alert('no formula: ' + range.getFormula());  
}

Upvotes: 5

Max Makhrov
Max Makhrov

Reputation: 18717

function getFormula() may be handy:

getFormula()

Output. String

Returns the formula (A1 notation) for the top-left cell of the range, or an empty string if the cell is empty or doesn't contain a formula.

Please try:

if (range.getFormula().substring(0, 1) === '='
{
  // there is a formula
}

substring(0,1) gets the left symbol:

https://www.w3schools.com/jsref/jsref_substring.asp

Upvotes: 2

Related Questions