# 半角から全角変換（保存時）

**URL:** <https://community.cybozu.dev/t/topic/5592>\
**Category:** kintone 開発相談\
**Created:** [2021 年 12 月 22 日午前 6:53 UTC](https://community.cybozu.dev/t/topic/5592 "2021-12-22T06:53:06Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2469](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Legacy\_Account2469](https://community.cybozu.dev/u/Legacy_Account2469)\
**Post date:** [2021 年 12 月 22 日午前 6:53 UTC](https://community.cybozu.dev/t/topic/5592/1 "2021-12-22T06:53:06Z")

</div>

お世話になります。

項目変更時に半角⇒全角への変換は下記の通り実装できたのですが

```
(function(){
"use strict";
var events = [
'app.record.create.change.顧客名', //環境のフィールドコードを記載
'app.record.edit.change.顧客名', //環境のフィールドコードを記載
'app.record.create.change.事業所', //環境のフィールドコードを記載
'app.record.edit.change.事業所', //環境のフィールドコードを記載
'app.record.create.change.住所', //環境のフィールドコードを記載
'app.record.edit.change.住所' //環境のフィールドコードを記載

];
kintone.events.on(events,function(event){
event.changes.field.value=event.changes.field.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');
return event;
});
})();

```

保存時の実装が上手くいきません。

何が原因でしょうか。

&nbsp;

```
(function(){
"use strict";
var events = [
'app.record.create.submit.顧客名', //環境のフィールドコードを記載
'app.record.edit.submit.顧客名', //環境のフィールドコードを記載
'app.record.create.submit.事業所', //環境のフィールドコードを記載
'app.record.edit.submit.事業所', //環境のフィールドコードを記載
'app.record.create.submit.住所', //環境のフィールドコードを記載
'app.record.edit.submit.住所', //環境のフィールドコードを記載

];
kintone.events.on(events,function(event){
event.submit.field.value=event.submit.field.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');
return event;
});
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2476](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@Legacy\_Account2476](https://community.cybozu.dev/u/Legacy_Account2476)\
**Post date:** [2021 年 12 月 22 日午前 7:27 UTC](https://community.cybozu.dev/t/topic/5592/2 "2021-12-22T07:27:11Z")

</div>

ADM 様

こんにちは。

app.record.create.submit と app.record.edit.submit の後ろのフィールドコードがまず必要ありません。

[kintone JavaScript API（イベント）一覧 – cybozu developer network](https://community.cybozu.dev/hc/ja/articles/360000361686)

kintone.events.on の第一引数は上記のフィールドコードが無いイベントタイプを２つ指定して頂ければ、保存時のイベントとして処理されると思います。

また、event.submit.field.value は存在しませんので event.records.顧客名.value 等で保存イベント時にフィールドを一つ一つチェックしていく必要があるかと思います。

ざっくりお伝えすると以上です。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2469](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Legacy\_Account2469](https://community.cybozu.dev/u/Legacy_Account2469)\
**Post date:** [2021 年 12 月 22 日午前 8:42 UTC](https://community.cybozu.dev/t/topic/5592/3 "2021-12-22T08:42:46Z")

</div>

回答いただきありがとうございます。

記載がそもそも違っていたんですね。

訂正し、実行してみたのですが動作しないようです。

```
(function(){
"use strict";
var events = [
'app.record.create.submit', //環境のフィールドコードを記載
'app.record.edit.submit' //環境のフィールドコードを記載

];
kintone.events.on('app.record.create.submit','app.record.edit.submit',function(event){
event.records.顧客名.value=event.records.顧客名.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

event.records.事業所.value=event.records.事業所.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

event.records.住所.value=event.records.住所.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

return event;
});
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account382](https://avatars.discourse-cdn.com/v4/letter/l/4491bb/32.png) [@Legacy\_Account382](https://community.cybozu.dev/u/Legacy_Account382)\
**Post date:** [2021 年 12 月 22 日午前 8:48 UTC](https://community.cybozu.dev/t/topic/5592/4 "2021-12-22T08:48:22Z")

</div>

ADM様 横から失礼します。

event.records.事業所.value=event.records.事業所.value.replace(/[A-Za-z0-9]/g,function(s){

event.recordsからevent.recordに変更でいかがでしょうか。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2469](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Legacy\_Account2469](https://community.cybozu.dev/u/Legacy_Account2469)\
**Post date:** [2021 年 12 月 22 日午前 8:52 UTC](https://community.cybozu.dev/t/topic/5592/5 "2021-12-22T08:52:12Z")

</div>

ありがとうございます。

変更かけてみました、がうまくいかないようです・・・

なぜでしょうね

```
(function(){
"use strict";
var events = [
'app.record.create.submit', //環境のフィールドコードを記載
'app.record.edit.submit' //環境のフィールドコードを記載

];
kintone.events.on('app.record.create.submit','app.record.edit.submit',function(event){
event.record.顧客名.value=event.record.顧客名.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

event.record.事業所.value=event.record.事業所.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

event.record.住所.value=event.record.住所.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

return event;
});
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2476](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@Legacy\_Account2476](https://community.cybozu.dev/u/Legacy_Account2476)\
**Post date:** [2021 年 12 月 22 日午前 9:26 UTC](https://community.cybozu.dev/t/topic/5592/6 "2021-12-22T09:26:29Z")

</div>

ADM 様

こんにちは。

ishikawayuito 様が仰られた通り、record が正しいですね。

申し訳ございませんでした。

あと改善点は、kintone.events.on の第一引数は複数指定する場合は配列になります。

ですので

```
kintone.events.on('app.record.create.submit','app.record.edit.submit',function(event){ // この行ですが

```

↓

```
kintone.events.on(['app.record.create.submit','app.record.edit.submit'],function(event){ // このようになります。

```

変更して動作を確認してみて下さい。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2476](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@Legacy\_Account2476](https://community.cybozu.dev/u/Legacy_Account2476)\
**Post date:** [2021 年 12 月 22 日午前 9:29 UTC](https://community.cybozu.dev/t/topic/5592/7 "2021-12-22T09:29:21Z")

</div>

もしくは、せっかく配列用意されているので。

```
kintone.events.on(events,function(event){

```

このようにされても良いかと思います。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2469](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Legacy\_Account2469](https://community.cybozu.dev/u/Legacy_Account2469)\
**Post date:** [2021 年 12 月 22 日午後 11:36 UTC](https://community.cybozu.dev/t/topic/5592/8 "2021-12-22T23:36:48Z")

</div>

新屋様

ありがとうございます。

変更したところ下記のエラーが発生しました。

```
Uncaught TypeError: Cannot read properties of undefined (reading 'replace')
at download.do?app=24&contentId=597579&jsType=DESKTOP&hash=d6c7c56921ee4dc3a7c49fa1f11a81ed77c0f07c:13
at show.js:269
at new Jk (show.js:131)
at Dea (show.js:268)
at show.js:268
at e.o (show.js:133)
at Yk (show.js:135)
at Vk (show.js:135)
at Jk.K (show.js:1246)
at Ck (show.js:131)

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2469](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Legacy\_Account2469](https://community.cybozu.dev/u/Legacy_Account2469)\
**Post date:** [2021 年 12 月 22 日午後 11:40 UTC](https://community.cybozu.dev/t/topic/5592/9 "2021-12-22T23:40:00Z")

</div>

配列でも同じ結果になってしまいました。

kintone.events.on(events,function(event){ の中を顧客名だけにすれば動きました。ただ、このファンクションを３つに分けて動かしてみたところたまに処理が走るものの、ほぼエラーで止まってしまいます。

どれか１つだとうまく動作します。

```
(function(){
"use strict";
var events = [
'app.record.create.submit', //環境のフィールドコードを記載
'app.record.edit.submit' //環境のフィールドコードを記載

];
kintone.events.on(events,function(event){
event.record.顧客名.value=event.record.顧客名.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

event.record.事業所.value=event.record.事業所.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

event.record.住所.value=event.record.住所.value.replace(/[A-Za-z0-9]/g,function(s){
return String.fromCharCode(s.charCodeAt(0)+65248);
}).replace(/[‐－―ー-]/g,'－');

return event;
});
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2476](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@Legacy\_Account2476](https://community.cybozu.dev/u/Legacy_Account2476)\
**Post date:** [2021 年 12 月 23 日午前 2:02 UTC](https://community.cybozu.dev/t/topic/5592/10 "2021-12-23T02:02:53Z")

</div>

ADM 様

こんにちは。

エラーの件ですが、13行目なのでおそらく事業所に値が入っていなかったのではないでしょうか？

その場合は、replace を実行した場合に ご報告を頂いたようなエラーを出します。

回避方法としては対象のフィールドの値を if などで評価して実行をスキップさせるようにすると回避できるかと思います。

&nbsp;

もしくは回避方法として、顧客名・事業所・住所 をフィールドの設定で必須にしてしまうと良いかもしれませんね。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2469](https://avatars.discourse-cdn.com/v4/letter/l/f17d59/32.png) [@Legacy\_Account2469](https://community.cybozu.dev/u/Legacy_Account2469)\
**Post date:** [2021 年 12 月 23 日午前 2:23 UTC](https://community.cybozu.dev/t/topic/5592/11 "2021-12-23T02:23:27Z")

</div>

[新屋 様](https://community.cybozu.dev/hc/ja/profiles/901078399426-%E6%96%B0%E5%B1%8B-%E8%82%B2%E7%94%B7)

まさにおっしゃる通りでした。

空欄を回避したら上手く動作するようになりました。

良かったです！ありがとうございます！

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/cybozu/original/1X/d5c841c3c5a6cb66eed514001c1b45147c9e1f4f.png) [@system](https://community.cybozu.dev/u/system)\
**Post date:** [2025 年 3 月 14 日午前 4:51 UTC](https://community.cybozu.dev/t/topic/5592/12 "2025-03-14T04:51:54Z")

</div>

クローズされました: 2025 年 3 月 14 日午前 4:51 UTC

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。
