【问题标题】:If any row in range (G11:G25) contains boolean (true) then run function, else msgBox如果范围 (G11:G25) 中的任何行包含布尔值 (true) 则运行函数,否则为 msgBox
【发布时间】:2023-03-12 12:51:01
【问题描述】:

我在工作表“第 2 节”中运行的函数 (clearRowContents) 将清除列表中任何选中项 (col H) 以及复选框本身 (col G) 的内容和验证。然后将对剩余的未选中框和列表项进行排序,以清除由 clearRowContents 函数刚刚创建的任何空白行。此功能经过测试。

但是,如果没有选中任何项目(col G == false)并且按下“清除”按钮,我怎样才能弹出一条消息让用户知道他们必须首先选中旁边的框该项目,然后按按钮从列表中清除其内容?我正在尝试弄清楚如何为 clearItemMessage 函数编写脚本。

此外,出于编写脚本的目的,此工作表将被多次复制,以便为不同的主题创建各种验证菜单...每张工作表将是手册中的不同“章节”,并具有自己独特的下拉菜单集(在主下拉选项卡)。

工作表链接:https://docs.google.com/spreadsheets/d/1ZdlJdhA0ZJOIwLA9dw5-y5v1FyLfRSywjmQ543EwMFQ/edit?usp=sharing

代码:

      function clearItemMessage(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var checkboxRange = ss.getRangeList("$G$11:$G$25").getValues();
  if (checkboxRange == true){
    clearRowContents (col);
  } else (Browser.msgBox("To delete items, select the box next to the items and then press the delete button."));
}


function clearRowContents (col){ // col is the index of the column to check for checkbox being true
  var col = 7; //col G
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); 
  var data = ss.getDataRange().getValues();

//Format font & size
  var sheetFont = ss.getRange("A:Z");
  var boxFont = ss.getRange("$G$11:$G$25");
  var listFont = ss.getRange("$H$11:$H$25");
  
  sheetFont.setFontFamily("Montserrat");
  boxFont.setFontSize(8)
       .setFontColor("#434343")
       .setBackground("#ffffff");
  listFont.setFontSize(12)
       .setFontColor("#434343")
       .setBackground("#ffffff");

  //clear 'true' data validations     
  var deleteRanges = data.reduce(function(ar, e, i) {
    if (e[col - 1] === true) { 
      return ar.concat(["H" + (i + 1), "G" + (i + 1)]);
    }
    return ar;
  }, []);
  if (deleteRanges.length > 0) { 
    ss.getRangeList(deleteRanges).clearContent().clearDataValidations();
  }

  //sort based on checkbox value
  var range = ss.getRange("$G$11:$H$25");
  range.sort({column: 7, ascending: false});
 
}

【问题讨论】:

  • 我必须为我糟糕的英语水平道歉。不幸的是,我无法理解However, if no item is checked (col F == false) and the "clear" button is pressed, how can I have a message pop up letting the user know that they must first check the box next to the item and then press the button to clear its contents from the list?。而且,当我看到您的示例电子表格时,我找不到您所说的 the "clear" button。我为此道歉。我可以问你问题的细节吗?
  • 而且,在你的标题中,你说If any row in range (G11:G25) contains boolean (true) then run function, else msgBox。但在你的问题中,你说if no item is checked (col F == false) and the "clear" button is pressed。当我看到您的示例电子表格时,我在“F”列和清除按钮中找不到值。所以我担心您可能弄错了示例电子表格。
  • 嗨@Tanaike-首先感谢您的回复(我的 clearRowContents 函数的一部分来自您为帮助其他人而编写的代码!)。我有两个功能。如果范围 G11:G25 (Boolean) 中的任何行为真,我想运行 clearRowContents 函数,否则 ('else') 如果范围 G11:G25 (Boolean) 为假或空,那么我想显示一个带有说明的 msgBox。
  • @Tanaike 我还纠正了我原来问题中的一些错误。布尔列(带有复选框)是 Col G (G11:G25),对应的项目列表是 Col H (H11:H25)。
  • “清除”按钮是一个圆圈图像,里面有一个“减号”,旁边是“删除所选项目”。

标签: if-statement google-apps-script boolean range msgbox


【解决方案1】:

你的情况,如何修改clearItemMessage()如下?

修改后的脚本:

function clearItemMessage(){
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var checkboxes = ss.getRange("$G$11:$G$25").getValues();
  if (checkboxes.filter(([g]) => g === true).length > 0){ // or if (checkboxes.some(([g]) => g === true)) {
    clearRowContents();
  } else {
    Browser.msgBox("To delete items, select the box next to the items and then press the delete button.");
  }
}
  • 根据您的问题,我了解您的clearRowContents 作品。所以我提议修改clearItemMessage
  • 在您的clearRowContents 中,使用了var col = 7。所以我觉得function clearRowContents (col){可以修改为function clearRowContents (){

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2016-12-24
    相关资源
    最近更新 更多