I need to have multiple placeholders in the localization text, but having a hard time differentiating them. For example:
English:
"some_currency %# %#" = "%# %#"; // 1233454.34 USD
Arabic:
"some_currency %# %#" = "%# %#"; // USD ١٢٣٣٤٥٤.٣٤
...
let balance = CurrencyFormatter.string(1233454.34)
let currency = "USD"
LocalizedStringKey("some_currency \(balance) \(currency)")
How can I specify which placeholders go where since I need to swap them?
Related
I'm using typescript langage. I need to extract currency sign out of balance money, for example from "£1,070,449.00" I need "£" only. There are other currencies too, some of which are added at start of value like "£1,700" or some are appended at the end of value like "1,070,449.00 kr". I also have value 10070449 in a variable.
Summarising, I have:
parent1 = £1,070,449.00
child1 = 10070449
requirement1 = £
parent2 = 1,070,449.00 kr
child2 = 10070449
requirement2 = kr
# python
from re import sub
from decimal import Decimal
money = '$6,150,593.22'
value = Decimal(sub(r'[^\d.]', '', money))
currency_sign = sub(r'[\d.,]', '', money)
print(value)
print(currency_sign)
and this is output:
6150593.22
$
the kr one also works, you can modify this to meet you needs.
I am sorry I didn't notice it is typescript, but the regex is language independent, so it is not too much different.
what we need to do is to remove the digit, ',' and '.' from the string.
var money = '$6,150,593.22'
var pattern = /[\d.,]/g;
console.log(money.replace(pattern, ""))
and the output is:
$
I would like to know if there is a practical way of extracting the cells that are used in a formula in google scripts?
For an example:
Let's say A1 has a formula as below
=page1!C2*0,8+page2!B29*0,15+page3!C144*0,05
I would like var myCellsrecord the data of
page1!C2
page2!B29
page3!C144
Please let me know how would you make this.
Thanks in advance
Description
Here is an sample script that can parse equations as shown into the reference cells.
Note this only works for the specific formula you specified.
Code.gs
function test() {
try {
let spread = SpreadsheetApp.getActiveSpreadsheet();
let sheets = spread.getSheets().map( sheet => sheet.getName() );
// for this test
sheets = ["page1","page2","page3"];
let sheet = spread.getSheetByName("Sheet1");
let formula = sheet.getRange("A1").getFormula();
console.log(formula);
// break into parts
let parts = formula.split("*"); // but notice this is for specific case of *
parts.pop() // the last part doesn't contain any cell reference
console.log(parts);
let i = 0;
let results = [];
parts.forEach( part => { let j = sheets.findIndex( sheet => part.indexOf(sheet) >= 0 )
// remove sheet from range
let k = part.split('!')[1]; // this give cell A1 notation
results.push(sheets[j]+k)
}
);
console.log(results);
}
catch(err) {
console.log(err);
}
}
Execution log
6:54:44 AM Notice Execution started
6:54:46 AM Info =page1!C2*0,8+page2!B29*0,15+page3!C144*0,05
6:54:46 AM Info [ '=page1!C2', '0,8+page2!B29', '0,15+page3!C144' ]
6:54:46 AM Info [ 'page1C2', 'page2B29', 'page3C144' ]
6:54:45 AM Notice Execution completed
Reference
Array.map
Range.getFormula()
String.split()
Array.pop()
Array.forEach()
Array.findIndex()
Use range.getFormula() to get the formula and then use regex with String.match to get the cells:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const f =
'=page1!C2:C*0,8+page2!B29*0,15+page3!C144*0,056+sheet1!c:c* Sheet56!D10:D-D5:G10';
const matched = f.match(/(\w+!)?[A-Za-z]+\d*(:[A-Za-z]+\d*)?/g);
console.log(JSON.stringify(matched));
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
(\w+!)? - [?optional]Matches one or more word followed by ! for sheet name(eg: page1!)
[A-Za-z]+\d* - Matches one or more letters [A-Z] followed by zero or more digits \d* for range string(eg: C2)
(:[A-Za-z]+\d*)? - [optional] another range string match preceded by a :(eg: :C50)
New to Google Apps Script. Could someone tell me to to write stacked IFs in Apps Script.
I want to create a custom function that calculates the total sixteenths in a custom feet-inches-sixteens format.
The format is different depending on the length of the string. 1=1', 12=12',508 = 5 1/2", 1604 = 16 1/4", etc...and in order to convert to feet-inches-sixteen I have to calculate the total 16ths first.
Any help is appreciated.
Your question is a little hard to understand, but I think you have values in sheets represented as 1' 3 1/2" (1 ft, 3.5 in) which you'd like to convert to sixteenth-inch units.
The following function should do what you want:
function SIXTEENTHS(string) {
var regex = /^\s*([0-9]*(?=\'))?\'?\s*([0-9]+(?![0-9]))?\s*([0-9]+\/[0-9]+)?\"?\s*$/;
var parsed = string.match(regex);
if (!parsed) {
return 'Bad input';
}
var inches = (parsed[1] | 0) * 12; // Parse feet
inches += (parsed[2] | 0); // Parse inches
if (parsed[3]) {
inches += (parsed[3].split('/').reduce((a, b) => a/b)); // Parse fractions
}
return inches*16;
}
Parsing the string is a little tricky, but you can see what the regex is doing and test it at Regex 101.
I have one scenario
CF-123/NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123
Regex should be compatible with java and it needs to be done in one statement.
wherein I have to pick some information from this string.which are prefixed with predefined tags and have to put them in named groups.
(CF-) confirmationNumber = 123
(Name-) name = ANUBHAV
(RT-) rate = INR 450
(SI-) specialInformation = No smoking
(SC-) serviceCode = 123
I have written below regex:
^(CF-(?<confirmationNumber>.*?)(\/|$))?(([^\s]+)(\/|$))?(NAME-(?<name>.*?)(\/|$))?([^\s]+(\/|$))?(RT-(?<rate>.*?)(\/|$))?([^\s]+(\/|$))?(SI-(?<specialInformation>.*?)(\/|$))?([^\s]+(\/|$))?(SC-(?<serviceCode>.*)(\/|$))?
There can be certain scenarios.
**1st:** CF-123/**Ignore**/NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123
**2nd:** CF-123//NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123
**3rd:** CF-123/NAME-ANUBHAV/RT-INR 450/**Ignore**/SI-No smoking/SC-123
there can be certain tags in between the string separated by / which we don't need to capture in our named group.enter code here
Basically we need to pick CF-,NAME-,RT-,SI-,SC- and have to assign them in confirmationNumber,name,rate,specialInformation,serviceCode. Anything coming in between the string need not to be captured.
To find the five bits of information that you are interested, you can use a pattern with named groups, compiling the pattern with the regex Pattern
Then, you can use the regex Matcher to find groups
String line = "CF-123/**Ignore**/NAME-ANUBHAV/RT-INR 450/SI-No smoking/SC-123";
String pattern = "CF-(?<confirmationNumber>[^/]+).*NAME-(?<name>[^/]+).*RT-(?<rate>[^/]+).*SI-(?<specialInformation>[^/]+).*SC-(?<serviceCode>[^/]+).*";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
After that, you can work with the matched groups:
if (m.find( )) {
String confirmationNumber = m.group("confirmationNumber");
String name = m.group("name");
String rate = m.group("rate");
String specialInformation = m.group("specialInformation");
String serviceCode = m.group("serviceCode");
// continue with your processing
} else {
System.out.println("NO MATCH");
}
I need help with with three regular expressions for date validation. The date formats to validate against should be:
- MMyy
- ddMMyy
- ddMMyyyy
Further:
I want the regular expressions to match the exact number of digits in the formats above. For instance, January should be 01, NOT 1:
060117 // ddMMyy format: Ok
06117 // ddMMyy format: NOT Ok
Hyphens and slashes are NOT allowed, like: 06-01-17, or 06/01/17.
Below are the regex:es that I use. I cannot get them quite right though.
string regex_MMyy = #"^(1[0-2]|0[1-9]|\d)(\d{2})$";
string regex_ddMMyy = #"^(0[1-9]|[12]\d|3[01])(1[0-2]|0[1-9]|\d)(\d{2})$";
string regex_ddMMyyyy = #"^(0[1-9]|[12]\d|3[01])(1[0-2]|0[1-9]|\d)(\d{4})$";
var test_MMyy_1 = Regex.IsMatch("0617", regex_MMyy); // Pass
var test_MMyy_2 = Regex.IsMatch("617", regex_MMyy); // Pass, do NOT want this to pass.
var test_ddMMyy_1 = Regex.IsMatch("060117", regex_ddMMyy); // Pass
var test_ddMMyy_2 = Regex.IsMatch("06117", regex_ddMMyy); // Pass, do NOT want this to pass.
var test_ddMMyyyy_1 = Regex.IsMatch("06012017", regex_ddMMyyyy); // Pass
var test_ddMMyyyy_2 = Regex.IsMatch("0612017", regex_ddMMyyyy); // Pass, do NOT want this to pass.
(If anyone could take allowed days for each month, and leap years into account, that would be a huge bonus :)).
Thanks,
Best Regards