【发布时间】:2019-12-19 02:49:29
【问题描述】:
关于我的电子表格:
- 第 1 行最后一列的单元格 B1 充满了复选框。
- 第 5 行到第 16 行的 A 列充满了复选框。
我需要从这个宏中得到什么:
- 我想创建一个 onEdit 函数,当单元格 A5:A16 中的复选框被选中时运行。
- 我希望它首先检查第 1 行中的复选框并找到第一个未选中的复选框。
- 然后我希望它识别该列,以便该行(即刚刚选中的复选框的行)和列(即第 1 行中第一个未选中框的列)中的值。
- 我想取这个单元格中的值,然后从同一列第 17 行的值中减去。
这是我的电子表格示例的链接:https://docs.google.com/spreadsheets/d/1xet-VnYidcVitRx4ffjimXBjGNDQoYl2SbaxfIUJV7A/edit?usp=sharing
到目前为止我的尝试:
function onEdit(e) {
//This IF statement ensures that this onEdit macro only runs when cells A5:A16 is edited
if (
e.source.getSheetName() == "Finances 2020" &&
e.range.getColumn() == 1 &&
5<=e.range.getRow()<=16
) {
//Cells A5:A16 are checkboxes. This section ensures the following script only runs when the checkbox is checked (and not when unchecked).
var checkboxtest = e.range.getValue()
if (checkboxtest == true) {
//THE ABOVE HAS BEEN TESTED AND WORKS SUCCESFULLY.
//I CANNOT GET THE FOLLOWING TO WORK:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var Finances_2020 = spreadsheet.getSheetByName("Finances 2020");
var Billrow = e.range.rowStart
//Deduct the value in the first unchecked column of the edited row, from the value in row 17 of that same column:
var AllFortnightCheckboxes = Finances_2020.getRange(1, 30, 1).getValues(); //Create a variable consisting of all the values in the row 1 checkboxes. This is used to find the first unchecked box.
for(var i=0; i<AllFortnightCheckboxes.length ; i++){ //This For loop will run through all of the row 1 checkboxes.
if (AllFortnightCheckboxes[i] == false){ //This if statement will look for the first unchecked box.
//Copies the value to row 17 of that column:
//*******I have not been able to test the above before writing this bit of script. Once I have identified the column of the first unchecked box in row 1, I should be able to create this section.
//Stop once the first unchecked checkbox has been found:
break;
}
}
}
}
}
;
【问题讨论】:
-
需要样品表链接,您的要求还不够清楚。看起来这可以完全在没有脚本的情况下完成,但我需要一个示例输入输出表。
-
试试这个链接:docs.google.com/spreadsheets/d/… 链接也添加到帖子正文中。
-
正如我所怀疑的,我不知道您在选中此框时想要发生什么,请用一句话改写它。给我一个简单的例子......告诉我要选中和取消选中什么,并准确告诉我你想要发生什么。
-
OP 希望脚本在第 5 行和第 16 行之间的第 1 列上触发以建立工作行,然后 OP 希望在第 1 行中找到第一个未选中的复选框,即工作列。然后取第 17 行和工作列的值,并从中减去工作行和工作列的值。很清楚。
-
@Cooper 只是很混乱,我不明白为什么为此编写脚本是个好主意?如果我继续取消选中并检查它将一遍又一遍地增加价值。这样做的目的是什么?他的意思是让这些复选框充当按钮吗?
标签: google-apps-script google-sheets google-sheets-macros