【问题标题】:Is there a way to stop an event from completing in google apps script? (ie) e.preventDefault()有没有办法阻止事件在谷歌应用脚​​本中完成? (即) e.preventDefault()
【发布时间】:2020-09-09 15:13:56
【问题描述】:

我目前正在将用户通过 google 表单输入的日期与今天的日期进行比较,如果日期尚未出现,我想停止将表单数据插入到 google 响应电子表格中。我已经用谷歌搜索了这个,但找不到类似于具有 e.preventDefault() 的 DOM 的东西。这是我的代码在谷歌脚本编辑器中的样子:

//first, verify the date of the submission is not before the date of the game (has not happened yet)
var todayDate = new Date();
var gameDate = new Date(ogkGameDate);
if (gameDate < todayDate)
{
  //send an email to admin and user that submission was rejected because the game date is in the future.
var subject = "WFRA Online Game Keeping Submission";

var message = "Hello! Thank you for submitting to the online game keeping system. \n";
message += "\n Unfortunately, the submission was rejected because this game has not yet occurred:";

message +="\n Game Keeper: " + ogkGameKeeper;
message +="\n Game Date: " + gameDate;
message +="\n Submission Date: " + todayDate;

//message += "\n Administrators can view the submission sheet at " + ogkURL + " \n";
message += "\n Please reply to this email if you experience technical issues. \n";


// Send yourself an email with a link to the document.
GmailApp.sendEmail("wfringette@gmail.com", subject, message,{bcc: "thetroutlakemonster@gmail.com", replyTo: "webmaster@westferrisringette.ca"});

//reject the submission to the sheet
    //HERE IS WHERE I WOULD LIKE TO CANCEL THE EVENT TO AVOID DATA INPUT TO THE SPREADSHEET

//exit the code

return;  }

提前感谢您的任何建议!

【问题讨论】:

  • 这段代码是如何触发的?
  • 您想阻止用户提交表单,还是阻止目标电子表格的填充/表单提交时触发脚本?
  • 我的代码是在我的谷歌表单的 onFormSubmit 中触发的,我的目标是防止电子表格的填充。非常感谢
  • 我刚刚发现我可以清除活动范围,然后什么都没有通过。这只是糟糕的编程吗?
  • 您的编码是绑定到表单还是电子表格?我建议您创建一个minimal reproducible example 这将有助于使答案更具体。

标签: google-apps-script google-sheets triggers google-forms preventdefault


【解决方案1】:

来自 OP 的评论

我的代码是在我的谷歌表单的 onFormSubmit 中触发的,我的目标是防止电子表格的填充。非常感谢

如果您将表单设置为将表单响应发送到电子表格,则无法阻止填充电子表格,但如果使用表单提交触发器来填充电子表格,则可以。

这样做的方法是在填充电子表格之前设置适当的条件。

顺便说一句,无法将e.preventDefault() 之类的内容添加到 Google 表单,因为无法编辑表单 HTML 或添加自定义 JavaScript。


问题中包含的代码包含比较两个 Dates 对象的条件,但无法直接比较它们。

代替

if (gameDate < todayDate)

使用

if (gameDate.getTime() < todayDate.getTime())

相关

【讨论】:

    【解决方案2】:

    示例如何使用所需数据填充 onFormSubmit 您自己的电子表格

    如果您的脚本绑定到您的表单,请执行以下操作:

    • 创建您自己的目标电子表格
    • 修改您的 if 条件以按照 Ruben 的解释正确比较日期
    • 重要提示:如果您想发送电子邮件并避免将来提交游戏日期的表单,正确的查询应该是 if (gameDate.getTime() &gt; todayDate.getTime())
    • 添加else 语句以实现如果游戏日期已经发生,则应将表单数据提交到您的自定义电子表格中

    示例

    function bindMeOnFormSubmitTrigger(e){
      //please define
      var ogkGameKeeper="";
      //sample date
      var ogkGameDate = "2020-09-12";
      var todayDate = new Date();
      var gameDate = new Date(ogkGameDate);
      if (gameDate.getTime() > todayDate.getTime())  {
        //send an email to admin and user that submission was rejected because the game date is in the future.
        var subject = "WFRA Online Game Keeping Submission";
        
        var message = "Hello! Thank you for submitting to the online game keeping system. \n";
        message += "\n Unfortunately, the submission was rejected because this game has not yet occurred:";
        
        message +="\n Game Keeper: " + ogkGameKeeper;
        message +="\n Game Date: " + gameDate;
        message +="\n Submission Date: " + todayDate;
        
        //message += "\n Administrators can view the submission sheet at " + ogkURL + " \n";
        message += "\n Please reply to this email if you experience technical issues. \n";
        
        Logger.log("send email");  
        // Send yourself an email with a link to the document.
        GmailApp.sendEmail("wfringette@gmail.com", subject, message,{bcc: "thetroutlakemonster@gmail.com", replyTo: "webmaster@westferrisringette.ca"});
      } else{
        //paste form data into your own spreadsheet
        var formResponse = e.response;
        var rowData = [];
        var timeStamp = formResponse.getTimestamp();
        rowData.push(timeStamp);
        var itemResponses = formResponse.getItemResponses();
        for (var j = 0; j < itemResponses.length; j++) {
          var itemResponse = itemResponses[j];
          rowData.push(itemResponse);
        }
        //append the form submit data to your own custom sheet
        var mySheet = SpreadsheetApp.openById("PASTE HERE THE ID OF YOUR CUSTOME SPREADSHEET").getSheetByName("PASTE HERE THE NAME OF THE SHEET").appendRow(rowData);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-09
      • 2017-06-08
      • 2021-06-16
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多