I need to test if a given spreadsheet cell is in bold font or not. This is my script code so far.
function ifBold(cell, val1, val2) {
if(cell.getFontWeight() == 'bold')
return val1;
return val2;
}
It errors, telling me that 'cell' does not have the function getFontWeight(). How can I make this work?
Open the script project containing your add-on in the script editor. Click Run > Test as add-on. Under Execute Saved Test, find the test to execute and select it. Click Test.
After we key in any information within a cell, Google Sheets recognizes the type of content going into the cell, and it automatically adjusts the data format accordingly. Just to quickly check, we navigate to the Format > Number option through the menu, and we see that the format is set to 'Automatic' by default.
ISBLANK returns TRUE if value is empty or a reference to an empty cell, and FALSE if it contains data or a reference to data.
One of the best ways to search your data to see if a cell contains a certain value is to use the REGEXMATCH function. This function will search a cell and return TRUE if a piece of text matches your regular expression, or FALSE if it does not.
You have to pass the cell as a string. ifBold("A1", 1, 0)
function ifBold(a1Notation, val1, val2) {
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange(a1Notation);
if(cell.getFontWeight() == 'bold')
return val1;
return val2;
}
If you pass in a cell or range without sending it as a string, then it will be received as a string or array, respectively. And it won't be able to be processed as a Range Object. For more information on this, see these links:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=354
How do I pass a cell argument from a spreadsheet to a custum function as a range?
EDIT:
To write the function in a way that is Dynamic, it requires use of the builtin functions ROW and COLUMN.
=ifBold(ROW(A1), COLUMN(A1), 1, 0)
function ifBold(row, column, val1, val2) {
var range = SpreadsheetApp.getActiveSpreadsheet().getDataRange().getCell(row, column);
if(range.getFontWeight() == 'bold')
return val1;
return val2;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With