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);
}
}