Google 表格上的打卡进出系统

Clock In & Out System on Google Form

我创建了一个 Google 应用程序脚本,它创建了一个表单,允许用户从下拉列表中选择他们的名字,它将引导他们到下一页。在下一页上,有他们的名字和一个小提示,说明他们需要打卡下班(见图片),以及他们选择是否要打卡打卡的选项。

在我 运行 这个脚本之后,我创建了一个包含所有这些选项的表单。但问题是如何设置一个描述,说他们上次打卡是 "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)
  ]);
}

图片如下:

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

添加最后一次回复签入消息的方法是 运行 将消息添加到表单的函数。在大多数情况下,最方便的方法可能是使用 onFormSubmit 触发器来更新应该包含 "last clocked in" 消息的表单项。

NOTE: Script adopters should adapt it to their specific needs, but first they should understand what it does and how it does that.

为简单起见,假设我们使用 Title(Apps 脚本将其称为 Section Header)来保存相关的消息,我们有一个只有标题和一个多项选择题的表格,消息应该使用变量时钟 in/out 和响应时间戳。以下脚本将更新表单提交时的标题描述,因此下次用户打开表单提交时钟时 in/out 他们将看到上次提交回复的时间戳。它应该被添加到绑定到表单的脚本项目中。

/**
 * 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 案例,采用者可以添加一些逻辑,用 TitleClock 的相应索引替换 0 索引上班或下班? 相应地形成项目和项目响应。假设 ij 用于保存相应的索引,那么除了添加代码以设置以下行中的索引之外,还应将 0 索引替换为 ij相应地:

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

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