0
1

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 3 years have passed since last update.

Confluence:Excel VBAからConfluenceのページを作成する (Server/DataCenter)

Last updated at Posted at 2021-10-06

手順

1.VBA-JSONの追加

1)以下よりVBA-JSONをダウンロードします

2)「JsonConverter.bas」 をプロジェクトに追加します

image.png

2.参照設定の追加

「Microsoft XML v6.0」と、「Microsoft Scripting Runtime」を追加します。
image.png

3.VBAコードの追加

Microsoft Visual Basic for Applications を開き、次のコードを入力し、実行します。

親ページを指定せずに新規ページを作成する場合

Sub CreatePage()
  Dim httpReq As New XMLHTTP60
  Dim sUrl As String

  Dim page As New Dictionary
  Dim pageSpace As New Dictionary
  Dim pageBody As New Dictionary
  Dim pageBodyStorage As New Dictionary
  Dim sJson As String

  pageSpace.Add "key", "<スペースKey>"
  pageBodyStorage.Add "value", "<ページ本文>"
  pageBodyStorage.Add "representation", "storage"
  pageBody.Add "storage", pageBodyStorage

  page.Add "type", "page"
  page.Add "title", "<ページタイトル>"
  page.Add "space", pageSpace
  page.Add "body", pageBody

  ' JSONテキストへコンバートする
  sJson = ConvertToJson(page, Whitespace:=2)
  Debug.Print (sJson) ' JSONテキストをイミディエイトに出力

  sUrl = "https://<ConfluenceのベースURL>/rest/api/content/"

  ' HTTPリクエストする
  With httpReq
    .Open "POST", sUrl
    .setRequestHeader "Authorization", "Basic <Basic認証のユーザー名とパスワード(Base64エンコード)>"
    .setRequestHeader "Content-Type", "application/json; charset=UTF-8"
    .setRequestHeader "X-Atlassian-Token", "no-check"
    .send (sJson)
    Debug.Print .responseText ' HTTPレスポンスをイミディエイトに出力
  End With
End Sub

親ページを指定して(指定されたページの子ページとして)新規ページを作成する場合

Sub CreatePage()
  Dim httpReq As New XMLHTTP60
  Dim sUrl As String

  Dim page As New Dictionary
  Dim pageSpace As New Dictionary
  Dim pageBody As New Dictionary
  Dim pageBodyStorage As New Dictionary
  Dim pageAncestors As New Dictionary
  Dim sJson As String

  pageSpace.Add "key", "<スペースKey>"
  pageBodyStorage.Add "value", "<ページ本文>"
  pageBodyStorage.Add "representation", "storage"
  pageBody.Add "storage", pageBodyStorage
  pageAncestors.Add "id", "<親ページID>"

  page.Add "type", "page"
  page.Add "title", "<ページタイトル>"
  page.Add "space", pageSpace
  page.Add "body", pageBody
  page.Add "ancestors", Array(pageAncestors)

  ' JSONテキストへコンバートする
  sJson = ConvertToJson(page, Whitespace:=2)
  Debug.Print (sJson) ' JSONテキストをイミディエイトに出力

  sUrl = "https://<ConfluenceのベースURL>/rest/api/content/"

  ' HTTPリクエストする
  With httpReq
    .Open "POST", sUrl
    .setRequestHeader "Authorization", "Basic <Basic認証のユーザー名とパスワード(Base64エンコード)>"
    .setRequestHeader "Content-Type", "application/json; charset=UTF-8"
    .setRequestHeader "X-Atlassian-Token", "no-check"
    .send (sJson)
    Debug.Print .responseText ' HTTPレスポンスをイミディエイトに出力
  End With
End Sub

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?