Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active May 1, 2018 09:25
Show Gist options
  • Save tanaikech/585597adda7954ba1cde3e724582bac5 to your computer and use it in GitHub Desktop.
Save tanaikech/585597adda7954ba1cde3e724582bac5 to your computer and use it in GitHub Desktop.
CLEAN method for Google Apps Script

CLEAN method for Google Apps Script

This is a sample script which works the same action with the CLEAN method of VBA. The CLEAN method of VBA removes the characters of 0-31, 127, 129, 141, 143, 144, 157. Although I had looked for such method for Google Apps Script, I couldn't find it. So I created this. If this is useful for you, I'm glad.

function cleanForGAS(str) {
  if (typeof str == "string") {
    var escaped = escape(str.trim());
    for (var i = 0; i <= 31; i++) {
      var s = i.toString(16);
      var re = new RegExp("%" + (s.length == 1 ? "0" + s : s).toUpperCase(), "g");
      escaped = escaped.replace(re, "");
    }
    var remove = ["%7F", "%81", "%8D", "%8F", "%90", "%9D"];
    remove.forEach(function(e) {
      var re = new RegExp(e, "g");
      escaped = escaped.replace(re, "");
    });
    return unescape(escaped).trim();
  } else {
    return str;
  }
}

function main() {
    var str = "###";
    var res = cleanForGAS(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment