# Googleカレンダーに反映させるボタンが表示されません

**URL:** <https://community.cybozu.dev/t/topic/5370>\
**Category:** kintone 開発相談\
**Created:** [October 14, 2021, 6:24am UTC](https://community.cybozu.dev/t/topic/5370 "2021-10-14T06:24:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2886](https://avatars.discourse-cdn.com/v4/letter/l/49beb7/32.png) [@Legacy\_Account2886](https://community.cybozu.dev/u/Legacy_Account2886)\
**Post date:** [October 14, 2021, 6:24am UTC](https://community.cybozu.dev/t/topic/5370/1 "2021-10-14T06:24:50Z")

</div>

[https://developer.cybozu.io/hc/ja/articles/115005646066-kintone%E3%81%AE%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88-%E3%83%95%E3%82%A7%E3%82%A2%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AE%E6%97%A5%E7%A8%8B%E3%82%92Google%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AB%E5%85%AC%E9%96%8B%E3%81%97%E3%82%88%E3%81%86-](https://community.cybozu.dev/hc/ja/articles/115005646066-kintone%E3%81%AE%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88-%E3%83%95%E3%82%A7%E3%82%A2%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AE%E6%97%A5%E7%A8%8B%E3%82%92Google%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AB%E5%85%AC%E9%96%8B%E3%81%97%E3%82%88%E3%81%86-)

このページの内容でkintoneのアプリからGoogleカレンダーへ予定の反映させる設定をおこなっていたのですが、添付画像のようなエラーが出てしまい、カレンダーへ反映させるためのボタンが表示されません。

上記ページに記載されているコード以外にgapiを定義する文が必要なのでしょうか?

コードは下記になります。(コードモードでの貼付がなぜかできませんでした)

)

(function() {  
‘use strict’;

//GCPプロジェクト名kintonetoGoogleCalendarholiday  
// API キー  
const api\_key = ‘GoogleAPIで生成したAPIキー’;  
// クライアントID  
const client\_id = ‘GoogleAPIで生成したクライアントID’;  
// カレンダーID  
const calendar\_id = ‘GoogleカレンダーID’;  
// 認証用URL（読み取り／更新）  
const scope = ‘[https://www.googleapis.com/auth/calendar](https://www.googleapis.com/auth/calendar)’;  
// Discovery Docs  
const discovery\_docs = [‘[https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest](https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest)’];

// APIクライアントライブラリの初期化とサインイン  
function initClient() {  
gapi.client.init({  
‘apiKey’: api\_key,  
‘discoveryDocs’: discovery\_docs,  
‘clientId’: client\_id,  
‘scope’: scope  
}, (error) =\> {  
alert('Googleへの認証に失敗しました。: ’ + error);  
});  
}

// APIクライアントとOAuth2ライブラリのロード  
gapi.load(‘client:auth2’, initClient);

// レコード詳細画面の表示後イベント  
kintone.events.on(‘app.record.detail.show’, (event) =\> {  
// 増殖バグ回避  
if (document.getElementById(‘publish\_button’) !== null) {  
return event;  
}  
// 画面下部にボタンを設置  
const publishButton = document.createElement(‘button’);  
publishButton.id = ‘publish\_button’;  
publishButton.innerHTML = ‘カレンダーへ反映する’;  
publishButton.className = ‘button-simple-cybozu geo-search-btn’;  
publishButton.style = ‘margin-top: 30px; margin-left: 10px;’;  
publishButton.addEventListener(‘click’, () =\> {  
publishEvent();  
});  
kintone.app.record.getSpaceElement(‘publish\_button\_space’).appendChild(publishButton);  
return event;  
});

kintone.events.on([‘app.record.create.show’, ‘app.record.edit.show’], (event) =\> {  
// フィールドを編集不可へ  
event.record.event\_id.disabled = true;  
return event;  
});

async function publishEvent() {  
// レコードのデータの取得  
const record = kintone.app.record.get().record;  
if (record) {  
// Google認証済みのチェック  
if (!gapi.auth2.getAuthInstance().isSignedIn.get()) {  
// Google認証の呼び出し  
await gapi.auth2.getAuthInstance().signIn();  
}  
// API リクエスト  
// リクエストパラメータの設定  
const params = {  
// イベントのタイトル  
‘summary’: record[‘申請者’][‘value’][0][‘name’] & record.Googleカレンダー表示用.value,

// 日付の指定  
“start”: {  
“date”: record.event\_date.value,  
“timeZone”: ‘Asia/Tokyo’  
},

“end”: {  
“date”: record.event\_date.value,  
“timeZone”: ‘Asia/Tokyo’  
},

};  
let request;  
// リクエストメソッドとパラメータの設定  
if (record.event\_id.value) { // 公開済みイベントを更新  
request = gapi.client.calendar.events.update(  
{  
‘calendarId’: calendar\_id,  
‘eventId’: record.event\_id.value,  
‘resource’: params  
});  
} else { // 未公開のイベントを追加  
request = gapi.client.calendar.events.insert(  
{  
‘calendarId’: calendar\_id,  
‘resource’: params  
});  
}  
// Googleカレンダーへのイベント登録の実行  
request.execute((resp) =\> {  
if (resp.error) {  
alert(‘イベントの登録に失敗しました。’ + resp.error.message);  
} else {  
const body = {  
‘app’: kintone.app.getId(),  
‘id’: record.$id.value,  
‘record’: {  
‘event\_id’: {  
‘value’: resp.result.id  
}  
}  
};  
return kintone.api(kintone.api.url(‘/k/v1/record’, true), ‘PUT’, body).then((success) =\> {  
alert(‘カレンダーにイベントを登録しました。’);  
location.reload();  
}).catch((error) =\> {  
alert(‘Google イベントIDの登録に失敗しました。’ + error);  
});  
}  
return true;  
}, (error) =\> {  
alert(‘Google イベントIDの登録に失敗しました。’ + error);  
});  
}  
}  
})();

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1072](https://avatars.discourse-cdn.com/v4/letter/l/b9e5f3/32.png) [@Legacy\_Account1072](https://community.cybozu.dev/u/Legacy_Account1072)\
**Post date:** [October 17, 2021, 11:23pm UTC](https://community.cybozu.dev/t/topic/5370/2 "2021-10-17T23:23:03Z")

</div>

エラーの通り、gapiが宣言されていないという状況ですね。

[参照元の記事](https://community.cybozu.dev/hc/ja/articles/115005646066-kintone%E3%81%AE%E3%82%A4%E3%83%99%E3%83%B3%E3%83%88-%E3%83%95%E3%82%A7%E3%82%A2%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AE%E6%97%A5%E7%A8%8B%E3%82%92Google%E3%82%AB%E3%83%AC%E3%83%B3%E3%83%80%E3%83%BC%E3%81%AB%E5%85%AC%E9%96%8B%E3%81%97%E3%82%88%E3%81%86-)に書いてありますが、  
本件のスクリプトより先にurl指定でjsを追加する必要があるようです。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2886](https://avatars.discourse-cdn.com/v4/letter/l/49beb7/32.png) [@Legacy\_Account2886](https://community.cybozu.dev/u/Legacy_Account2886)\
**Post date:** [October 18, 2021, 7:08am UTC](https://community.cybozu.dev/t/topic/5370/3 "2021-10-18T07:08:27Z")

</div>

\>[岡崎 光輝](https://community.cybozu.dev/hc/ja/profiles/380232352732-%E5%B2%A1%E5%B4%8E-%E5%85%89%E8%BC%9D)さん

ご教示いただきましてありがとうございます！

ご指摘いただいた箇所を見落としていました。。。

URLを追加することでGoogleカレンダーへの反映を実現できました。

ありがとうございました！
