Spreadsheet function that gives the number of Google indexed pages I've developed this spreadsheet in order to scrape a website's number of indexed pages through Google and Google Spreadsheets.

I'm not a developer, so how can I improve this code in order to have less code, to use less resources, or to go faster?

I've explained everything here.

//-------------------------------------------
//   scrape a website's number of indexed pages through Google and Google Spreadsheets.
//-------------------------------------------
function indexedpages(myUrl)
    {   
    var request = "http://www.google.com/search?&amp;hl=en&amp;q=site:http://" + unescape(myUrl); // Google Request for indexed pages

    var sourcecode = UrlFetchApp.fetch(request).getContentText(); // scrape the page content

    Utilities.sleep(1000); // 1000ms pause to prevent spam

    var codebefore = '&lt;div id="resultStats"&gt;About '; // the code before the number we want

    var codeafter = ' results&lt;'; // the code after the number we want

    var theresult = sourcecode.substring(sourcecode.indexOf(codebefore)+codebefore.length, sourcecode.indexOf(codeafter)); // scrape the number we want

    var theresult =  theresult.replace(",", ""); // delete the ","

    if (isNaN(theresult)) // if the result is not a number

    {        
        var codebefore = '&lt;div id="resultStats"&gt;'; // the code before the number we want

        var codeafter = ' result&lt;'; // the code after the number we want

        var theresult = sourcecode.substring(sourcecode.indexOf(codebefore)+codebefore.length, sourcecode.indexOf(codeafter)); // scrape the number we want           

        if (isNaN(theresult)) // if the result is not a number

        {
            var codebefore = '&lt;div id="resultStats"&gt;'; // the code before the number we want

            var codeafter = ' results&lt;'; // the code after the number we want

            var theresult = sourcecode.substring(sourcecode.indexOf(codebefore)+codebefore.length, sourcecode.indexOf(codeafter)); // scrape the number we want  

            if (isNaN(theresult)) // if the result is not a number (0 result)

            {          
                return 0; // returns 0
            }

            else

            {
                return theresult; // returns the number we want
            }                            
        }

        else 
        {
            return theresult; // returns the number we want
        }

    }

    else 
    {
        return theresult; // returns the number we want
    }

}
Comments


Never place them between the closing parenthesis and the opening bracket of the statement.
Comments are meant to be read, and not set aside. Here are suggestions for cleaner comments:

//Before the lines concerned, like this one
someObject.someFunction();

//Inside blocks of statements
if(foo){
  //If foo, then call foo
  foo();
} else if(bar){
  //If bar, then call bar
  bar();
}



Prettify Code

To make your code a bit readable, but save yourself the hassle of formatting it manually, use a prettifier. An example is jsbeautifier.

Code

function indexedpages(myUrl) {

  // JavaScript doesn't have block scope like C. Variables declared in blocks
  // other than functions are pulled up and declared in the nearest scope.
  var i;
  var codeBefore;
  var codeAfter;
  var result;

  // So I assume your code was about looking for numbers between a certain set
  // of strings. It's a repetetive task, we'll use an array to store these set
  // of strings and a loop that accesses them using an index.
  var wrappers = [
    ['&lt;div id="resultStats"&gt;About ', ' results&lt;'],
    ['&lt;div id="resultStats"&gt;', ' result&lt;'],
    ['&lt;div id="resultStats"&gt;', ' results&lt;']
    //add more wrappers if you want
  ];

  var wrappersLength = wrappers.length;

  // Make the request
  var request = "http://www.google.com/search?&amp;hl=en&amp;q=site:http://" + unescape(myUrl);
  var sourceCode = UrlFetchApp.fetch(request).getContentText();

  Utilities.sleep(1000);

  // Then loop through each of the wrappers and testing them
  for (i = 0; i &lt; wrappersLength; ++i) {

    // Get the codeBefore and codeAfter from an entry in the array
    codeBefore = wrappers[i][0];
    codeAfter = wrappers[i][1];

    // Parse
    result = sourceCode.substring(sourceCode.indexOf(codeBefore) + codeBefore.length, sourceCode.indexOf(codeAfter)).replace(",", "");

    // Then check if the number is not NaN and if so, return which effectively
    // make the code break out of the loop and of the function as well.
    if (!isNaN(result)) return result;
    else return "0";    
  }
}


When free of comments, it's just this short:

function indexedpages(myUrl) {
  var i;
  var codeBefore;
  var codeAfter;
  var result;
  var wrappers = [
    ['&lt;div id="resultStats"&gt;About ', ' results&lt;'],
    ['&lt;div id="resultStats"&gt;', ' result&lt;'],
    ['&lt;div id="resultStats"&gt;', ' results&lt;']
  ];
  var wrappersLength = wrappers.length;
  var request = "http://www.google.com/search?&amp;hl=en&amp;q=site:http://" + unescape(myUrl);
  var sourceCode = UrlFetchApp.fetch(request).getContentText();
  Utilities.sleep(1000);
  for (i = 0; i &lt; wrappersLength; ++i) {
    codeBefore = wrappers[i][0];
    codeAfter = wrappers[i][1];
    result = sourceCode.substring(sourceCode.indexOf(codeBefore) + codeBefore.length, sourceCode.indexOf(codeAfter)).replace(",", "");
    if (!isNaN(result)) return result;
    else return "0"
  }
}