Spreadsheet function that gives the number of Google indexed pages
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"
  }
}