英文:
Why I cannot use any methods in Google Apps Script?
问题
当我在电子表格上运行此代码时,出现“TypeError: table_range.setBackgroundObject不是函数”错误。
我显然漏掉了某些东西,但我不知道是什么。任何帮助都将不胜感激!
英文:
I'm creating a function that checks if certain scores are equal to max score. If all of them meet this condition I want the entire range of cells to change it's background color.
function ColorsForTable(selected_scores,max_score,table_range) {
var isMax=true;
for(var i=0;i<selected_scores.length;i++){
if(selected_scores[i][0]!==max_score){
isMax=false;
break;
}
}
if(isMax){
table_range.setBackgroundObject('#D4AF37');
}
}
When I run this code on my spreadsheet I get
TypeError: table_range.setBackgroundObject is not a function
I'm clearly missing something but I don't know what. Any help is highly appreciated!
答案1
得分: 0
根据你的回复,我理解你正在使用名为 ColorsForTable(selected_scores, max_score, table_range)
的自定义函数。在这种情况下,有几个问题。
修改要点:
- 对于
=ColorsForTable(K35:P35,20,K15:P34)
情况,K15:P34
是单元格的值。因此,table_range
不是 Class Range。这就是你当前遇到的TypeError: table_range.setBackgroundObject is not a function
错误的原因。 setBackgroundObject
的参数应该是 SpreadsheetApp.Color 对象。但是你使用的是字符串#D4AF37
。应该使用setBackground(''#D4AF37'')
。- 在当前阶段,不能在自定义函数中使用
setBackgroundObject
和setBackground
。即使将table_range
修改为 Class Range 对象并使用setBackground(''#D4AF37'')
,还会出现类似Exception: You do not have permission to call setBackground
的错误。 - 对于
K35:P35
的值,它是[[1, 2, 3,,,]]
。在这种情况下,selected_scores[i][0]
是[1, 2, 3,,,]
。因此,只比较单元格 "K35"。
我认为这些要点可能是回答“为什么我不能在 Google Apps Script 中使用任何方法”的答案。如果你想使用这个脚本,作为样本修改,你可以尝试在脚本编辑器中运行你的脚本。当然,可以通过自定义菜单、电子表格上的按钮、侧边栏和对话框来执行此脚本。
修改后的脚本:
请将以下脚本复制并粘贴到电子表格的脚本编辑器中,并设置工作表名称并保存脚本。selected_scores, max_score, table_range
的值来自你的回复。要运行此脚本,请在脚本编辑器中运行 sample()
函数。
function ColorsForTable(selected_scores, max_score, table_range) {
var isMax = true;
for (var i = 0; i < selected_scores.length; i++) {
if (selected_scores[i] !== max_score) {
isMax = false;
break;
}
}
if (isMax) {
table_range.setBackground(''#D4AF37'');
}
}
// 请使用脚本编辑器运行此函数。
function sample() {
var sheetName = "Sheet1"; // 请设置你的工作表名称。
var selected_scores = "K35:P35";
var max_score = 20;
var table_range = "K15:P34";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
ColorsForTable(
sheet.getRange(selected_scores).getValues()[0],
max_score,
sheet.getRange(table_range)
);
}
- 在这个修改后的脚本中,当所有 "K35:P35" 的值都等于 20 时,将 "K15:P34" 的背景颜色更改为 "#D4AF37"。
注意:
-
这只是对你当前问题的简单修改脚本。请根据你的实际情况进行修改。
-
作为另一个样本脚本,如果使用侧边栏,可以使用以下样本脚本。在这种情况下,请使用脚本编辑器运行函数
main
。function ColorsForTable(selected_scores, max_score, table_range) { var isMax = true; for (var i = 0; i < selected_scores.length; i++) { if (selected_scores[i] !== max_score) { isMax = false; break; } } if (isMax) { table_range.setBackground(''#D4AF37''); } } function main(e) { if (!e) { var html = `<input id="selected_scores" value="K35:P35" placeholder="Please input selected_scores."> <input type="number" id="max_score" value="20" placeholder="Please input max_score."> <input id="table_range" value="K15:P34" placeholder="Please input table_range."> <input type="button" value="Run script" onclick="main()"><script>function main() { const values = ["selected_scores","max_score", "table_range"].map(e => document.getElementById(e).value); console.log(values) google.script.run.withSuccessHandler(_ => console.log("Done.")).main(values); }</script>`; SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html)); return; } var [selected_scores, max_score, table_range] = e; var sheet = SpreadsheetApp.getActiveSheet(); ColorsForTable( sheet.getRange(selected_scores).getValues()[0], Number(max_score), sheet.getRange(table_range) ); }
- 当运行函数
main
时,会在电子表格中打开一个侧边栏。在此样本中,你的样本值已经设置。单击按钮时,脚本将运行。ColorsForTable
会使用有效值运行。
- 当运行函数
-
例如,如果想在
isMax
为 false 时设置其他颜色,请将ColorsForTable
修改如下。-
从
if (isMax) { table_range.setBackground(''#D4AF37''); }
-
到
table_range.setBackground(isMax ? ''#D4AF37'' : ''#ffffff''); // 请将 ''#ffffff'' 设置为你期望的颜色。
-
参考:
英文:
From your following reply,
> I run it like this: =ColorsForTable(K35:P35 , 20 , K15:P34)
(added spaces for visibility) In K35:P35 there are only numbers and in K15:P34 there are some text variables.
I understood that you are using your function ColorsForTable(selected_scores,max_score,table_range)
as a custom function. In this case, there are several issues.
Modification points:
- In the case of
=ColorsForTable(K35:P35,20,K15:P34)
,K15:P34
is the cell values. By this,table_range
is not Class Range. This is the reason for your current issue ofTypeError: table_range.setBackgroundObject is not a function
. - And, the argument of
setBackgroundObject
is SpreadsheetApp.Color object. But, you use a string#D4AF37
. It is required to besetBackground('#D4AF37')
. - And, in the current stage,
setBackgroundObject
andsetBackground
cannot be used with the custom function. Even whentable_range
is modified to Class Range object andsetBackground('#D4AF37')
is used, another error likeException: You do not have permission to call setBackground
occurs. - And, in the case of the value of
K35:P35
, it is[[1, 2, 3,,,]]
. In this case,selected_scores[i][0]
is[1, 2, 3,,,]
. In this case, only the cell "K35" is compared.
I thought that these points might be an answer to Why I cannot use any methods in Google Apps Script?
. If you want to use this script, as a sample modification, how about running your script with the script editor? Of course, this script can be executed by the custom menu, a button on the Spreadsheet, a sidebar, and a dialog.
Modified script:
Please copy and paste the following script to the script editor of Spreadsheet and set the sheet name and save the script. The values of selected_scores, max_score, table_range
are from your reply. When you use this script, please run the function sample()
with the script editor. By this, the script is run.
function ColorsForTable(selected_scores, max_score, table_range) {
var isMax = true;
for (var i = 0; i < selected_scores.length; i++) {
if (selected_scores[i] !== max_score) {
isMax = false;
break;
}
}
if (isMax) {
table_range.setBackground('#D4AF37');
}
}
// Please run this function with the script editor.
function sample() {
var sheetName = "Sheet1"; // Please set your sheet name.
var selected_scores = "K35:P35";
var max_score = 20;
var table_range = "K15:P34";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
ColorsForTable(
sheet.getRange(selected_scores).getValues()[0],
max_score,
sheet.getRange(table_range)
);
}
- From
If all of them meet this condition I want the entire range of cells to change it's background color.
, in this modified script, when this script is run, when all values of "K35:P35" are the same value with 20, the background color of "K15:P34" is changed to "#D4AF37".
Note:
-
This is a simple modified script for explaining the reason for your current issue. So, please modify this to your actual situation.
-
As another sample script, when a sidebar is used, the sample script is as follows. In this case, please run the function
main
with the script editor.function ColorsForTable(selected_scores, max_score, table_range) { var isMax = true; for (var i = 0; i < selected_scores.length; i++) { if (selected_scores[i] !== max_score) { isMax = false; break; } } if (isMax) { table_range.setBackground('#D4AF37'); } } function main(e) { if (!e) { var html = `<input id="selected_scores" value="K35:P35" placeholder="Please input selected_scores."> <input type="number" id="max_score" value="20" placeholder="Please input max_score."> <input id="table_range" value="K15:P34" placeholder="Please input table_range."> <input type="button" value="Run script" onclick="main()"><script>function main() { const values = ["selected_scores","max_score", "table_range"].map(e => document.getElementById(e).value); console.log(values) google.script.run.withSuccessHandler(_ => console.log("Done.")).main(values); }</script>`; SpreadsheetApp.getUi().showSidebar(HtmlService.createHtmlOutput(html)); return; } var [selected_scores, max_score, table_range] = e; var sheet = SpreadsheetApp.getActiveSheet(); ColorsForTable( sheet.getRange(selected_scores).getValues()[0], Number(max_score), sheet.getRange(table_range) ); }
- When the function
main
is run, a sidebar is opened to the Spreadsheet. In this sample, the values of your sample have already been set. When you click a button, the script is run. And,ColorsForTable
is run with valid values.
- When the function
-
For example, if you want to set other color when
isMax
is false, please modifyColorsForTable
as follows.-
From
if (isMax) { table_range.setBackground('#D4AF37'); }
-
To
table_range.setBackground(isMax ? '#D4AF37' : '#ffffff'); // Please set '#ffffff' to your expected color.
-
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论