Sum if Bold with criteria in google sheet - google-sheets

I have used google script below
function sumWhereIsFontWeight(rangeSpecification,) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange(rangeSpecification);
var x = 0;
for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {
var cell = range.getCell(i, j);
if(cell.getFontWeight() == 'bold')
x += parseFloat(cell.getValue());
}
}
return x;
}
Then used formula =SUMIF(A:A,"A"&sumWhereIsFontWeight(B1:B6),B1:B6)
But cannot get the desired result "10" of sum only bold numbers with criteria as "A". Check the image attached

Usage
=ArrayFormula(SUM(IF(isbold("A1:B10"),A1:B10,0)))
The function
function isBold(rangeSpecification) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var range = sheet.getRange(rangeSpecification);
var result = [];
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var row = [];
for (var i = 1; i <= numRows; i++) {
row = [];
for (var j = 1; j <= numCols; j++) {
var bold = (range.getCell(i,j).getFontWeight() === 'bold');
row.push(bold);
}
result.push(row);
}
return result;
}
Caution
A custom function won't update automatically when you add new bold cells. You need to re-enter the formula.

Related

Summing Values of Cells of Certain Font Color in Google Sheets

I have a table with cells that contain numbers styled with fonts of different colors. I would like to sum all cell values of a certain color and output that sum into a separate cell. How can I do that?
Try (B1 contains a checkbox)
=sumColor(A:A,B1)
put the formula in a range with the same color as the data to be added
add a check box to allow you updating the value when colors will be changed
the formula can be located in another sheet
with this custom function
function sumColor(range) {
var r = SpreadsheetApp.getActiveRange();
var color = r.getFontColors();
var total = 0;
var addresses = r.getFormula().match(/(?<=\().*(?=\))/g)[0].split(/[;|,]/)
for (var i = 0; i < addresses.length - 1 ; i++) {
try {
var sh = SpreadsheetApp.getSheetByName(addresses[i].split('!')[0].replace("'", ""));
var address = addresses[i].split('!')[1].trim();
}
catch (e) {
var sh = SpreadsheetApp.getActiveSheet();
var address = addresses[i].trim()
}
var colors = sh.getRange(address).getFontColors();
var values = sh.getRange(address).getValues();
for (var j = 0; j < colors.length; j++)
for (var k = 0; k < colors[i].length; k++)
if (colors[j][k] == color)
if ((typeof values[j][k]) == 'number')
total += values[j][k];
}
return total;
};

Format cell color based on the cell text content

I want to accomplish something like this:
I have a sort of "Relational" Spreadsheet, and I want rows to be colored according.
I manually choosing a unique color for each Category on the "Categories" Sheet or generating a unique color based on the string content, either would work for my use case.
Not the best solution but it works
function onEdit(e) {
if(e){
var ss = e.source.getActiveSheet();
var range = e.source.getActiveRange();
var r1 = range.getRow();
var c1 = range.getColumn();
var rowsCount = range.getNumRows();
for(var i=0; i<rowsCount; i++){
var row = ss.getRange(r1+i,1,1,ss.getMaxColumns());
updateRow(row, ss);
}
}
}
function updateRow(row, ss){
if (ss.getName() == "Entries") { // This is the sheet name
var cell = row.getCell(1,1);
var firstCellValue = cell.getValue();
if(firstCellValue){
cell.setBackgroundColor(stringToColor(firstCellValue));
}
else{
cell.setBackgroundColor(null);
}
}
}
function stringToColor(str) {
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
var colour = '#';
for (var i = 0; i < 3; i++) {
var value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
Based on this answer
in conditional formatting select the range you want to apply colors, select color, choose Text is exactly and set value:

Google Spreadsheet ||Typeerror : cannot read property '0'

I have a spreadsheet for project data with time-sheet for each month logged against each project ID
I want to iterate through each sheet and if there is matching project ID , I want to sum up the number of hours logged for each project.
I have written the following code but keep getting the
TypeError: Cannot read property "0" from undefined. (line 31).
This is my sheet : https://goo.gl/rrsSxI
And this is my Code.
function TotalHours(TaskID) {
var a = SpreadsheetApp.getActiveSpreadsheet().getSheets().length;
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sum = 0;
// var fcol = 0;
for (var i = 1; i <= a; ++i) {
// var sheetname = sheets[i].getName();
//var cell = sheets[i].getActiveCell();
//Set active cell to A1 on each sheet to start looking from there
SpreadsheetApp.setActiveSheet(sheets[i])
//var sheet = sh.getActiveSheet();
var range = sheets[i].getRange("A1");
//* sheets[i].setActiveRange(range);
var data = sheets[i].getDataRange().getValues();
for (var row = 2; row <= data.length; ++row) {
if (data[row][0] == TaskID) {
for (var col = 2; col <= 31; ++col) {
sum += sheets[i].getRange(row, col).getValue();
}
}
}
}
return sum;
}
Can someone help me with what I am doing wrong.
I assume you want to exclude the sheet where the formula is going to be used ("Tracker" ?
See if this works ?
function TotalHours(TaskID) {
var sum = 0,
s = SpreadsheetApp.getActive(),
active = s.getActiveSheet().getName(),
sheets = s.getSheets();
for (var i = 0, slen = sheets.length; i < slen; i++) {
if(sheets[i].getName() != active) {
var sheetVal = sheets[i].getDataRange()
.getValues();
for (var j = 0, vlen = sheetVal.length; j < vlen; j++) {
if (sheetVal[j][0] == TaskID) {
for (var k = 2, rlen = sheetVal[j].length; k < rlen; k++) {
var c = sheetVal[j][k]
sum += c && !isNaN(parseFloat(c)) && isFinite(c)? c : 0; //check if cell holds a number
}
}
}
}
}
return sum;
}

How to count cell values based on the background color in Spreadsheet

I am trying to count the values in a cell based on the background color. Someone I got some help while online search I am not getting the complete solution. Does anyone help me out with this prob. I have sample code script which I got from the online search.
Also the script which am pasting here is grouping the values( For Example if i have a value A in three cells it should return the value as 3 instead it is returning AAA. Can someone help me out with the script to count the values and return it based on the background color
Thanks in Advance,
Here is the script:
function sumBackgroundColors(rangeString, color) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var sumRange = s.getRange(rangeString);
var sum = 0;
var sumRangeBackground = sumRange.getBackgroundColors();
var sumRangeValues = sumRange.getValues();
for(var row = 0; row < sumRangeBackground.length; row++ ) {
for(var col = 0; col < sumRangeBackground[0].length; col++ ) {
if( sumRangeValues[row][col]=="LG M"&& sumRangeBackground[row][col] == color ) {
sum = sum + parseFloat(sumRangeValues[row][col]);
}
}
}
return sum;
}
I recommend you to not use a custom function to do this because of its caching feature (explanation here).
function countBackgroundColors() {
var rangeString = "A1:A100";
var color = "green";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var range = s.getRange(rangeString);
var count = 0;
var rangeColors = range.getBackgroundColors();
for( var i in rangeColors )
for( var j in rangeColors[i] )
if( rangeColors[i][j] == color )
++count;
return count;
}

How to Count and return the Values

I am facing a problem in the following script. I am not much into scripting at all and this is not my script also but here am getting the result which is grouping the values( For Example if i have a value A in three cells it should return the value as 3 instead it is returning AAA. Can someone help me out to count the values and return it
Thanks in Advance,
Here is the script :
function sumBackgroundColors(rangeString, color) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var sumRange = s.getRange(rangeString);
//var sum = 0;
var openCount = 0;
var sumRangeBackground = sumRange.getBackgroundColors();
var sumRangeValues = sumRange.getValues();
for(var row = 0; row < sumRangeBackground.length; row++ ) {
for(var col = 0; col < sumRangeBackground[0].length; col++ ) {
if( sumRangeValues[row][col]=="LG M"&& sumRangeBackground[row][col] == color ) {
openCount = openCount + sumRangeValues[row][col];
//if(sumRangeBackground[row][col] == color && sumRangeValues[row][col] == 1 ) {
// sum = sum + parseFloat(sumRangeValues[row][col]);
}
}
}
return openCount;
//return sum;
}
Here is a function which will take the value to be searched as argument. Rest of the things have been explained in comment lines.
function searchCount(value){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get the whole data from activesheet in a 2D array
var data = sheet.getDataRange().getValues();
//Initialize counter
var count = 0;
//Iterate through the array
for(var i in data){
for(var j in data[i]){
// if a match found, increament the counter
if(value.toString == data[i][j]){
count++;
}
}
}
// return the count value
return count;
}
your problem could be due to openCount = openCount + sumRangeValues[row][col];
According to your example sumRangeValues[row][col] isn't an int. int + not an int = ??? if you want to keep a count of things you probably want openCount++ to replace that line instead, which is just a shortcut to openCount = openCount + 1;
Even if sumRangeValues[row][col] was an int, that line still wouldn't be what you're looking for. If you're searching for all the 3s in your spreadsheet your code would find your first 3, and then that line would execute 0 = 0 + 3 congrats, you just found 3 threes. You would continue to add three every time you found a three.
Waqar's code is essentially your code (amazingly simplified, but it iterates the same way) except he doesn't check color and he uses ++ instead of that line.

Resources