表单提交后,可安装触发器会随机触发多次

时间:2019-02-18 10:28:35

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

我有一个Google表单,可以将填写的数据提交给Google表格。借助于一个可安装的触发器(在提交表单时将其触发),我通过向该行添加新行来将最新行复制到新表中。但是,有时触发器会随机触发多次,因此会在另一张工作表中附加具有完全相同值的2行或更多行。我确信触发器是问题所在,因为我在触发器的G Suite开发人员中心概述页面中多次发现了触发器触发的证据。 其他遇到相同问题的人,或者知道什么解决方法会起作用吗?

编辑: 这是完整的代码。我有2张纸,一张叫做“ FormResponse”,其中的表格输入会自动复制。第二张工作表称为“ Checkliste”,只要有表单响应进入,就会触发可安装的触发器。然后它会触发函数“ insertIntoChecklist()”。我还有第二个可安装的触发器,用于“ edited()”。该方法尚未实现。但是,将来在用户编辑“清单”工作表时需要触发一种方法。

可安装触发器在G Suite开发人员中心内设置:

Triggers

function edited() {
  var sheet = SpreadsheetApp.getActiveSheet();

  switch(sheet.getSheetName()){
    case "Checkliste":
      //checkIfFinished(); --> Method will be implemented later
      break;
    default:
      Logger.log("Unknown Sheet name in onEdit method!");
  }
}

// Method to insert into the Checklist sheet depending on the team
function insertIntoChecklist() {
  // Spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // FormResponse
  var formResponse = ss.getSheetByName("FormResponse");
  var lastRowFormResponse = formResponse.getLastRow();

  // Values FormResponse
  var prename = formResponse.getRange(lastRowFormResponse, 2.0).getValue();
  var surname = formResponse.getRange(lastRowFormResponse, 3.0).getValue();
  var team = formResponse.getRange(lastRowFormResponse, 4.0).getValue();
  var date = formResponse.getRange(lastRowFormResponse, 5.0).getValue();

  // Checklist
  var checklist = ss.getSheetByName("Checkliste");
  var lastRowChecklist = checklist.getLastRow();
  lastRowChecklist++;
  // Values Checklist
  var googleAccount = checklist.getRange("E" + lastRowChecklist);
  var phone = checklist.getRange("F" + lastRowChecklist);
  var workstation = checklist.getRange("H" + lastRowChecklist);

  // Based on the submitted team create instantiate a method that gets evoked
  switch(team){
    case "Immo":
      createImmo(checklist, prename, surname, team, date, email, googleAccount, phone, workstation);
      break;
    case "IT":
      createIT(checklist, prename, surname, team, date, email, googleAccount, phone, workstation);
      break;
    default:
      checklist.appendRow(["no team detected"]);
  }
}


var hardwareTypes = {
  winPC : "- Windows PC\n",
  iMac:  "- iMac\n",
};

var phoneTypes = {
  hardphone : "- Hardphone\n",
  softphone : "- Softphone\n"
};

function createImmo(checklist, prename, surname, team, date, email, googleAccount, phone, workstation) {
  checklist.appendRow([prename, surname, team, date]);
  addCheckbox(googleAccount);
  addCheckbox(phone);
  addNote(phone, phoneTypes.softphone);
  addCheckbox(workstation);
  addNote(workstation, hardwareTypes.winLap);
}

function createIT(checklist, prename, surname, team, date, email, googleAccount, phone, workstation) {
  checklist.appendRow([prename, surname, team, date]);
  addCheckbox(googleAccount);
  addCheckbox(phone);
  addNote(phone, phoneTypes.hardphone);
  addCheckbox(workstation);
  setNotAvailableCell(checklist);
}

function addCheckbox(checkboxRange) {
  var enforceCheckbox = SpreadsheetApp.newDataValidation();
  enforceCheckbox.requireCheckbox();
  enforceCheckbox.setAllowInvalid(false);
  enforceCheckbox.build();
  checkboxRange.setDataValidation(enforceCheckbox);
}

function addNote(range, note) {
  range.setNote(note);
}

function setNotAvailableCell(checklist) {
  var rowRange = checklist.getRange(checklist.getLastRow(), 1, 1, 
checklist.getLastColumn());
  var values = rowRange.getValues();

  for (var i = 0; i <= 0; i++){
    for (var j = 4; j < values[i].length; j++) {
      var currentValue = rowRange.getCell(i+1,j+1);
      var currentRange = 
checklist.getRange(currentValue.getRow(),currentValue.getColumn());

      if (values[i][j] !== false && values[i][j] !== true) {
        currentRange.setValue("n/a");
        currentRange.setBackground("#A9A9A9");
        currentRange.setHorizontalAlignment("center");
      }
    }
  } 
}

0 个答案:

没有答案