Google表单触发器并不总是触发

时间:2016-08-18 13:44:54

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

我有一个Google表单触发器,每次在表单上按下提交按钮时,它都会在我的响应电子表格中运行一个函数。该功能将向特定人员发送电子邮件。

要从我的回复电子表格中运行该功能,我将来自我的回复电子表格的Google脚本导出为库并将其导入我的google表单脚本。

一切运作良好,但是当在表单上按下提交按钮时,触发器始终不会触发。

电子表格中的脚本:

function onReportOrApprovalSubmit() {

  var SUPERVISOR_1_APPROVAL_SPREADSHEET_ID = "1eIN7NaeVmDEFck_gPH5hA1VuAHFzkOiHOcS67fXqtlw";
  var SUPERVISOR_1_FORM_URL = "https://docs.google.com/a/londonhydro.com/forms/d/e/1FAIpQLSf_tA7g29dKR2JaB4v4F3cBqkSbdrNnHqmumoMjJFiLLdQXJQ/viewform";
  var MANAGER_APPROVAL_SPREADSHEET_ID = "1WbriYc0aCKaeSywAOw2ZDL-g7PmF_CBCrlgB522Rg7k";
  var MANAGER_FORM_URL = "https://docs.google.com/a/londonhydro.com/forms/d/e/1FAIpQLSc9Tbf-ZMQLBK56EXR3hY3oFp-y6NMa_NzWR8BByfm9_ebwpw/viewform";
  var STATE_SUPERVISOR_EMAIL = "SUPERVISOR_EMAIL";
  var STATE_MANAGER_EMAIL = "MANAGER_EMAIL";
  var STATE_APPROVED = "APPROVED";
  var STATE_DENIED = "DENIED";
  var COLUMN_STATE_SUPERVISOR = 89;
  var COLUMN_STATE_MANAGER = 90;


  // This is the injury report form spreadsheet
  var ss = SpreadsheetApp.openById("1fZq6zY-4PFhp-qExizNs0Bra0s6XAAtUJWF7QdsZTIo");
  var sheet = ss.getSheets()[0];

  // Also open the Approval Spreadsheets
  var approvalsSupervisorSpreadSheet = SpreadsheetApp.openById(SUPERVISOR_1_APPROVAL_SPREADSHEET_ID);
  var approvalsManagerSpreadSheet = SpreadsheetApp.openById(MANAGER_APPROVAL_SPREADSHEET_ID);


  var approvalsSupervisorSheet = approvalsSupervisorSpreadSheet.getSheets()[0]; //get 1st sheet
  var approvalsManagerSheet = approvalsManagerSpreadSheet.getSheets()[0];
  // Fetch all the data from the injury report form spreadsheet
  var data = getRowsData(sheet);

  // Fetch all the data from approval sheets
  var approvalsSupervisorData = getApprovalSupervisorData(approvalsSupervisorSheet);
  var approvalsManagerData = getApprovalManagerData(approvalsManagerSheet);

  // For every injury report
  for (var i = 0; i < data.length; i++) {

    var row = data[i];
    row.rowNumber = i + 2;





    if (!row.supervisorState) {
       Logger.log("no supervisor email yet");
      sendReportToSupervisor(row);
      sheet.getRange(row.rowNumber, COLUMN_STATE_SUPERVISOR).setValue(row.supervisorState);

    } else if (row.supervisorState == STATE_SUPERVISOR_EMAIL) {

      for (var j = 0; j < approvalsSupervisorData.length; ++j) {

        var approval = approvalsSupervisorData[j];

        if (row.rowNumber != approval.reportID) {
          Logger.log("failed expenseId: " + approval.reportID + " rowNumber: " + row.rowNumber);
          continue;
        }

        Logger.log("pass");
        // Email the employee to notify the Manager's decision about the report.
        sendSupervisorApprovalResults(row, approval);
        // Update the state of the report to APPROVED or DENIED
        sheet.getRange(row.rowNumber, COLUMN_STATE_SUPERVISOR).setValue(row.supervisorState);
        break;
      }//end for

    }//end else if



    if(!row.managerState && row.supervisorState != STATE_SUPERVISOR_EMAIL && row.supervisorState){

      Logger.log("no manager email yet");
      sendReportToManager(row);
      sheet.getRange(row.rowNumber, COLUMN_STATE_MANAGER).setValue(row.managerState);

    }//end if
    else if(row.managerState == STATE_MANAGER_EMAIL && row.supervisorState != STATE_SUPERVISOR_EMAIL && row.supervisorState){

      for (var j = 0; j < approvalsManagerData.length; ++j) {

        var approval = approvalsManagerData[j];

        if (row.rowNumber != approval.reportID) {
          Logger.log("failed expenseId: " + approval.reportID + " rowNumber: " + row.rowNumber);
          continue;
        }//end if

        Logger.log("pass");
        // Email the employee to notify the Manager's decision about the report.
        sendManagerApprovalResults(row, approval);
        // Update the state of the report to APPROVED or DENIED
        sheet.getRange(row.rowNumber, COLUMN_STATE_MANAGER).setValue(row.managerState);
        break;
      }//end for

    }//end else if 

  }//end for every injury report

}//end onReportOrApprovalSubmit

我的表格中的脚本:

function onSubmit() {
  EmailChain.onReportOrApprovalSubmit();
}

0 个答案:

没有答案