20

我试图获取一个范围的值,而不是从单元格中删除所有点。

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

它让我找不到替换功能。Google Apps 脚本中是否有任何函数可以让我替换字符串?

4

5 回答 5

26

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 :)

于 2013-11-09T16:55:21.540 回答
8

有一种更强大、更简单的方法可用:TextFinder

这个问题的公认答案需要一个额外的步骤来将替换的字符串发布回单元格。

TextFinder 方法不需要您将数据写回单元格。

如果你想搜索多个单元格,那么这个方法可以为你节省迭代。

  var FILE = SpreadsheetApp.openById("xyz");
  var CONTENT = FILE.getSheetByName("Sheet1");
  var A1 = CONTENT.getRange("I17");
  A1.createTextFinder(".").replaceAllWith("");

我还没有在大型数据集上对其进行测试,但我怀疑这会很快。


编辑:我为此写了一个简短的教程

于 2021-03-18T10:48:51.770 回答
2

出于某种原因,这个解决方案对我不起作用。这是我应该用“无”替换“+”符号的整个代码

  // 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;
于 2018-07-10T09:12:37.643 回答
1

希望这对你有帮助

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);
};
于 2020-07-03T23:31:11.117 回答
0

Bannager Bong在此 Google 文档编辑器帮助论坛线程中分享一个非常有用的解决方案。对函数进行了轻微修改,使其接受查找、替换值的参数,然后添加范围参数,以便函数可以针对特定区域。即便如此,这种方法非常慢(我的工作表有 5k 行)。

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;}
    }
  }
};
于 2019-04-07T16:48:02.890 回答