Google Spreadsheet Script - 如何测试Cell是否为粗体 [英] Google Spreadsheet Script - How to test if Cell is Bold

查看:198
本文介绍了Google Spreadsheet Script - 如何测试Cell是否为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试给定的电子表格单元格是否使用粗体。这是我的脚本代码。

 函数ifBold(cell,val1,val2){
if(cell。 getFontWeight()=='bold')
返回val1;
返回val2;
}

它的错误告诉我'cell'没有函数getFontWeight )。我怎样才能做到这一点?

解决方案

您必须将单元格作为字符串传递。 ifBold(A1,1,0)

 函数ifBold(a1Notation,val1,val2){
var cell = SpreadsheetApp.getActiveSpreadsheet()。getRange(a1Notation);

if(cell.getFontWeight()=='bold')
return val1;
返回val2;



$ b $ p
$ b

如果你传入一个单元格或范围而不将它作为字符串发送,那么它将分别作为字符串或数组接收。它将无法作为Range对象进行处理。有关详情,请参阅以下链接:

http://code.google.com/p/google-apps-script-issues/issues/detail?id=354

我该如何从电子表格传递单元格参数作为范围的custum函数?



编辑:

要以动态方式编写函数,它需要使用内建函数ROW和COLUMN。

= ifBold(ROW(A1),COLUMN(A1),1,0)

 函数ifBold(row,column,val1,val2){
var range = SpreadsheetApp.getActiveSpreadsheet()。getDataRange()。getCell (行列);

if(range.getFontWeight()=='bold')
return val1;
返回val2;
}


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?

解决方案

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

这篇关于Google Spreadsheet Script - 如何测试Cell是否为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆