将更改格式功能添加到“查找和替换”功能的脚本

问题描述 投票:0回答:1

Google文档脚本的新功能,目前正在寻找现有脚本,以便将更改格式功能添加到“查找和替换”功能。例如,选择列B,使用“查找和替换”查找单词的每个实例,而不是仅能够替换单词,具有将格式更改为粗体或斜体的功能。

我已经搜索过并且自己找不到任何东西,所以如果有人知道一个剧本,我将不胜感激。

如果没有,有人可以给我一个从哪里开始的提示,关于自己创建这个脚本。

谢谢。

javascript google-apps-script google-sheets
1个回答
1
投票

您可以使用此代码查找和替换Google文档中的文本,但是如果要更改找到的文本的格式,则需要进行修改。

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);
}
© www.soinside.com 2019 - 2024. All rights reserved.