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 中放置字母而不是数字的项目总成本的总和
解决方案
可能有多种方法可以计算您想要的值。将此视为一种方法。
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);
}
推荐阅读
- c# - 用户删除断言
- r - 将单元格与R中同一列中每个第n个单元格的总和相除
- html - 如何像在 Microsoft Word 中一样以模式制作表格的边框颜色
- sql - 按顺序优化删除查询
- r - Ggplot2 固定色标的限制
- javascript - 如何为 React js 的 jsPdf 中的 autoTable 指定坐标或提供边距?
- flutter - Flutter 中的文本选择功能错误
- firebase - 无法访问用于列表视图的 Firestore 数据库文档,并且异步 querySnapshot 也存在错误
- sql - MS SQL Server:选择月薪最高的员工详细信息
- python - 如何通过将第一行标签视为 x 轴来绘制折线图(在我的情况下 01-02-2018 - 30-03-2018 应该作为 x 轴标签)?