Created
December 10, 2012 11:24
-
-
Save beezly/4250079 to your computer and use it in GitHub Desktop.
Google Script Spreadsheet function to iterate over all cells and insert the value 0 if the cell is blank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function zeroCells() { | |
var ss=SpreadsheetApp.getActiveSpreadsheet(); | |
var sheet=ss.getSheets()[0]; | |
var selection=sheet.getDataRange(); | |
var columns=selection.getNumColumns(); | |
var rows=selection.getNumRows(); | |
for (var column=1; column < columns; column++) { | |
for (var row=1; row < rows; row++) { | |
var cell=selection.getCell(row,column); | |
if (cell.isBlank()) { | |
cell.setValue(0); | |
} | |
} | |
} | |
} |
@moxventura is correct, because row and column indexes start at 1.
Thanx man,
Helped me to create a function to validate whether a string is present in sheet or not.
/* Searches for a string in a sheet*/ function searchString(stringToSearch,sheetName) { var sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); var selection=sheet.getDataRange(); var columns=selection.getNumColumns(); var columnsnames = selection.getColumn() var rows=selection.getNumRows(); for (var column=1; column < columns; column++) { for (var row=1; row < rows; row++) { var cell=selection.getCell(row,column); if (cell.getValue() == stringToSearch ) { return true; } } } }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'll miss the last row and last colum due to the <