我一直收到此错误“我们很抱歉,发生了服务器错误。请稍等一下再试一次。”因为我运行我的代码而且我真的需要一些帮助。
我怀疑错误是邮件应用程序
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);
}
}
非常感谢你的帮助!
答案 0 :(得分: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循环的开头
for (j=0;j<TW_data.length;j++)
因为其他代码看起来很快就可以了。