单元格是公式?在谷歌脚本中

Cell is formula? In googlescript

如果选中的单元格是公式,我想做一个动作,怎么办?

像 Google 表格中的 isformula() 这样的命令。

例如: if (cell == "formula"?) {做点什么} 否则{做点什么}

function getFormula() 可能很方便:

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.

请尝试:

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

substring(0,1)得到左边的符号:

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

getFormula return 如果有公式则为真,否则为假。无需检查字符串中的“=”。

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