0

Update - It seems that this may be a localized bug. Will do further testing to verify...

I have a CSV String (with spaces) that looks something like this:

var myString = "Here is a value, I am an important value, One last value"

I split that String into a String array using var myArray = myString.split(", "). So the values in myArray are the following:

// myArray[0] = "Here is a value"
// myArray[1] = "I am an important value"
// myArray[2] = "One last value"

Now, I am using a simple if statement inside a for loop to evaluate if "I am an important value" was in that CSV String just like this:

for (var i = 0; i < myArray.length; ++ i) {
  if (myArray[i] == "I am an important value") {
    myBool = true;
  }
}

I also tried if (myArray[i] == 'I am an important value') as well as `if (myArray[i].toString() == "I am an important value")

The bizarre thing is that the value is contained in the CSV String, they are being separated correctly (no trailing spaces and such), but the conditional is not returning true for some reason. Does anyone know if there is some hidden thing I'm missing? I intentionally use == so that it does the type conversion. === made no difference in the result. It's iterating through every element in myArray and marks myBool as true as soon as it sees the value, so I'm having a really hard time understanding why it's not immediately working.

The even more bizarre thing is that I counted the characters of each element in myArray and matched them to the input String. They match exactly, character for character. So why is the equality not behaving correctly?

PS - The really really really weird thing about this problem I'm having is that I have the same type of approach earlier in the function, with the same exact input type and it works just fine. If I were to, for example remove "Here is a value" from the original CSV String, then the condition is met and myBool becomes true (seems to be as long as the comparison value is the 1st element, then the condition holds; otherwise, it does not).

PPS - The individual values in the original CSV String will never have commas in them, so myString.split(", "); works for what I am doing.

UPDATE: I separated the function into its own script file, and tested it. And here's where the bizarre behavior is (what exactly is String.split() doing?

/*
* This is how the data is set up in my script. There is an Object that has a property which is a String.
* The object itself is passed as a paramter, and I check the
*/

function start() {

  var myObject = {
    "property": "Here is a value, I am an important value, One last value",
    "extra": false,
  }

  var result = stringSplitTest(myObject);

  Logger.log("Result: " + result);
}


function stringSplitTest(someObject) {

  var myBool = false;

  var stringToSplit = someObject.property;
  Logger.log(stringToSplit);

  var array = stringToSplit.split(", ");

  for (var i = 0; i < array.length; ++ i) {
    Logger.log("Index " + i + ": " + array[i]);

    if (array[i] == "I am an important value") {
      myBool = true;
      break;
    }
  }
  return myBool;
}

Logging output:

[13-07-25 11:29:45:628 EDT] Here is a value, I am an important value, One last value
[13-07-25 11:29:45:628 EDT] Index 0: Here is a value
[13-07-25 11:29:45:628 EDT] Index 1: I am an important value
[13-07-25 11:29:45:628 EDT] Result: true
Chris Cirefice
  • 5,475
  • 7
  • 45
  • 75
  • It works fine: http://jsfiddle.net/sKkdT/1/ , so I'm betting there's something weird with encoding or something like that? – Ian Jul 24 '13 at 21:33
  • Hm, Google documentation really is lacking, so I couldn't find anything about what specific Javascript implementation they use for Google Apps Script (which is what I'm using). As far as I know, `String.split()` is a native Javascript function, so theoretically it should work as in @Ian 's JSFiddle... As I said in my original post, if the value I'm looking for is the **1st** element of the array at index 0, the script works. But otherwise it fails. Might there be some sort of hidden indexing superiority complex that I'm missing? I figured that this should work, but Google... man... – Chris Cirefice Jul 24 '13 at 21:41
  • WRT which Javascript implementation is being used: [This answer](http://stackoverflow.com/a/17266648/1677912) linked to the portion of Google's docs that claims ECMAScript 5 / js 1.8. – Mogsdad Jul 26 '13 at 01:58
  • Thanks Mogsdad, bookmarked :) – Chris Cirefice Jul 31 '13 at 20:54

1 Answers1

0

your code works in GAS too, as you said this is using straight javascript... the issue must be somewhere else.

GAS function to test :

var myString = "Here is a value, I am an important value, One last value",
    myArray = myString.split(", "),
    myBool = false;
function test(){
for (var i = 0; i < myArray.length; ++ i) {
    if (myArray[i] == "I am an important value") {
        myBool = true;
        break;
    }
  }
Logger.log(myBool);
}

enter image description here

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Serge, thanks for always helping with my silly GAS questions :P I split up the function and am now noticing that `String.split()` is truncating the last value in the string and omitting it from the array. This is why in one case (where there was only one value in the string, no splitting necessary) it was returning true, but when the value I was looking for (unfortunately) happened to be in the last *would-be* index of the array, it was never "actually there". So `String.split()` is omitting the final value, I have no idea why. Is there a way to fix this? – Chris Cirefice Jul 25 '13 at 15:32
  • Update... so frustrating... originally I copy/pasted the value I was looking for in the script from the cell in the spreadsheet where it appeared (checkbox form submission value), and I put that in the `if` conditional. It was always returning false. So I logged the result and copy/pasted from there. The two strings are LITERALLY exactly the same character for character, but magically the log-based copy/paste works and copying from the spreadsheet doesn't? I have no idea... but it's really confusing. Any thoughts on why that would occur? – Chris Cirefice Jul 25 '13 at 15:47
  • Oh and so many comments but PS - I converted the cell I copy/pasted from to plain text after it didn't work the first time, that made no difference. The two strings are exactly the same, character for character, white space for white space. My only possible assumption is that the Spreadsheets are encoded differently than GAS, and they don't play nice together when text is up in the air. So all in all, computers are mental. Is this a bug though? It isn't expected behavior for sure, and I can easily re-create the problem which shouldn't be occurring. – Chris Cirefice Jul 25 '13 at 15:56
  • I just noticed that you where splitting on ', ' (comma+space) and not comma only. You should change that, there is no spaces between array elements it's just a representation.If you need to get rid of leading/trailing spaces use a regex function to achieve that. there are plenty of excellent [examples on this forum (javascript tag)](http://stackoverflow.com/search?q=%5Bjavascript%5Dleading+spaces+regex) – Serge insas Jul 25 '13 at 17:05
  • Oh I know there aren't any spaces in the array elements.The initial form is a list of options in a "check all that apply" manner, and the form sends it to the spreadsheet in the form **"Element 1, Element 2, Element 3, Element etc"**, so theoretically splitting by `", "` should work just fine and give `[0] = "Element 1", [1] = "Element 2"`, etc.? Like I said, I copy/pasted the comparison text from the spreadsheet and it *didn't work*. I then copy/pasted from the log output and it *did*. something is up because I didn't change the `.split()` parameters, and the original string didn't change. – Chris Cirefice Jul 25 '13 at 17:45
  • Serge, do you know of any conversions that happen in the background when a value is fetched by a GAS from a Spreadsheet cell? I can re-create this issue by doing a `String.split(", ") on a list of values fetched from a G Spreadsheet. If you type out the value for comparison (like my OQ) in the Script, it doesn't always return true (seems to be position-based or something). However, if you *log* that value then copy/paste from the Logger into the Script (`if statement`), then it recognizes the value correctly every time. I'm not so sure now that Spreadsheets and GAS play nicely together. – Chris Cirefice Jul 25 '13 at 18:58
  • your question is probably too specific to your situation, I can't see any way to clarify your issue. All I can say is that I've never had any issue with either of these methods and that there is no "special" trick in data transfer between SS and GAS. You should try to isolate a case and if you think you found an issue report it on the issue tracker. – Serge insas Jul 25 '13 at 21:30
  • Alright, I'll do a little bit more testing with fresh documents and scripts, and see if the error pops up. Thanks for the help :) – Chris Cirefice Jul 31 '13 at 20:50