Last active
October 30, 2019 09:48
-
-
Save dsottimano/cd0d9479647e96d6b1d338222011ecee to your computer and use it in GitHub Desktop.
Substitute all custom function for apps script - sheets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Substitute multiple words or characters at once | |
* @param {"@,hello,test"} params REQUIRED The words you want to remove separated by commas "," | |
* @param {"a1"} text REQUIRED The string you want to subsitute from | |
* @param {"-"} replacement OPTIONAL The replacement string. Default is blank space | |
* @return Returns combination of protocols + www subdomains | |
* @customfunction | |
*/ | |
function SUBSTITUTE_ALL(params, text,replacement) { | |
try { | |
if (text.map) { | |
return text.map(function(t) { return SUBSTITUTE_ALL(params, t,replacement)}); | |
} else { | |
var replacement = replacement || ''; | |
var newText = ''; | |
var nparams = params.split(","); | |
nparams.forEach(function(character) { | |
newText = text.replace(new RegExp(escapeRegExp(character),"g"),replacement); | |
text = newText; | |
}) | |
newText = text.replace(/\s{2,}/g, ' ').trim(); | |
return newText; | |
} | |
} catch(e) { | |
return e; | |
} | |
} | |
var escapeRegExp= function(str) { | |
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment