使用自定义功能(脚本)设置单元格的背景色 [英] Setting the cell's background color with a custom function (script)
问题描述
我想根据另一个单元格中存储的颜色代码为单元格设置背景色.
I want to set a background color for a cell based on a color code stored in another cell.
我编写了以下Google脚本来实现此目的:
I wrote a following google script to implement this:
function colorCode2Background(code) {
if (typeof code != 'string') {
return null;
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Returns the active cell
var cell = sheet.getActiveCell();
return cell.setBackground(code);
}
问题是,当我将单元格的公式设置为 = colorCode2background(%CELL_NUMBER%)
时,单元格的值是颜色代码(即#ff00ff
),我得到了一个错误:虽然您已授予脚本所有权限,但是您没有权限调用setBackground函数(第12行)".
The problem is that when I set the cell's formula to =colorCode2background(%CELL_NUMBER%)
where cell's value is a color code (i.e. #ff00ff
), I get an error: "You have no permission to call function setBackground (line 12)", although I granted all the permissions to my script.
推荐答案
每个自定义函数都必须返回一个要显示的值.在您的函数中情况并非如此.
Every custom function must return a value to display. That is not the case in your function.
这篇关于使用自定义功能(脚本)设置单元格的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!