在Google Apps脚本中,如何运行2D数组并删除特定字符?例如,如果我想删除";"符号,我可以使用.replace(";","(,但如何在数组的每一行和每一列中运行它?我认为这可以从下面的公式开始设置,但只需要解决中间的函数:
var cleanedarray = originalarray.map(function (row){ });
答案:
您可以在原始映射中使用内部映射函数来替换.getValues()
返回的2D数组中的所有ocurrance
代码:
function replaceAll() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sheet.getDataRange().setValues(
sheet.getDataRange().getValues().map(
x => x.map(
y => y.replace(';', ',')
)
));
}
参考文献:
- Array.prototype.map((-JavaScript|MDN
function removeCharacter(ch) {
var ch=ch||';';
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const rg=sh.getDataRange();
const re=new RegExp(ch,'g');//build the regex with RegExp
var v=rg.getValues();
v.forEach(function(r,i){
r.forEach(function(c,j){
v[i][j]=c.toString().replace(re,'');
});
});
rg.setValues(v);
}