gpt4 book ai didi

google-apps-script - 谷歌电子表格 : Sum of all bold cells

转载 作者:行者123 更新时间:2023-12-04 18:33:21 25 4
gpt4 key购买 nike

我正在尝试在 Google 电子表格中学习脚本编写,并且我已经获得了一些简单的脚本,但这个脚本真的很痛苦。

我想制作一个脚本,使用 onEdit() 函数更新特定单元格以显示电子表格中所有粗体值的总和。

外汇:

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;
}

关于google-apps-script - 谷歌电子表格 : Sum of all bold cells,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14248726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com