我正在根据相关列使用 If 语句 - 无法使其工作
I am using an If statement depending on a relative column - cannot get it to work
我正在使用 google 张纸。我在单元格 E1:AD1 中有复选框。选中其中一个复选框后,我希望该列第 1 行中的单元格的背景颜色为绿色。
最终我希望能够对列中的不同行执行相同的操作。请在下面查看我的 onEdit 脚本。
/** @OnlyCurrentDoc */
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells E1:AD1 are edited in the sheet named "Finances 2020"
if (
e.source.getSheetName() == "Finances 2020" &&
e.range.columnStart == 5 &&
e.range.columnEnd == 30 &&
e.range.rowStart >= 1 &&
e.range.rowEnd <= 1
) {
//This if statement checks if the checkbox was checked or unchecked:
var checkboxtest = e.range.getValue()
if (checkboxtest == true) {
//If so, colour the cell in row 1 of that column as green:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var Finances_2020 = spreadsheet.getSheetByName("Finances 2020");
var Fortnightcolumn = e.range.columnStart
Finances_2020.getRange(1, Fortnightcolumn).setBackground('#d9ead3');
}else{
}
}
}
;
- 如果您想对第 1 行中第 5 列和第 30 列之间的任何已编辑单元格应用背景更改 - 正确的陈述是:
if (e.source.getSheetName() == "Finances 2020" && 5<=e.range.getColumn()<=30 &&
e.range.getRow() == 1)
- 此外,请记住只有当 sheet 名称为
Finances 2020
时才进入第一个 if
循环。因此,要设置背景颜色,您不需要单独处理 sheet,而是可以定义 e.range.setBackground('#d9ead3');
.
摘要
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells E1:AD1 are edited in the sheet named "Finances 2020"
if (e.source.getSheetName() == "Finances 2020" &&
5<=e.range.getColumn()<=30 &&
e.range.getRow() == 1) {
//This if statement checks if the checkbox was checked or unchecked:
var checkboxtest = e.range.getValue();
if (checkboxtest == true) {
//If so, colour the cell in row 1 of that column as green:
e.range.setBackground('#d9ead3');
}
}
}
如果您还想将背景更改应用到其他行,例如第 1 行到第 5 行 - 将 if 语句的条件从 ...&& e.range.getRow() == 1
更改为 ...&& 1 <= e.range.getRow() <= 5
我正在使用 google 张纸。我在单元格 E1:AD1 中有复选框。选中其中一个复选框后,我希望该列第 1 行中的单元格的背景颜色为绿色。
最终我希望能够对列中的不同行执行相同的操作。请在下面查看我的 onEdit 脚本。
/** @OnlyCurrentDoc */
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells E1:AD1 are edited in the sheet named "Finances 2020"
if (
e.source.getSheetName() == "Finances 2020" &&
e.range.columnStart == 5 &&
e.range.columnEnd == 30 &&
e.range.rowStart >= 1 &&
e.range.rowEnd <= 1
) {
//This if statement checks if the checkbox was checked or unchecked:
var checkboxtest = e.range.getValue()
if (checkboxtest == true) {
//If so, colour the cell in row 1 of that column as green:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var Finances_2020 = spreadsheet.getSheetByName("Finances 2020");
var Fortnightcolumn = e.range.columnStart
Finances_2020.getRange(1, Fortnightcolumn).setBackground('#d9ead3');
}else{
}
}
}
;
- 如果您想对第 1 行中第 5 列和第 30 列之间的任何已编辑单元格应用背景更改 - 正确的陈述是:
if (e.source.getSheetName() == "Finances 2020" && 5<=e.range.getColumn()<=30 &&
e.range.getRow() == 1)
- 此外,请记住只有当 sheet 名称为
Finances 2020
时才进入第一个if
循环。因此,要设置背景颜色,您不需要单独处理 sheet,而是可以定义e.range.setBackground('#d9ead3');
.
摘要
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells E1:AD1 are edited in the sheet named "Finances 2020"
if (e.source.getSheetName() == "Finances 2020" &&
5<=e.range.getColumn()<=30 &&
e.range.getRow() == 1) {
//This if statement checks if the checkbox was checked or unchecked:
var checkboxtest = e.range.getValue();
if (checkboxtest == true) {
//If so, colour the cell in row 1 of that column as green:
e.range.setBackground('#d9ead3');
}
}
}
如果您还想将背景更改应用到其他行,例如第 1 行到第 5 行 - 将 if 语句的条件从 ...&& e.range.getRow() == 1
更改为 ...&& 1 <= e.range.getRow() <= 5