0

i keep getting this error " We're sorry, a server error occurred. Please wait a bit and try again. " as I run my code and I really need some help on this.

I suspect the error is the mail app though

    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);

   }




  } 

Will be grateful for help on this!

user3429475
  • 51
  • 1
  • 1
  • 4

1 Answers1

0

This part might be causing issues:

for (j in TW_data){

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

  var row_number = j + 2;

In this example, each j is the actual value in the row. So, if the column contains these 3 rows: "Apple", "Samsung" and "Sony", j itself would be each one of them one by one, so it's pointless to do TW_data[j] as it would actually mean TW_data["Samsung"], therefore the error. Also, with row_number you are summing "Samsung" with 2, which is not quite possible.

To make it work, you should simply replace the beginning of for loop with for (j=0;j<TW_data.length;j++) as the rest of the code looks ok on a quick look

Adelin
  • 6,791
  • 5
  • 33
  • 58