我正在尝试使用Google表为餐厅制作视觉rota。我需要计算一排的背景颜色,以便在当天工作的半小时时间内获得数字。
这是Google表的链接:
https://docs.google.com/spreadsheets/d/19iedgzypi3nvt55-oayvpo__pbv0sturq3wcjz1mhck/edit?usp = sharing
和我正在使用的脚本:
function countBG(range, colorref) {
var sheet = SpreadsheetApp.getActiveSheet();
var color = sheet.getRange(colorref).getBackground();
var range = sheet.getRange(range);
var rangeVal = range.getValues();
var count = 0;
var allColors = range.getBackgrounds();
for (var i = 0; i < allColors.length; i++) {
for (var j = 0; j < allColors[0].length; j++) {
if (allColors[i][j] == color) count += 1;
};
};
return count;
}
我发现该脚本第一次运行时起作用,但是此后会出现错误:
找不到范围(第4行,"代码"(
很高兴能在工作时提供任何帮助,我是新来的Google表格,脚本很可能缺少明显的东西。
谢谢,
db。
如果要以自定义函数运行并将单行作为输入运行,则将其起作用。
您将通过=countBG("D6:AH6")
function countBG(input) {
var sheet = SpreadsheetApp.getActiveSheet();
var colors = sheet.getRange(input).getBackgrounds();
var count = 0;
Logger.log(colors);
for (var i = 0; i < colors.length; i++) {
for (var j = 0; j < colors[0].length; j++) {
if (colors[i][j] != "#ffffff") count += 1;
};
};
Logger.log(count);
return count;
}
在此处的表中的示例AI
出于某种原因 SpreadsheetApp.getActiveSheet().getRange(...)
仅给我一个值矩阵,而不是实际范围。
我的解决方案(请注意,此处的范围是String
(:
function countColoredCellsInRange(countRange, expectedBackground) {
var range = SpreadsheetApp.getActiveSpreadsheet().getRange(countRange);
var backgrounds = range.getBackgrounds();
var coloredCellsAmount = 0;
for (var i = 0; i < backgrounds.length; ++i) {
for (var j = 0; j < backgrounds[i].length; ++j) {
var currentBackground = backgrounds[i][j];
if (currentBackground == expectedBackground) {
++coloredCellsAmount;
}
}
}
return coloredCellsAmount;
};
示例用法:
=countColoredCellsInRange("Sheet!A2:A" ; "#00ff00")
此外,这是工作中的示例(计算"进度图"表中的百分比(。
这是我为类似的东西组合在一起的功能。而不是硬编码的值,或者需要事先知道颜色;第一个输入是您要从中获取颜色的单元格。
/**
* Counts the number of cells with the given color within the range.
*
* @param {"B32"} target_color_cell Cell reference to extract the color to count.
* @param {"A1:F22"} range_count_color Range to cycle through and count the color.
* @returns integer The count of the number of cells within range_count_color
* which match the color of target_color_cell.
* @customfunction
*/
function count_cells_by_color( target_color_cell, range_count_color ) {
if (typeof target_color_cell !== "string")
throw new Error( "target_color_cell reference must be enclosed in quotes." );
if (typeof range_count_color !== "string")
throw new Error( "range_count_color reference must be enclosed in quotes." );
var count=0;
var target_color = SpreadsheetApp.getActiveSheet().getRange( target_color_cell ).getBackground();
var range_colors = SpreadsheetApp.getActiveSheet().getRange( range_count_color ).getBackgrounds();
var t=""
for(var row=0; row<range_colors.length;row++){
for(var column=0;column<range_colors[row].length;column++){
let cell_color = range_colors[row][column];
if(cell_color == target_color){
count = count+1;
}
}
}
return count;
}
如何使用此方法:
创建或打开Google Sheet。
页面顶部导航至扩展名 - &gt;应用脚本
从上面的块中删除代码中的样本并粘贴在代码中。
单击"保存"按钮。
在您现在想进行此计算的电子表格的任何单元格中,输入" ="按钮,然后指定函数名称&quort'count_cells_by_color&quot。例如,以下将从单元格A1中获取背景颜色,并计算出在单元格A1至g15中出现的次数。
。= count_cells_by_color(;