语法
参数
说明
arg
(必须) - 需要转化为单词的数字
您可以像上面的示例一样直接在单元格内输入SPELLNUMS公式,或者您可以使用setFormula方法来应用该公式。
使用说明
SPELLNUMS公式非常易于使用。它需要一个数字类型的参数,代表您希望转换为文本的值。
请注意,如果您的数字有小数部分,它将被四舍五入到最接近的整数。
优点
将数字更改为文本可能有多种原因。例如: 在发票总额中或在电子表格中包含文本段落时(数字可能会破坏可读性,因此出于书写目的,最好将数字转为单词)。
此功能使您可以将给定的数字转换为单词,而不必创建复杂的自定义函数和方法。
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), {
sheetCount: 2
});
initSpread(spread);
};
function initSpread(spread) {
spread.suspendPaint();
var sheet = spread.getActiveSheet();
//custom function - should be a genuine one
function NumbersToWords() {
this.name = "SPELLNUMS";
this.maxArgs = 1;
this.minArgs = 1;
}
NumbersToWords.prototype = new GC.Spread.CalcEngine.Functions.Function();
NumbersToWords.prototype.evaluate = function (arg) {
if (arguments.length === 1 && !isNaN(parseInt(arg))) {
var string = Math.floor(arg).toString(), units, tens, scales, start, end, chunks, chunksLen, chunk, ints, i, word, words, and = 'and';
/* Remove spaces and commas */
string = string.replace(/[, ]/g,"");
if( parseInt( string ) === 0 ) {
return 'zero';
}
units = [ '', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' ];
tens = [ '', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ];
scales = [ '', 'thousand', 'million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quatttuor-decillion', 'quindecillion', 'sexdecillion', 'septen-decillion', 'octodecillion', 'novemdecillion', 'vigintillion', 'centillion' ];
start = string.length;
chunks = [];
while( start > 0 ) {
end = start;
chunks.push( string.slice( ( start = Math.max( 0, start - 3 ) ), end ) );
}
chunksLen = chunks.length;
if( chunksLen > scales.length ) {
return '';
}
words = [];
for( i = 0; i < chunksLen; i++ ) {
chunk = parseInt( chunks[i] );
if( chunk ) {
/* Split chunk into array of individual integers */
ints = chunks[i].split( '' ).reverse().map( parseFloat );
/* If tens integer is 1, i.e. 10, then add 10 to units integer */
if( ints[1] === 1 ) {
ints[0] += 10;
}
/* Add scale word if chunk is not zero and array item exists */
if( ( word = scales[i] ) ) {
words.push( word );
}
/* Add unit word if array item exists */
if( ( word = units[ ints[0] ] ) ) {
words.push( word );
}
/* Add tens word if array item exists */
if( ( word = tens[ ints[1] ] ) ) {
words.push( word );
}
/* Add 'and' string after units or tens integer if: */
if( ints[0] || ints[1] ) {
/* Chunk has a hundreds integer or chunk is the first of multiple chunks */
if( ints[2] || ! i && chunksLen ) {
if(ints.length > 2){
words.push( and );
}
}
}
/* Add hundreds word if array item exists */
if( ( word = units[ ints[2] ] ) ) {
words.push( word + ' hundred' );
}
}
}
return words.reverse().join( ' ' );
}
return "#VALUE!";
};
var cusFunction = new NumbersToWords();
sheet.addCustomFunction(cusFunction);
sheet.setColumnWidth(0, 120);
sheet.setColumnWidth(1, 120);
sheet.setValue(0,0, 'Number');
sheet.setValue(0,1, 'Formula');
sheet.setValue(0,2, 'Result');
sheet.setValue(1,0, '23679');
sheet.setValue(1,1, '=SPELLNUMS(A2)');
sheet.setFormula(1,2, 'SPELLNUMS(A2)');
sheet.setValue(2,0, '34');
sheet.setValue(2,1, '=SPELLNUMS(A3)');
sheet.setFormula(2,2, 'SPELLNUMS(A3)');
sheet.setValue(3,0, '4567893400');
sheet.setValue(3,1, '=SPELLNUMS(A4)');
sheet.setFormula(3,2, 'SPELLNUMS(A4)');
spread.resumePaint();
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="zh-cn" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js"
type="text/javascript"></script>
<script
src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script>
<script src="app.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"></div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: 100%;
height: 100%;
overflow: hidden;
float: left;
}
.options-container {
float: right;
width: 280px;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
overflow: auto;
}
.option-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}