因此,我一直在寻找“查找和替换”脚本的这两个版本。我遇到的问题是,脚本的任何连续使用都会删除以前的使用。 As seen here。我被引荐到these solutions,但是我要么错误地实现了它们,要么它们不是所需的解决方案。
如果可能的话,我想继续使用脚本而不是一次性的查找和替换?
这绝对不是最好的,如果我在这里遇到一些容易的错误,对不起!谢谢:)
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 Staff Names
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")');
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;
}
}
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();
var range = sheet.getDataRange();
var replaceObj = {
//to_replace: imgur id
T: 'Dxl893F',
A: 'omc7F9l',
R: '12ZmSp3',
M: 'kh7RqBD',
H: 'u0O7fsS',
F: 'Hbs3TuP',
};
var regex = new RegExp('^(' + Object.keys(replaceObj).join('|') + ')$', 'g');// /^(T|A)$/
function replacer(match) {
return '=image("https://i.imgur.com/' + replaceObj[match] + '.png")';
}
range.setValues(
range.getValues().map(function(row) {
return row.map(function(original_value) {
return original_value.toString().replace(regex, replacer);
});
})
);
var data = range.getValues();
data = range.getFormulas().map(function(e, i) {//i=index of row(e)
return e.map(function(f, j) {//j = index of column(f)
return f === "" ? data[i][j] : f;
});
});
}
答案 0 :(得分:0)
如果我的理解是正确的,那么该修改如何?在此修改中,我在您的问题中使用了以下脚本。请进行如下修改。
setFormulas()
代替setValues()
。range.setValues(
range.getValues().map(function(row) {
return row.map(function(original_value) {
return original_value.toString().replace(regex, replacer);
});
})
);
var data = range.getValues();
data = range.getFormulas().map(function(e, i) {//i=index of row(e)
return e.map(function(f, j) {//j = index of column(f)
return f === "" ? data[i][j] : f;
});
});
至:
// This part is not used in this modification.
// range.setValues(
// range.getValues().map(function(row) {
// return row.map(function(original_value) {
// return original_value.toString().replace(regex, replacer);
// });
// })
// );
var data = range.getValues();
range.setFormulas(
range.getFormulas().map(function(e, i) {//i=index of row(e)
return e.map(function(f, j) {//j = index of column(f)
return f === "" ? data[i][j].toString().replace(regex, replacer) : f;
});
})
);
如果我误解了您的问题,而这不是您想要的结果,我深表歉意。