在Google表单提交后,Google应用脚本会触发两次

时间:2019-03-15 17:49:28

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

我的脚本在Google表单和链接的表格中两次运行时遇到问题。我有一个表单提交触发器。如果删除此触发器,则脚本根本不会在表单提交上运行。如果我重新添加触发器,它将触发两次-但是工作表响应中只有一个表单提交。我以为也许我两次单击了表单提交,但事实并非如此。谢谢。

这是代码:

function myFunction(e) {


  // Set values for event "e" from Response form, each number being a column in the spreadsheet. Column zero is not used - it is the timestamp
  var JobNumber = e.values[1];

  // Create an array from the PNEZD field in the form then find the array location for the inputted shot number
  var PNEZD_field = e.values[2];
  var PNEZD_array = Utilities.parseCsv(PNEZD_field);

  var StructureNumber = e.values[3];

  for (i = 0; i < PNEZD_array.length; i++) {
    if (PNEZD_array[i][0] == StructureNumber) {
      var Elevation = PNEZD_array[i][3];
      var Easting = PNEZD_array[i][2];
      var Northing = PNEZD_array[i][1];
    }
  }


  // STRUCTURE TYPE AND SIZE
  var StructureType = e.values[4];
  var StructureSize = e.values[5];

  // PIPE NUMBER 1  
  var PipeSize1 = e.values[6];
  var PipeMaterial1 = e.values[7];
  var PipeDirection1 = e.values[8];
  var PipeMeasureDown1 = e.values[9];

  // PIPE NUMBER 2 
  var PipeSize2 = e.values[10];
  var PipeMaterial2 = e.values[11];
  var PipeDirection2 = e.values[12];
  var PipeMeasureDown2 = e.values[13];

  // PIPE NUMBER 3  
  var PipeSize3 = e.values[14];
  var PipeMaterial3 = e.values[15];
  var PipeDirection3 = e.values[16];
  var PipeMeasureDown3 = e.values[17];

  // PIPE NUMBER 4 
  var PipeSize4 = e.values[18];
  var PipeMaterial4 = e.values[19];
  var PipeDirection4 = e.values[20];
  var PipeMeasureDown4 = e.values[21];

  // PIPE NUMBER 5 
  var PipeSize5 = e.values[22];
  var PipeMaterial5 = e.values[23];
  var PipeDirection5 = e.values[24];
  var PipeMeasureDown5 = e.values[25];

  //  Calculate Invert Elevations:  
  var Dip1 = Elevation - PipeMeasureDown1;
  var Dip2 = Elevation - PipeMeasureDown2;
  var Dip3 = Elevation - PipeMeasureDown3;
  var Dip4 = Elevation - PipeMeasureDown4;
  var Dip5 = Elevation - PipeMeasureDown5;

  // Build structure information
  var firstLine = StructureType + " " + StructureSize + "\n";
  var secondLine = "Rim=" + Elevation + "\n";
  var thirdLine = PipeSize1 + " " + PipeMaterial1 + " I.E. " + PipeDirection1 + "=" + Dip1 + "'\n";
  var fourthLine = PipeSize2 + " " + PipeMaterial2 + " I.E. " + PipeDirection2 + "=" + Dip2 + "'\n";
  var fifthLine = PipeSize3 + " " + PipeMaterial3 + " I.E. " + PipeDirection3 + "=" + Dip3 + "'\n";
  var sixthLine = PipeSize4 + " " + PipeMaterial4 + " I.E. " + PipeDirection4 + "=" + Dip4 + "'\n";
  var seventhLine = PipeSize5 + " " + PipeMaterial5 + " I.E. " + PipeDirection5 + "=" + Dip5 + "'";

  var Mtext = firstLine + secondLine + thirdLine + fourthLine + fifthLine + sixthLine + seventhLine;

  var calcsheet = SpreadsheetApp.openById('-removed-').getSheetByName('Calculations');
  var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss is now the spreadsheet the script is associated with
  var sheet = ss.getSheets()[1]; // sheets are counted starting from 0
  // sheet is the second worksheet in the spreadsheet
  var cell = sheet.getRange("A1");
  cell.setValue(Mtext);



  // Dump variable to logger log
  //    var dump = "Variable dumps:  first the URL: " + PNEZD_fileURL + " and next the file ID alone: " + PNEZD_fileID + " and now the job number: " + JobNumber;
  //    Logger.log( dump );
  //    Logger.log(JobNumber); 
  //    Logger.log("Data dump: " + PNEZD_field);
  //    Logger.log("Data dump: " + PNEZD_array[3][3]);
  //    Logger.log(StructureNumber + "," + Northing + "," + Easting + "," + Elevation);




}

0 个答案:

没有答案