查找文本并用脚本替换Google工作表中的整个单元格值



我正在使用外部源作为原始数据,它提供带有国家代码+其他一些文本的列头(例如会话美国,活跃用户美国)。我正在寻找一个脚本,我可以使一个按钮,取代列标题如下:

  • sessions US—>完全被——>美利坚合众国
  • 活跃用户—>完全被——>美利坚合众国

简而言之,它删除其他所有内容并将US替换为United States of America作为新的单元格值。因此,脚本查找单元格值(US)的"一部分",并将整个单元格值替换为我配置的内容。我得到的脚本如下:

SpreadsheetApp.getActive()
.createTextFinder("US")
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(false)
.replaceAllWith("United States of America");
SpreadsheetApp.getActive()
.createTextFinder("IT")
.matchEntireCell(true)
.matchCase(true)
.matchFormulaText(false)
.ignoreDiacritics(false)
.replaceAllWith("Italy");
}

然而,这个脚本将"US"替换为"United States of America"。这意味着会发生以下情况:

  • sessions US—>部分被——>届会美利坚合众国
  • 活跃用户—>部分被——>活跃用户美利坚合众国

还需要注意的是,该脚本应该同时适用于多个国家/地区代码(例如CA—>加拿大,IT——>意大利)。我只需要把textfinder部分复制到

上我根本没有足够好的脚本来弄清楚这一点,因为没有用例我可以找到和逆向工程自己。希望这里有人能帮我。

试试这个

function myFunction() {
SpreadsheetApp.getActive().createTextFinder("US").findNext().setValue('United States of America')
}

如果出现多次,可以使用

function myFunction() {
SpreadsheetApp.getActive()
.createTextFinder("US")
.matchEntireCell(false)
.matchCase(true)
.matchFormulaText(false)
.findAll()
.forEach(function (range) {
range.setValue("United States of America");
});
}

findAll ()

最新更新