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

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

我一直在开发一个 Appscript,如果申请人考试不及格,我可以自动删除回复。

我正在使用这个脚本:

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-apps-script
1个回答
0
投票

问题似乎出在这一行:

if (row[20] == 'delete')

这是引用 T 列而不是 A 列。

更改为:

if (row[0] == 'delete')

我认为这对你有用。

(我设置了一个 onEdit 触发器来测试它......)

© www.soinside.com 2019 - 2024. All rights reserved.