GAS

Google Apps Script

雲端Google試算表應用

表單成績更新回學生平時成績

建立平時測驗表單,完成測驗後在「回覆」內『在試算表中查看』(成績結果:測驗成績試算表)。

於「測驗成績試算表」內,撰寫GAS程式如下:

function updateScores() {

  var testSheetId = "測驗成績試算表ID"// 表單測驗試算表ID

  var testSheet = SpreadsheetApp.openById(testSheetId).getSheetByName("表單回應 1"); //工作表:表單回應 1

 

  var studentSheetId = "學生資料試算表ID"// 學生資料試算表ID

  var studentSheet = SpreadsheetApp.openById(studentSheetId).getSheetByName("班級工作表名稱"); //一班一個工作表

 

  var testEmails = testSheet.getRange("B:B").getValues();  //取得測驗試算表的電子郵件欄位(二維)

  var testScores = testSheet.getRange("C:C").getValues();  //取得測驗試算表的測驗分數欄位(二維)

 

  var studentEmails = studentSheet.getRange("F:F").getValues();  //取得學生資料試算表的電子郵件欄位(欄位F)

  var studentScores = studentSheet.getRange("U:U");  //取得學生資料試算表的平時成績欄位(欄位U)

 

  var lastRow = studentSheet.getLastRow();  // 取得學生資料試算表最後一列的索引

 

  // 將測驗分數填入學生資料試算表的平時成績欄位

  var scoreUpdated = 0;

  for (var i = 2; i <= lastRow; i++) { //學生資料試算表:row從1開始、第二列是2。

    var studentEmail = studentEmails[i - 1][0];

    for (var j = 2; j <= testEmails.length; j++) {

      var testEmail = testEmails[j - 1][0]; //二維陣列,第一筆索引從0開始。

      if (studentEmail === testEmail) {

        var testScore = testScores[j - 1][0];

        studentScores.getCell(i, 1).setValue(testScore); //學生資料試算表

        scoreUpdated++;

        break;

      }

    }

  }


  Logger.log("已完成"+ scoreUpdated + "筆成績更新。");

}


以上由chatGPT熱情贊助。(重點是要看得懂它在寫什麼)


參考文獻 Google Apps Script Workspace

https://developers.google.com/google-ads/scripts/docs/features/dates?hl=zh-tw (官網指令)


https://developers.google.com/apps-script/reference/spreadsheet/range?hl=zh-tw

getRange、getDataRange: https://ithelp.ithome.com.tw/m/articles/10266813

必看:https://hackmd.io/@kaoshou/rJyqMASvs (資料豐富,舉例甚多。)