我需要测试给定的电子表格单元格是否为粗体。这是到目前为止我的脚本代码。
function ifBold(cell, val1, val2) {
if(cell.getFontWeight() == 'bold')
return val1;
return val2;
}
它出错了,告诉我“cell”没有函数 getFontWeight()。我怎样才能做到这一点?
最佳答案
您必须将单元格作为字符串传递。 ifBold("A1", 1, 0)
function ifBold(a1Notation, val1, val2) {
var cell = SpreadsheetApp.getActiveSpreadsheet().getRange(a1Notation);
if(cell.getFontWeight() == 'bold')
return val1;
return val2;
}
如果您传入一个单元格或范围而不将其作为字符串发送,那么它将分别作为字符串或数组接收。并且它无法作为 Range 对象进行处理。有关这方面的更多信息,请参阅以下链接:
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?
编辑:
要以动态方式编写函数,需要使用内置函数 ROW 和 COLUMN。
=ifBold(行(A1), 列(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-apps-script - Google 电子表格脚本 - 如何测试单元格是否为粗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15811805/