【问题标题】:I have an onEdit function that I need to search for an unchecked checkbox from a row of checkboxes [Google Sheets]我有一个 onEdit 功能,我需要从一排复选框中搜索未选中的复选框 [Google Sheets]
【发布时间】: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


【解决方案1】:
function onEdit(e) {
  //Logger.log(JSON.stringify(e));
  //e.source.toast('Flag1');
  var sh=e.range.getSheet();
  if(sh.getName()=="Finances 2020" && e.range.columnStart==1 && e.range.rowStart>4 && e.range.rowStart<17 && e.value=="TRUE") {
    //e.source.toast('flag2');
    var rA=sh.getRange(1,3,1,sh.getLastColumn()-2).getValues()[0];//flattened
    //Logger.log(rA);
    for(var i=0;i<rA.length;i++) {
      //e.source.toast('flag3');
      if(rA[i]==false) {
        //e.source.toast('flag4');
        var col=3+i;
        sh.getRange(17,col).setValue(sh.getRange(17,col).getValue()-sh.getRange(e.range.rowStart,col).getValue());
        break;//stop after first one
      }
    }
  }
}

【讨论】:

  • 好的,我点击复选框应该发生什么?
  • 刚刚尝试过,仍然没有为我做任何事情,但未注释时吐司可以工作。我真的不明白这些复选框应该如何工作。
  • 我做了一些改变。确保您使用的是最后一个。
猜你喜欢
  • 2012-05-31
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 2015-08-25
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多