I have a function that Bulk find and replaces regexes that I repeatedly use. From here.
Bulk find-and-replace regexs in Google Sheets
But it only works on a single column.
I want the function to iterate through an entire array.
I have read about Reduce and came away with only the simplest of understandings. Not enough to reconfigure the function
function processColumn(column)
{
// Add more as needed:
// [Regex, Replacement]
let replaceTable =
[
[/\bN\./g, 'North'],
[/\bS\./g, 'South'],
[/\bSt\./g, 'Street']
];
// A column is an array of rows
// A row is an array of values.
return column.map(row =>
// This is an optimization to skip over blank values
row[0]
? replaceTable.reduce(
// Replace one value at a time, in order
(curString, tableEntry) => curString.replaceAll(tableEntry[0], tableEntry[1]),
row[0]
)
: ''
);
}
I know I can do this in other ways like:
values = sheet.getDataRange().getDisplayValues()
values = values.map(outer => outer.map(inner => inner
.replaceAll(/\bN\./g, 'North')
.replaceAll(/\bS\./g, 'South')
.replaceAll(/\bSt\./g, 'Street')
)
);
How to reconfigure processColumn function to iterate through an entire array ?
CodePudding user response:
In your script, how about the following modification?
Modified script:
function processColumn(values) {
let replaceTable = [
[/\bN\./g, 'North'],
[/\bS\./g, 'South'],
[/\bSt\./g, 'Street']
];
return values.map(row =>
row.map(c => c ? replaceTable.reduce((curString, tableEntry) => curString.replaceAll(...tableEntry), c) : '', "")
);
}
- By this modification, 2 dimensional array is used as the argument as
values
ofprocessColumn(values)
.