Skip to content

メモとスレッド形式のコメントを付ける ​

Excel 365 と同じ方法でセルに注釈を付けるときの手順です。単純なメモと、作成者を記録し、ほかの人をメンションできる会話形式のスレッドを扱います。モデルの説明は コメントとメモ に、呼び出しは WASM API の コメントとハイパーリンク と スレッド形式のコメントと人物、および Python API にあります。

メモを追加する ​

メモは、従来のセルコメントを Excel 365 で呼ぶ名前で、作成者名の付いたテキストボックスです。setComment(cell, comment) で書き込み、null で削除します。作成者のないメモは、ほかの欠けた値と同じく author が null(Python では None)として読み戻されます。空の文字列を渡した作成者も、作成者なしとして保存されます。listComments() はシート上のすべてのメモをセルとともに返します。メモはポインターをセルに重ねるまで表示されません。setCommentVisible を使うと常に表示されます。

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
  const ws = wb.sheet(0)
  ws.setComment('B2', { text: 'Check this figure', author: 'Ann' })
  ws.setComment('B3', { text: 'No author' })
  ws.setCommentVisible('B2', true)

  console.log(JSON.stringify(ws.getComment('B2'))) // {"text":"Check this figure","author":"Ann"}
  console.log(ws.getComment('B3').author) // null
  console.log(ws.getCommentVisible('B2')) // true
  console.log(JSON.stringify(ws.listComments()[0].cell)) // {"row":1,"col":1}

  ws.setComment('B2', null)
  console.log(ws.getComment('B2')) // null
} finally {
  wb.dispose()
}
python
from formulon import Comment, Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ws.set_comment("B2", Comment("Check this figure", author="Ann"))
    ws.set_comment("B3", Comment("No author"))
    ws.set_comment_visible("B2", True)

    print(ws.get_comment("B2"))  # Comment(text='Check this figure', author='Ann')
    print(ws.get_comment("B3").author)  # None
    print(ws.get_comment_visible("B2"))  # True
    print(ws.list_comments()[0].cell)  # CellAddress(row=1, col=1)

    ws.set_comment("B2", None)
    print(ws.get_comment("B2"))  # None

1 つのセルが持てるのはメモかスレッドのどちらか一方です。どちらかがあるセルにもう一方を追加すると、StatusCode.AlreadyExists で失敗します。

人物を登録する ​

スレッド形式のコメントは、作成者をワークブックの人物一覧の id で指定します。wb.addPerson は人物を追加して id を返します。id を省くと、波括弧付きの GUID が生成されます。userId と providerId はその名前に対応するアカウントを表します。アカウントのない人物では省いてください。

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
  const ann = wb.addPerson({ displayName: 'Ann Lee' })
  wb.addPerson({ displayName: 'Bob Kim', userId: '[email protected]', providerId: 'None' })

  console.log(/^\{[0-9A-F-]{36}\}$/.test(ann)) // true
  console.log(wb.listPeople().map((p) => p.displayName).join()) // Ann Lee,Bob Kim
  console.log(wb.getPerson(ann).userId) // null
} finally {
  wb.dispose()
}
python
import re

from formulon import PersonInput, Workbook

with Workbook.create_default() as wb:
    ann = wb.add_person(PersonInput(display_name="Ann Lee"))
    wb.add_person(PersonInput(display_name="Bob Kim", user_id="[email protected]", provider_id="None"))

    print(bool(re.fullmatch(r"\{[0-9A-F-]{36}\}", ann)))  # True
    print(",".join(p.display_name for p in wb.list_people()))  # Ann Lee,Bob Kim
    print(wb.get_person(ann).user_id)  # None

スレッドを開始して返信する ​

cell を指定した addThreadedComment は、そのセルでスレッドを開始し、コメントの id を返します。parentId を指定したコメントはそのスレッドへの返信になり、cell は無視されます。listThreadedComments(cell) はスレッドを開始コメントから順に返します。created はタイムスタンプで、その読み方は次の節で説明します。

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
  const ws = wb.sheet(0)
  const ann = wb.addPerson({ displayName: 'Ann Lee' })
  const bob = wb.addPerson({ displayName: 'Bob Kim' })

  const thread = ws.addThreadedComment({
    cell: 'C3',
    personId: ann,
    text: 'Is this total final?',
    created: new Date('2026-11-07T09:30:00Z'),
  })
  ws.addThreadedComment({
    parentId: thread,
    personId: bob,
    text: 'Yes, signed off.',
    created: new Date('2026-11-07T10:05:00Z'),
  })

  const comments = ws.listThreadedComments('C3')
  console.log(comments.length) // 2
  console.log(comments[1].text, comments[1].parentId === thread) // Yes, signed off. true
  console.log(wb.getPerson(comments[1].personId).displayName) // Bob Kim
} finally {
  wb.dispose()
}
python
from datetime import datetime, timezone

from formulon import PersonInput, ThreadedCommentInput, Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ann = wb.add_person(PersonInput(display_name="Ann Lee"))
    bob = wb.add_person(PersonInput(display_name="Bob Kim"))

    thread = ws.add_threaded_comment(ThreadedCommentInput(
        cell="C3",
        person_id=ann,
        text="Is this total final?",
        created=datetime(2026, 11, 7, 9, 30, tzinfo=timezone.utc),
    ))
    ws.add_threaded_comment(ThreadedCommentInput(
        parent_id=thread,
        person_id=bob,
        text="Yes, signed off.",
        created=datetime(2026, 11, 7, 10, 5, tzinfo=timezone.utc),
    ))

    comments = ws.list_threaded_comments("C3")
    print(len(comments))  # 2
    print(comments[1].text, comments[1].parent_id == thread)  # Yes, signed off. True
    print(wb.get_person(comments[1].person_id).display_name)  # Bob Kim

save() は、各スレッドと一緒に、古いバージョンの Excel 向けにスレッド形式のコメントがあることを伝えるメモを書き出します。この代替のメモは listComments() には現れません。

タイムスタンプを UTC で指定する ​

created は UTC で保存されます。JavaScript の Date を UTC のゲッターで読む日付入力は、これだけです。セルの値や固定した時計など、ほかの Date 入力はすべてローカルのゲッターで読みます。そのため created の値は Date.UTC(...) か末尾が Z の ISO 文字列で作り、セルの日付は new Date(year, monthIndex, day) で作ります。

created に渡した CivilTime レコードは UTC として扱います。Python では、created に渡したタイムゾーンなしの datetime を UTC として読み、タイムゾーン付きのものは変換します。秒までは保持し、ミリ秒は切り捨てます。

created を省くと現在の UTC 時刻が入ります。ワークブックの時計を固定している場合(setPinnedNow)は、その壁時計の時刻を変換せずにそのまま書き込みます。

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
  const ws = wb.sheet(0)
  const ann = wb.addPerson({ displayName: 'Ann Lee' })

  const id = ws.addThreadedComment({
    cell: 'B2',
    personId: ann,
    text: 'Logged at 09:30 UTC',
    created: new Date(Date.UTC(2026, 10, 7, 9, 30)),
  })
  console.log(JSON.stringify(ws.getThreadedComment(id).created)) // {"year":2026,"month":11,"day":7,"hour":9,"minute":30,"second":0}

  ws.setValue('A1', new Date(2026, 10, 7, 9, 30))
  const local = ws.getDate('A1')
  console.log(local.getHours(), local.getMinutes()) // 9 30
} finally {
  wb.dispose()
}
python
from datetime import datetime, timezone

from formulon import PersonInput, ThreadedCommentInput, Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ann = wb.add_person(PersonInput(display_name="Ann Lee"))

    comment_id = ws.add_threaded_comment(ThreadedCommentInput(
        cell="B2",
        person_id=ann,
        text="Logged at 09:30 UTC",
        created=datetime(2026, 11, 7, 9, 30, tzinfo=timezone.utc),
    ))
    print(ws.get_threaded_comment(comment_id).created)  # 2026-11-07 09:30:00

    ws["A1"] = datetime(2026, 11, 7, 9, 30)
    print(ws.get_date("A1"))  # 2026-11-07 09:30:00

created は UTC のまま読み戻されます。JavaScript では CivilTime レコード、Python ではタイムゾーンなしの datetime です。表示するときは読み手のタイムゾーンに変換してください。

人物をメンションする ​

メンションは、コメントの文字列の一部を人物に結び付けます。start と end は半開区間 [start, end) で、通常は @Name の部分を指します。位置はバインディングごとに、その言語の文字列のインデックスで数えます。JavaScript は indexOf、length、slice と同じ UTF-16 コード単位、Python は str.index、len、スライスと同じコードポイントです。

多くの絵文字のように基本多言語面の外の文字を含む文字列では、同じ文字列でも 2 つの言語で位置の値は異なります。ただし、どちらの値もそのまま渡せます。

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
  const ws = wb.sheet(0)
  const ann = wb.addPerson({ displayName: 'Ann Lee' })
  const bob = wb.addPerson({ displayName: 'Bob Kim' })

  const text = '👍 @Bob Kim'
  const start = text.indexOf('@Bob Kim')
  const id = ws.addThreadedComment({
    cell: 'C3',
    personId: ann,
    text,
    mentions: [{ personId: bob, start, end: start + '@Bob Kim'.length }],
  })

  const [mention] = ws.getThreadedComment(id).mentions
  console.log(mention.start, mention.end) // 3 11
  console.log(text.slice(mention.start, mention.end), mention.personId === bob) // @Bob Kim true
} finally {
  wb.dispose()
}
python
from formulon import MentionInput, PersonInput, ThreadedCommentInput, Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ann = wb.add_person(PersonInput(display_name="Ann Lee"))
    bob = wb.add_person(PersonInput(display_name="Bob Kim"))

    text = "👍 @Bob Kim"
    start = text.index("@Bob Kim")
    comment_id = ws.add_threaded_comment(ThreadedCommentInput(
        cell="C3",
        person_id=ann,
        text=text,
        mentions=[MentionInput(person_id=bob, start=start, end=start + len("@Bob Kim"))],
    ))

    mention = ws.get_threaded_comment(comment_id).mentions[0]
    print(mention.start, mention.end)  # 2 10
    print(text[mention.start:mention.end], mention.person_id == bob)  # @Bob Kim True

解決、編集、削除 ​

updateThreadedComment(id, patch) はコメントの text と mentions を変更し、done でスレッドを解決済みにしたり再開したりします。done を指定できるのはスレッドの開始コメントだけです。removeThreadedComment(id) はコメントを 1 件削除します。

js
import createFormulon from '@libraz/formulon'

const formulon = await createFormulon()
const wb = formulon.Workbook.createDefault()
try {
  const ws = wb.sheet(0)
  const ann = wb.addPerson({ displayName: 'Ann Lee' })
  const thread = ws.addThreadedComment({ cell: 'C3', personId: ann, text: 'Is this total final?' })
  const reply = ws.addThreadedComment({ parentId: thread, personId: ann, text: 'Draft reply' })

  ws.updateThreadedComment(reply, { text: 'Yes, signed off.' })
  ws.updateThreadedComment(thread, { done: true })
  console.log(ws.getThreadedComment(thread).done) // true
  console.log(ws.getThreadedComment(reply).text) // Yes, signed off.

  ws.removeThreadedComment(reply)
  console.log(ws.listThreadedComments('C3').length) // 1
} finally {
  wb.dispose()
}
python
from formulon import PersonInput, ThreadedCommentInput, Workbook

with Workbook.create_default() as wb:
    ws = wb[0]
    ann = wb.add_person(PersonInput(display_name="Ann Lee"))
    thread = ws.add_threaded_comment(ThreadedCommentInput(cell="C3", person_id=ann, text="Is this total final?"))
    reply = ws.add_threaded_comment(ThreadedCommentInput(parent_id=thread, person_id=ann, text="Draft reply"))

    ws.update_threaded_comment(reply, text="Yes, signed off.")
    ws.update_threaded_comment(thread, done=True)
    print(ws.get_threaded_comment(thread).done)  # True
    print(ws.get_threaded_comment(reply).text)  # Yes, signed off.

    ws.remove_threaded_comment(reply)
    print(len(ws.list_threaded_comments("C3")))  # 1