Is there a way to alter this code to have it examine substrings rather than the whole cell and also globally rather than just once per cell? I am using google scripts to alter a spreadsheet and have to find and replace dozens of terms.
I have very limited javascript skills and have searched all over for something that works. I found some other code that is inefficient and times out when I run it, this works, but does not include substrings or work globally. It has to work globally because I also need to replace "," with ";" for when there are more than 2 countries.
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);
}
}
EDIT:
I am trying to replace country names to standardize them with another set of data. So, United Kingdom becomes U.K., USA becomes U.S., all the commas "," become semi-colons ";". There are several more, not all are countries, but this is the main issue. "Bulgaria, France, USA" becomes "Bulgaria; France; U.S."
Here is the other code that DOES account for substrings, but times out about halfway through and does not replace multiple instances of ",". For brevity, this is only 1/2 of the countries on the list in this function.
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;}
}
}
}