0

The below code will find NAME 1 and replace it with NM1 as per the replaceInSheet line of code

I would like the code to look at a list of data in SHEET 2 to do the Find&Replace IN SHEET 1. So Column A has names and is repalced by what is in Column B

enter image description here

function runReplaceInSheet() {

var spreadsheet = SpreadsheetApp.openById(""); // UPDATE ID
var sheet = SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]); // UPDATE number in square 
brackets
var range = sheet.getRange("F2:F");
//  get the current data range values as an array
//  Lesser calls to access the sheet, lower overhead 
var startRow = 2; // First row of data to process. start at Row 3
var numRows = 2; // Specify what column to look at. 
// Fetch the range of cells 
var dataRange = sheet.getRange(startRow, 150 , numRows, 1) // Numbers of rows to process
// Fetch values for each row in the Range
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var v = row[2]; // edit: don't need this
var values = range.getValues();  



replaceInSheet(values, 'Name 1', 'N1');
replaceInSheet(values, 'Name 2', 'N2');

//write the updated values to the sheet, again less call;less overhead
range.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;
}}
16
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand about I would like the code to look at a list of data in SHEET 2 to do the Find&Replace IN SHEET 1. So Column A has names and is repalced by what is in Column B and your sample image. In order to correctly understand about your current issue and goal, can you provide the sample input and output you expect? Commented Sep 6, 2020 at 7:51
  • Tanaike i will share a sheet with you Commented Sep 6, 2020 at 7:53
  • Thank you for replying. I thought that when the sample input and output you expect might help to understand about your current situation and goal. Commented Sep 6, 2020 at 7:55
  • here is first spreadsheet docs.google.com/spreadsheets/d/… Commented Sep 6, 2020 at 7:56
  • here is 2nd link docs.google.com/spreadsheets/d/… Commented Sep 6, 2020 at 7:57

1 Answer 1

1

I believe your goal as follows.

  1. You want to retrieve the values of columns "A" and "B" of "Replace Agency Name" in "Master - Agency Name Change".
  2. You want to change the column "F" of "D2D MACRO" in "D2D Macro" by the column "B" corresponding to the column "A" of "Replace Agency Name".

Modification points:

  • In your script, getValues() is used in the loop. In this case, the process cost will become high.
  • The values for replacing are directly used in the script.

From above situation, I would like to propose the following flow.

  1. Retrieve values for replacing are retrieved as the source values from the sheet "Replace Agency Name" in "Master - Agency Name Change".
  2. Replace the values using the retrieved source values.

Sample script 1:

In this sample script, the values are replaced by setValues.

function myFunction() {
  // 1. Retrieve values for replacing are retrieved as the source values from the sheet "Replace Agency Name" in "Master - Agency Name Change".
  var srcSheet = SpreadsheetApp.openById("###").getSheetByName("Replace Agency Name");
  var srcValues = srcSheet.getRange(`A2:B${srcSheet.getLastRow()}`).getValues();
  var srcObj = srcValues.reduce((o, [a, b]) => Object.assign(o, {[a]: b}), {});
  
  // 2. Replace the values using the retrieved source values.
  var dstSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("D2D MACRO");
  var dstRange = dstSheet.getRange(`F2:F${dstSheet.getLastRow()}`);
  var dstValues = dstRange.getValues().map(([f]) => [srcObj[f] || f]);
  dstRange.setValues(dstValues);
}

Sample script 2:

In this sample script, the values are replaced by TextFinder.

function myFunction() {
  // 1. Retrieve values for replacing are retrieved as the source values from the sheet "Replace Agency Name" in "Master - Agency Name Change".
  var srcSheet = SpreadsheetApp.openById("###").getSheetByName("Replace Agency Name");
  var srcValues = srcSheet.getRange(`A2:B${srcSheet.getLastRow()}`).getValues();
  var dstSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("D2D MACRO");

  // 2. Replace the values using the retrieved source values.
  var dstRange = dstSheet.getRange(`F2:F${dstSheet.getLastRow()}`);
  srcValues.forEach(([a, b]) => {
    if (a.toString() != "") dstRange.createTextFinder(a).matchEntireCell(true).replaceAllWith(b)
  });
}

Note:

  • In above scripts, srcSheet is "Replace Agency Name" in "Master - Agency Name Change". And dstSheet is "D2D MACRO" of the active Spreadsheet. Please be careful this.

References:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.