I want to create a button to perform a find + replace for a specific set of data in my sheet. I've had a look at other questions, but can't seem to get it to work!
This is a replica of my sheet: https://docs.google.com/spreadsheets/d/1KX4xBzIPq468WN57HKRelkCwN4sr_9Nd3v36m4jj4jI/edit#gid=0
The whole of column B is the data set I want to perform the F&R in. The values I want to use:
Find: " "
Replace "%20"
Essentially, replacing the spaces between words in column B with %20.
This, with some concatenation will then create some URLs for me.
In your sample Spreadsheet, it seems that the cells "A2, "B2" are the values of https://www.harrods.com/en-gb/shopping/women-clothing/brands and 4 Moncler Simone Rocha, respectively. And, at the output column, the cell "C2" is the formula of =CONCATENATE($A$2,"#dcp=1&dppp=100&OrderBy=rank&Filter=ABRA%5E",B2). In this case, how about modifying the formula as follows?
=CONCATENATE($A$2,"#dcp=1&dppp=100&OrderBy=rank&Filter=ABRA%5E",B2)
=CONCATENATE($A$2,"#dcp=1&dppp=100&OrderBy=rank&Filter=ABRA%5E",SUBSTITUTE(B2," ","%20"))
https://www.harrods.com/en-gb/shopping/women-clothing/brands#dcp=1&dppp=100&OrderBy=rank&Filter=ABRA%5E4%20Moncler%20Simone%20Rocha is obtained at the cell "C2".If you want to achieve this using Google Apps Script, how about the following script?
Please copy and paste the following script to the script editor of Spreadsheet and save it. And when you want to run this script with an image button, please put an image to the Spreadsheet and assign the function name of myFunction to the button. By this, when you click the button, the script works. And, when the script is run, a single space of the values of column "B" are replaced with %20.
function myFunction() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Filter URLs");
sheet.getRange("B2:B" + sheet.getLastRow()).createTextFinder(" ").replaceAllWith("%20");
}
As additional information, when you want to convert your current formula with Google Apps Script, you can also use the following script.
function myFunction2() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Filter URLs");
const url = sheet.getRange("A2").getValue();
const values = sheet.getRange("B2:B" + sheet.getLastRow()).getValues().map(([b]) => [url + "#dcp=1&dppp=100&OrderBy=rank&Filter=ABRA%5E" + b.replace(/ /g,"%20")]);
sheet.getRange(2, 4, values.length, 1).setValues(values);
}
getRange(2, 4, values.length, 1) to getRange(2, 3, values.length, 1).I've added a new sheet ("Filter URLs - Erik") to your sample spreadsheet. There, I deleted everything in Column C (including the header) and placed the following single formula in cell C1:
=ArrayFormula({"Output";IF(B2:B="",,$A$2&"#dcp=1&dppp=100&OrderBy=rank&Filter=ABRA%5E"&SUBSTITUTE(TRIM(B2:B)," ","%"))})
Expanding on one of @Tanaike's points, this one formula will create the header and all results for all rows without the need for dragging the formula, using scripts or using other Find/Replace methods.
If you're only looking to create urls, use
=ENCODEURL(B2)
instead. This will automatically convert all non url characters to it's percent encoding.