0

我不断收到此错误“我们很抱歉,发生了服务器错误。请稍等,然后重试。”当我运行我的代码时,我真的需要一些帮助。

我怀疑错误是邮件应用程序

    function onOpen() {
     var submenu = [{name:"Report", functionName:"responseMech"}];
     SpreadsheetApp.getActiveSpreadsheet().addMenu('Generator', submenu);  

    }



   function responseMech(){

 var response = Browser.inputBox('Report Generator', 'Please enter the Trackwise record             number you are searching for', Browser.Buttons.OK_CANCEL);
    report(response)
 }


  function report(response) {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Form Responses");



   var TW_data = ss.getRange(2,3,ss.getLastRow(),1).getValues();

 for (j in TW_data){

if(response == TW_data[j] && response != ""){
  //Main program can take place here

  var row_number = j + 2;

  var report_heading = new Array();
  var report_data = new Array();


  for (var i = 0; i < ss.getLastColumn(); ++i) {
    report_heading.push([]);
    report_data.push([]);
  }

  for (var i = 0; i < ss.getLastColumn(); ++i) {
    report_heading[i][0]=ss.getRange(1,i+1).getValues();
    report_data[i][0]=ss.getRange(row_number,i+1).getValues();
  }


  var final_heading = new Array();
  var final_data = new Array();



  for(var i = 0; i < ss.getLastColumn(); ++i){
    if(report_data[i][0] != ""){
      final_heading.push(report_heading[i][0]);
      final_data.push(report_data[i][0]);
    }
  }


  var doc = DocumentApp.openById("1vVs5A94GcPhsdowZjMDXnlTdL7iH_AiT52uemWgcVYU");



  doc.setText('')
  InputDataToReport(doc, final_heading, final_data);
  var sender = Session.getActiveUser().getEmail();

  var subject = " Report for TW# " + response;
  var message = "Please see attached";
//  


 var file = DriveApp.getFileById('1vVs5A94GcPhsdowZjMDXnlTdL7iH_AiT52uemWgcVYU');




  //Send the freshly constructed email 
MailApp.sendEmail(sender, subject, message, {
 name: 'Automatic Emailer Script',
 attachments: [file.getAs(MimeType.PDF)]
 });


}




     } 

  }



  function InputDataToReport(doc,heading, responses) { 
    //define header cell style which we will use while adding cells in header row
      //Backgroud color, text bold, white
     var headerStyle = {};
      headerStyle[DocumentApp.Attribute.BACKGROUND_COLOR] = '#336600';
        headerStyle[DocumentApp.Attribute.BOLD] = true;
        headerStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#FFFFFF';

      //Style for the cells other than header row
       var cellStyle = {};
   cellStyle[DocumentApp.Attribute.BOLD] = false;
    cellStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';

       //By default, each paragraph had space after, so we will change the paragraph style to add zero space
     //we will use it later
    var paraStyle = {};
   paraStyle[DocumentApp.Attribute.SPACING_BEFORE] = 0;
        paraStyle[DocumentApp.Attribute.SPACING_AFTER] = 0;
    paraStyle[DocumentApp.Attribute.LINE_SPACING] = 1;


     //get the body section of document
    var body = doc.getBody();

    //Add a table in document
     var table = body.appendTable();



    for(var i=0; i<heading.length; i++){
var tr1 = table.appendTableRow();
var td1 = tr1.appendTableCell(heading[i][0]);
td1.setAttributes(headerStyle);
var paraInCell = td1.getChild(0).asParagraph();
paraInCell.setAttributes(paraStyle);

var tr2 = table.appendTableRow();
var td2 = tr2.appendTableCell();
var para2 = td2.appendParagraph(responses[i][0]);
td2.setAttributes(cellStyle);
var paraInCell2 = td2.getChild(0).asParagraph();
paraInCell2.setAttributes(paraStyle);

   }




  } 

将不胜感激这方面的帮助!

4

1 回答 1

0

这部分可能会导致问题:

for (j in TW_data){

if(response == TW_data[j] && response != ""){
  //Main program can take place here

  var row_number = j + 2;

在此示例中,每个j都是行中的实际值。因此,如果该列包含这 3 行:和"Apple",本身将是一一对应的,因此按照它的实际含义去做是没有意义的,因此是错误。此外,与你相加,这是不太可能的。"Samsung""Sony"j TW_data[j]TW_data["Samsung"]row_number"Samsung"2

为了让它工作,你应该简单地替换 for 循环的开头, for (j=0;j<TW_data.length;j++)因为其余的代码看起来不错

于 2014-04-09T18:19:58.000 回答