javascript - 查找和替换单元格范围 [示例脚本]
问题描述
我有一个示例脚本,它使用脚本运行查找和替换值。
我想知道我将如何调整这个现有的脚本来找到一系列单元格来替换文本 [不指定脚本中的文本,而是运行一列值来查找,第二列表示要做什么替换为]。
https://docs.google.com/spreadsheets/d/1zmY48XQT_esYji58pMbk7oJy6Vncr0Oh5urJiK3ka4U/edit#gid=0
这是最接近我的查询的,但无法复制 [循环不是我的强项]
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 替换任何实例
解决方案
推荐阅读
- python - 如何在类中使用 def __init__(self): 来调用我的 sqlite3 数据库?
- angular - 使用 url 的第一部分设置服务状态
- php - Instagram is_private 正则表达式
- excel - 如果列中的单元格包含特定文本,则有条件地格式化列
- facebook - 无法测试 Facebook Messenger 机器人。Webhook 未收到事件
- java - 如何实现删除用户?
- vba - 我们可以在 VBA 中格式化创建后的查询字段属性吗?
- r - 使用 ggplot2 的发散条形图(y-hline 不同于 0)
- vue.js - Vuex 模块初始化最佳实践
- python - 使用 Jupyter 中的 Python 设置 Drive API 时出错