本帖最后由 amtath 于 2025-6-26 22:47 编辑
效果:
步骤一:让AI写JS函数
步骤二:定义数值单元格名称myval,文本单元格名称myvaltext
步骤三:添加按钮命令
- //从myval单元格获取数值
- var myval=Forguncy.Page.getCell("myval").getValue();
- //调用下方numberToWords函数,进行翻译
- var myvalstr=numberToWords(myval);
- //设置单元格命令,将翻译好的结果赋值给myvaltext单元格
- Forguncy.Page.getCell("myvaltext").setValue(myvalstr);
- function numberToWords(num) {
- if (num === 0) return 'zero';
-
- const units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
- const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'];
- const tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'];
- const scales = ['', 'thousand', 'million', 'billion', 'trillion'];
-
- let words = [];
- let scaleIndex = 0;
-
- while (num > 0) {
- let chunk = num % 1000;
- num = Math.floor(num / 1000);
-
- if (chunk !== 0) {
- let chunkWords = [];
-
- // 处理百位
- if (chunk >= 100) {
- chunkWords.push(units[Math.floor(chunk / 100)] + ' hundred');
- chunk %= 100;
- }
-
- // 处理十位和个位
- if (chunk >= 20) {
- chunkWords.push(tens[Math.floor(chunk / 10)]);
- chunk %= 10;
- if (chunk > 0) {
- chunkWords.push(units[chunk]);
- }
- } else if (chunk >= 10) {
- chunkWords.push(teens[chunk - 10]);
- } else if (chunk > 0) {
- chunkWords.push(units[chunk]);
- }
-
- if (scaleIndex > 0) {
- chunkWords.push(scales[scaleIndex]);
- }
-
- words.unshift(chunkWords.join(' '));
- }
-
- scaleIndex++;
- }
-
- return words.join(' ');
- }
复制代码
效果:
扩展:
1、页面加载就翻译,将命令放到页面加载命令中执行
2、多金额,多币种
- var myval=Forguncy.Page.getCell("myval").getValue();
- var myval2=Forguncy.Page.getCell("myval2").getValue();
- var bibie=Forguncy.Page.getCell("bibie").getValue();
- var bibie2=Forguncy.Page.getCell("bibie2").getValue();
- var myvalstr=currencyToWords(bibie,myval,bibie2,myval2);
- Forguncy.Page.getCell("myvaltext").setValue(myvalstr);
复制代码
- function currencyToWords(currency1, amount1, currency2, amount2) {
- function numberToWords(num) {
- if (num === 0) return 'zero';
-
- const units = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];
- const teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen',
- 'seventeen', 'eighteen', 'nineteen'];
- const tens = ['', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy',
- 'eighty', 'ninety'];
- const scales = ['', 'thousand', 'million', 'billion', 'trillion'];
-
- let words = [];
- let scaleIndex = 0;
-
- while (num > 0) {
- let chunk = num % 1000;
- num = Math.floor(num / 1000);
-
- if (chunk !== 0) {
- let chunkWords = [];
-
- if (chunk >= 100) {
- chunkWords.push(units[Math.floor(chunk / 100)] + ' hundred');
- chunk %= 100;
- }
-
- if (chunk >= 20) {
- chunkWords.push(tens[Math.floor(chunk / 10)]);
- chunk %= 10;
- if (chunk > 0) {
- chunkWords.push(units[chunk]);
- }
- } else if (chunk >= 10) {
- chunkWords.push(teens[chunk - 10]);
- } else if (chunk > 0) {
- chunkWords.push(units[chunk]);
- }
-
- if (scaleIndex > 0) {
- chunkWords.push(scales[scaleIndex]);
- }
-
- words.unshift(chunkWords.join(' '));
- }
-
- scaleIndex++;
- }
-
- return words.join(' ');
- }
- const amount1Words = numberToWords(Math.floor(amount1));
- const amount2Words = numberToWords(Math.floor(amount2));
-
- return `${amount1Words} ${currency1} and ${amount2Words} ${currency2}`;
- }
复制代码
|