Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Spreadsheet Script - How to test if Cell is Bold

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?

like image 372
HelpyHelperton Avatar asked Apr 04 '13 12:04

HelpyHelperton


People also ask

How do I test a Google spreadsheet script?

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.

How do you check the cell type in Google Sheets?

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.

How do I check if a cell is empty in Google script?

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.

How do I check if a cell contains a specific text in Google Sheets?

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.


1 Answers

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;
}
like image 188
Rucent88 Avatar answered Oct 17 '22 09:10

Rucent88