我有一个示例脚本,该脚本运行查找并用该脚本替换值。
我想知道我将如何适应这个现有脚本以查找一定范围的单元格,以用[[未在脚本中指定文本,而是在第二列中查找一列值来查找]注明要用什么替换]。
https://docs.google.com/spreadsheets/d/1zmY48XQT_esYji58pMbk7oJy6Vncr0Oh5urJiK3ka4U/edit#gid=0
这是我可以查询到的最接近的值,但无法复制[循环不是我的强项]
Google Script 'replace' function with range
function runReplaceInSheet(){
var sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Replace Staff Names
replaceInSheet(values, 'This is a sentence.', 'This is a word');
replaceInSheet(values, 'Hello my name is', 'Your name is');
replaceInSheet(values, '.', '-');
replaceInSheet(values, '!', '.');
replaceInSheet(values, 'Bob', 'Sagget');
// Write all updated values to the sheet, at once
sheet.getDataRange().setValues(values);
}
function replaceInSheet(values, to_replace, replace_with) {
//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;
}
}
我希望脚本在整个工作表中找到单元格D21:D23中的内容,并用E21:E23替换任何实例