如何使用Google Apps脚本制作自定义验证功能(例如requireTextMatchesPattern(pattern)函数)? [英] How to make a custom validation function like requireTextMatchesPattern(pattern) function using Google apps script?

查看:136
本文介绍了如何使用Google Apps脚本制作自定义验证功能(例如requireTextMatchesPattern(pattern)函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最终目标是能够为Google Forms项目定义自定义验证器,例如

The final objective is to have the ability to define custom validator for Google Forms item like requireTextMatchesPattern(pattern).

例如,将使用此自定义验证器来比较用户在表单项字段中输入的具有多个值的内容.或至少具有自定义功能,以便在用户在字段中输入无效数据时执行该功能.

This custom validator will be used for example to compare what the user enters in the form item field with more than one value. or at least to have a custom functionality to execute when the user enters not valid data in the field.

我有3位参与者,我想建立一个简单的身份验证机制,以确保目标受众将要参加.我有一个包含3个密码的电子表格.表格中的第一个问题将要求用户输入密码.如果密码与电子表格中存储的密码之一不匹配,则会向用户显示一条验证消息.

I have 3 participants, I want to make a simple authentication mechanism to make sure that the targeted audiences are going to participate. I have a spreadsheet that contains 3 passwords. The first question in the Form will require the user to enter a password. If the password doesn't match with one of the stored passwords in the spreadsheet, then, a validation message will appear to the user.

基于此问题,我们可以使用requireTextMatchesPattern验证程序或直接从用户界面进行简单的验证.问题在于该验证器将比较值限制为一个.

Based on this question we can make a simple validation using requireTextMatchesPattern validator or directly from UI. The problem is that this validator limits the compare values to one.

function validatePassword() {
  // Create new custom form
  var form = FormApp.create('New Form');
  var ss = SpreadsheetApp.openById('SHEETID');
  var password = ss.getSheetByName('SHEETNAME').getRange('A1').getValue();

  // Create first question to check the password, it must be required so that the user does not have access to the rest
  // of the form if failed to log in
  var item = form.addTextItem().setRequired(true);
  item.setTitle('TEST TITLE');

  // Create validation for this question matching the password that we got from the sheet
  var textValidation = FormApp.createTextValidation()
    .setHelpText('You must enter the right password')
    .requireTextMatchesPattern(password)
    .build();
  item.setValidation(textValidation);
}

我想做的是将.requireTextMatchesPattern(password)替换为对执行某些验证过程的自定义验证函数的调用,然后返回

What I am trying to do is to replace the .requireTextMatchesPattern(password) with a call to a custom validation function that does some validation process and then returns the type of TextValidationBuilder.

我找到了

I found this source code which defines an interface of TextValidationBuilder. I don't know if it is the key to accomplish the main objective.

谢谢!

推荐答案

我从您的问题中可以理解的是,例如,您在3个单元格中有3个密码(单词)(例如:从A1到A3).因此,您想将它们用作表单的条件,并且目前摆在您面前的问题是,您只能使用一个密码(单词)来做到这一点.

What I can understand from your question is that for example, you have 3 passwords (words) in 3 cells (Ex: from A1 to A3). Hence, you want to use them as conditions for a form and the issue ahead of you for the moment is that you are only able to do it with only one password (word).

您可能已经注意到, requireTextMatchesPattern(pattern )的参数是pattern,因此您可以将正则表达式构造为word1|word2|word3,它将验证3个密码是否正确.您的代码现在将如下所示:

As you probably noticed, the requireTextMatchesPattern(pattern)'s argument is a pattern, therefore you can have a Regex structured as word1|word2|word3, which will verify if the 3 passwords are the correct ones. Your code will look like this now:

function validatePassword() {
  // Create new custom form
  var form = FormApp.create('New Form');
  var ss = SpreadsheetApp.openById('your-sheet-id');
  var passwords = ss.getSheetByName('Sheet1').getRange('A1:A3').getValues();
  // Ex passwords: asd, 123, asd123
  const conditions = passwords.map(element => `${element}`).join('|')
  // Create first question to check the password, it must be required so that the user does not have access to the rest
  // of the form if failed to log in
  var item = form.addTextItem().setRequired(true);
  item.setTitle('TEST TITLE');
  // Create valid ation for this question matching the password that we got from the sheet
  var textValidation = FormApp.createTextValidation()
    .setHelpText('You must enter the right password')
    .requireTextMatchesPattern(conditions)
    .build();
  item.setValidation(textValidation);
}

这篇关于如何使用Google Apps脚本制作自定义验证功能(例如requireTextMatchesPattern(pattern)函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆