-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.gs
151 lines (129 loc) · 5.29 KB
/
code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function updateGraph() {
var ss = SpreadsheetApp.openById("SpreadSheet ID");
var sheet = ss.getSheetByName('Form Sheet Name'); // フォームの回答が記録されているシート名を指定
var data = sheet.getDataRange().getValues();
// 新しく追加:メールアドレスリストの取得
var emailSheet = ss.getSheetByName(' EmailAddress Sheet Name'); // メールアドレスが記録されているシート名を指定
var emailData = emailSheet.getRange('A:A').getValues(); // メールアドレスが記録されている列を指定
var validEmails = emailData.map(function(row) { return row[0]; });
//エントリーNo.を入力
var entryNo = 1;
//グラフを事前に削除
var charts = ss.getSheetByName('Graph Sheet Name').getCharts();// グラフを表示するシート名を指定
for (var j = 0; j < charts.length; j++) {
sheet.removeChart(charts[j]);
}
var processedData = {};
var allEntriesData = {};
for(var i = data.length - 1; i >= 1; i--) {
var row = data[i];
var email = row[1];
// 新しく追加:メールアドレスがリストに存在するか確認
if (validEmails.indexOf(email) < 0) continue;
var entry = row[2];
var creativity = row[3];
var interest = row[4];
var effort = row[5];
var clarity = row[6];
var design = row[7];
if(!allEntriesData.hasOwnProperty(email)) {
allEntriesData[email] = {};
}
allEntriesData[email][entry] = [creativity, interest, effort, clarity, design];
if(entry == entryNo && (!processedData.hasOwnProperty(email) || !processedData[email].hasOwnProperty(entry))) {
if(!processedData.hasOwnProperty(email)) {
processedData[email] = {};
}
processedData[email][entry] = [creativity, interest, effort, clarity, design];
}
}
var categories = ['創造性', '面白さ', '工夫', '分かりやすさ', 'デザイン性']; //評価項目
var graphData = [['カテゴリ', 'エントリーNo.' + entryNo + `の平均`, '全エントリーの平均']]; //表示するデータ群
for(var i = 0; i < categories.length; i++) {
var totalScoreForEntry = 0;
var totalCountForEntry = 0;
for(var email in processedData) {
for(var entry in processedData[email]) {
totalScoreForEntry += processedData[email][entry][i];
totalCountForEntry++;
}
}
var totalScoreForAllEntries = 0;
var totalCountForAllEntries = 0;
for(var email in allEntriesData) {
for(var entry in allEntriesData[email]) {
totalScoreForAllEntries += allEntriesData[email][entry][i];
totalCountForAllEntries++;
}
}
if(totalCountForEntry > 0 && totalCountForAllEntries > 0) {
totalScoreForEntry /= totalCountForEntry;
totalScoreForAllEntries /= totalCountForAllEntries;
graphData.push([categories[i], totalScoreForEntry, totalScoreForAllEntries]);
}
}
var graphSheet = ss.getSheetByName('Graph Sheet Name');
if(!graphSheet) {
graphSheet = ss.insertSheet('Graph Sheet Name');
}
graphSheet.clear();
graphSheet.getRange(1, 1, graphData.length, 3).setValues(graphData);
var chart = graphSheet.newChart()
.setChartType(Charts.ChartType.LINE)
.addRange(graphSheet.getRange(2, 1, graphData.length, 3))
.setPosition(8, 1, 0, 0)
.setOption('title', '回答結果 エントリーNo.' + entryNo)
.setOption('hAxis.title', 'カテゴリ')
.setOption('vAxis.title', 'スコア')
.setOption('series', {
0: {
labelInLegend: 'エントリーNo.' + entryNo + "の平均",
},
1: {
labelInLegend: '全エントリーの平均',
},
})
.build();
graphSheet.insertChart(chart);
//Scoresシートに結果を追加
var scoresSheet = ss.getSheetByName('Scores Sheet Name');// 回答結果の平均を記録したいシート名を指定
if(!scoresSheet) {
scoresSheet = ss.insertSheet('Scores Sheet Name');
}
// Determine the row in which to write the scores. If the entry number already exists in column 1, overwrite it.
// Otherwise, use the next empty row.
var row = 2; // Start from row 2
var found = false;
while(scoresSheet.getRange(row, 1).getValue() !== "") {
if(scoresSheet.getRange(row, 1).getValue() === entryNo) {
found = true;
break;
}
row++;
}
if(!found) {
scoresSheet.getRange(row, 1).setValue(entryNo);
}
var scoresData = [];
var totalScoreSum = 0; // Sum of all totalScoreForEntry values for calculating the average
for(var i = 0; i < categories.length; i++) {
var totalScoreForEntry = 0;
var totalCountForEntry = 0;
for(var email in processedData) {
for(var entry in processedData[email]) {
totalScoreForEntry += processedData[email][entry][i];
totalCountForEntry++;
}
}
if(totalCountForEntry > 0) {
totalScoreForEntry /= totalCountForEntry;
scoresData.push(totalScoreForEntry);
totalScoreSum += totalScoreForEntry; // Add the totalScoreForEntry to the sum
}
}
var averageTotalScore = totalScoreSum / scoresData.length; // Calculate the average totalScoreForEntry
scoresData.push(averageTotalScore); // Add the average to the scoresData array
for(var i = 0; i < scoresData.length; i++) {
scoresSheet.getRange(row, i+2).setValue(scoresData[i]); // i+2 to skip the category column
}
}