如何将数字转换为字母

时间:2019-07-22 22:17:56

标签: google-sheets

我有一个显示项目成本的工作表。我想做的而不是显示数字我想使用下面的BLACKHORSE是B = 1,L = 2,A = 3,C = 4,K = 5,H = 6,7 = O,8 = R,9 = S且E = 0。如何将其放在Google表格中的脚本中,将单元格h9的总费用之和放在字母(而不是数字)中?

1 个答案:

答案 0 :(得分:1)


可能有很多方法来计算所需的值。将此视为一种方法。


function so5715442701() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheetname = "57154427";
  var sheet = ss.getSheetByName(sheetname);

  // code to find the key for a given value in a javascript key:value array
  Object.prototype.getKey = function(value){
    for(var key in this){
    if(this[key] == value){
      return key;
    }
  }
    return null;
  };

 // a key:value array for BLACKHORSE
  var blackhorse = { 
    B : 1,
    L : 2,
    A : 3,
    C : 4,
    K : 5,
    H : 6,
    O : 7,
    R : 8,
    S : 9,
    E : 0
  };

  // get the cost value from a cell    
  var costprice = sheet.getRange("C15").getValue();

  // convert the number to a string
  var cost = costprice.toString(); // convert to string
  //Logger.log("DEBUG: cost = "+cost+", length = "+cost.length);

  // set a variable to accumulate the results
  var costtotal="";

  // loop through the characters in the cost value
  for (var i = 0; i < cost.length; i++) {
    var letter = cost.charAt(i);
    var costkey = blackhorse.getKey(letter);
    var costtotal = costtotal+costkey  
  }

  //Logger.log("DEBUG: cost = "+cost+", Blackhourse cost = "+costtotal); 
  sheet.getRange("D15").setValue(costtotal);

}

CREDIT
 -How can I process each letter of text using Javascript?
 -How to get a key in a JavaScript object by its value?