2

I am using Google Apps Script

I have a Google Sheet where column 1 has 120 domain names (120 rows) and i need to write the status of these domains as "Domain Verified" / "Domain Not Verified" in Column 7

I wrote a script which is using Admin Directory API service and using AdminDirectory.get.domains.verified which results in boolean (True = domain verified, False = domain not verified) to check the status of domains to see if they are verified domains in google.

my below script works absolutely fine, it checks for each domain name row and puts the status as either in column 7, however the problem is that my loop stops as soon as it comes to any domain which is NOT yet registered in Google, in the logs it says "Execution failed: Domain not found. (line 36, file "Code") [1.412 seconds total runtime]"

where i expect it to be running till the last row (120th) regardless of the result.

What I actually want is, regardless of the result, my loop should cover all 120 rows, can you help?

Here is my Script-:

function domainList() {

  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var sheet = ss.getActiveSheet()
  var data = sheet.getRange("B2:B").getValues()
  var customer = "my_customer"

  for(i=0; i<data.length; i++){
    var dcheck = AdminDirectory.Domains.get(customer, data[i]);

    var status = dcheck.verified
    if(status === true){
      status = "Domain Verified"}
    else if(status === false){
    status = "Domain Not Verified"}
    else if(status === ({})){
      continue;
    }

    Logger.log(status)

    var range = ss.getSheets()[0].getRange(2+i, 7).clear()

 var range = ss.getSheets()[0].getRange(2+i, 7)
    range.setValue(status)
  }}

2 Answers 2

2

You could try putting some of the code into a try/catch and if there is an error, simply continue looping:

try {
  var dcheck = AdminDirectory.Domains.get(customer, data[i]);

  if (dcheck) {//Check for truthy value
    var status = dcheck.verified;
  } else {
    continue;//If "get" returned a falsy value then continue
  }
} catch(e) {
  continue;//If error continue looping
}
Sign up to request clarification or add additional context in comments.

Comments

0

Here's another way of doing the same thing. I wasn't clear on what this if(status === ({})) was so I thought it might be interesting to make that third alternative a little less narrow because if it's expected to be boolean and it's neither true nor false then it's not clear to me what else is left. It might take a while but I'd try to run through this with the debugger. But I'm not familiar with AdminDirectory so I'm kinda guessing here.

function domainList() 
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var data = sheet.getRange("B2:B").getValues();
  var customer = "my_customer";
  var value = '';
  for(i=0; i<data.length; i++)
  {
    value = '';
    var dcheck = AdminDirectory.Domains.get(customer, data[i]);
    var status = dcheck.verified;
    switch(status)
    {
      case true:
        value = 'Domain Verified';
        break;
      case false:
        value = 'Domain Not Verified';
        break;
      default:
        value = 'unknown';    
    }
    Logger.log(status);
    var range = ss.getSheets()[0].getRange(2+i, 7)
    range.clear();
    range.setValue(value);
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.