简体   繁体   English

如何在 Google Apps 脚本中修复或调整此查找和替换

[英]How to fix or adapt this Find and Replace in Google Apps Script

I have a sheet which I regularly have to paste a large amount of data into and need to do ten iterations of "Find and Replace" to fix some of the data.我有一张表,我经常需要将大量数据粘贴到其中,并且需要进行十次“查找和替换”迭代来修复一些数据。 The data I paste in comes from a database which I will not be given direct access to and for some reason whenever they export it many common special characters such as é or ' get scrambled.我粘贴的数据来自一个数据库,我将无法直接访问该数据库,并且由于某种原因,每当他们将其导出时,许多常见的特殊字符(例如é'都会被打乱)。

I stole and adapted this excellent solution to make a script which replaces substrings with other strings wherever found in the data.我窃取并改编了这个出色的解决方案来制作一个脚本,该脚本将子字符串替换为数据中任何位置的其他字符串。 The script searches the active sheet for the specific strings which are messed up and replaces them with the correct characters.该脚本在活动工作表中搜索被弄乱的特定字符串并用正确的字符替换它们。

The script has worked great, except it has for some unknown reason occasionally replaced the single character - with a ' .该脚本运行良好,只是由于某些未知原因偶尔将单个字符替换- ' I could work with this if it was consistent but for some reason it only occurs intermittently.如果它是一致的,我可以使用它,但由于某种原因,它只是间歇性地发生。 I suspect this has something to do with the character |我怀疑这与角色有关| essentially functioning as an OR in lines 8 or 9 below, but I cannot figure out why it would only be doing it from time to time.基本上在下面的第 8 行或第 9 行中起到 OR 的作用,但我无法弄清楚为什么它只会不时地这样做。

A perfect solution would be to stop this intermittent replacement occurring.一个完美的解决方案是阻止这种间歇性更换的发生。 The next best thing would be adapting the script to replace the occurrences of ' back into a - in a single column G after doing what it's currently doing.下一个最好的事情是调整脚本以在完成当前正在执行的操作之后将出现的'替换为-在单个列 G 中。 What I thought was more attainable for my skill level (zero), was making a copy of the script which would only replace ' with - in column G我认为对于我的技能水平(零)来说更容易实现的是制作脚本的副本,该副本只会在 G 列中将'替换为-

My understanding is that the getValues() in line 5 basically selects the entire sheet, I've tried replacing this with various other get thingies to just grab G5:G, but I can't quite get it to work.我的理解是第 5 行中的getValues()基本上选择了整个工作表,我尝试将其替换为各种其他get事物以获取 G5:G,但我无法让它正常工作。 I have absolutely no coding experience unless you count some pretty artful use of Sheets' built in functions.我绝对没有编码经验,除非你算上对 Sheets 内置函数的一些巧妙使用。 Unfortunately, I don't understand the error messages I'm getting when I debug.不幸的是,我不明白调试时收到的错误消息。

Current script:当前脚本:

function runReplaceInSheet(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  //  get the current data range values as an array
  //  Fewer calls to access the sheet -> lower overhead 
  var values = sheet.getDataRange().getValues();  

  // Replace Subject Names
  replaceInSheet(values, /\'|-/g, "'");
  replaceInSheet(values, /\’|-/g, "'");
  replaceInSheet(values, /\√°|-/g, "á");
  replaceInSheet(values, /\√©|-/g, "é");
  replaceInSheet(values, /\√≠|-/g, "í");
  replaceInSheet(values, /\√≥|-/g, "ó");
  replaceInSheet(values, /\√Å|-/g, "Á");
  replaceInSheet(values, /\√Ø|-/g, "ï");
  replaceInSheet(values, /\ƒó|-/g, "ė");
  replaceInSheet(values, /\≈´|-/g, "ū");

  // Write all updated values to the sheet, at once
  sheet.getDataRange().setValues(values);
}

function replaceInSheet(values, to_replace, replace_with) {
  //loop over the rows in the array
  for(var row in values){
    //use Array.map to execute a replace call on each of the cells in the row.
    var replaced_values = values[row].map(function(original_value) {
      return original_value.toString().replace(to_replace,replace_with);
    });

    //replace the original row values with the replaced values
    values[row] = replaced_values;
  }
}

I made various attempts at modifying it to only replace getValues with either a named range PO_EligibilityGG or just the G Column, but for brevity I'll paste just the version which I think came the closest to working:我做了各种尝试来修改它,只用命名范围PO_EligibilityGG或 G 列替换getValues ,但为简洁起见,我将只粘贴我认为最接近工作的版本:

function runReplaceInSheet(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet()
  //  get the current data range values as an array
  //  Fewer calls to access the sheet -> lower overhead 
  var values = sheet.getRange('PO_EligibilityGG');  

  // Replace Subject Names
  replaceInSheet(values, /\'|-/g, "-");


  // Write all updated values to the sheet, at once
  sheet.getRange('PO_EligibilityGG').setValues(values);
    }
...

Error Message:错误信息:

Exception: The parameters (SpreadsheetApp.Range) don't match the method signature for SpreadsheetApp.Range.setValues.例外:参数 (SpreadsheetApp.Range) 与 SpreadsheetApp.Range.setValues 的方法签名不匹配。 runReplaceInSheet @ Code.gs:11 runReplaceInSheet @ Code.gs:11

I have to run this replacement 2 or 3 times a day at the moment on 3 different sheets and eliminating this problem would just take app the pain out of this.我现在每天必须在 3 张不同的纸张上运行此替换 2 或 3 次,消除此问题只会让应用程序摆脱痛苦。

Based on what I can see in your case, it seems you don't get the values in Column G using getRange correctly.根据我在您的情况下看到的情况,您似乎没有正确使用 getRange 获得 G 列中的值。

Try this code in getting the values in Column G using getRange:在使用 getRange 获取 G 列中的值时尝试此代码:

function valuesOfColumnG(){
  var ss = SpreadsheetApp.getActiveSheet();
  var len = ss.getLastRow();
  for(var i = 1; i < len +1; i++){
    var valuesOfG = ss.getRange("G"+i).getValue();
    console.log('Values of G ' + valuesOfG);
  }
}

Screenshot of the spreadsheet:电子表格的屏幕截图:

在此处输入图像描述

Actual value in the logs that gets the values from Column G:从 G 列获取值的日志中的实际值:

在此处输入图像描述

References:参考:

https://developers.google.com/apps-script/reference/spreadsheet/sheet#getRange(String) https://developers.google.com/apps-script/reference/spreadsheet/sheet#getRange(String)

how to apply script to entire columns in google spredsheet 如何将脚本应用于谷歌电子表格中的整个列

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

相关问题 如何在 Google Apps 脚本中查找和替换值? - How to Find & Replace Value in Google Apps Script? Google Apps脚本中的“ .Replace” - “.Replace” in Google Apps Script 用于在 Google 表格中进行多次查找和替换的 Google Apps 脚本 - Google Apps Script for Multiple Find and Replace in Google Sheets 在附加到Google文档的Google Apps脚本中通用查找和替换 - Universal find and replace within google apps script attached to google docs Google Apps脚本多个在Google表格中查找和替换正则表达式 - Google Apps Script Multiple Find and replace regex in Google Sheets Google Apps 脚本或 JavaScript - 查找和替换结果比要求的高一排 - 如何将其上移一排以进行替换? - Google Apps Script or JavaScript - Find & Replace result is one row higher than required - How do I move it one row up to replace? Google Apps脚本:如何解决“序言中不允许内容” - Google Apps Script: How to fix “content is not allowed in prolog” Google 应用程序脚本一直将我的计算视为字符串,如何解决? - Google apps script keep considering my calculation as string, how to fix it? 如何修复以随机顺序运行的 Google Apps 脚本功能 - How to fix Google Apps Script Function Running in random sequence 如何使用 Google Apps 脚本替换电子表格中的文本? - How do I replace text in a spreadsheet with Google Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM