1

We're trying to replace all Diacritics in a sheet with their more database-friendly counterparts. I want to be able to apply the solution found here (Remove accents/diacritics in a string in JavaScript) to an Array.

We've already visited the topic on "Remove accents/diacritics in a string in Javascript" and the solution works great for one cell. However, when I try to do it with my current code, it only changes one value and pastes it to the whole array. What am I doing wrong?

(Rest of code is visible in link)

function removeDiacritics () {
  var range = "A2:B3";
  var array = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cleaner").getRange(range).getValues();
  var base = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cleaner").getRange(range);
  var newArray = [];

  for(var row=0;row<array.length;row++){
    for(var i=0;row<array[row].length;i++){

    var newText = array[row][i].replace(/[^\u0000-\u007E]/g, function(a){ 
      return diacriticsMap[a] || a;
    });
    newArray.push(newText);
  }
  }

      base.setValue(newArray);
}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Coooley
  • 11
  • 1

1 Answers1

0

Answer:

You are iterating through the incorrect counter variable in your nested loop.

Fix:

In your nested loop, in order to change the element of the array which is being compared, you need to change:

for(var row=0;row<array.length;row++){
  for(var i=0;row<array[row].length;i++){
    // code
  }
}

to:

for(var row = 0; row < array.length; row++){
  for(var i = 0; i < array[row].length; i++){
    //code
  }
}
Community
  • 1
  • 1
Rafa Guillermo
  • 14,474
  • 3
  • 18
  • 54