現在、集団面接のイベントを行っているのですが、その際に参加する学生が予め入力した志望先に会わせて面接のスケジュールを組むシステムを組んでおります。
その際、第一志望に選択した企業と第二志望に選択した企業との面接時間が被る問題が生じました。
列1 | 列2 | 列3 |
---|---|---|
氏名 | 第一志望 | 第二志望 |
山田太郎 | 山田証券 | 佐藤運輸 |
鈴木花子 | 佐藤運輸 | 愛知重工 |
佐藤次郎 | アスキー商会 | 山田証券 |
井上洋子 | 愛知重工 | アスキー商会 |
列1 | 列2 |
---|---|
企業名 | |
山田証券 | |
佐藤運輸 | |
アスキー商会 | |
愛知重工 |
GAS
1const ss = SpreadsheetApp.getActiveSpreadsheet(); 2 3class Company{ 4 constructor(name){ 5 this.name = name; 6 this.timeTable = { 7 1:"", 8 2:"", 9 3:"", 10 4:"" 11 } 12 } 13 setTimeTable(persons){ 14 15 const rows = []; 16 for(let i = 0; i < persons.length; i++){ 17 if(persons[i].req["1"] === this.name || persons[i].req["2"] === this.name){ 18 rows.push(persons[i]); 19 } 20 } 21 const table = [1, 2, 3, 4]; 22 table.sort((a, b)=>{ 23 a = Math.floor(Math.random() * 4) + 1; 24 b = Math.floor(Math.random() * 4) + 1; 25 return b - a; 26 }); 27 for(let i = 0; i < 2; i++){ 28 const num = table.pop(); 29 this.timeTable[num] = rows[i].name; 30 } 31 } 32} 33 34class Person { 35 constructor(name, req){ 36 this.name = name; 37 this.req = { 38 1:req[0], 39 2:req[1] 40 } 41 } 42} 43 44function myFunction() { 45 const persons = compilePerson("学生リスト"); 46 const companies = compileCompany("企業リスト"); 47 for(company of companies){ 48 const p = persons.filter((p)=>{ 49 if(p.req["1"] === company.name){ 50 return true; 51 }else if(p.req["2"] === company.name){ 52 return true; 53 }else{ 54 return false; 55 } 56 }); 57 company.setTimeTable(p); 58 } 59 60 inputTimeTable("スケジュール", companies); 61} 62 63const compilePerson = (shName) =>{ 64 const sh = ss.getSheetByName(shName); 65 const values = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getValues(); 66 values.shift(); 67 return values.map((p)=>{return new Person(p[0], [p[1], p[2]])}); 68} 69 70const compileCompany = (shName) => { 71 const sh = ss.getSheetByName(shName); 72 const values = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getValues(); 73 values.shift(); 74 return values.map((c)=>{return new Company(c[0]);}); 75} 76 77const inputTimeTable = (shName, companies) =>{ 78 const ROW = 2; 79 const COL = 3; 80 const sh = ss.getSheetByName(shName); 81 82 for(let i = 0; i < companies.length; i++){ 83 for(let j = 0; j < 4; j++){ 84 const num = j + 1; 85 sh.getRange(ROW + j, COL + i).setValue(companies[i]["timeTable"][num]); 86 } 87 } 88}
しかし、これだとランダムに設定しているのであって、偶に重複することがありました。
実行した結果
列1 | 列2 | 列3 | 列4 | 列5 | 列6 |
---|---|---|---|---|---|
No | タイムテーブル | 山田証券 | 佐藤運輸 | アスキー商会 | 愛知重工 |
1 | 9:30~9:45 | 井上洋子 | 井上洋子 | ||
2 | 9:50~10:05 | 鈴木花子 | |||
3 | 10:10~10:25 | 山田太郎 | 山田太郎 | ||
4 | 10:30~10:45 | 佐藤次郎 | 鈴木花子 | 佐藤次郎 |
今回のデータはテスト用のためデータ数が少ないのですが、実際に使用するデータでは企業が15社前後、学生が30名前後を想定しております。
同じ行にデータが被った際、再計算する仕組みを組みたいのですが、どのようにソースを改修すればよろしいでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。