Skip to content

Commit

Permalink
工数割合を表示するよう変更
Browse files Browse the repository at this point in the history
  • Loading branch information
tsukuboshi committed Mar 28, 2024
1 parent 936c95f commit f713ec0
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/example-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function main(
writeHeaderColumn(currentMonthSheet);

// イベントをシートに書き込む
writeEventsToSheetByColor(
writeEventsToSheet(
allEvents,
currentMonthSheet,
calendarNames,
Expand All @@ -85,7 +85,7 @@ export function main(
const workHoursMap = calculateWorkHoursByColor(allEvents);

// 計算した工数をシートに記載
const dataRange = writeWorkHoursToSheet(
const dataRange = writeSummaryToSheet(
currentMonthSheet,
workHoursMap,
colorMap,
Expand Down Expand Up @@ -165,6 +165,7 @@ function writeHeaderColumn(sheet: GoogleAppsScript.Spreadsheet.Sheet): void {
const cellg1 = sheet.getRange('G1');
const celli1 = sheet.getRange('I1');
const cellj1 = sheet.getRange('J1');
const cellk1 = sheet.getRange('K1');

// ヘッダー行に値を設定
cella1.setValue('カレンダー名');
Expand All @@ -176,6 +177,7 @@ function writeHeaderColumn(sheet: GoogleAppsScript.Spreadsheet.Sheet): void {
cellg1.setValue('工数(h)');
celli1.setValue('ラベル名一覧');
cellj1.setValue('工数合計(h)');
cellk1.setValue('工数割合(%)');

// ヘッダー行のセルを配列にまとめる
const cells = [
Expand All @@ -188,6 +190,7 @@ function writeHeaderColumn(sheet: GoogleAppsScript.Spreadsheet.Sheet): void {
cellg1,
celli1,
cellj1,
cellk1,
];

cells.forEach(cell => {
Expand All @@ -210,7 +213,7 @@ function writeHeaderColumn(sheet: GoogleAppsScript.Spreadsheet.Sheet): void {
}

// イベントをシートに書き込む関数
function writeEventsToSheetByColor(
function writeEventsToSheet(
events: GoogleAppsScript.Calendar.CalendarEvent[],
sheet: GoogleAppsScript.Spreadsheet.Sheet,
calendarNames: string[],
Expand Down Expand Up @@ -262,7 +265,7 @@ function calculateWorkHoursByColor(
if (status === '') {
status = 'ETC';
}
// ステータスがOWNER, YES, ETCの予定のみを対象とする
// ステータスがOWNER, YESの予定のみを対象とする
if (status === 'OWNER' || status === 'YES') {
// イベントの情報を取得
const eventContent = getEventContent(event);
Expand All @@ -286,8 +289,8 @@ function calculateWorkHoursByColor(
return workHoursMap;
}

// スプレッドシートのG列に色番号、H列に計算した工数合計を記載する関数
function writeWorkHoursToSheet(
// 集計結果ををシートに記載する関数
function writeSummaryToSheet(
sheet: GoogleAppsScript.Spreadsheet.Sheet,
workHoursMap: Map<string, number>,
colorMap: { [key: string]: string },
Expand All @@ -296,6 +299,10 @@ function writeWorkHoursToSheet(
// デフォルトカラーの色番号('0')の工数をMapから削除する
workHoursMap.delete('0');

// 全工数の合計を計算する
let totalWorkHours = 0;
workHoursMap.forEach(hours => (totalWorkHours += hours));

// ヘッダー行を考慮して2行目から開始
let row = 2;

Expand All @@ -307,9 +314,13 @@ function writeWorkHoursToSheet(
// イベントの色名をラベル名に変換
const labelName = LabelMap[colorName];

// イベントの工数の割合を計算
const workPercentage = ((hours / totalWorkHours) * 100).toFixed(1);

// イベントの情報をシートに書き込む
sheet.getRange('I' + row).setValue(labelName);
sheet.getRange('J' + row).setValue(hours);
sheet.getRange('K' + row).setValue(workPercentage);
row++;
});

Expand All @@ -323,11 +334,11 @@ function createOrUpdateChart(
sheet: GoogleAppsScript.Spreadsheet.Sheet,
dataRange: GoogleAppsScript.Spreadsheet.Range
): void {
// チャートの種類を棒グラフに指定
const chartType = Charts.ChartType.BAR;
// チャートの種類を円グラフに指定
const chartType = Charts.ChartType.PIE;

// チャートの位置をL列(12列目)に設定
const column = 12;
// チャートの位置をM列(13列目)に設定
const column = 13;

// シートから既存のチャートを取得
const charts = sheet.getCharts();
Expand Down

0 comments on commit f713ec0

Please sign in to comment.