using isBlank() in google apps script to mark column

14.2k views Asked by At

How exactly do you use isBlank() in a google apps script function?

function pushGeo()
{
var ssA = SpreadsheetApp.openById('###')
var ss = ssA.getSheetByName('mySheet');
var lastRow = ss.getLastRow();
  var data = ss.getRange('G2:G'+lastRow);
  for(var row=2;row<=lastRow;row++) 
  {  
    ss.getRange('G'+row+':G'+row).isblank(); // not working
    if(isblank(data),'0','G'+row); // not working
  }
}

I want to check a column 'G' for blank cells. If the cell is blank, I want to set its value to '0'. Ultimately, I want to take the column 'G' that was just checked for empty cells and compare it to another column 'N'. If the row in column 'G' is NOT empty (now with value '0') then push the value of column 'G' into column 'N'. Wanted to change the empty cells to '0' so that I can "see" the code better, easier comparison, etc.

1

There are 1 answers

0
Cooper On

If you have a a lot of data on a sheet you might want to consider doing it this way it runs a lot faster because you get and set all data just one time.

function ifGIsBlankThenMakeItZero()
{
  var ssA = SpreadsheetApp.getActive();//changed from openById() for my convenience
  var ss = ssA.getActiveSheet();//change from getSheetByName() for my convenience
  var lastRow = ss.getLastRow();
  var range = ss.getRange(2,7,lastRow,1);//row 2 column 7 (G) lastRow 1 column 
  var data = range.getValues();//Gets all data
  for(var i=0;i<data.length;i++)//this runs over entire selected range 
  {  
    if(!data[i][0])//If true then it's blank
    {
      data[i][0]=0;//notice this is data[i][0] because there is only one column in the range.
    }
  }
  range.setValues(data);//Sets all data.  
}