0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[GAS] Backlogタスクの定期作成

Last updated at Posted at 2024-12-25

Backlog のタスク定期自動作成を GAS を使って実現した

1. スプレッドシートの準備

まず、スプレッドシートにタスクの情報を入力します。以下のような形式にします。

タスクのタイトル タスクの詳細 起動条件1(月) 起動条件2(日)
◯◯の更新(#YY年#MM月次) タスクの詳細説明 プルダウン(毎月 or 3,6,9,12月) 25

2. GAS の作成

スプレッドシートに関連付けた Google Apps Script(GAS)を使って、定期的にタスクを自動で作成するスクリプトを作成します。

/**
 * Backlogタスクの定期作成(Googleスプレッドシートの一覧を元に生成)
 */
const API_KEY = 'MY_API_KEY';  // 自分のAPIキー
const PROJECT_ID = 'MY_PROJECT_ID';  // 自分のプロジェクトID
const SPREADSHEET_ID = 'MY_GOOGLE_SPREAD_SHEET_ID';  // スプレッドシートID
const SHEET_NAME = 'Googleスプシの画面下にあるシート名';  // シート名

// メイン関数:一覧走査 & タスク生成
function createTasksFromSpreadsheet() {
  const sheet = SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName(SHEET_NAME);
  const data = sheet.getDataRange().getValues();

  const currentYear  = new Date().getFullYear();
  const currentMonth = new Date().getMonth() + 1;
  const currentDay   = new Date().getDate();

  // ヘッダー行をスキップ(行方向は1オリジンでループ)
  let title, desc, cond_1, cond_2
  for (let ii = 1; ii < data.length; ii++) {

    title  = data[ii][0];  // A列: タスク名
    title  = title.replace('#YY', currentYear);  // #YY を currentYear  に置き換え
    title  = title.replace('#MM', currentMonth); // #MM を currentMonth に置き換え
    desc   = data[ii][1];  // B列: 説明

    cond_1 = data[ii][2];  // D列: 起動条件1
    cond_2 = data[ii][3];  // E列: 起動条件2

    console.log("[" + ii + "] タスク名:" + title);
    console.log(" 起動条件1:" + cond_1);
    console.log(" 起動条件2:" + cond_2 + "");

    if (cond_1 === '毎月' || (cond_1==='3,6,9,12月' && [3,6,9,12].includes(currentMonth))) {
      console.log(ii + "件目のタスクを処理する月です!");
      if (cond_2===currentDay) {
        console.log(ii + "件目のタスクを処理する日です!■ タスクを生成します ■");
        // API によるタスク作成
        createBacklogTask(title, desc);
      }
    }
  }
}

// Backlog へのタスク作成
function createBacklogTask(title, desc) {
  const url = 'https://pettyl.backlog.com/api/v2/issues?apiKey=' + API_KEY;
  
  const payload = {
    "projectId": PROJECT_ID,
    "summary": title,
    "issueTypeId": 2531668, // 課題種別ID(2531668はタスク)
    "description": desc,
    "priorityId": 3  // 優先度ID(例: 3=中)
  };

  const options = {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer ' + API_KEY,
      'Content-Type': 'application/json'
    },
    payload: JSON.stringify(payload)
  };

  try {
    const response = UrlFetchApp.fetch(url, options);
    const jsonResponse = JSON.parse(response.getContentText());
    console.log('タスク作成成功: ', jsonResponse);
  } catch (error) {
    console.error('タスク作成エラー: ', error);
  }
}
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?