3

I am attempting to determine whether a passed value to the function addToCalendar (sport) includes the String "soccer" by using the code below:

function addToCalendar(sport, date, time){

  var sportName = String(sport);
  Logger.log(sportName.includes("Soccer"));

}

However, I am getting this error: TypeError: Cannot find function includes in object Soccer: Girls JV. (line 77, file "Code") how would I fix this?

7
  • Where are you running this function? In a browser? Commented Apr 7, 2018 at 20:41
  • @bugs yes, in the Google Apps script editor Commented Apr 7, 2018 at 20:41
  • String.prototype.includes is an ES6 feature, it might not be supported by that editor yet Commented Apr 7, 2018 at 20:42
  • @bugs ok, is their another way to determine whether a string contains a string of letters in a specific order? Commented Apr 7, 2018 at 20:44
  • sure, you can use sportName.indexOf('Soccer'), which returns -1 if the substring is not contained, or the correct index if otherwise Commented Apr 7, 2018 at 20:45

3 Answers 3

6

String.prototype.includes is an ES6 feature, it might not be supported by that environment yet. An alternative way of achieving the same result is using sportName.indexOf('Soccer'), which returns -1 if the substring is not contained, or the correct index otherwise.

String.prototype.indexOf

Sign up to request clarification or add additional context in comments.

3 Comments

Of course this is true, but I doubt it is the case here
"supported by that editor" would be better stated as "supported in that environment" since it is not an editor related issue
@charlietfl correct, that's what I meant. I'll edit the answer to be more precise
1

As others have stated String.prototype.includes is not yet supported in Apps Script. However, you can leverage the following polyfill available from MDN:

if (!String.prototype.includes) {
  String.prototype.includes = function(search, start) {
    'use strict';
    if (typeof start !== 'number') {
      start = 0;
    }

    if (start + search.length > this.length) {
      return false;
    } else {
      return this.indexOf(search, start) !== -1;
    }
  };
}

Comments

0

Here is an ExtendScript function to check a strings contents for .contains().

function contains (word, content) {
    for(var i = 0; i < (word.length-content.length+1); i++){
        if(word.substring(i, i+content.length) == content) return true;
    }
    return false;
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.