如何根据消息框响应调用函数

问题描述 投票:0回答:1

我一直在开发一个 Apps 脚本,如果申请人考试失败,我可以自动删除回复。

我正在使用这个脚本:

function onEdit1(e) {
  const sh=e.range.getSheet();
  if(sh.getName()=='{I} Apps' && e.range.columnStart==13 && e.value=='TRUE') {
    var resp=SpreadsheetApp.getUi().alert('Did the applicant pass their exam?', SpreadsheetApp.getUi().ButtonSet.YES_NO);
    if(resp==SpreadsheetApp.getUi().Button.YES) {
      var resp=SpreadsheetApp.getUi().alert('Send out the appropriate letter so they can do their practical', SpreadsheetApp.getUi().ButtonSet.OK);
      if(resp==SpreadsheetApp.getUI().Button.OK){
        return;
      }
    }else{
      var resp=SpreadsheetApp.getUi().alert('By pressing OK, you confirm the applicant failed their exam, that you sent the correct letter and that their previous results can be removed.', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
      if(resp==SpreadsheetApp.getUI().Button.OK){
        deleteExam();
        return;
    }
     
      return;
    }
  }
}

function deleteExam(){
    var sheet = SpreadsheetApp.getActive().getSheetByName('{I} Exams')
    var rows = sheet.getDataRange();
    var numRows = rows.getNumRows();
    var values = rows.getValues();

    var rowsDeleted = 0;
    for (var i = 0; i <= numRows - 1; i++) {
    var row = values[i];
    if (row[20] == 'delete') { // This searches all cells in columns A (change to row[1] for columns B and so on) and deletes row if cell has value 'delete'.
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
      rowsDeleted++;
    }
  }
}

供参考

{I} Apps -> 我的概述选项卡,我在其中选中一个框,这会触发(现在也成功)出现消息框。

{I} 考试 -> 表单提交选项卡。

很高兴知道各个功能按预期工作(出现消息框,如果我手动运行 deleteExam 功能,这也有效)。

我尝试过的;

  • 使用触发器让deleteExam工作,但不起作用
  • deleteExam 中的代码放在 if 响应 = OK 的正下方,现在将在此处放置 deleteExam()。
  • 在单独的脚本上具有功能。
  • 三次检查了deleteExam 的列号以及名称引用

当我在确认消息框中按“确定”时,是否有人建议如何更改才能使“deleteExam”功能正常工作?

google-sheets google-apps-script dialog
1个回答
0
投票

我怀疑这不起作用,但也许你可以解释一下原因,然后我也许可以提出替代方案

function onMyEdit(e) {
  const sh = e.range.getSheet();
  if (sh.getName() == '{I} Apps' && e.range.columnStart == 13 && e.value == 'TRUE') {
    var resp = SpreadsheetApp.getUi().alert('Did the applicant pass their exam?', SpreadsheetApp.getUi().ButtonSet.YES_NO);
    if (resp == SpreadsheetApp.getUi().Button.YES) {
      resp = SpreadsheetApp.getUi().alert('Send out the appropriate letter so they can do their practical', SpreadsheetApp.getUi().ButtonSet.OK);
      if (resp == SpreadsheetApp.getUI().Button.OK) {
        return;
      }
    } else {
      resp = SpreadsheetApp.getUi().alert('By pressing OK, you confirm the applicant failed their exam, that you sent the correct letter and that their previous results can be removed.', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
      if (resp == SpreadsheetApp.getUI().Button.OK) {
        let sh2 = e.source.getSheetByName('{I} Exams')
        var vs = sh2.getRange(2,1,sh2.getLastRow() - 1, sh2.getLastColumn()).getValues();
        var d = 0;
        vs.forEach((r, i) => {
          if (r[20] == 'delete') {
            sh2.deleteRow((i + 1) - d++);
          }
        })
      }
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.