最后一行写了两次

时间:2015-03-25 08:21:55

标签: loops google-apps-script

我有一个破解的代码,我已经设法工作(原来的问题在这里https://stackoverflow.com/questions/29127005/multiple-spreadsheet-rows-from-multiple-forms),但循环写了最后一行两次,我的编码技巧不够好,我找不到原因...呢!

任何人都可以了解我所包含的内容吗?

function InsertDataInSheet(e)  //Function to insert data into spreadsheet on clicking submit
{

var app = UiApp.getActiveApplication();

//get number of rows to input
  var num = parseInt(e.parameter.table_tag);  
  var num = num+1;

//set increment step through  
  for (var i = 1; i < num ; i++ ) {

//Declare varialbe fields to collect data from
    var user         = Session.getActiveUser().getEmail();
    var date         = e.parameter['DateBox'+i];
    var location     = e.parameter['LocationListBox'+i];
    var source       = e.parameter['SourceListBox'+i];
    var reporter     = e.parameter['ReporterTextBox'+i];
    var priority     = e.parameter['PriorityListBox'+i];
    var hazard       = e.parameter['HazardListBox'+i];
    var details      = e.parameter['DetailsTextBox'+i];
    var description  = e.parameter['DescriptionTextBox'+i];
    var timeStamp    = new Date();

    //Decide date that this needs to be closed by
    if (priority === '02 - WITHIN 24-48 HOURS') {
      var dateTemp = new Date(date);
      dateTemp.setDate(dateTemp.getDate()+2);
      var actiondate = dateTemp; 
    }
    if (priority === '03 - WITHIN 1 WEEK') {
        var dateTemp = new Date(date);
      dateTemp.setDate(dateTemp.getDate()+7);
      var actiondate = dateTemp;  
    } 


      //establish email addresses
  //Declare correct range to obtain values
    var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes")
  //Start with central maintenance department
    var email00 = LocationSheet.getRange(33,5).getValue()
  //followed by other emails as they appear
    var email01 = LocationSheet.getRange(3,5).getValue();
    var email02 = LocationSheet.getRange(4,5).getValue();
     //declare the correct Depots to check
    var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes");
    var depot01 = LocationSheet.getRange(3,4).getValue();
    var depot02 = LocationSheet.getRange(4,4).getValue();

  //if source is recorded as '08 - Maitenance Request System', the recipient is maintenance deparment
    if (source === "08 - Maintenance Request System"){
    var recipient = email00;
  //or depots as listed
    } else if(location === depot01){
    var recipient = email01;

    } else if(location === depot02){
    var recipient = email02; } 

else {
  //and send an email to the error catch all if no code supplied
    var recipient = email00; //change as necessary  
    }

    var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName('LOG');
    var lastRow = sheet.getLastRow();
    var lrp1 = lastRow+1
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);


//Notification Page
app = UiApp.getActiveApplication().remove(0);
  app.createVerticalPanel()
  .setId('info')
  .setVisible(true)
  .setStyleAttribute('left', 10)
  .setStyleAttribute('top', 10)
  .setStyleAttribute('zIndex', '1')
  .setStyleAttribute('position', 'fixed')
  .setStyleAttribute('background', 'white')
  .setHeight('400px')
  .setStyleAttribute('text-align', 'center')
  .setBorderWidth(1)
  .setWidth('500px');

  app.add(app.createLabel('Your submission has been added to the database & the Site Coordinator will be emailed with notification.'));
//  app.add(app.createLabel('The details you entered were recorded as:'));
//  app.add(app.createLabel('Username: [' +user+ ']  Timestamp: [' +timeStamp+ ']  Reporter: [' +reporter+ ']'));
//  app.add(app.createLabel('Location:  [' +location+ '] Hazard: [' +hazard+ ']  Source: [' +source+ ']'));
//  app.add(app.createLabel('You listed the priority as: [' +priority+ '], please be aware this may not be the same priority given by the Site Coordinator responsible.'));
  app.add(app.createLabel('Please refresh this page to submit more entries'));
return app.close();

  } 

    var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LOG");
    var lastRow = sheet.getLastRow();
    var lrp1 = lastRow+1
  //Amend [getRange(lastRow+1, 1, 1, **)] integer to reflet number of headers being written if more added
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);
    var Body = 'A new [' +source+ '] log entry has been recorded at [' +location+ '], listed as [' + hazard+ ']. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].'

    }

2 个答案:

答案 0 :(得分:0)

两次添加最后一行的原因是因为在循环之后重复了以下代码:

var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LOG");
    var lastRow = sheet.getLastRow();
    var lrp1 = lastRow+1
  //Amend [getRange(lastRow+1, 1, 1, **)] integer to reflet number of headers being written if more added
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);
    var Body = 'A new [' +source+ '] log entry has been recorded at [' +location+ '], listed as [' + hazard+ ']. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].'

希望有所帮助!

答案 1 :(得分:0)

感谢KRR,我将其与通知页面之后的“}”之一移动到它之前。更正的代码是:

    var sheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName('LOG');
    var lastRow = sheet.getLastRow();
    var lrp1 = lastRow+1
    var targetRange = sheet.getRange(lastRow+1, 1, 1, 12).setValues([[timeStamp,date,source,location,reporter,user,hazard,details,description,priority,recipient,actiondate]]);

 }
///// TRAIL NOTIFICATION PAGE FROM WITHIN SYSTEM FROM HERE /////
app = UiApp.getActiveApplication().remove(0);
  app.createVerticalPanel()
  .setId('info')
  .setVisible(true)
  .setStyleAttribute('left', 10)
  .setStyleAttribute('top', 10)
  .setStyleAttribute('zIndex', '1')
  .setStyleAttribute('position', 'fixed')
  .setStyleAttribute('background', 'white')
  .setHeight('400px')
  .setStyleAttribute('text-align', 'center')
  .setBorderWidth(1)
  .setWidth('500px');

  app.add(app.createLabel('Your submission has been added to the database & the Site Coordinator will be emailed with notification.'));
//  app.add(app.createLabel('The details you entered were recorded as:'));
//  app.add(app.createLabel('Username: [' +user+ ']  Timestamp: [' +timeStamp+ ']  Reporter: [' +reporter+ ']'));
//  app.add(app.createLabel('Location:  [' +location+ '] Hazard: [' +hazard+ ']  Source: [' +source+ ']'));
//  app.add(app.createLabel('You listed the priority as: [' +priority+ '], please be aware this may not be the same priority given by the Site Coordinator responsible.'));
  app.add(app.createLabel('Please refresh this page to submit more entries'));
return app.close();

}