【问题标题】:Multiple spreadsheet rows from multiple forms来自多个表单的多个电子表格行
【发布时间】:2015-05-21 12:42:13
【问题描述】:

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

修改后的代码为:

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 部分。我的代码仍然失败,这是因为(如果我正确理解 Sandy(再次感谢顺便说一句))没有循环。

代码以我需要的正确顺序写入我需要的内容,它只是不会为每个条目写入新行 - 我只写入第一行。

非常感谢任何帮助。

伪代码如下:

  1. 创建用于填写嵌入网站的表单。

  2. 根据需要创建用于添加或删除行的 + 和 - 按钮。

  3. 如果 + 单击添加行。

  4. 如果 - 单击删除行。

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

  6. 提交后创建通知页面。

我目前的代码是:

    //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;
  }

【问题讨论】:

  • 您使用的是UiApp,现在已弃用。 Google Documentation - UiApp这不会直接影响你的问题,但我想我会让你知道的。
  • 是的,我知道,但感谢桑迪。我希望让这个工作,所以我有机会在迁移时用 html 重写它。

标签: loops google-apps-script


【解决方案1】:

电子表格有两张表,一张名为“LocationCodes”。代码引用该表:

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

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

还有另一个名为“LOG”的工作表。这就是输入表单中所有数据的去向。

您希望代码查看 LOG 表中的每一行,并为 LOG 表中存在的每一行发送一封电子邮件。从某处查找电子邮件。显而易见的选择是 LOG 表的“F”列中的“电子邮件地址”,但我需要绝对确定,因为 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();

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

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

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

【讨论】:

  • 嗨桑迪,感谢您的回复。我没有解释清楚。 IF 语句只获得一个电子邮件地址,因为它为每个提交的表单实例输入它。 “addMemberRow”、“addButtons”和“_addRemoveRow”函数在站点中创建表单。 'insertDataInSheet' 例程应该为创建的每个表单行查找电子邮件地址,然后通过电子邮件发送给该人,然后转到下一个表单行。我创建了一个复制站点并在此处复制代码和工作表:工作表link 代码link & 站点link
  • 感谢您的链接,这很有帮助。请参阅答案中的更新文本。然后回复,让我知道我是否理解正确。
  • 我希望代码浏览 LOG 表,并为 LOG 表中的每一行发送一封电子邮件到 K 列中的电子邮件地址,并抄送 F 列中的电子邮件。行数总会有所不同。我想它总是会发送电子邮件,所以我将添加一个列以声明“已发送”,如果该列为空白,则发送电子邮件,如果它声明“已发送”则忽略它。
  • 该列已添加到测试电子表格中,因此“SENT”列现在是 L 列。
  • 我已经更改了我的代码,它现在可以正常工作,因为它可以按预期将多个表单行写入电子表格。但是,如果有一行数据为所有条目写入未定义,如果有不止一行,它会写入除最后一行之外的所有数据,但会复制倒数第二个!有什么建议吗?上面的代码有问题。
猜你喜欢
  • 1970-01-01
  • 2021-08-19
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多