GAS プラクティス

# クラスを使用する
## 例
slackのレスポンスからメンバー情報をつくる

```javascript:class/Member.gs
class Member{
  constructor(res, ws){
    this.ws = ws.name;
    this.id = res.id;
    this.name = {
      display : res.profile.display_name,
      real : res.real_name,
    };
    this.email = res.profile.email;
    this.status = getStatus(res);
  }

  getName(){
    const name = (this.name.display) ? this.name.display : this.name.real;
    return name;
  }

  getOutList(){
    return [
      this.ws,
      this.id,
      this.name.real,
      this.name.display,
      this.email,
      this.status
    ];
  }
}
```

# IDやパスワードはjsonファイルに記載する

```javascript:config.gs
var CONFIG_FILE_ID = ‘(jsonファイルのファイルID)’;

function readConfig(){
  const file = DriveApp.getFileById(CONFIG_FILE_ID);
  const data = file.getBlob().getDataAsString();
  return JSON.parse(data);
}
```



# slackに送信する

```javascript:slack.gs
function slackChannel(webhook, message) {
  const jsonData = {
    'text' : message
  };

  const options = {
    'method' : 'post',
    'contentType' : 'application/json',
    'payload' : JSON.stringify(jsonData)
  };

  UrlFetchApp.fetch(webhook, options);
}

function slackDM(token, id, text){

  const url = 'https://slack.com/api/chat.postMessage';
  const option = {
    method: 'POST',
    payload : {
      token: token,
      channel: '@' + id,
      text: text,
    },
  };

  UrlFetchApp.fetch(url, option);
}

function getSlackReactions(token, link){

  const {channel, timestamp} = getContentsFromSlackLink(link);

  const url = 'https://slack.com/api/reactions.get'
    + '?token=' + token
    + '&channel=' + channel
    + '&timestamp=' + timestamp
    + '&full=true'
  ;
  
  let res = UrlFetchApp.fetch(url, {muteHttpExceptions:true});
  return JSON.parse(res);
}

function getContentsFromSlackLink(link){
  link = link.replace(/https(.*?)archives\//, '');
  Logger.log(link);
  link = link.split('/');
  const channel = link[0];
  let timestamp = link[1].replace('p', '');
  const timestamp1 = timestamp.substr(0, 10);
  const timestamp2 = timestamp.substr(10);
  timestamp = timestamp1 + '.' + timestamp2;
  return {channel, timestamp};
}
```

# spreadsheet関係

```javascript:sheet.gs

function getSheetData(sheetConfig, spreadSheetId){
  let data = getSheetDataFull(sheetConfig, spreadSheetId);
  [...Array(sheetConfig.row.data - 1)].forEach(_ => data.shift());
  return data;
}

function getSheetDataFull(sheetConfig, spreadSheetId){
  const ss = (spreadSheetId !== undefined) ? SpreadsheetApp.openById(spreadSheetId) : SpreadsheetApp.getActive();
  const sheet = ss.getSheetByName(sheetConfig.name);
  return sheet.getDataRange().getValues();
}

function setText(sheetConfig, row, column, text){
  setList(sheetConfig, row, column, [[text]]);
}

function setList(sheetConfig, row, column, list){
  if(!list.length) return;
  const sheet = SpreadsheetApp.getActive().getSheetByName(sheetConfig.name);
  sheet.getRange(row, column, list.length, list[0].length).setValues(list);
}

function setListAnotherSheet(spreadSheetId, sheetConfig, row, column, list){
  if(!list.length) return;
  const sheet = SpreadsheetApp.openById(spreadSheetId).getSheetByName(sheetConfig.name);
  sheet.getRange(row, column, list.length, list[0].length).setValues(list);
}

function refreshSheet(sheetName, outList, startColumn, startRow){
  
  if(!outList[0].length) return;
  const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);

  startRow = startRow ? startRow : 2;
  startColumn = startColumn ? startColumn : 1;
  sheet.getRange(startRow, startColumn, sheet.getLastRow(), outList[0].length).clear();
  sheet.getRange(startRow, startColumn, outList.length, outList[0].length).setValues(outList);
}

function addSheet(sheetConfig, column, list){
  if(!list.length) return;
  const sheet = SpreadsheetApp.getActive().getSheetByName(sheetConfig.name);
  sheet.insertRows(sheetConfig.row.data, list.length);
  sheet.getRange(sheetConfig.row.data, column, list.length, list[0].length).setValues(list);
}

function addSheetLastRow(sheetConfig, list, column){
  if(!list.length) return;
  const sheet = SpreadsheetApp.getActive().getSheetByName(sheetConfig.name);

  sheet.getRange(
    sheet.getLastRow() + 1,
    column !== undefined ? column : 1,
    list.length,
    list[0].length
  ).setValues(list);
}

```