Home > Software engineering >  Map commas to # in an HTML form
Map commas to # in an HTML form

Time:01-20

I have this code here:

var name = $('#name01').val();
var description = $('#description').val();
var option1 = $('#Option1').val();
var option2 = $('#Option2').val();
var data = [name, description, option1, option2].join(",");
google.script.run.saveDataAsCSV(data, uploadParentFolderId);

Its function is basically to return the values ​​of the HTML form to the .gs file of my script, thus making it possible to generate a .csv file with these values.

The issue is that I would like any comma that is typed in this form to be replaced by # before generating the .csv file.

For that, I tried to modify the code like this here:

var name = $('#name01').val();
var description = $('#description').val();
var option1 = $('#Option1').val();
var option2 = $('#Option2').val();
var form_values = [name, description, option1, option2];
form_values = form_values.map(r => r.map(c => c.replaceAll(",", "#")));
var data = form_values.join(",");
google.script.run.saveDataAsCSV(data, uploadParentFolderId);

However, with this modification, the form stops working, opening a blank screen when it is submitted.

How to adjust it?

CodePudding user response:

You have too many calls to map(). r is a string, not an array, you can't map it.

form_values = form_values.map(r => r.replaceAll(",", "#"));
  • Related