LoginSignup
0
0

More than 1 year has passed since last update.

【GAS】ドキュメントファイルをGASでコピーしてChatWorkで通知するアレコレ

Last updated at Posted at 2022-06-15

概要

「毎週ドキュメントファイルをコピーして通知する作業を自動化したい」という要望があったので、パッと作ってみた

ご利用される環境にあわせて変更ください

const mainWorker = () => {
  const mtgDate = getMtgDate()
  const previousMtgDate = getMtgDate(true)

  const SHEET_NAME = " ファイル名"
  const previousMtgName = previousMtgDate + SHEET_NAME
  const mtgName = mtgDate + SHEET_NAME
  const TEMPLATE_NAME = "2022年度テンプレート"

  const DIRECTORY_ID = "複製したいファイルのあるディレクトリID"
  const TEMPLATE_DIRECTORY_ID = "テンプレートファイルのあるディレクトリID"
  const directory = DriveApp.getFolderById(DIRECTORY_ID)
  const templateDirectory = DriveApp.getFolderById(TEMPLATE_DIRECTORY_ID)

  const previousFile = directory.getFilesByName(previousMtgName)
  const templateFile = templateDirectory.getFilesByName(TEMPLATE_NAME)
  const result = previousFile.hasNext() ? copyFile(previousFile, directory, mtgName) : copyFile(templateFile, directory, mtgName)

  if (result) {
    const url = directory.getFilesByName(mtgName).next().getUrl()
    const message = "[toall]\n作成しましたよ!\n" + url;
    sendChatWork(message);
  }
}

/**
 * https://github.com/cw-shibuya/chatwork-client-gas
 */
const sendChatWork = (message) => {
  const client = ChatWorkClient.factory({ token: "API Token" });
  client.sendMessage({ room_id: 通知したいルームID, body: message });
}


const copyFile = (copyFile, directory, mtgName) => {
  if (!copyFile.hasNext()) {
    sendChatWork("テンプレートファイルが見つかりません。")
    return;
  }

  const file = copyFile.next();

  // 重複しない場合だけ作成
  if (!directory.getFilesByName(mtgName).hasNext()) {
    file.makeCopy(mtgName, directory)
    return true;
  }
  else {
    sendChatWork("重複するファイルが存在します")
    return;
  }
}

const getMtgDate = (isPrevious) => {
  let date = new Date()
  isPrevious ? date.setDate(date.getDate() - 7) : date.setDate(date.getDate() + 1)
  return Utilities.formatDate(date, 'JST', 'YYYY/MM/dd');
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0