我有一个脚本替换姓名的电子邮件地址,它的工作很棒!问题是它在整个工作表上都是这样做的,我需要它只在单个列中工作。
这是为了让G-Sheets仅在单列(A)中替换电子邮件地址的名称。我尝试添加变量,并在第5行将一个范围放在"sheet.getDataRange().getValues();“中,但我不知道如何执行此操作。我是Java Script的新手。任何帮助都将不胜感激。下面是脚本:
function runReplaceInSheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
// 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, 'Ted', 'ct@example.com');
replaceInSheet(values, 'Tilly', 'lt@example.com');
replaceInSheet(values, 'Tina Zed', 'xyz@example.com');
replaceInSheet(values, 'Tim Que', 'qt@example.com');
// 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;
}
}我只需要在我可以指定的范围内工作,而不是整个工作表。
发布于 2019-06-07 02:58:20
您可以使用getRange方法获取工作表的某个范围的值。
例如,如果您对从A列的第一行到B列的第二行的数据感兴趣,您可以这样做:
var values = sheet.getRange("A1:B2").getValues(); 您可以将选择器作为函数runReplaceInSheet的参数传递,以使其更具可重用性:
function runReplaceInSheet(range){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Test");
// get the current data range values as an array
// Fewer calls to access the sheet -> lower overhead
var values = sheet.getRange(range).getValues();
// Replace Staff Names
replaceInSheet(values, 'Ted', 'ct@example.com');
replaceInSheet(values, 'Tilly', 'lt@example.com');
replaceInSheet(values, 'Tina Zed', 'xyz@example.com');
replaceInSheet(values, 'Tim Que', 'qt@example.com');
// Write all updated values to the sheet, at once
sheet.getDataRange().setValues(values);
}然后使用以下范围调用您的函数:
runReplaceInSheet("A1:B2");https://stackoverflow.com/questions/56481893
复制相似问题