首页 > 解决方案 > 您如何使用应用脚本查找和替换字符?

问题描述

我想在名为“SF”的特定 Google 工作表中找到此字符 (’) 的每个实例,并将其从工作表中删除。

到目前为止,我有能力将其从该表中的特定范围(即 J:J)中删除,但希望该范围为(A:AT)

请告知我的功能在处理范围时哪里出错。

function CleanInputs() {
  var sf = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SF");
  var range = sf.getRange("J:J");

  //Finds and replace character encoding issues from Screamingfrog 
  range.setValues(range.getValues().map(function(row) {
  return [row[0].replace(/’/, "")];
}));
}

标签: google-apps-scriptgoogle-sheets

解决方案


使用TextFinderreplaceAllWith

function myFunction() {
  const sheetName = "SF";
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName(sheetName);
  sh.createTextFinder("’").replaceAllWith("");
}

推荐阅读