見出し画像

【GAS】Google Apps Script 活用事例 学校名から大学院か大学かを自動で判別するスクリプト

データの取得先

以前、大学名の一覧、DB的なものがあったら便利だなと思って作りました。今回はこちらのシートを使って学校名から大学院、大学、専門学校などを取得するスクリプトを書きました。

学校名から大学院、大学、専門学校かを取得するスクリプト

function generateSchoolTypeValues(){
  //全国大学一覧_2020_08
  const url         = 'https://docs.google.com/spreadsheets/d/15h9_v-GrwJKfaUeCBNhKp7MZ6pt5BIDQkVB3084VtCM/edit#gid=0';
  const spreadsheet = SpreadsheetApp.openByUrl(url);
  const sheet       = spreadsheet.getSheetByName('DB');
  const values      = sheet.getDataRange().getValues();
  const header      = values[0];

  console.log(`スプレッドシート名: ${spreadsheet.getName()}`);

  let newValues = [];

  const column = {
    universityName: header.indexOf('大学名'),
    url:            header.indexOf('URL'),
    address:        header.indexOf('住所'),
  }

  console.log(column);
  values.shift();

  for(let i = 0; i < values.length; i++){
    if(!values[i][0]) continue;
    
    const type = getSchoolType_(values[i][column.universityName]);
    console.log(`${values[i][column.universityName]} --> ${type}`);

    newValues.push([type]);
  }
  console.log(newValues);
}



/**
 * 学校名から大学院、大学、専門学校かを取得する
 * (例)千葉大学大学院 --> 大学院
 * (例)東京大学           --> 大学
 * 
 * @param  {string} string - 千葉大学大学院
 * @return {string} 大学院 or 大学 or 専門学校
 * 
 */
function getSchoolType_(string){
  let university = '';
  
  switch (true) {
    case /.*大学院.*/.test(string):
      university = string.match(/大学院/)[0];
      console.log(`結果: ${university}`);
      break;
      
    case /.*大学.*/.test(string):
      university = string.match(/大学/)[0];
      console.log(`結果: ${university}`);
      break;  
      
    case /.*専門学校.*/.test(string):
      university = string.match(/専門学校/)[0];
      console.log(`結果: ${university}`);
      break; 

    case /.*university.*|.*University.*/.test(string):
      university = '大学';
      console.log(`結果: ${university}`);
      break;  
      
    default:
      //HALなどを想定したパターン
      university = '専門学校'; 
      console.warn('どのパターンにも該当しませんでした。')
      console.log(`結果: ${university}`);
      break;

  }//switch
  return university
}//end

この記事が気に入ったらサポートをしてみませんか?