2

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('.', ','));

DEMO:

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]
4
  • 2
    Can you share what vals1 here looks like? Is it an array of strings or numbers? Commented Mar 27, 2020 at 7:10
  • I edited with log of vals1 Commented Mar 27, 2020 at 8:24
  • array vals1 looks like not strings Commented Mar 27, 2020 at 8:33
  • Errors are important and should be quoted in the question. Runnable demo should also be provided by simulating array values. Commented Mar 27, 2020 at 9:56

3 Answers 3

3

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 )

Sign up to request clarification or add additional context in comments.

Comments

0

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 )

2 Comments

sorry. It doesn't work. Error: dots.replace is not a function
What you mean? The snippet is running without any issues. I just copied your code snippet then updated it.
-1

You can also use

.replace(/\./g,',')

5 Comments

Why is this solution better? As far as I can tell this would replace everything with a ','. Since // introduces an Regex and . matches everything? The global flag would replace every character or did I miss something?
Why don't you try it. I use this in my projects and works nicely.
"foo bar".replace(/./g,',') returns ",,,,,,,"
Isn't that the same as what OP is doing? What difference does it make and why does this answer the question?
sorry, guys. It doesn't work. Error: dots.replace is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.