1

I'm struggling to figure out why the Javascript includes() function I'm using here isn't matching and returning true. The code is used in a Google Script on a Google Sheet to populate some cell values. Here's the relevant code from the full script:

const currentYear = getCurrentYear();

//Get current year from active sheet name
function getCurrentYear(){

  year = sheetName.slice(-7, -2);
  return year; //returns "2020"

}

//Return Current Control File ID
function getCurrentControlFileId(){
  
  const controlFolder = DriveApp.getFolderById(controlFolderId);
  const controlFiles = controlFolder.getFiles();
  
  while (controlFiles.hasNext()){
    let controlFile = controlFiles.next();
    let currentControlFileName = controlFile.getName(); // Evaluates to "CONTROL 2020"
    let searchString = `${currentYear}`; // Evaluates to "2020"
    //Have also tried:
    //let searchString = currentYear; 
    
    if (currentControlFileName.includes(searchString)){
      let controlFileId = controlFile.getId();
      return controlFileId;
    }
  }
  
}

controlFile.getName().includes(searchString) returns false even though controlFile.getName() value is "CONTROL 2020" and searchString value is "2020". Both are strings. If I manually enter "2020" into the if statement conditions like this:

controlFile.getName().includes("2020") it returns true and works as expected, returning the ID string. I've also tried wrapping currentYear and controlFile.getName() in String() but it still returns false. I really can't see what the problem is here, would appreciate some help.

Diagonali
  • 109
  • 1
  • 10
  • Can't reproduce the situation your are describing. Can you test this: `Logger.log(searchString==="2020")` ? If that returns false, then there is an issue in `searchString`. – Marios Sep 24 '20 at 14:30
  • @Marios Thanks for suggestions. In the Stackdriver logs it's coming back as false. Very odd since searchString returns what looks like "2020" but isn't interpreted that way. Any idea? – Diagonali Sep 24 '20 at 14:42
  • Can you check then : `Logger.log(typeof searchString)` ? also try to see if the value is 2020 and not "2020" by `Logger.log(searchString)` – Marios Sep 24 '20 at 14:43
  • @Marios The logger says typeof is String and the value itself is "2020" (without quotes). – Diagonali Sep 24 '20 at 15:06
  • I've added: 'ui.alert(typeof currentControlFileName); ui.alert(currentControlFileName); Logger.log(typeof currentControlFileName); Logger.log(currentControlFileName);' and the logger shows they are both strings and the values are "2020" for searchString and "CONTROL 2020" for currentControlFileName. – Diagonali Sep 24 '20 at 15:09

1 Answers1

2

Most likely year includes an empty space

You can doublecheck it with Logger.log(year.length);.

Beware that the when using array.slice(start, end) that the end index is the first element that it not included in your result. So, if you expect as output "2020" (corresponding to year.length=4):

end-start should equal to 4.

If your sheet name is something like "XXX 2020 YY" you should modify the definition of year to

year = sheetName.slice(-7, -3);

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • This works which is great, thanks very much, I've spent hours trying to fix this but I'm confused as to why the includes() function didn't work because "CONTROL 2020" did include " 2020" (with the space in front of the 2020) so it should have matched? Or did the way I sliced it cause a space after the 2020 like: "2020 " which doesnt match? – Diagonali Sep 24 '20 at 15:16
  • What is your sheet name? If you sliced it to "2020 " then it won't match. – ziganotschka Sep 24 '20 at 15:22
  • The sheet name is "CONTROL 2020" and I was searching for "2020". sheetName.slice(-7, -2) seemed to return "2020" so are you saying that it would have introduced a space before or after the "2020"? If before then it should still have matched. – Diagonali Sep 24 '20 at 15:40
  • 1
    @Diagonali It sure wasn't `CONTROL 2020`. "CONTROL 2020".slice(-7,-2) === "OL 20" and not 2020 – TheMaster Sep 24 '20 at 15:51
  • In addition to what @TheMaster said: Should you want to extract `2020` from `CONTROL 2020` you should query for `year = sheetName.slice(-4);`. Mind you that in this case you could also use a simplier approach, e.g. `year = sheetName.split(" ")[1];` – ziganotschka Sep 24 '20 at 15:55
  • I was incorrect in saying it was "CONTROL 2020" - it was actually "ORDERBOOK 2020 ". I think maybe the emoji was causing an issue? – Diagonali Sep 24 '20 at 15:58
  • 1
    Must be. If there is a space between `2020` and the emoji - one more reason to use `year = sheetName.split(" ")[1];`. https://www.w3schools.com/jsref/jsref_split.asp – ziganotschka Sep 24 '20 at 16:00
  • @Diagonali Emojis are sometimes combination of emojis. This emoji is actually 2 characters: `console.log("".length)` – TheMaster Sep 24 '20 at 16:02
  • We're way offtopic, but [here's](https://stackoverflow.com/a/59796758/) a interesting answer. cc@Diagonali – TheMaster Sep 24 '20 at 16:10
  • That explains it, thanks! I really wanted to understand why it didn't work rather than just avoid the error. @ziganotschka, that's good to know going forward, very much appreciate your help, thanks. – Diagonali Sep 24 '20 at 16:12
  • @TheMaster: that's useful to know! – ziganotschka Sep 24 '20 at 17:00