简体   繁体   English

使用Google Apps脚本替换数组中的变音符号

[英]Replacing Diacritics in an Array with Google Apps Script

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. 我希望能够将在此找到的解决方案( 在JavaScript中删除字符串中的重音符号/变音符号 )应用于数组。

We've already visited the topic on "Remove accents/diacritics in a string in Javascript" and the solution works great for one cell. 我们已经访问了“删除Java字符串中的重音符号/变音符号”主题,该解决方案适用于一个单元格。 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);
}

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
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM