Google Spreadsheet Script - How to test if Cell is Bold

♀尐吖头ヾ 提交于 2019-12-10 14:55:52

问题


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?


回答1:


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;
}



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/15811805/google-spreadsheet-script-how-to-test-if-cell-is-bold

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!