0

データの表を含む Google スプレッドシートがあります。フォームに入力すると、スクリプトがデータを取得してメールで送信します。データ範囲から必要な各値を取得してテーブルに入れるコードがありますが、それはすべて 100 を超える変数を識別することによって行われます。

データ範囲の各行のデータが html テーブルに取り込まれるように、何らかの配列を使用する方法はありますか?

私はプログラミングの初心者であることを率直に認めており、これはおそらく最悪のスパゲッティ コードの一部であることを認めています。どんな助けも大歓迎です(たとえそれがjavascriptの入門書であっても)。

function Gradereport() {

// このスクリプトは、フォームの内容を特定の受信者に電子メールで送信します

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Report");
var recloc = sheet.getRange("D2"); //cell location of student email
var recipient = recloc.getValue(); //student's email address
var lastloc = sheet.getRange("B2");
var last = lastloc.getValue();  
var firstloc = sheet.getRange("A2");
var first = firstloc.getValue();
var courseloc = sheet.getRange("E2");
var course = courseloc.getValue();
var dateloc = sheet.getRange("C2");
var date = dateloc.getValue();
var gradeloc = sheet.getRange("D5");
var grade = gradeloc.getValue();
var totloc = sheet.getRange("D4");
var total = totloc.getValue();
var perloc = sheet.getRange("D6");
var percent = perloc.getValue();
var dataRange = sheet.getRange("A4:B9");
var data = dataRange.getValues();
// assignment cell locations
var assignloc1 = sheet.getRange("A5");
var assignloc2 = sheet.getRange("A6");
var assignloc3 = sheet.getRange("A7");
var assignloc4 = sheet.getRange("A8");
var assignloc5 = sheet.getRange("A9");
// assignment values
var assign1 = assignloc1.getValue();
var assign2 = assignloc2.getValue();
var assign3 = assignloc3.getValue();
var assign4 = assignloc4.getValue();
var assign5 = assignloc5.getValue();
// assignment score locations
var scoreloc1 = sheet.getRange("B5");
var scoreloc2 = sheet.getRange("B6");
var scoreloc3 = sheet.getRange("B7");
var scoreloc4 = sheet.getRange("B8");
var scoreloc5 = sheet.getRange("B9");
// assignment score values
var score1 = scoreloc1.getValue();
var score2 = scoreloc2.getValue();
var score3 = scoreloc3.getValue();
var score4 = scoreloc4.getValue();
var score5 = scoreloc5.getValue();

  // error message
var errmess = first+' '+last+', your Pin code did not match. Please double check your entry and re-submit. Contact your professor if you get this message again.';
var subject = course+' Grade Report';
var body = first+' '+last+', here is your grade report, requested on '+date+'. Grade '+grade+'/'+total+', '+percent+'%. Score breakdown: '+data;
var bodyHTML1 = '<p>'+first+' '+last+', here is your grade report.<br> Grade '+grade+'/'+total+', '+percent+'%.</p>';
// var bodyHTML2 = '<p>'+data+'</p>';
var bodyHTML2 = '<table> <tr> <td> '+assign1+' </td> <td> '+score1+' </td> </tr> <tr> <td> '+assign2+' </td> <td> '+score2+' </td> </tr> <tr> <td> '+assign3+' </td> <td> '+score3+' </td> </tr> <tr> <td> '+assign4+' </td> <td> '+score4+' </td> </tr> <tr> <td> '+assign5+' </td> <td> '+score5+' </td> </tr> </table>';

var bodyHTML3 = '<p>Sent by the <a href="http://www.steegle.com/">Steegle.com</a> Contact Us Form Google Apps Script</p>';
var advancedArgs = {htmlBody:bodyHTML1+bodyHTML2 ,};
var pinloc = sheet.getRange("G2");
var pin = pinloc.getValue();

if(pin == 1){       
MailApp.sendEmail(recipient, subject, body, advancedArgs);
}else{
MailApp.sendEmail(recipient, subject, errmess);
}
}
4

1 に答える 1