繁体   English   中英

使用Google脚本中的数组查找和替换子字符串

[英]Find and replace substrings using an array in google scripts

有没有一种方法可以更改此代码,以使其检查子字符串而不是整个单元格,还可以全局而不是每个单元格一次? 我正在使用Google脚本来更改电子表格,并且不得不查找和替换数十个术语。

我的JavaScript技能非常有限,并且在各处搜索了一些可行的方法。 我发现一些其他代码效率低下,并且在运行时会超时,这可以工作,但不包含子字符串或全局工作。 它必须在全球范围内工作,因为我还需要将“,”替换为“;”。 当有两个以上的国家时。

function findReplace() {
//create array
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getActiveSheet();
var lngLastRow = sheet1.getLastRow();
var lngLastCol = sheet1.getLastColumn();
var shValues = sheet1.getSheetValues(1,1,lngLastRow,lngLastCol);

//for each cell in shValues, perform replacement, and string trim
for (c=0;c<lngLastCol;c++){
for(r=1;r<lngLastRow;r++){
  if(shValues[r][c] != ""){
    var strString = String(shValues[r][c]); 
    var strHeading = String(shValues[0][c]);  

    if(strHeading.indexOf("Country of Origin") >-1 ){
      if(strString == "United Kingdom") (strString = "U.K.");

//set
    shValues[r][c] = strString;

  }
}

}

//set values
var range1 = sheet1.getRange(1,1,lngLastRow,lngLastCol);
range1.setValues(shValues);
}
}

编辑:
我正在尝试替换国家/地区名称,以使用另一组数据对其进行标准化。 因此,英国成为英国,美国成为美国,所有逗号“,”成为分号“;”。 还有其他几个国家,但不是全部,但这是主要问题。 “保加利亚,法国,美国”变为“保加利亚;法国;美国”

这是DOES处理子字符串的其他代码,但是它们在中途超时,并且不会替换多个“,”实例。 为简便起见,此功能仅是列表中国家的1/2。

function fandrCountrynames() {
// standardizes country names
var r=SpreadsheetApp.getActiveSheet().getRange("J2:K");
var rws=r.getNumRows();
var cls=r.getNumColumns();
var i,j,a,find,repl;
find="United States";
repl="U.S.";
for (i=1;i<=rws;i++) {
for (j=1;j<=cls;j++) {
  a=r.getCell(i, j).getValue();
  if (r.getCell(i,j).getFormula()) {continue;}
  try {
    a=a.replace(find,repl);
    r.getCell(i, j).setValue(a);
  }
  catch (err) {continue;}
}
}

 // standardizes country names
find="Korea; Republic of";
repl="South Korea";
for (i=1;i<=rws;i++) {
for (j=1;j<=cls;j++) {
  a=r.getCell(i, j).getValue();
  if (r.getCell(i,j).getFormula()) {continue;}
  try {
    a=a.replace(find,repl);
    r.getCell(i, j).setValue(a);
  }
  catch (err) {continue;}
}
}

   // standardizes country names

find="Not Specified";
repl=" ";
for (i=1;i<=rws;i++) {
for (j=1;j<=cls;j++) {
  a=r.getCell(i, j).getValue();
  if (r.getCell(i,j).getFormula()) {continue;}
  try {
    a=a.replace(find,repl);
    r.getCell(i, j).setValue(a);
  }
  catch (err) {continue;}
}
}


  // standardizes country names

find="Iran; Islamic Republic of";
repl="Iran";
for (i=1;i<=rws;i++) {
for (j=1;j<=cls;j++) {
  a=r.getCell(i, j).getValue();
  if (r.getCell(i,j).getFormula()) {continue;}
  try {
    a=a.replace(find,repl);
    r.getCell(i, j).setValue(a);
  }
  catch (err) {continue;}
}
}
}

编辑 :这是不完全正确的,因为它是在看到更多解释情况的评论之前被编写和发布的。 第一部分仍然可以工作,但是需要遍历一组值来替代from和to。

下面的行将String(shValues [r] [c])中的UK替换为UK的任何实例:

var strString = String(shValues[r][c]).replace("United Kingdom", "U.K.");

要搜索文件中的所有单元格,您必须将工作表加载到内存中并循环遍历。

编辑 :以下内容与OP中的注释无关,但留给其他人使用。

如果您希望在其他单元格中替换此文本,则可以使用标准的Google表格公式= SUBSTITUTE(text_to_search,search_for,replace_with,[occurrence_number])因此,要使单元格D2显示单元格C2的内容,请替换相同的内容上面的字符串,您将拥有:

=SUBSTITUTE(C2, "United Kingdom", "U.K.")

如果要将此应用于所有D列,请将其放在单元格D2中:

=ARRAYFORMULA(IF(ISBLANK(C2:C),,SUBSTITUTE(C2:C, "United Kingdom", "U.K.")))

编辑2:经过下面的一些评论,我们来:编辑您以后提供的代码时,我发现您多次调用.getValue()。 这会将请求发送回Google的服务器,以读取每个呼叫的文件。 这使许多呼叫返回到服务器,并导致超时。 我们通过使用r.getValues()将所有必要的数据从工作表中读取到内存中以从范围r中提取所有值来进行限制。 在您的情况下,我们仅获取2列,即J和K。然后遍历此数据并遍历替换对象以更改所有国家/地区:

function fandrCountrynames() {
  // standardizes country names

  //Define an Object of the replacement strings
  var country = [];
  country[0] = {searchFor: "Not Specified", replacement: " "};
  country[1] = {searchFor: "United Kingdom", replacement: "U.K."};
  country[2] = {searchFor: "United States", replacement: "U.S."};
  country[3] = {searchFor: "Korea; Republic of", replacement: "South Korea"};
  country[4] = {searchFor: "Iran; Islamic Republic of", replacement: "Iran"};

  //Read the items in the range below
  var sheet = SpreadsheetApp.getActiveSheet()
  var r = sheet.getRange("A2:B");
  var sheetData = r.getValues();

  //Loop through each row in the sheet
  for (var i = 0; i < sheetData.length; ++i) {
    //Loop through each repalcement string pair and save the replaced string back to the data object
    for (var j = 0; j < country.length; ++j) {
      var strString = sheetData[i][0].replace(country[j].searchFor, country[j].replacement);
      sheetData[i][0] = strString;
      strString = sheetData[i][1].replace(country[j].searchFor, country[j].replacement);
      sheetData[i][1] = strString;
    }
  }

  //Save the data back to the file
  r.setValues(sheetData);
}

暂无
暂无

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

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