I'm building a custom function using decodeuricomponent and handling URIError: malformed URI with "try/catch". It was fine until I had to change the code to work with arrayformula (built-in function).
I've already tried several ways to do it, but none of them worked.
The else part works fine, but when it's an array (always true when arrayformula is used) returns encodedString.map(row => row.map(cell => unescape(cell))) to all cells.
I need a solution that works on a cell level (every array position separately) even when I use arrayformula.
My Script
function stringDecoder(encodedString) {
if(Array.isArray(encodedString)===true){
try{
return encodedString.map(row => row.map(cell => decodeURIComponent(cell)))
}
catch(err){
return encodedString.map(row => row.map(cell => unescape(cell)))
}
}
else{
try{
return decodeURIComponent(encodedString)
}
catch(err){
return unescape(encodedString)
}
}
}
Try modifying the loop like this, so it applies the try/catch to each cell individually:
function stringDecoder(encodedString) {
if (Array.isArray(encodedString)) {
return encodedString.map(row => row.map(cell => {
try {
return decodeURIComponent(cell);
} catch (err) {
return unescape(cell);
}
}));
} else {
try {
return decodeURIComponent(encodedString);
} catch (err) {
return unescape(encodedString);
}
}
}