我正在尝试在 Google 电子表格中学习脚本,并且我已经得到了一些简单的脚本来工作,但是这个真的很痛苦。
我想制作一个使用 onEdit() 函数来更新特定单元格的脚本,以显示电子表格中所有粗体值的总和。
外汇:
1 2 3
4
那么该单元格的值为 (3+4) 7。
希望这是有道理的!
我正在尝试在 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;
}
这些答案很棒,但对我来说,当通过 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;
}
仅在我的情况下进行了测试,尽管...
我想为汤姆的出色回答添加一点点。它当前返回一个字符串。要返回一个数字,请更改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;
}
感谢这段代码,但不幸的是,它有多个值,它返回一个字符串。我在底部的 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;
}