仅在表单提交的第一个实例上触发脚本

时间:2017-01-28 10:00:55

标签: google-apps-script google-sheets google-apps google-form

我们的寄宿学校使用Google表格让学生在抵达和离开时登录和退出。 我有一个函数 - dailyRecord() - 每晚制作一个记录,当晚学生正在登机。 有两个自定义菜单,一个用于清除表单响应的半个周期并执行(周末外出),另一个用于清空每个学期结束时使用的登机之夜记录。

目前清除表单回复还将禁用dailyRecord()函数(我们不想在学校中没有学生的情况下创建记录)。 我试图创建一个脚本,在下次提交表单响应时重新启用dailyRecord()函数(即学生在周末/半学期后再次开始登录)。问题在于它在提交表单的每个时间运行,这意味着每晚都会创建多个记录而不是一个。我只需要在休息后的第一个表单提交时重新启用该功能。 任何人的想法

//ONOPEN - Menu allowing user to choose to delete previous form submissions//
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Clear Data')
  .addItem('Exeat & HT - Purge Form reponses', 'responsesAlert')
  .addItem('End of Term - Empty History Sheets', 'historyAlert')
      .addToUi();
}

//DELETETRIGGER - Stand alone function to delete 'dailyRecord' Trigger (stop recording nightly register)// 
function deleteTrigger() {
  var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() === 'dailyRecord') {
  ScriptApp.deleteTrigger(triggers[i]);
}
}}

//RESPONSESALERT - Alert box deleting previous form submissions 'YES', doing nothing if not//
function responsesAlert() {
  var ui = SpreadsheetApp.getUi();
  var result = ui.alert(
     'This will remove previous form responses - The nightly record (History Sheet) will remain intact',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);
  // Process the user's response.
  if (result == ui.Button.YES) {
  //Empty 'Form Responses 1' Sheet of data
  var ui = SpreadsheetApp.getUi();
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses 1");
  var lastRow = source.getLastRow()
  source.deleteRows(2, lastRow)}
  //Empty the Google Form of all Responses
  var form, urlForm = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
  if (urlForm) {}
    form = FormApp.openByUrl(urlForm);
    if (form) form.deleteAllResponses();
  //Delete 'dailyRecord' Trigger (stop recording nightly register)
  var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() === 'dailyRecord') {
  ScriptApp.deleteTrigger(triggers[i]);
}
}}

//HISTORYALERT - Alert box deleting previous form submissions 'YES', doing nothing if not//
function historyAlert() {
  var ui = SpreadsheetApp.getUi();
  var result = ui.alert(
     'This will clear all daily records from the History Sheet',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);
  // Process the user's response.
  if (result == ui.Button.YES) {
  //Empty 'History' Sheet of data
  var ui = SpreadsheetApp.getUi();
  var source = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("History");
  var lastColumn = source.getLastColumn()
  source.deleteColumns(4, lastColumn-3)}
}


//DAILYTRIGGER - Trigger running record-keeping function c.10pm each night//
function dailyTrigger() {
  ScriptApp.newTrigger('dailyRecord')
  .timeBased()
  .atHour(22)
  .everyDays(1)
  .create();
}

//DAILYRECORD - Record-keeping function, copy/value-pasting record into next clear column//
function dailyRecord() {
    var ss = SpreadsheetApp.getActive()
        .getSheetByName('History'),
        lastColumn = ss.getLastColumn(),
        colC = ss.getRange("C:C")
            .getValues();
    ss.getRange(1, lastColumn + 1, colC.length, 1)
        .setValues(colC);
}

2 个答案:

答案 0 :(得分:0)

以下代码为firstSubmission函数设置onFormSubmit触发器,该函数在提交表单时调用,该函数删除其自己的触发器,基本上成为一次触发器或第一次提交触发器。

在reponseAlert功能结束时调用此函数 像这样:

function responsesAlert() {
  var ui = SpreadsheetApp.getUi();
  ....
  var form, urlForm = SpreadsheetApp.getActiveSpreadsheet().getFormUrl();
  if (urlForm) {}
  form = FormApp.openByUrl(urlForm);
  ....
  var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() === 'dailyRecord') {
  ScriptApp.deleteTrigger(triggers[i]);
}
}
firstSubmissionTrigger(form)
}

function firstSubmissionTrigger(form){
    ScriptApp.newTrigger('firstSubmission').forForm(form).onFormSubmit().create()
// Sets trigger to call firstSubmission to be called when the submission is made
}

function firstSubmission(){
 dailyTrigger()
// Starts the dailyTrigger
// Below code deletes the triggers that calls firstSubmission function 
// Essentially making it a one time trigger or First submission trigger      
 var triggers = ScriptApp.getProjectTriggers();
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getHandlerFunction() === 'firstSubmission') {
  ScriptApp.deleteTrigger(triggers[i]);
}

}
}

答案 1 :(得分:0)

感谢所有的评论和帮助。我最终变得尽可能简单...脚本设置每日触发脚本编辑器和编程脚本运行如果多个表单条目。表格在每个周末都会被清空。半学期。

//RECORDTRIGGER
 function recordTrigger() {
  var responses = FormApp.openByUrl('formurl.com').getResponses().length
  if (responses > 1) {
      var ss = SpreadsheetApp.getActive().getSheetByName('History'),
        lastColumn = ss.getLastColumn(),
        colC = ss.getRange("C:C").getValues();
    ss.getRange(1, lastColumn + 1, colC.length, 1).setValues(colC);
}}

测试时似乎工作 - 非常感谢支持。