将 Google 表格中的数字转换为文本/单词
Posted
技术标签:
【中文标题】将 Google 表格中的数字转换为文本/单词【英文标题】:Convert numbers in Google Sheets to Text / Words 【发布时间】:2019-02-22 16:10:36 【问题描述】:例子:
value | desired output |
---|---|
300 | Three Hundred |
300.50 | Three Hundred and 50 cents |
我在 Google 表格中找不到执行此操作的方法,而且我还不够精明,无法构建任何东西。
【问题讨论】:
【参考方案1】:这个网站 https://www.excelforum.com/tips-and-tutorials/1015010-convert-a-numeric-value-to-words-without-vba.html
确实显示了可以执行此操作的 Excel 公式。我刚刚在 Google 表格中进行了测试,这也有效。
公式是
=IF(OR(LEN(FLOOR(E3,1))>=13,FLOOR(E3,1)<=0),"Out of range",PROPER(SUBSTITUTE(CONCATENATE(CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),1,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),2,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),3,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),2,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),3,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),2,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),3,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),IF(E3>=10^9," billion ",""),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),4,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),5,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),6,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),5,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),6,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),5,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),6,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),4,3))>0," million ",""),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),7,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),8,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),9,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),8,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),9,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),8,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),9,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),7,3))," thousand ",""),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),10,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),11,1)+1,"",CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),12,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),11,1))>1,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),12,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),IF(VALUE(MID(TEXT(INT(E3),REPT(0,12)),11,1))=0,CHOOSE(MID(TEXT(INT(E3),REPT(0,12)),12,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")))," "," ")&IF(FLOOR(E3,1)>1," dollars"," dollar")))
【讨论】:
【参考方案2】:dominic's 答案在这里和那里的小调整完全符合您的需求:
=REGEXREPLACE(IF(OR(LEN(FLOOR(A1,1))>=13,FLOOR(A1,1)<=0),"Out of range",
PROPER(SUBSTITUTE(CONCATENATE(CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),1,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),2,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),3,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),2,1))>1,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),3,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),2,1))=0,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),3,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),
IF(A1>=10^9," billion ",""),
CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),4,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),5,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),6,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),5,1))>1,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),6,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),5,1))=0,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),6,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),4,3))>0," million ",""),CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),7,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),8,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),9,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),8,1))>1,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),9,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),8,1))=0,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),9,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),7,3))," thousand ",""),CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),10,1)+1,"","one hundred ","two hundred ","three hundred ","four hundred ","five hundred ","six hundred ","seven hundred ","eight hundred ","nine hundred "),
CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),11,1)+1,"",CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),12,1)+1,"ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"),"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),11,1))>1,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),12,1)+1,"","-one","-two","-three","-four","-five","-six","-seven","-eight","-nine"),
IF(VALUE(MID(TEXT(INT(A1),
REPT(0,12)),11,1))=0,CHOOSE(MID(TEXT(INT(A1),
REPT(0,12)),12,1)+1,"","one","two","three","four","five","six","seven","eight","nine"),"")))," "," "))&
IF(IFERROR(QUERY(A1,"where Col1 Contains '.'"),)<>"",
" and "®EXEXTRACT(TO_TEXT(A1), "\.(.+)")&" cents", ))," ", " ")
【讨论】:
您的编辑也会输出小数位,对吧? @Khan 确实【参考方案3】:由于这是专门用于 Google 表格的,因此您可以使用它
=REGEXEXTRACT(JOIN(", ",QUERY(IMPORTDATA("https://www.calculatorsoup.com/calculators/conversions/numberstowords.php?number=" & A1 & "&format=words&letter_case=lowercase&action=solve"),"where Col1 contains '<div id=""answer""><br>'")),">([^<]+)")
其中 A1 是您要转换的数字
信用:spell number formulae or number to words。该链接中还有许多替代解决方案,例如这是一种使用数组公式的方法
=ArrayFormula(if(A2:A="","",if(len(int(A2:A))<13,"",if(right(left(int(A2:A),len(int(A2:A))-12),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-12),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-12),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-12),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-12),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-12),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" trillion")&" "&if(len(int(A2:A))<10,"",if(right(left(int(A2:A),len(int(A2:A))-9),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-9),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-9),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-9),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-9),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-9),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" billion")&" "&if(len(int(A2:A))<7,"",if(right(left(int(A2:A),len(int(A2:A))-6),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-6),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-6),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-6),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-6),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-6),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" million")&" "&if(len(int(A2:A))<4,"",if(right(left(int(A2:A),len(int(A2:A))-3),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-3),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-3),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-3),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-3),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-3),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" thousand")&" "&if(len(int(A2:A))<1,"",if(right(left(int(A2:A),len(int(A2:A))-0),3)+0>99,choose(left(right(left(int(A2:A),len(int(A2:A))-0),3))+0,"one","two","three","four","five","six","seven","eight","nine")&" hundred ",)&if(right(right(left(int(A2:A),len(int(A2:A))-0),3),2)*1>19,choose(left(right(right(left(int(A2:A),len(int(A2:A))-0),3),2))-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(right(left(int(A2:A),len(int(A2:A))-0),3))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(right(right(left(int(A2:A),len(int(A2:A))-0),3),2)+1,"","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen")))&if(int(A2:A)=0,"0 dollar",if(int(A2:A)<2," dollar"," dollars"))&iferror(" and "&if(round(mod(A2:A,1)*100,2)>19,choose(left(mod(A2:A,1)*100)-1,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety")&choose(right(round(mod(A2:A,1)*100,2))+1,""," one"," two"," three"," four"," five"," six"," seven"," eight"," nine"),choose(round(mod(A2:A,1)*100,2)+1,"no","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"))&" cents")))
输出是
300 three hundred dollars and no cents
300.5 three hundred dollars and fifty cents
如果您的数字在不同的位置,只需将数组公式中的A2:A
更改为正确的位置
另一种方法是通过选择 File > Make a copy 克隆以下 2 张已制作的工作表,然后使用
https://docs.google.com/spreadsheets/d/1jQkM5kqvaG-T0M1nqrZO5Wc9A9yjdKeAI2hFCjWa9sA/edit?usp=sharing https://docs.google.com/spreadsheets/d/13f7ITHSPIy8CWyhkDpGlqJL0EzFjBrMEAnEm08V7ytg/edit?usp=sharing信用:Amount or Numbers to Words Converter Using Google Sheets。作者还介绍了只需要一张纸的较新版本。如果你想使用那些clone it from here
【讨论】:
【参考方案4】:你可以试试the Numbertext Add-on
=NUMBERTEXT("EUR 25") => twenty-five euro
=MONEYTEXT(25,"EUR") => twenty-five euro
【讨论】:
【参考方案5】: 复制此文件:converter 然后将这两个工作表添加到您的电子表格中,或者使用IMPORTRANGE
在F6 中导入值,另一个IMPORTRANGE
从B11导入转换后的值>
【讨论】:
【参考方案6】:您可以使用此方法将您的货币换算成文字:
function convertToWords(input)
var a, b, c, d, e, output, outputA, outputB, outputC, outputD, outputE;
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
if (input === 0) // Zero
output = "Rupees zero";
else if (input == 1) // One
output = "Rupee one only";
else // More than one
// Tens
a = input % 100;
outputA = oneToHundred_(a);
// Hundreds
b = Math.floor((input % 1000) / 100);
if (b > 0 && b < 10)
outputB = ones[b];
// Thousands
c = (Math.floor(input / 1000)) % 100;
outputC = oneToHundred_(c);
// Lakh
d = (Math.floor(input / 100000)) % 100;
outputD = oneToHundred_(d);
// Crore
e = (Math.floor(input / 10000000)) % 100;
outputE = oneToHundred_(e);
// Make string
output = "Rupees";
if (e > 0)
output = output + " " + outputE + " crore";
if (d > 0)
output = output + " " + outputD + " lakh";
if (c > 0)
output = output + " " + outputC + " thousand";
if (b > 0)
output = output + " " + outputB + " hundred";
if (input > 100 && a > 0)
output = output + " and";
if (a > 0)
output = output + " " + outputA;
output = output + " only";
return output;
function oneToHundred_(num)
var outNum;
var ones = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
var teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
var tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
if (num > 0 && num < 10) // 1 to 9
outNum = ones[num]; // ones
else if (num > 9 && num < 20) // 10 to 19
outNum = teens[(num % 10)]; // teens
else if (num > 19 && num < 100) // 20 to 100
outNum = tens[Math.floor(num / 10)]; // tens
if (num % 10 > 0)
outNum = outNum + " " + ones[num % 10]; // tens + ones
return outNum;
【讨论】:
【参考方案7】:更新
建议的解决方案在某些情况下会显示错误的结果。需要更多测试。
请尝试:
=GOOGLETRANSLATE(BAHTTEXT(7245.01),"th","en")
结果是
七千二百四十五泰铢,一沙当
然后使用正则表达式或文本函数来摆脱/替换这个:
泰铢 → 换成美元或货币 satang → 到美分或其他。这里是翻译的语言代码列表:
https://www.loc.gov/standards/iso639-2/php/code_list.php
【讨论】:
对于124451
,我得到十万、二十、四千四百、五十一。这是错误的。
@Markus von Broady 感谢您的测试,当我对错误有一些一般逻辑时将编辑我的答案,现在我想这是一个错误,因为 BAHTTEXT
目前已记录功能跨度>
以上是关于将 Google 表格中的数字转换为文本/单词的主要内容,如果未能解决你的问题,请参考以下文章