Skip to content

Commit

Permalink
sync calendar作成
Browse files Browse the repository at this point in the history
  • Loading branch information
k1tikurisu committed Jan 12, 2025
1 parent 6b42ae6 commit 85d4d05
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 0 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/deploy-sync-calendar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Build & Deploy Sync Calendar

on:
push:
branches:
- main
paths:
- packages/sync-calendar/**
workflow_dispatch:

jobs:
deploy:
runs-on: ubuntu-22.04
timeout-minutes: 15
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js 20.11.0
uses: actions/setup-node@v4
with:
node-version: '20.11.0'

- name: Install Packages
run: |
corepack enable
yarn workspaces focus sync-calendar
- name: Create .clasprc.json
run: |
echo "${{ secrets.CLASP_RC_BASE64 }}" | base64 --decode > ~/.clasprc.json
- name: Create .clasp.json
run: |
cat > ./packages/sync-calendar/.clasp.json <<EOF
{
"scriptId": "${{ secrets.SYNC_CALENDAR_SCRIPT_ID }}",
"rootDir": "./dist"
}
EOF
- name: Build & Deploy
run: |
yarn workspace sync-calendar push
14 changes: 14 additions & 0 deletions packages/sync-calendar/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"timeZone": "Asia/Tokyo",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Calendar",
"version": "v3",
"serviceId": "calendar"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
10 changes: 10 additions & 0 deletions packages/sync-calendar/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { GasPlugin } = require("esbuild-gas-plugin");

require("esbuild")
.build({
entryPoints: ["src/main.ts"],
bundle: true,
outfile: "dist/main.js",
plugins: [GasPlugin],
})
.catch(() => process.exit(1));
17 changes: 17 additions & 0 deletions packages/sync-calendar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "sync-calendar",
"version": "0.0.0",
"scripts": {
"build": "node build.js && cp appsscript.json dist/",
"push": "yarn build && clasp push"
},
"devDependencies": {
"@google/clasp": "2.4.2",
"@types/google-apps-script": "1.0.77",
"esbuild": "0.19.6",
"esbuild-gas-plugin": "0.8.0"
},
"dependencies": {
"utils": "workspace:*"
}
}
75 changes: 75 additions & 0 deletions packages/sync-calendar/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
function main() {
// スプレッドシートのIDとシート名を設定
const spreadsheetId = "1-Hy-8r_Qoruq_LwpIg1Uxwn0mlixL_X40wXXgW3RHCA";
const sheetName = "シート1"; // シート名を入力

// スプレッドシートを開いてデータを取得
const sheet =
SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName);
const data = sheet?.getDataRange().getValues(); // シートの全データを取得

// ヘッダーを除くデータ部分を処理
const rows = data?.slice(1); // 1行目(ヘッダー)を除外

if (rows === undefined) {
return null;
}

for (const row of rows) {
const name = row[0]; // 名前
const workCalendarId = row[1]; // org_id
const personalCalendarId = row[2]; // personal_id

try {
// 同期対象期間(過去1日から未来30日まで)
const now = new Date();
const startTime = new Date(now.getTime() - 24 * 60 * 60 * 1000); // 過去1日
const endTime = new Date(now.getTime() + 30 * 24 * 60 * 60 * 1000); // 未来30日

// カレンダーオブジェクトを取得
const workCal = CalendarApp.getCalendarById(workCalendarId);
const personalCal = CalendarApp.getCalendarById(personalCalendarId);

Logger.log(workCal);
Logger.log(personalCal);
if (!workCal || !personalCal) {
throw new Error("カレンダーが見つかりません");
}

// 個人カレンダーの予定を取得
const personalEvents = personalCal.getEvents(startTime, endTime);

// 社用カレンダーの予定を確認して既存の予定を取得
const workEvents = workCal.getEvents(startTime, endTime);

// 社用カレンダーにコピー済みの予定を識別するための識別子(タイトルにタグを追加)
const SYNC_TAG = "[Synced]";

// 既存の社用カレンダーの予定タイトルを収集
const workEventTitles = new Set(
workEvents.map((event) => event.getTitle()),
);

// 個人カレンダーの予定を社用カレンダーにコピー
for (const event of personalEvents) {
const title = `${SYNC_TAG} ${event.getTitle() || "プライベート予定"}`;

// 同じタイトルの予定が既に社用カレンダーに存在するか確認
if (workEventTitles.has(title)) {
return; // 既存の場合はスキップ
}

// 社用カレンダーに予定を作成
workCal.createEvent(title, event.getStartTime(), event.getEndTime(), {
description: "この予定は個人用カレンダーから同期されています。",
visibility: CalendarApp.Visibility.PRIVATE,
});
}

// ログに出力(デバッグ用)
Logger.log(`同期完了: ${name}`);
} catch (error) {
Logger.log(`エラー発生 (${name}): ${error}`);
}
}
}

0 comments on commit 85d4d05

Please sign in to comment.