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); }
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.
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.
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.
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.
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"); }
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); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With