How to replace text in Google Spreadsheet using App Scripts?

前端 未结 4 1335
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 22:07

I tried to get the value of a range and than remove all points from the cell.

var FILE = SpreadsheetApp.openById(\"xyz\");
var CONTENT = FILE.getSheetByName(         


        
相关标签:
4条回答
  • 2020-12-08 22:37

    Hope this help you

    function removeAccents() {
      var spreadsheet = SpreadsheetApp.getActive();
      var range = spreadsheet.getRange("F3:F");
      var data  = range.getValues();
    
      for (var row = 0; row < data.length; row++) {
        for (var col = 0; col < data[row].length; col++) {
          data[row][col] = (data[row][col]).toString().replace(/é/g, 'e');
          data[row][col] = (data[row][col]).toString().replace(/ã/g, 'a');
        }
      }
      range.setValues(data);
    };
    
    0 讨论(0)
  • 2020-12-08 22:53

    If this is an exact copy of your script then you have a space in-between A1. and replace but I assume it is not.

    @SergeInsas is right the content needs to be a string for the replace() function to work, so if your trying to replace the . in a decimal number then you can use the toString() method first like below.

    var FILE = SpreadsheetApp.openById("xyz");
    var CONTENT = FILE.getSheetByName("Sheet1");
    var A1 = CONTENT.getRange("I17").getValue();
    var A1String = A1.toString().replace(".", "");
    

    Or you can multiply the number to get rid of the decimal but this will depend on how many decimal places you have :)

    0 讨论(0)
  • 2020-12-08 22:54

    For some reason, this solution doesn't work for me. Here is my whole code that should replace the '+' symbol with 'nothing'

      // I need to replace more occurrences of different strings, so this is just an example..
      var ui = SpreadsheetApp.getUi();
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var range = ss.getRange("G5:G7").getValues();
    
      // this is a loop, to go through multiple cells that may contain the text, that needs to be replaced.
        for (var i = 0 ; i<range.length ; i++) {
            var le = range.length;
            var stri = range[i].toString().replace("+", "");
            Logger.log(stri);
      }  
    
      var msg = ui.alert("Replaced?");
      return msg;
    
    0 讨论(0)
  • 2020-12-08 22:58

    Sharing a very helpful solution from Bannager Bong on this Google Docs Editor Help Forum thread. Made a slight modification to the function so that it accepts arguments for the find, replace values and then added a range argument so that the function can target a specific region. Even so, this method is extremely slow (my sheets have 5k rows).

    function Cleanup12m() {
      var spreadsheet = SpreadsheetApp.getActive();
      //fandr(",", "");
      //fandr("\"","");
      fandr("�","",spreadsheet.getRange('BA:BA')); //uses specific range
    };
    
    function fandr(find, repl) {
      var r=SpreadsheetApp.getActiveSheet().getDataRange();
      var rws=r.getNumRows();
      var cls=r.getNumColumns();
      var i,j,a,find,repl;
      //find="abc";
      //repl="xyz";
    
      for (i=1;i<=rws;i++) {
        for (j=1;j<=cls;j++) {
          a=r.getCell(i, j).getValue();
          if (r.getCell(i,j).getFormula()) {continue;}
          //if (a==find) { r.getCell(i, j).setValue(repl);}
          try {
            a=a.replace(find,repl);
            r.getCell(i, j).setValue(a);
          }
          catch (err) {continue;}
        }
      }
    };
    
    //Revised to apply to a selected range
    function fandr(find, repl, range) {
      var r= range;//SpreadsheetApp.getActiveSheet().getDataRange();
      var rws=r.getNumRows();
      var cls=r.getNumColumns();
      var i,j,a,find,repl;
      //find="abc";
      //repl="xyz";
    
      for (i=1;i<=rws;i++) {
        for (j=1;j<=cls;j++) {
          a=r.getCell(i, j).getValue();
          if (r.getCell(i,j).getFormula()) {continue;}
          //if (a==find) { r.getCell(i, j).setValue(repl);}
          try {
            a=a.replace(find,repl);
            r.getCell(i, j).setValue(a);
          }
          catch (err) {continue;}
        }
      }
    };
    
    0 讨论(0)
提交回复
热议问题