7

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?

j0k
  • 22,600
  • 28
  • 79
  • 90
HelpyHelperton
  • 1,715
  • 3
  • 14
  • 10

3 Answers3

7

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;
}
Community
  • 1
  • 1
Rucent88
  • 810
  • 1
  • 6
  • 12
0

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:

function ifBold() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Bold');
  var cells = sheet.getRange('A2:A');
  for (var i=1; i <= cells.getNumRows(); i++) {
    var isBold = false;
    if(cells.getCell(i, 1).getFontWeight() == 'bold')
      isBold = true;
    sheet.getRange(i+1, 2).setValue(isBold);
  }
}

See example:

https://docs.google.com/spreadsheet/ccc?key=0AmR0r1Y14zgydG03QS1Dd0dLRDA3SWgtLXp6TzV3d3c#gid=3

opowell
  • 568
  • 4
  • 20
-1

This is great but it is not possible to drag down the function to different cells as the cell address is in "" annotations.

function ifBold() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Bold');
  var cells = sheet.getRange('A2:A');
  for (var i=1; i <= cells.getNumRows(); i++) {
    var isBold = false;
    if(cells.getCell(i, 1).getFontWeight() == 'bold')
      isBold = true;
    sheet.getRange(i+1, 2).setValue(isBold);
  }
}

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

Volkan
  • 1