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?
getFontWeight() is a function of Range, so as long as what you are passing to your function above is a Range object (sheet.getRange(...)), it should work:
https://developers.google.com/apps-script/reference/spreadsheet/range
This is a general script that will check for every element in A whether it is bold or not:
See example:
https://docs.google.com/spreadsheet/ccc?key=0AmR0r1Y14zgydG03QS1Dd0dLRDA3SWgtLXp6TzV3d3c#gid=3
This is great but it is not possible to drag down the function to different cells as the cell address is in "" annotations.
to be able to drag the function from one cell to the next cells you need to use this: if you want to use it for one cell:
=ifBold(CELL("address"; A1)) or =ifBold(CELL("address", A1))
; or , can be different due to your language region
new google sheets: =myFunction(CELL("address",A1)&":"&CELL("address",A2)) or old google sheets: =myFunction(ADDRESS(row(A1),COLUMN(A1),4)&":"&ADDRESS(row(A2),COLUMN(A2),4))
you may need to change , to ; again for this. like:
=myFunction(CELL("address";A1)&":"&CELL("address";A2))
reference: https://webapps.stackexchange.com/questions/10629/how-to-pass-a-range-into-a-custom-function-in-google-spreadsheets
You have to pass the cell as a string. ifBold("A1", 1, 0)
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)