Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadheets Scripts: check if cell is empty

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); } 
like image 957
orobinec Avatar asked Jan 15 '17 18:01

orobinec


People also ask

How do I check if a cell is empty in Google script?

value - Reference to the cell that will be checked for emptiness. ISBLANK returns TRUE if value is empty or a reference to an empty cell, and FALSE if it contains data or a reference to data.

How do you check if a range of cells is empty in Google Sheets?

The ISBLANK function can be used in arrays. That means it can check multiple cells at a time. Assume you want to test the range A1:A10 for blank cells. Then use this range in ISBLANK and wrap the entire formula with the ARRAYFORMULA function.

Is not empty Google Sheets formula?

To evaluate the cells are Not Blank you need to use either the logical expression Not Equal to Blank (<>””) of ISBLANK function in logical_test argument of IF formula. In case of logical expression Not Equal to Blank (<>””) logical_test argument returns TRUE if the cell is Not Blank, otherwise, it returns FALSE.

How do I check if a cell has a value in Google Sheets?

Using the ISBLANK function, it can tell whether a value occupies the cell. Where when the value occupies the cell it will return False . If you were to remove the value from the cell, the cell will be empty and the function will return True since it is blank/empty.


2 Answers

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"); } 
like image 76
A. Masson Avatar answered Sep 28 '22 00:09

A. Masson


Try:

function myFunction() {   var ss=SpreadsheetApp.getActiveSpreadsheet();   var s=ss.getSheetByName('odpovědi')   var lr=s.getLastRow()     var ax=s.getRange(lr, 10).getValue();      if(ax == ""){         ax = "foo";         s.getRange(lr, 10).setValue(ax);   }   } 
like image 44
Ed Nelson Avatar answered Sep 28 '22 01:09

Ed Nelson