Hi Ragicians!
I’m in a bit of a bind. The out-of-the-box SPELLNUMBER does not cover my locale (TH), so I decided to create a JS workflow that will read positive integers in Thai.
Global JS: (because this is shared between many sheets)
function decimalExpansionArray(val) {
//expands val into array of digits, from rightmost, assuming that val > 0
let valToRead = val;
let expansion = new Array();
while (valToRead > 0) {
expansion.push(valToRead % 10);
valToRead = (valToRead - (valToRead % 10))/10;
}
return expansion;
}
function readCurrentDigitTh(digit, exponent) {
//reads the current digit in Thai
//e.g., the 2 in 21 is read ยี่สิบ
const digits = ['', 'หนึ่ง', 'สอง', 'สาม', 'สี่', 'ห้า', 'หก', 'เจ็ด', 'แปด', 'เก้า'];//0 is never read, and digits correspond to indices
const exponents = ['', 'สิบ', 'ร้อย', 'พัน', 'หมื่น', 'แสน', 'ล้าน']; //no word for the units digit
let readDigit = '';
if (digit == 0) {
return readDigit;
}
readDigit = digits[digit] + exponents[exponent];
readDigit = readDigit.replace('หนึ่งสิบ', 'สิบ');
readDigit = readDigit.replace('สองสิบ', 'ยี่สิบ');
return readDigit;
}
function spellNumberTh(val) {
//reads val in Thai
if (val == 0) {
return 'ศูนย์';
}
return decimalExpansionArray(val).map(readCurrentDigitTh).reduceRight((accumulator, currentValue) => accumulator.concat(currentValue),);
}
Sheet-specific JS: (post-workflow because people will complain otherwise)
function setGrandTotalInThai() {
let query = db.getAPIQuery("/ragicsales/10006");
let entry = query.getAPIEntry(param.getNewNodeId(2000246));
let grandTotalInWords = spellNumberTh(Math.round(entry.getFieldValue(2000245)));
entry.setFieldValue(1000159, grandTotalInWords);
entry.save();
}
However, when a record is saved, errors show up:
:3:6 Expected ; but found valToRead let valToRead = val; ^ in at line number 3 at column number 6
:50:6 Expected ; but found query let query = db.getAPIQuery("/ragicsales/10006"); ^ in at line number 50 at column number 6
and the field isn’t updated.
Running the global JS on JSFiddle works fine.
It could definitely use some work with numbers bigger than 10,000,000 and negative numbers, but a recursion should suffice.
Replacing all the let
and const
with var
doesn’t help, so I’m at wit’s end. Could anybody help me? I would be very grateful.