title: Trim and Clean Google script
body: <p>I can Trim my dada with the script below</p>
  
  <p>But is there a way Clean data in Google using a Google Script similar to Clean in VBA?</p>
  
  <p>i.e. Remove all non-printing characters</p>
  
  <p>I am unable to find-replace on data copied and pasted into GS from another source</p>
  
  <p>Thanks</p>
  
  <pre><code>function trimSpacesSHT(shtName) {
   var sheet = SpreadsheetApp.getActive().getSheetByName(shtName);
   var activeRange = sheet.getDataRange();
  
    // Read Height and Width only once
    var maxHeight = activeRange.getHeight();
    var maxWidth = activeRange.getWidth();
  
    // Read all values and formulas at once
    var rangeValues = activeRange.getValues();
   // iterate through all cells in the selected range
   for (var cellRow = 0; cellRow &lt; maxHeight; cellRow++) {
      for (var cellColumn = 0; cellColumn &lt; maxWidth; cellColumn++) {
      rangeValues[cellRow][cellColumn] = rangeValues[cellRow][cellColumn].toString().trim().replace(/\s(?=\s)/g,'');
    }
  }
   // Write back all values at once
   activeRange.setValues(rangeValues);
  }
  </code></pre>