I just wrote up a simple google sheets function to fix some URLs. This function works fine in a browser, when passed the array of values manually. When called from google sheets, the function fails for **every other row**. 

This isn't a problem with data, since I can make it work for the "failing" rows by moving the formula down one row, or calling it individually for each cell. I think this may be an issue with `regex` inside google sheets. 

```
var pattern = /^http:\/\/(.*\/\d\/.*)_(.*)\/(g\d+p.*)$/ig;

function encode(input) {
  if (!input) return "";
  if (input.map) {
    return input.map(encode);
  } else {
    try {
      // same error happens, at this location, w/ or w/o toString()
      var matches = pattern.exec(input.toString());
      return matches[1] + encodeURIComponent(matches[2]) + matches[3];
    } catch (e) {
      return "error=" + e.message + " value = [" + input + "] ";
    }
  }
}

```

*Edit*: To make things clearer for those who come after, this also fails the same way when the regex is inside the "else" clause:

```
else { 
     var matches = /^(http:\/\/.*\/\d\/.*_)(.*)(\/g\d+p.*)$/ig.exec(input.toString());
     ... continues as normal

```

For alternating rows of the data, I get this error message:

```
error=Cannot read property "1" from null.  value =  [ http://... ]

```

I have tried:

* Putting the regex inside the `try{}`
* Putting the regex inside the `encode{}` function
* Writing two separate functions (one for doing 1 value)

In the failure case I have data like this:

* A1-A8 have URLs in them
* B1 has the formula "=encode(A1:A8)"
* Data in B1, B3, B5, B7 calculate perfectly
* Data in B2, B4, B6, B8 error out (my error message shows up)

Moving the formula to cell "B2" and saying `=encode(A2:A8)` causes all the "failing" rows to calculate and the others to fail!