Google表单上的时钟输入和输出系统

时间:2018-09-09 00:21:06

标签: google-apps-script javascript

我创建了一个Google App脚本,该脚本创建了一个表单,允许用户从下拉列表中选择其名称,并将其定向到下一页。在下一页上,用他们的名字和一个小提示提示他们需要退出时钟(请参见图片),以及选择是否要退出时钟的选项。

运行此脚本后,创建了一个包含所有这些选项的表单。但是问题是如何设置一个描述,使它们最后一次进入的状态是“ mm / DD / yyyy” +您需要离开“”,反之亦然。

我将如何通过Google脚本执行此操作?

这是脚本:

function setUpForm() {
  //Set up form
  var form = FormApp.create('Clock In & Out System');
  form.setTitle('Clock In Form');

  //Set up first page
  var item1 = form.addListItem()
                  .setTitle('Employee Name')
                  .setRequired(true);
  var page2 = form.addPageBreakItem()
                  .setTitle('Fred');

  //Set up second page (Fred)
  var item2 = form.setTitle('Fred');
  var item2a = form.addMultipleChoiceItem()
                  .setTitle('Clocking in or out?')
                  .setChoiceValues(["Clock in", "Clock out"])
                  .setRequired(true);
  //Change last time clock in/out message and update reminder on what to do next
  var page3 = form.addPageBreakItem()
                  .setTitle('Wilma')
                  .setGoToPage(FormApp.PageNavigationType.SUBMIT);

  //Set up third page (Wilma)
  var item3 = form.setTitle('Wilma');
  var item3a = form.addMultipleChoiceItem()
                   .setTitle('Clocking in or out?')
                   .setChoiceValues(["Clock In", "Clock Out"])
                   .setRequired(true);
  var page4 = form.addPageBreakItem()
                  .setTitle('Betty')
                  .setGoToPage(FormApp.PageNavigationType.SUBMIT);

  //Setup forth page (Betty)
  var item4 = form.setTitle('Betty')
  var item4a = form.addMultipleChoiceItem()
                   .setTitle('Clocking in or out?')
                   .setChoiceValues(["Clock In", "Clock Out"])
                   .setRequired(true);

  //Set up name choices on first page
  item1.setChoices([
   item1.createChoice("Fred", page2),
   item1.createChoice("Wilma", page3),
   item1.createChoice("Betty", page4)
  ]);
}

这是图片:

Picture in Google Form

1 个答案:

答案 0 :(得分:0)

我们要记住的第一件事是,应在受访者打开表格之前对表格进行所有更改。

添加上次响应的已签入消息的时间的一种方法是运行将该消息添加到表单的函数。在大多数情况下,最方便的方法可能是使用onFormSubmit触发器,该触发器更新应保存“上次输入时钟”消息的表单项。

  

注意:脚本采用者应该使它适应他们的特定需求,但是首先,他们应该了解它的作用以及它的作用方式。

为简单起见,假设我们使用标题(应用脚本将其命名为 Section Header )来保存相关消息,并且在标题和一个“多项选择题”,该消息应使用变量“时钟输入/输出”和“响应时间戳”。以下脚本将更新表单提交上的标题说明,因此,下一次用户打开表单提交时钟时,他们将看到上次提交响应的时间戳。应该将其添加到与表单绑定的Script项目中。

/**
 * Update help text of first section header on form submit
 *
 * @param Object e Form submit event object
 * @param Object e.authMode A value from the ScriptApp.AuthMode enum.
 * @param Object e.response A FormResponse object, representing the user's response to the form as a whole.
 * @param Object e.source   A Form object, representing the Google Forms file to which the script is bound.
 * @param Object e.triggerUid   ID of trigger that produced this event.
 */
function updateHelpText(e) {
  // Get section headers.
  var formItems = e.source.getItems(FormApp.ItemType.SECTION_HEADER);

  // Get response timestamp
  var timestamp = e.response.getTimestamp();

  // Get script timezone
  var timeZone = Session.getScriptTimeZone();

  // Set timestamp format
  var format = 'mm/dd/yyyy hh:mm';

  // Get the first section header. This form item will hold the message.
  var item = formItems[0];

  // Get clocked type
  var type = e.response.getItemResponses()[0].getResponse();

  // Set message
  var message = Utilities.formatString('The last time you %s was %s',
                                       type,
                                       Utilities.formatDate(timestamp, timeZone, format));

  // Add message to section header
  item.setHelpText(message);

}

/**
 * Auxiliary function to validate on form submit function
 *
 */
function test(){

  // Create a form response
  var form = FormApp.getActiveForm();
  var items = form.getItems();
  var x = items[1].getTitle();
  var formResponse = form.createResponse();
  formResponse
    .withItemResponse(
      items[1].asMultipleChoiceItem().createResponse('Clock in')
    );

  // Create on form submit event object
  var e = {}; // Form submit event object
  e.authMode    = ScriptApp.AuthMode.FULL; // A value from the ScriptApp.AuthMode enum.
  e.source = form;  // A Form object, representing the Google Forms file to which the script is bound.
  e.response = formResponse.submit(); // A FormResponse object, representing the user's response to the form as a whole.
  e.triggerUid  = '00000000'; // ID of trigger that produced this event.

  // Call the on form submit function
  updateHelpText(e);
}

将以上代码添加到表单脚本项目后,请按照Installable triggers上的说明添加触发器。

为使以上内容适用于OP情况,采用者可以添加一些逻辑,以将标题 Clock in or Clock out?的相应索引替换为0索引。

表单项和相应的项响应。假设使用ij来保存相应的索引,然后除了添加代码以设置应将以下行上的索引替换为i和{{ 1}}:

j