how to check whether cell has anything in it

2019-08-26 06:30发布

问题:

I have a spreadsheet with 6 columns, the first 3 rows are reserved for heading. Once the user enters information in 5 cells of the row he would execute a script that would send an email. The 6th cell is for showing EMAIL_SENT after the email was sent.

What i have problem with is error checking, i dont want the user to send an email without all five cells having data in it.

function Email() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getDataRange();
range = range.offset(3, 0, range.getNumRows()-3);

range.getValues().forEach( function( recipient, index, data ){

        if (recepient[0] == ""){Browser.msgBox("No data in column A");}
        .
        .         
        .    
});
}

I was hoping i could check if the cell is equal to nothing, blank and notify but this is not working.

Your help is much appreciated. :)

回答1:

The function you are using works but is not able to call the Browser service...

If you replace with a Logger.log you get the result as expected.

  range.getValues().forEach( function( recipient, index, data ){
    if (recipient[0] == ""){Logger.log("Missing data in column A");}
  });

IMHO, it is after all far more convenient than getting many popups appearing in my UI...


EDIT:

below is a code that will show a warning when no data is present in col A

  range = range.offset(3, 0, range.getNumRows()-3);
  var result=false;
  range.getValues().forEach( function( recipient, index, data ){
    if (recipient[0] == ""){result="Missing data in column A"}
  });
  if(result){Browser.msgBox(result);  return };


回答2:

Have you tried isBlank() in the Range class?

https://developers.google.com/apps-script/reference/spreadsheet/range#isBlank()