我正在尝试学习 Google 电子表格中的脚本编写,并且已经得到了一些简单的脚本可以工作,但这一个确实很痛苦。
我想制作一个使用 onEdit() 函数更新特定单元格的脚本,以显示电子表格中所有粗体值的总和。
Fx:
1
2
3
4
那么该单元格的值为 (3+4) 7。
希望这是有道理的!
虽然有点晚了,但值得回答,而且我一直在研究类似的问题。
使用的公式是:
=sumIfBold(A1:B4,COLUMN(A1), ROW(A1))
脚本是:
/**
* Sums cell values in a range if they are bold. The use of startcol and startrow
* is to enable the formula to be copied / dragged relatively in the spreadsheet.
*
* @param {Array.Array} range Values of the desired range
* @param {int} startcol The column of the range
* @param {int} startrow The first row of the range
*
* @return {int} Sum of all cell values matching the condition
*/
function sumIfBold(range, startcol, startrow){
// convert from int to ALPHANUMERIC
// - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
var start_col_id = String.fromCharCode(64 + startcol);
var end_col_id = String.fromCharCode(64 + startcol + range[0].length -1);
var endrow = startrow + range.length - 1
// build the range string, then get the font weights
var range_string = start_col_id + startrow + ":" + end_col_id + endrow
var ss = SpreadsheetApp.getActiveSpreadsheet();
var getWeights = ss.getRange(range_string).getFontWeights();
var x = 0;
var value;
for(var i = 0; i < range.length; i++) {
for(var j = 0; j < range[0].length; j++) {
if(getWeights[i][j].toString() == "bold") {
value = range[i][j];
if (!isNaN(value)){
x += value;
}
}
}
}
return x;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)