4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GASからContactsApiを使用して読みがなをつける

Last updated at Posted at 2016-04-05

背景

GoogleAppsScriptで連絡先を操作することになり、こんなの簡単だーと思っていたらGASのAPIではフリガナを扱うことができませんでした…orz

参考

ググってみると同じ内容でハマっている人の投稿がありました。(soundTrickerさんの記事にはいつもお世話になっております)
GASのAPIではなくUrlFetchを使用してContact APIを使用するとのこと。
サンプルコードは取得だけだったので、これを参考に新規に登録するコードを書きました。

APIの叩き方

この手のAPIで一番きつい(と思ってる)のは認証部分ですが、これは上記のサンプルコードをそのまま流用します。

新規連絡先の作り方は下記のリンク先のPROTOCOLタブのものを使用します。
https://developers.google.com/google-apps/contacts/v3/?hl=ja#creating_contacts

リクエスト情報詳細

ヘッダーに書く情報とリクエストするテンプレートが記載されています。
フリガナ情報はgd:givenName、gd:familyNameにyomiという属性情報で付加されています。

HeaderはUrlFetchのオプションに記述します。
BodyはXmlServiceで作るよりは、雛形のhtmlをGASプロジェクト内に作ってそれを呼び出して必要部分を置き換える形を取りました。
このBodyはUrlFetchのpayloadとして記述します。

UrlFetchApp.fetchのAPIドキュメント。payloadの項に「the payload (e.g. POST body) for the request. 」と記載されています。
https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetchurl-params

コード

新規に作成するパターンです。
resには作成された連絡先のxmlが返されます。このxmlには作成されたidなどが付与されています。

コード.gs
function sampleCode() {
  //以下は魔法のコメントです。GASの機能で認証して、ScriptApp.getOAuthToken()でTokenを取得するために書いてあります。
  //ContactsApp.getContacts()
  
  //AccessTokenの取得
  var token = ScriptApp.getOAuthToken();
  var newEntry = HtmlService.createHtmlOutputFromFile("entry").getContent();

  ///////////////////////////////////////////////////////////////////////////////
  // ここに必要部分を置換する処理を書く
  ///////////////////////////////////////////////////////////////////////////////
  
  //APIの呼び出し
  var res = UrlFetchApp.fetch("https://www.google.com/m8/feeds/contacts/default/full",{
    contentType: 'application/atom+xml',
    method : "post",
    payload: newEntry,
    headers : {
      "GData-Version" : "3.0",
      "Authorization":  "Bearer " + token
    }
  });
}

ファイル→新規作成からhtmlを選択しentryという名前で作っておきます。
<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/hoge%40gmail.com/base/6'/> を追加することでMyコンタクトに追加することができます。(メールアドレス部分を自分のものに変更してください)

entry.html
<atom:entry xmlns:atom="http://www.w3.org/2005/Atom"
    xmlns:gd="http://schemas.google.com/g/2005"
    xmlns:gContact="http://schemas.google.com/contact/2008">
  <atom:category scheme="http://schemas.google.com/g/2005#kind"
    term="http://schemas.google.com/contact/2008#contact"/>
  <gd:name>
     <gd:givenName yomi='なまえ'>名前</gd:givenName>
     <gd:familyName yomi='みょうじ'>苗字</gd:familyName>
     <gd:fullName>名前 苗字</gd:fullName>
  </gd:name>
  <atom:content type="text">Notes</atom:content>
  <gd:email rel="http://schemas.google.com/g/2005#work"
    primary="true"
    address="" displayName="名前 苗字"/>
  <gd:email rel="http://schemas.google.com/g/2005#home"
    address=""/>
  <gd:phoneNumber rel="http://schemas.google.com/g/2005#work"
    primary="true">
    99999999
  </gd:phoneNumber>
  <gd:structuredPostalAddress
      rel="http://schemas.google.com/g/2005#work"
      primary="true">
  </gd:structuredPostalAddress>
<gContact:groupMembershipInfo deleted='false' href='http://www.google.com/m8/feeds/groups/hoge%40gmail.com/base/6'/>
</atom:entry>
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?