【GAS】スプレッドシートにGmailからスクレイピング~スプレッドシートへの書き込み~【第7回】

2022/08/10

前回は、データを書き込むスプレッドシートを作りました。今回は、実際にスプレッドシートに書き込んでみたいと思います。

目次

今回やること

スプレッドシートに実際に書き込みます。

スプレッドシートを読み込む

スプレッドシートを読み込むには、スプレッドシートのIDとシート名が必要です。

const st = SpreadsheetApp.openById('ID’).getSheetByName('シート名’);

スプレッドシートのIDは、スプレッドシートのURLを見ればわかります。

https://docs.google.com/spreadsheets/d/スプレッドシートのID/edit#gid=0

要素が入っている最後の行番号を取り出す

要素が入っている最後の行番号を取り出します。関数が入っているとさらに下から番号を取り出すので注意が必要です。

st.getLastRow();

行列を指定してセルの中身を取り出す

行と列を指定するとセルの中身を取り出せます。

st.getRange(行,列);

セルの要素を置き換える

セルの中身を取り出すgetRangeも使うと、セルの要素の置き換えができます。

st.getRange(行,列).setValue(置き換える要素);

配列.pushで配列の要素数を取り出す

配列.push();で配列の要素数を取り出せます。書き込み時の繰り返しの回数を決める時に便利です。

GAS全体

function myFunction() {
  
  const query ='さんの脳トレ記録をお届けします。';
  const start = 0;
  const max = 10;
  const threads = GmailApp.search(query,start,max);
  const st = SpreadsheetApp.openById('スプレッドシートのID').getSheetByName('シート1');
  const st2 = SpreadsheetApp.openById('スプレッドシートのID').getSheetByName('シート2');

  const fetchData = (str ,pre ,suf) =>{
    const reg = new RegExp(pre + '.*?' + suf,'g');
    const preg = new RegExp(pre,'g');
    const sreg = new RegExp(suf,'g');
    var data = str.match(reg);
    var data1 = null;
    if(data !== null){
      data1 = data[0].replace(preg,'').replace(sreg,'');
    }
    return data1;
  };
  const fetchData1 = (str ,pre ,suf) =>{
    const reg = new RegExp(pre + '*?' + suf,'g');
    const preg = new RegExp(pre,'g');
    const sreg = new RegExp(suf,'g');
    var data = str.match(reg);
    var data1 = null;
    if(data !== null){
      data1 = data[0].replace(preg,'').replace(sreg,'');
    }
    return data1;
  };

  const moment = (str1) =>{
    const minute = str1.replace('分',':').replace('秒','.');
    return minute;
  };

  var date10 = [['日付','経過日数','脳年齢','抑制力','処理速度','短期記憶','計算100','人数数え','ID']];
  var calc1001 = null;
  var people1 = null;
  var calc250 = null;
  var double_task1 = null;
  var kanji1 = null;
  var last_photo1 = null;
  var finger_calc1 = null;
  var finger_exe1 = null;

  for(const thread of threads){
    const messages = GmailApp.getMessagesForThread(thread);

    for(const message of messages){
      const plainBody = message.getPlainBody();
      const id = message.getId();
      const continuity = fetchData(plainBody,'脳トレ\\s','日目');
      const date = fetchData(plainBody,'日目\\s','(');
      const ba = fetchData(plainBody,'今回:脳年齢\\s','才');
      const ba_suppression =fetchData(plainBody,'・抑制力\\s','才');
      const ba_process = fetchData(plainBody,'・処理速度\\s','才');
      const ba_memory = fetchData(plainBody,'・短期記憶\\s','才');

      const calc100 = fetchData(plainBody,'計算100\\s・','(');
      const calc25 = fetchData(plainBody,'計算25\\s・','(');
      const ns_read =fetchData(plainBody,'新聞音読\\s・','音');
      const song_perf = fetchData(plainBody,'名曲演奏\\s・','点(');
      const people = fetchData(plainBody,'人数数え\\s・','/6問');
      const kanji = fetchData(plainBody,'漢字合成\\s・','(')
      const last_photo = fetchData(plainBody,'直前写真\\s・','(');
      const ins_memory = fetchData(plainBody,'瞬間記憶\\s・','個(');
      const finger_calc = fetchData(plainBody,'指計算\\s・','(');
      const finger_exe = fetchData(plainBody,'指体操\\s・','(');
      const double_task = fetchData(plainBody,'二重課題\\s・','(');
      const sudoku_class = fetchData(plainBody,'数独(',')');
      const sudoku_art = fetchData(plainBody,'・','問クリア');
      const anecdote = fetchData1(plainBody,'川島教授の脳の小話\\s[\\s\\S]','\\s\\s\\s\\s')

      if(calc25 !== null){
        calc250 = moment(calc25);
      }
      else{calc250=null;}
      if(calc100 !== null){
        calc1001 = moment(calc100);
      }
      else{calc1001 = null;}
      if(people !== null){
        people1 = moment(people);
      }
      else{people1 = null;}
      if(kanji !== null){
        kanji1 = moment(kanji);
      }
      else{kanji1 = null;}
      if(last_photo !== null){
        last_photo1 = moment(last_photo);
      }
      else{last_photo1 = null;}
      if(finger_calc !== null){
        finger_calc1 = moment(finger_calc);
      }
      else{finger_calc1 = null;}
      if(finger_exe !== null){
        finger_exe1 = moment(finger_exe);
      }
      else{finger_exe1 = null;}
      if(double_task !== null){
        double_task1 = moment(double_task);
      }
      else{double_task1=null;}
      date10.push([date,continuity,ba,ba_suppression,ba_process,ba_memory,calc1001,calc250,ns_read,song_perf,people1,kanji1,last_photo1,ins_memory,finger_calc1,finger_exe1,double_task1,sudoku_class,sudoku_art,id,anecdote]);
    }
  }
  date10.sort(function(a,b){
    return new Date(a[0]) - new Date(b[0]);
  });
  console.log(date10);
  var j = date10.push();
  console.log(j);
  for(let k=1;k<j;k++){
    const lastrow = st.getLastRow()+1;
//日付
    st.getRange(lastrow,1).setValue(date10[k][0]);
//経過日数
    st.getRange(lastrow,2).setValue(date10[k][1]);
//脳年齢
    st.getRange(lastrow,4).setValue(date10[k][2]);
//脳年齢-抑制力-処理速度-短期記憶
    st.getRange(lastrow,5).setValue(date10[k][3]);
    st.getRange(lastrow,6).setValue(date10[k][4]);
    st.getRange(lastrow,7).setValue(date10[k][5]);
//トレーニング結果-計算100-計算25-新聞音読-名曲演奏
    st.getRange(lastrow,9).setValue(date10[k][6]);
    st.getRange(lastrow,11).setValue(date10[k][7]);
    st.getRange(lastrow,12).setValue(date10[k][8]);
    st.getRange(lastrow,13).setValue(date10[k][9]);
//トレーニング-人数数え-漢字合成-直前写真-瞬間記憶
    st.getRange(lastrow,14).setValue(date10[k][10]);
    st.getRange(lastrow,16).setValue(date10[k][11]);
    st.getRange(lastrow,18).setValue(date10[k][12]);
    st.getRange(lastrow,20).setValue(date10[k][13]);
//トレーニング-指計算-指体操-二重課題-数独級-数独数-GmailID
    st.getRange(lastrow,21).setValue(date10[k][14]);
    st.getRange(lastrow,23).setValue(date10[k][15]);
    st.getRange(lastrow,25).setValue(date10[k][16]);
    st.getRange(lastrow,26).setValue(date10[k][17]);
    st.getRange(lastrow,27).setValue(date10[k][18]);
    st.getRange(lastrow,28).setValue(date10[k][19]);

    const lastrow1 = st2.getLastRow()+1;
    st2.getRange(lastrow1,1).setValue(date10[k][20]);
  }
}

 

実行結果

実行してみると、スプレッドシートに書き込みはされました。しかし、もともと入力されていたデータもあったので同じ日付があります。

また、複数回実行すると全く同じデータが何度も書きこまれます。

最後に

今回は、スプレッドシートに書き込みまでしてみました。問題点は、重複して書き込まれてしまうところですね。

また、関数を下の行までいれてしまうと、正しい位置に書き込まれなくなります。

次回は、重複して書き込まれないように対策をしたいと思います。