繁体   English   中英

如何使用 Google Apps 脚本替换电子表格中的文本?

[英]How do I replace text in a spreadsheet with Google Apps Script?

我想在电子表格中找到指定的文本并将其替换为其他单词。 我是这样试的。

sheet = SpreadsheetApp.getActiveSheet()
sheet.replaceText ('ga: sessions','Sessions');

然后它说"Cannot find function replaceText in object Sheet."

来自 Cameron Roberts 的回答几乎适用于所有情况(如果所有单元格都只填充字符串)并且只是为了输入方便(请接受他的回答)这里是相同的脚本,但在地图函数中略有变化:我添加了 toString()到返回参数。

function testReplaceInSheet(){
    var sheet = SpreadsheetApp.getActiveSheet()
    replaceInSheet(sheet,'values','test');
}

function replaceInSheet(sheet, to_replace, replace_with) {
  //get the current data range values as an array
  var values = sheet.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(to_replace,replace_with);
    });

    //replace the original row values with the replaced values
    values[row] = replaced_values;
  }

  //write the updated values to the sheet
  sheet.getDataRange().setValues(values);
}

您可以通过读取工作表中的所有值(作为数组)、循环遍历数组、替换值,然后将整个数组写回工作表来实现这一点。

下面有一个基本的例子。 如果您的工作表包含公式,或者您想在给定单元格中多次替换文本,则可能需要修改此设置。

请注意,在您读入和写出数据之间对工作表所做的任何更改都将丢失,并且可能会破坏此示例。

function testReplaceInSheet(){
    var sheet = SpreadsheetApp.getActiveSheet()
    replaceInSheet(sheet,'ga: sessions','Sessions');
}

function replaceInSheet(sheet, to_replace, replace_with) {
  //get the current data range values as an array
  var values = sheet.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.replace(to_replace,replace_with);
    });

    //replace the original row values with the replaced values
    values[row] = replaced_values;
  }

  //write the updated values to the sheet
  sheet.getDataRange().setValues(values);
}

文档:

Array.map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

String.replace: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Sheet.getDataRange: https : //developers.google.com/apps-script/reference/spreadsheet/sheet#getDataRange()

range.GetValues: https : //developers.google.com/apps-script/reference/spreadsheet/range#getValues()

TextFinder 于 2019 年 4 月发布:

function textFinder() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getDataRange();
  // Creates  a text finder for the range.
  const textFinder = range.createTextFinder('findValue');
  const allOccurrences = textFinder.replaceAllWith('replaceValue');
}

这对我有用,而且很简单。

 function changevalues() { var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('NameOfSheet'); var rangecells = sheet.getRange('DesiredRange'); rangecells.setValue('DesiredValue') }

getActiveSheet() 函数返回一个Sheet对象,其功能在链接中记录。 如果有这样的 API 那就太棒了,但目前您需要删除和插入(行、列或特定范围的内容)才能进行这种替换。

尝试这样的事情:

var sheet  = SpreadsheetApp.getActiveSheet();
var cell = sheet.getRange("A1:A1");
cell.setValue('ga: sessions','Sessions');

所选答案不适用于包含日期单元格的数据。 实际上,它将这些单元格更改为 String,这不是预期的行为。

我修改了代码以检查值是否为数字并仅更改字符串值。

function testReplaceInSheet(){
    var sheet = SpreadsheetApp.getActiveSheet()
    replaceInSheet(sheet,'values','test');
}

function replaceInSheet(sheet, to_replace, replace_with) {
  //get the current data range values as an array
  var values = sheet.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){
      if (typeof original_value != 'string') {
        return original_value;
      } else {
        return original_value.replace(to_replace,replace_with);
      }
    });

    //replace the original row values with the replaced values
    values[row] = replaced_values;
  }

  //write the updated values to the sheet
  sheet.getDataRange().setValues(values);
}

祝你今天过得愉快

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM