Google Spreadheets Scripts: check if cell is empty

orobinec picture orobinec · Jan 15, 2017 · Viewed 81k times · Source

I want to input a variable in a cell only if the cell is empty. The if statement, however, does not work. Any advice?

var ss=SpreadsheetApp.getActiveSpreadsheet();
var r=ss.getRange("'odpovědi'!A2:J");

var rws=r.getNumRows();

ax=r.getCell(rws-1, 10).getValue();

if (ax == "") {
  ax = "foo";
  r.getCell(rws-1, 9).setValue(ax);
}

Answer

A. Masson picture A. Masson · May 14, 2017

No need to extact the value to determine if the cell is empty. Google Spreadsheet API already has a method for this: Range - isBlank method

var cell = r.getCell(rws-1, 10);

if (cell.isBlank()) {
    cell.setValue("foo");
}