Skip to content

Instantly share code, notes, and snippets.

@dsottimano
Last active October 30, 2019 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsottimano/cd0d9479647e96d6b1d338222011ecee to your computer and use it in GitHub Desktop.
Save dsottimano/cd0d9479647e96d6b1d338222011ecee to your computer and use it in GitHub Desktop.
Substitute all custom function for apps script - sheets
/**
* 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