見出し画像

【GAS】Google Apps Script 活用事例 Wantedlyの応募者情報をスプレッドシートに書き出すために使ったスクリプト

おいおい、それマジで言っていますか?

応募者のCSVエクスポートがなかったので、スクリーンショットを撮って、その画像をGoogle Driveにアップロードし、Googleドキュメントで開くと、OCRで画像内の文字列をかなり正確に転記してくれます。

今回は読み取った名前と年齢をスプレッドシートに転記するという一連の作業を自動化してみました。

function myFunction() {
  const url      = 'https://docs.google.com/document/d/*******************';
  const document = DocumentApp.openByUrl(url);
  const body     = document.getBody();
  const text     = body.getText();
  // console.log(text);

  const array = text.split('\n');
  console.log(array);

  let newValues = []

  for(const row of array){
    const match = row.match(/\.*\s(.*)/);
  
    if(match !== null && row.includes('からの応募')){
      console.log(match);
      const name = row.replace(/\(.*/, '').replace(/からの応募.*/, '');
      console.log(`応募者名:${name}`);

      const age = match[0].replace(/\).*/, '').replace(/.*\(/, '');
      console.log(`年齢:${age}`);

      newValues.push([name, age]);
    }
  }

  console.log(newValues);

  //複雑な処理になる場合は別関数に分けてもいいかも
  const sheetUrl  = 'https://docs.google.com/spreadsheets/d/*****************';
  const sheet     = SpreadsheetApp.openByUrl(sheetUrl).getSheetByName('Wantedly');
  const targetRow = sheet.getLastRow() + 1;
  sheet.getRange(targetRow, 1, newValues.length, newValues[0].length).setValues(newValues);
}

いや、マジでCSVのエクスポートくらい用意しろよな…..。

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