多个表单中的多个电子表格行

时间:2015-03-18 16:06:35

标签: loops google-apps-script

好的,正在取得进展:我已经设法将表单条目写入电子表格并且它正在工作,除了最后一个条目被输入两次。有什么想法,我做错了什么?

修改后的代码是:

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;  
    } 
     if (priority === '04 - WITHIN 1 MONTH') { 
        var dateTemp = new Date(date);
      dateTemp.setDate(dateTemp.getDate()+31);
      var actiondate = dateTemp;  
    }  
     if (priority === '05 - WITHIN 3 MONTHS') { 
        var dateTemp = new Date(date);
      dateTemp.setDate(dateTemp.getDate()+90);
      var actiondate = dateTemp;  
    } 
     if (priority === '06 - FOR MANAGEMENT DISCUSSION') { 
      var dateTemp = new Date(date);
      dateTemp.setDate(dateTemp.getDate()+365);
      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();
    var email03 = LocationSheet.getRange(5,5).getValue();
    var email04 = LocationSheet.getRange(6,5).getValue();
    var email05 = LocationSheet.getRange(7,5).getValue();
    var email06 = LocationSheet.getRange(8,5).getValue();
    var email07 = LocationSheet.getRange(9,5).getValue();
    var email08 = LocationSheet.getRange(10,5).getValue();
    var email09 = LocationSheet.getRange(11,5).getValue();
    var email10 = LocationSheet.getRange(12,5).getValue();
    var email11 = LocationSheet.getRange(13,5).getValue();
    var email12 = LocationSheet.getRange(14,5).getValue();
    var email13 = LocationSheet.getRange(15,5).getValue();
    var email14 = LocationSheet.getRange(16,5).getValue();
    var email15 = LocationSheet.getRange(17,5).getValue();
    var email16 = LocationSheet.getRange(18,5).getValue();
    var email17 = LocationSheet.getRange(19,5).getValue();
    var email18 = LocationSheet.getRange(20,5).getValue();
    var email19 = LocationSheet.getRange(21,5).getValue();
    var email20 = LocationSheet.getRange(22,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();
    var depot03 = LocationSheet.getRange(5,4).getValue();
    var depot04 = LocationSheet.getRange(6,4).getValue();
    var depot05 = LocationSheet.getRange(7,4).getValue();
    var depot06 = LocationSheet.getRange(8,4).getValue();
    var depot07 = LocationSheet.getRange(9,4).getValue();
    var depot08 = LocationSheet.getRange(10,4).getValue();
    var depot09 = LocationSheet.getRange(11,4).getValue();
    var depot10 = LocationSheet.getRange(12,4).getValue();
    var depot11 = LocationSheet.getRange(13,4).getValue();
    var depot12 = LocationSheet.getRange(14,4).getValue();
    var depot13 = LocationSheet.getRange(15,4).getValue();
    var depot14 = LocationSheet.getRange(16,4).getValue();
    var depot15 = LocationSheet.getRange(17,4).getValue();
    var depot16 = LocationSheet.getRange(18,4).getValue();
    var depot17 = LocationSheet.getRange(19,4).getValue();
    var depot18 = LocationSheet.getRange(20,4).getValue();
    var depot19 = LocationSheet.getRange(21,4).getValue();
    var depot20 = LocationSheet.getRange(22,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 if(location === depot03){
    var recipient = email03;
  
    } else if(location === depot04){
    var recipient = email04;
  
    } else if(location === depot05){
    var recipient = email05;
  
    } else if(location === depot06){
    var recipient = email06;
  
    } else if(location === depot07){
    var recipient = email07;
  
    } else if(location === depot08){
    var recipient = email08;
  
    } else if(location === depot09){
    var recipient = email09;
  
    } else if(location === depot10){
    var recipient = email10;
  
    } else if(location === depot11){
    var recipient = email11;
  
    } else if(location === depot12){
    var recipient = email12;
  
    } else if(location === depot13){
    var recipient = email13;
  
    } else if(location === depot14){
    var recipient = email14;
  
    } else if(location === depot15){
    var recipient = email15;
  
    } else if(location === depot16){
    var recipient = email16;
  
    } else if(location === depot17){
    var recipient = email17;
  
    } else if(location === depot18){
    var recipient = email18;
  
    } else if(location === depot19){
    var recipient = email19;
  
    } else if(location === depot20){
    var recipient = email20;
  
    } 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]]);
 
  } 
    
    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部分。我的代码仍然失败,这是因为(如果我正确理解桑迪(再次感谢btw))没有循环。

代码以正确的顺序写出我需要的东西,它只是没有为每个条目写一个新行 - 我只写了第一行。

非常感谢任何帮助。

伪代码如下:

  1. 创建表单以填写网站中嵌入的表单。

  2. 创建+&amp; - 根据需要添加或删除行的按钮。

  3. 如果+点击添加行。

  4. 如果 - 点击删除行。

  5. 点击提交后,打开电子表格并为表单中创建的每一行写一个新的数据行。

  6. 提交后,请创建通知页。

  7. 我到目前为止的代码是:

        //spreadsheet key is needed to access the correct spreadsheet. It is located AFTER "/d/" and before "/edit" in the URL
    var itemSpreadsheetKey = '1mgp5d6gZydrn-bI6KfCNNOsIj9sh0dLJMzVq4Al4ypY';
    var LogSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("Log");
    var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes");
    var HazardSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("HazardCodes");
    var SourceSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("SourceCodes");
    
    
    function doGet() //the 'doGet' function retrieves the code from the embedded URL in the gadget
      {
          var app = UiApp.createApplication();
          var panel = app.createVerticalPanel();
          var table = app.createFlexTable().setId('table').setTag('0').setBorderWidth(0);
    
      //Write the header for the table
          var headerArray = ['DATE' , 'DEPOT/SITE' , 'REPORTER', 'SOURCE CODE', 'HAZARD CODE' , 'BRIEF DETAILS' , 'FULL DESCRIPTION' , 'PRIORITY'];
          for(var i=0; i<headerArray.length; i++)
          {
          table.setWidget(0, i, app.createLabel(headerArray[i]));
          }
    
      //Add the first row of form elements to input Member information
          addMemberRow(app);
    
      //Add a button to submit the info
          var button = app.createSubmitButton('CLICK TO SUBMIT');
    
      // call the InsertDataInSheet routine
          var handler = app.createServerHandler('InsertDataInSheet');
          handler.addCallbackElement(panel);
          button.addMouseUpHandler(handler);
          panel.add(table)
          .add(button);
          app.add(panel);
          return app;
      }
    
    
    function InsertDataInSheet(e) //Function to insert data in the sheet on clicking the submit button
      {
        var app = UiApp.getActiveApplication();
      //get input values from last row of data
        var num = parseInt(e.parameter.table_tag);  
        var num = num+1;
      //Set increment value to step through
        for (var i = 1; i < num ; i++ )
        {    
      //Declare variable 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 user            = Session.getUser().getUserLoginId();
        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;  
        }
        if (priority == '04 - WITHIN 1 MONTH')
        {
        var dateTemp = new Date(date);
        dateTemp.setDate(dateTemp.getDate()+31);
        var actiondate = dateTemp;  
        }
        if (priority == '05 - WITHIN 3 MONTHS')
        {
        var dateTemp = new Date(date);
        dateTemp.setDate(dateTemp.getDate()+90);
        var actiondate = dateTemp;  
        }
        if (priority == '06 - FOR MANAGEMENT DISCUSSION')
        {
        var actiondate = "N/A";  
        }
    
        app = UiApp.getActiveApplication().remove(0);
        app.createVerticalPanel()
        .setId('info')
        .setVisible(true)      
        .setStyleAttribute('left', 0)  
        .setStyleAttribute('top', 0)        
        .setStyleAttribute('zIndex', '1')
        .setStyleAttribute('position', 'fixed')
        .setStyleAttribute('background', 'white')
        .setHeight('400px')
        .setStyleAttribute('text-align', 'center')
        .setBorderWidth(0)
        .setWidth('500px');
        app.add(app.createLabel(''));
        app.add(app.createLabel('Thank you. Your form has been submitted to the Proactive/Reactive Hazard Logging database and ' +recipient+ ' (the Health & Safety Site Coordinator) has been alerted.'));
        app.add(app.createLabel('A copy of this email has been sent to ' +user+ ' with the timestamp ' +timeStamp+ ' which should be quoted if follow up is required.'));
        app.add(app.createLabel('Please refresh this page to add submit another form, or close the page to exit'));
        return app.close();
        }
      //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();
        var email03 = LocationSheet.getRange(5,5).getValue();
        var email04 = LocationSheet.getRange(6,5).getValue();
        var email05 = LocationSheet.getRange(7,5).getValue();
        var email06 = LocationSheet.getRange(8,5).getValue();
        var email07 = LocationSheet.getRange(9,5).getValue();
        var email08 = LocationSheet.getRange(10,5).getValue();
        var email09 = LocationSheet.getRange(11,5).getValue();
        var email10 = LocationSheet.getRange(12,5).getValue();
        var email11 = LocationSheet.getRange(13,5).getValue();
        var email12 = LocationSheet.getRange(14,5).getValue();
        var email13 = LocationSheet.getRange(15,5).getValue();
        var email14 = LocationSheet.getRange(16,5).getValue();
        var email15 = LocationSheet.getRange(17,5).getValue();
        var email16 = LocationSheet.getRange(18,5).getValue();
        var email17 = LocationSheet.getRange(19,5).getValue();
        var email18 = LocationSheet.getRange(20,5).getValue();
        var email19 = LocationSheet.getRange(21,5).getValue();
        var email20 = LocationSheet.getRange(22,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();
        var depot03 = LocationSheet.getRange(5,4).getValue();
        var depot04 = LocationSheet.getRange(6,4).getValue();
        var depot05 = LocationSheet.getRange(7,4).getValue();
        var depot06 = LocationSheet.getRange(8,4).getValue();
        var depot07 = LocationSheet.getRange(9,4).getValue();
        var depot08 = LocationSheet.getRange(10,4).getValue();
        var depot09 = LocationSheet.getRange(11,4).getValue();
        var depot10 = LocationSheet.getRange(12,4).getValue();
        var depot11 = LocationSheet.getRange(13,4).getValue();
        var depot12 = LocationSheet.getRange(14,4).getValue();
        var depot13 = LocationSheet.getRange(15,4).getValue();
        var depot14 = LocationSheet.getRange(16,4).getValue();
        var depot15 = LocationSheet.getRange(17,4).getValue();
        var depot16 = LocationSheet.getRange(18,4).getValue();
        var depot17 = LocationSheet.getRange(19,4).getValue();
        var depot18 = LocationSheet.getRange(20,4).getValue();
        var depot19 = LocationSheet.getRange(21,4).getValue();
        var depot20 = LocationSheet.getRange(22,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 if(location === depot03){
        var recipient = email03;
    
        } else if(location === depot04){
        var recipient = email04;
    
        } else if(location === depot05){
        var recipient = email05;
    
        } else if(location === depot06){
        var recipient = email06;
    
        } else if(location === depot07){
        var recipient = email07;
    
        } else if(location === depot08){
        var recipient = email08;
    
        } else if(location === depot09){
        var recipient = email09;
    
        } else if(location === depot10){
        var recipient = email10;
    
        } else if(location === depot11){
        var recipient = email11;
    
        } else if(location === depot12){
        var recipient = email12;
    
        } else if(location === depot13){
        var recipient = email13;
    
        } else if(location === depot14){
        var recipient = email14;
    
        } else if(location === depot15){
        var recipient = email15;
    
        } else if(location === depot16){
        var recipient = email16;
    
        } else if(location === depot17){
        var recipient = email17;
    
        } else if(location === depot18){
        var recipient = email18;
    
        } else if(location === depot19){
        var recipient = email19;
    
        } else if(location === depot20){
        var recipient = email20;
    
        } 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
      //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+ '], please visit > http://goo.gl/LRjPI9 < for further details. This form was submitted by [' +user+ '] with the timestamp [' +timeStamp+ '].'
    
        }
    
    
    
    function addMemberRow(app) //Function adds new rows to enable multiple form entries
      {
        var table = app.getElementById('table');
        var tag = parseInt(table.getTag());
        var numRows = tag+1;
        if(numRows >1){
      //Row is same, as last row drops back one.
        table.removeCell(numRows-1, 8);
        table.removeCell(numRows-1, 8);
        }
      //Create date picker box
        var DateBox = app.createDateBox().setWidth('50px').setId('DateBox'+numRows).setName('DateBox'+numRows).setFormat(UiApp.DateTimeFormat.DATE_SHORT);
      // Create 'locations' List with drop down selections
        var LocationListBox = app.createListBox().setWidth('100px').setId('LocationListBox'+numRows).setName('LocationListBox'+numRows);
        LocationListBox.addItem('');
        LocationListBox.addItem('BH - COMPANY (H&S Projects Team)');
        LocationListBox.addItem('CS - Conway Street Depot');
        LocationListBox.addItem('CT - Crawley Travel Office');
        LocationListBox.addItem('CY - Crawley Depot');
        LocationListBox.addItem('DU - Durrington Outstation');
        LocationListBox.addItem('EB - Eastbourne Outstation');
        LocationListBox.addItem('HS - Horsham Crew Area');
        LocationListBox.addItem('LR - Lewes Road Depot');
        LocationListBox.addItem('NE - Newhaven Engineering');
        LocationListBox.addItem('NO - Newhaven Outstation');
        LocationListBox.addItem('OS - One Stop Travel - North Street');
        LocationListBox.addItem('RH - Redhill Bus Station');
        LocationListBox.addItem('UK - Uckfield Outstation');
        LocationListBox.addItem('VP - Vehicles Procurement (Budget)');
        LocationListBox.addItem('WK - Whitehawk Depot');  
      // Create 'Source Code' list with drop down selections
        var SourceListBox = app.createListBox().setWidth('100px').setId('SourceListBox'+numRows).setName('SourceListBox'+numRows);
        SourceListBox.addItem('');
        SourceListBox.addItem('01 - Accident/Incident investigations & reports');
        SourceListBox.addItem('02 - Company or location H&S Committee/Forum actions');
        SourceListBox.addItem('03 - Emergency Preparedness reviews/activities');
        SourceListBox.addItem('04 - Employee Hazard / Near Miss Reporting System');
        SourceListBox.addItem('05 - Fire Brigade visit actions');
        SourceListBox.addItem('06 - Group H&S audit actions');
        SourceListBox.addItem('07 - Internal general & speicific location monitoring processes');
        SourceListBox.addItem('08 - Maintenance Request System');
        SourceListBox.addItem('09 - Management/employee meetings/toolbox talks & questions');
        SourceListBox.addItem('10 - Planned Preventative Maintenance activities');
        SourceListBox.addItem('11 - Regulatory bodies visit actions');
        SourceListBox.addItem('12 - Statutory & other specific inspection/testing actions');
        SourceListBox.addItem('Other');    
      // Create text box for entering 'Reporters' name
        var ReporterTextBox = app.createTextBox().setWidth('100px').setId('ReportertTextBox'+numRows).setName('ReporterTextBox'+numRows);
      // Create 'Priority Code' list with drop down selections
        var PriorityListBox = app.createListBox().setWidth('100px').setId('PriorityListBox'+numRows).setName('PriorityListBox'+numRows);
        PriorityListBox.addItem('');
        PriorityListBox.addItem('02 - WITHIN 24-48 HOURS');
        PriorityListBox.addItem('03 - WITHIN 1 WEEK');
         PriorityListBox.addItem('04 - WITHIN 1 MONTH');
        PriorityListBox.addItem('05 - WITHIN 3 MONTHS');
        PriorityListBox.addItem('06 - FOR MANAGEMENT DISCUSSION');
      // Create 'Hazard Box' list with drop down selection
        var HazardListBox = app.createListBox().setWidth('100px').setId('HazardListBox'+numRows).setName('HazardListBox'+numRows);
        HazardListBox.addItem('');
        HazardListBox.addItem('01 - Access Equipment Fault');
        HazardListBox.addItem('02 - Assault - verbal/physical');
        HazardListBox.addItem('03 - Blocked/held open fire route/exit');
        HazardListBox.addItem('04 - Contractor/visitor non-compiance or poor/unsafe practice');
        HazardListBox.addItem('05 - Fire/emergency related');
        HazardListBox.addItem('06 - First Aid provision');
        HazardListBox.addItem('07 - Fork Lift Truck operation');
        HazardListBox.addItem('08 - Housekeeping & Spillages');
        HazardListBox.addItem('09 - Lifting Equipment');
        HazardListBox.addItem('10 - Lighting Fault');
        HazardListBox.addItem('11 - Manual Handling');
        HazardListBox.addItem('12 - Missing/unclear information/warning/directional signage');
        HazardListBox.addItem('13 - Occupational Health related item');
        HazardListBox.addItem('14 - Pit safety equipment/barriers missing/poorly in place');
        HazardListBox.addItem('15 - Road Traffic Accident');
        HazardListBox.addItem('16 - Slips, trips & falls');
        HazardListBox.addItem('17 - Staff non-compliance (e.g. PPE) or poor/unsafe practice');
        HazardListBox.addItem('18 - Washroom/mess rooms facilities/housekeeping ');
        HazardListBox.addItem('19 - Work equipment fault');
        HazardListBox.addItem('20 - Workplace transport movement');
      // Create 'Details Box' for recording the hazard/defect
        var DetailsTextBox = app.createTextArea().setWidth('150px').setId('DetailsTextBox'+numRows).setName('DetailsTextBox'+numRows);
      // Create 'Description Box' for giving further details
        var DescriptionTextBox = app.createTextArea().setWidth('150px').setId('DescriptionTextBox'+numRows).setName('DescriptionTextBox'+numRows);
      //Add to page
        table.setWidget(numRows, 0, DateBox)
           .setWidget(numRows, 1, LocationListBox)
           .setWidget(numRows, 2, ReporterTextBox)
           .setWidget(numRows, 3, SourceListBox)
           .setWidget(numRows, 4, HazardListBox)
           .setWidget(numRows, 5, DetailsTextBox)
           .setWidget(numRows, 6, DescriptionTextBox)
           .setWidget(numRows, 7, PriorityListBox)       
        table.setTag(numRows.toString());
        addButtons(app);
      }
    
    
    function addButtons(app) //add or remove buttons as required
      {
        var table = app.getElementById('table');
        var numRows = parseInt(table.getTag());
      //Create handler to add/remove row
        var addRemoveRowHandler = app.createServerHandler('_addRemoveRow');
        addRemoveRowHandler.addCallbackElement(table);
     //Add row button and handler
        var addRowBtn = app.createButton('+').setId('addOne').setTitle('Add row');
        table.setWidget(numRows, 8, addRowBtn);
        addRowBtn.addMouseUpHandler(addRemoveRowHandler);
      //remove row button and handler
        var removeRowBtn = app.createButton('-').setId('removeOne').setTitle('Remove row');
        table.setWidget(numRows, 9, removeRowBtn);
        removeRowBtn.addMouseUpHandler(addRemoveRowHandler);
      }
    
    
    function _addRemoveRow(e) //add or remove rows as required
      {
        var app = UiApp.getActiveApplication();
        var table = app.getElementById('table');
        var tag = parseInt(e.parameter.table_tag);
        var source = e.parameter.source;
        if(source == 'addOne'){
        table.setTag(tag.toString());
        addMemberRow(app);
        }
        else if(source == 'removeOne'){
        if(tag > 1){ 
      //Decrement the tag by one
        var numRows = tag-1;
        table.removeRow(tag);
      //Set the new tag of the table
        table.setTag(numRows.toString());
      //Add buttons in previous row
        addButtons(app); 
        }
        }
        return app;
      }
    

1 个答案:

答案 0 :(得分:0)

电子表格有两张纸,一张名为&#34; LocationCodes&#34;。代码引用该表:

var LocationSheet = SpreadsheetApp.openById(itemSpreadsheetKey).getSheetByName("LocationCodes");

并将LocationSheet变量创建为全局变量。

还有另一张名为&#34; LOG&#34;的表格。这就是来自输入表单的所有数据的所在。

您希望代码查看LOG表中的每一行,并且对于存在的LOG表中的每一行,都要发送电子邮件。电子邮件从某处查找。显而易见的选择是&#34;电子邮件地址&#34;在专栏#34; F&#34; LOG表格,但我绝对需要确定,因为K列中还有另一个电子邮件地址。

代码中需要一个循环,它只循环遍历LOG表中需要检查的行数。 LOG表中的行数总是一样吗?

代码逐行从表格中获取每封电子邮件:

    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();
    var email03 = LocationSheet.getRange(5,5).getValue();
    var email04 = LocationSheet.getRange(6,5).getValue();
    var email05 = LocationSheet.getRange(7,5).getValue();
    var email06 = LocationSheet.getRange(8,5).getValue();

上面的代码获取单个单元格值。如果您想一次通过电子邮件发送所有这些电子邮件,则需要使用不同的代码。

您需要使用编程循环遍历每封电子邮件。这将是为每个电子邮件地址发送单独的电子邮件。如果您想向大量电子邮件地址发送一封电子邮件,那么您需要将它们连接起来。

代码中没有循环,或任何会发送多封电子邮件的内容。