【发布时间】:2019-07-03 05:53:00
【问题描述】:
我正在使用这个脚本,但它正在替换 T、A 等的每个实例。我如何让它只替换完全匹配?只有当它是字母 T 而不是别的时。
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Underlevel");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getDataRange().getValues();
// Replace
replaceInSheet(values, "/^T$/", '=image("https://i.imgur.com/Dxl893F.png")');
replaceInSheet(values, 'A', '=image("https://i.imgur.com/omc7F9l.png")');
replaceInSheet(values, 'R', '=image("https://i.imgur.com/12ZmSp3.png")');
replaceInSheet(values, 'M', '=image("https://i.imgur.com/kh7RqBD.png")');
replaceInSheet(values, 'H', '=image("https://i.imgur.com/u0O7fsS.png")');
replaceInSheet(values, 'F', '=image("https://i.imgur.com/Hbs3TuP.png")');
// 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;
}
}
谢谢你:D
【问题讨论】:
-
您使用了除 Google-apps-script 标签之外的所有其他标签
标签: javascript regex google-apps-script google-sheets