我正在尝试在Google电子表格中学习脚本,我已经有了一些简单的脚本可以使用,但是这个脚本真的很痛苦。
我想制作一个使用onEdit()函数更新特定单元格的脚本,以显示电子表格中所有粗体值的总和。
Fx的:
1 2 3
4
然后单元格的值为(3 + 4)7。
希望它有意义!
答案 0 :(得分:6)
这有点晚了,但值得回答,我一直在研究类似的问题。
使用的公式是:
=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;
}
答案 1 :(得分:0)
我想在汤姆的出色回答中加一点点。当前返回一个字符串。要返回数字,请将return x;
更改为return x*1;
此外,对于希望将其转换为斜体而不是粗体的任何人,下面是代码:
/**
* Sums cell values in a range if they are italic. 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 sumIfItalic(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 styles
var range_string = start_col_id + startrow + ":" + end_col_id + endrow
var ss = SpreadsheetApp.getActiveSpreadsheet();
var getStyles = ss.getRange(range_string).getFontStyles();
var x = 0;
var value;
for(var i = 0; i < range.length; i++) {
for(var j = 0; j < range[0].length; j++) {
if(getStyles[i][j].toString() == "italic") {
value = range[i][j];
if (!isNaN(value)){
x += value;
}
}
}
}
return x*1;
}
答案 2 :(得分:0)
这些答案很好,但是对我来说,当通过Z列时(当我们有两个字母时,列号从26到27)会中断。
添加了一个小的更改以解决此问题(在斜体版本上)
/**
* Sums cell values in a range if they are italic. 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
* @customfunction
*/
function sumIfItalic(range, startcol, startrow){
// convert from int to ALPHANUMERIC
// - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
var secondColChar = parseInt(startcol / 27); //26 is the number of col Z, 26 letters, works for only for two char
var start_col_id = ""
var end_col_id = ""
if(secondColChar > 0) {
start_col_id += String.fromCharCode(64 + (secondColChar));
end_col_id += String.fromCharCode(64 + secondColChar + range[0].length -1);
}
start_col_id += String.fromCharCode(64 + (startcol % 27 + (secondColChar * 1)) );
end_col_id += String.fromCharCode(64 + (startcol % 27) + (secondColChar * 1) + range[0].length -1);
var endrow = startrow + range.length - 1
// build the range string, then get the font styles
var range_string = start_col_id + startrow + ":" + end_col_id + endrow
var ss = SpreadsheetApp.getActiveSpreadsheet();
var getStyles = ss.getRange(range_string).getFontStyles();
var x = 0;
var value;
for(var i = 0; i < range.length; i++) {
for(var j = 0; j < range[0].length; j++) {
if(getStyles[i][j].toString() == "italic") {
value = range[i][j];
if (!isNaN(value)){
x += value;
}```
}
}
}
return x*1;
}
仅在我的情况下进行了测试...
答案 3 :(得分:0)
感谢这段代码,但不幸的是,它返回了一个字符串,有多个值。我在底部的 FOR 循环块中做了一个小更新,并删除了一些虚假的反引号。
/**
* Sums cell values in a range if they are italic. 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
* @customfunction
*/
function sumIf_Italic(range, startcol, startrow){
// convert from int to ALPHANUMERIC
// - thanks to Daniel at http://stackoverflow.com/a/3145054/2828136
var secondColChar = parseInt(startcol / 27); //26 is the number of col Z, 26 letters, works for only for two char
var start_col_id = ""
var end_col_id = ""
if(secondColChar > 0) {
start_col_id += String.fromCharCode(64 + (secondColChar));
end_col_id += String.fromCharCode(64 + secondColChar + range[0].length -1);
}
start_col_id += String.fromCharCode(64 + (startcol % 27 + (secondColChar * 1)) );
end_col_id += String.fromCharCode(64 + (startcol % 27) + (secondColChar * 1) + range[0].length -1);
var endrow = startrow + range.length - 1
// build the range string, then get the font styles
var range_string = start_col_id + startrow + ":" + end_col_id + endrow
var ss = SpreadsheetApp.getActiveSpreadsheet();
var getStyles = ss.getRange(range_string).getFontStyles();
var x = 0;
var value;
for(var i = 0; i < range.length; i++) {
for(var j = 0; j < range[0].length; j++) {
if(getStyles[i][j].toString() == "italic") {
value = range[i][j];
if (!isNaN(value)){
x += value*1;
}
}
}
}
return x*1;
}