Using Google Sheets: I'm trying to extract the price e.g. 250, 990 and 1760, the currency e.g. CZK, and problematically the text /p, /2p or /3p from some text field. Some example lines are shown below.
250CZK /p /night
Extract in three cells 250, CZK, and /p
990CZK /night /2p room (/3p)
Extract in three cells 990, CZK, and /2p
1760CHF /3p, 1375 /2p
Extract in three cells 1760, CHF, and /3p
In the third example the 1375 /2p can be ignored. I only need the first instance. The sequence order of the entries can be random, e.g. I'd like the formula to be able to handle strings like:
/3p /night USD 550
resulting in
Extract in three cells 550, USD, and /3p
You want to analyse a number of strings that include a currency abbreviation, currency Value and per person value. Where there are more than one of any of these in any string, you want to return the first matching value.
Consider this script:
Logic
currencies
, contains all the relevant currency codes.indexOf
is used to identify a match.\d{3,4}
to find a number consisting of 3 or 4 digits.String.prototype.match
is used again with regex = /\d?p/
setValues
to the spreadsheet.function analyseString() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet = ss.getSheetByName("Sheet3")
var range = sheet.getRange("A3:A6")
var stringValues = range.getValues()
// get currency
var currencies = ["CZK", "CHF", "USD", "EUR", "GBP"]
// create a temportary array to hold results
var results = []
// loop through the stringValues
for (var v=0;v<stringValues.length;v++){
var string = stringValues[v][0]
// loop through currencies and get a match
for (var i=0;i<currencies[0].length;i++){
var currencyCode = currencies[i]
var searchIdx = string.indexOf(currencyCode)
if (searchIdx !=-1){
break
}
}
// get the Currency value
var unitsValue = string.match(/\d{3,4}/);
// get the perPerson value
const persons = string.match(/\/\d?p/);
Logger.log("v = "+v+", string value = "+string+", Unit value = "+unitValue+", Currency Code= "+currencyCode+", per persons = "+persons)
// push results to a temporary array
results.push([unitsValue,currencyCode,perPerson])
}
sheet.getRange(3,3,results.length,3).setValues(results)
}
SAMPLE - BEFORE
SAMPLE - console log
v = 0,
string value = 250CZK /p /night,
Unit value = 250,
Currency Code= CZK,
per persons = /pv = 1,
string value = 990CZK /night /2p room (/3p),
Unit value = 990,
Currency Code= CZK,
per persons = /2pv = 2,
string value = 1760CHF /3p, 1375 /2p,
Unit value = 1760,
Currency Code= CHF,
per persons = /3pv = 3,
string value = /3p /night USD 550,
Unit value = 550,
Currency Code= USD,
per persons = /3p
SAMPLE - AFTER