Reputation: 869
I have an array in single column in Google Sheets. the array contains dots "." as separator for fractional part (0.234, 23.6789, 34.987, etc.). I want to replace all dots in array with commas "," to get (0,234, 23,6789, 34,987, etc.) by Google Script. I wrote this code but it doesn't work.
function checkItNow() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('TESTSCRIPTS');
let vals1 = sheet.getRange(1, 2, sheet.getLastRow()).getValues().map(a => a[0]);
Logger.log(vals1);
let newVals1 = vals1.map(dots => dots.replace('.', ','));
let vals1 = [0.234, 23.6789, 34.987]
let newVals1 = vals1.map(dots => dots.replace('.', ','));
console.log( newVals1 )
I get the error:
Error: dots.replace is not a function
Where am I wrong and how to fix it?
My console log:
[20-03-27 15:16:29:211 ICT] [32.6942782575, 19.9155757705, 147.12263620000002, 489.1165073995, 892.202845, 0.37799999999999995, 0.023364250000000003, 0.26248482500000003, 0.15887690000000002, 23.026564925, 0.04906492500000001, 0.0, 706.7257752500001, 0.0, 0.0, 60.44003225000001, 0.0, 4.630720500000001, 618.5996974999999, 7.784899025, 108.97249249999999, 701.2664050000001, 1737.10503, 575.4707025, 3.8459734249999995, 0.6364439825, 0.456, 1.0932774250000001, 64.57385624999999, 23628.313777499996, 268.75, 1407.9519174999998, 8320.3449425, 10634.185062499999, 3.78603115, 6.715, 0.0, 6.715, 3.8336425000000003, 0.0, 4516.750427499999, 44.659411000000006, 1.1592136175, 1.7699011725000002, 8.076147557499999, 3.6708841524999993, 0.4781239375, 281.6134775, 2.4034999999999997, 219.05541475, 194.3, 10.0378800575]
Upvotes: 2
Views: 1031
Reputation: 73896
You can fix this issue by using the toString()
method:
let newVals1 = vals1.map(dots => dots.toString().replace(/\./g,','));
as dots
here is actually a number and not a string and replace()
method works only on strings.
let vals1 = [0.234, 23.6789, 34.987]
let newVals1 = vals1.map(dots => dots.toString().replace('.', ','));
console.log( newVals1 )
Upvotes: 3
Reputation: 1032
The regex should be like: /\./g
let regex = /\./g
let vals1 = ['0.234', '23.6789', '34.987']
let newVals1 = vals1.map(dots => dots.replace(regex, '\,'));
console.log( newVals1 )
Upvotes: 0