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?
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
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;
}
};
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With