title: Replace text in Google Sheet without reformatting values using Google App Script
body: <p>I have a problem i need some help with.</p>
  
  <p>I am using a function (which i found on elsewhere this site) to loop through all my spreadsheet rows to replace the word "undefined" with a blank space ( " " ).</p>
  
  <p>The code works fine, but it seems to also reformat the dates from mm/dd/yyyy format to a text string, due to a .toString method in the code. </p>
  
  <p>I do not want the date (or numbers) reformatted, but I haven't found a way to rewrite my code without the .toString method.</p>
  
  <p>Would anyone know of a way i can replace text in Google Sheet without having to format it to a string first? </p>
  
  <p>Here is the code below. I appreciate any help on this.</p>
  
  <pre><code>function replaceInSheet(sheet, to_replace, replace_with) {
  
    var Data = SpreadsheetApp.getActiveSpreadsheet(); // DATA spreadsheet
    //get the current data range values as an array
    var values = Data.getDataRange().getValues();
  
    //loop over the rows in the array
    for(var row in values){
  
      //use Array.map to execute a replace call on each of the cells in the row.
      var replaced_values = values[row].map(function(original_value){
        return original_value.toString().replace("undefined","");
      });
  
      //replace the original row values with the replaced values
      values[row] = replaced_values;
    }
  
    //write the updated values to the sheet
    Data.getDataRange().setValues(values);
  }
  </code></pre>