댓글은 파일에 대한 사용자 제공 의견입니다. 예를 들어 워드 프로세서 문서의 독자가 문장을 다시 작성하는 방법을 제안하는 의견이 있습니다. 댓글에는 고정 댓글과 고정되지 않은 댓글의 두 가지 유형이 있습니다. 고정된 댓글은 문서의 특정 버전 내에서 워드 프로세싱 문서의 문장과 같은 특정 위치와 연결됩니다. 반대로 앵커가 지정되지 않은 댓글은 문서와만 연결됩니다.
답글은 댓글에 첨부되며 댓글에 대한 사용자의 응답을 나타냅니다. Drive API를 사용하면 사용자가 앱에서 만든 문서에 댓글과 답글을 추가할 수 있습니다. 댓글과 답글을 통칭하여 토론이라고 합니다.
fields 매개변수 사용
comments
리소스의 모든 메서드 (delete
제외)의 경우 fields
시스템 매개변수를 설정하여 응답에 반환할 필드를 지정해야 합니다. 대부분의 Drive 리소스 메서드에서 이 작업은 기본값이 아닌 필드를 반환하는 데만 필요하지만 comments
리소스에는 필수입니다. fields
매개변수를 생략하면 메서드에서 오류를 반환합니다. 자세한 내용은 특정 필드 반환을 참고하세요.
댓글 제약 조건
Drive API를 사용하여 고정된 댓글과 고정되지 않은 댓글로 작업할 때는 다음 제약 조건이 적용됩니다.
댓글 유형 | 파일 형식 |
---|---|
고정 |
|
고정되지 않음 |
|
문서의 최신 수정 버전에 고정 댓글 추가
댓글을 추가할 때 파일의 특정 영역에 댓글을 고정할 수 있습니다. 앵커는 댓글이 참조하는 파일의 영역을 정의합니다. comments
리소스는 anchor
필드를 JSON 문자열로 정의합니다.
고정 댓글을 추가하려면 다음 단계를 따르세요.
(선택사항)
revisions
리소스에서list
메서드를 호출하여 문서의 모든revisionID
를 나열합니다. 최신 버전이 아닌 버전에 댓글을 고정하려면 이 단계를 따르세요. 최신 버전을 사용하려면revisionID
에head
를 사용합니다.fileID
매개변수, 댓글이 포함된comments
리소스,revisionID
(r
) 및 지역 (a
)이 포함된 JSON 앵커 문자열을 사용하여comments
리소스에서create
메서드를 호출합니다.
다음 코드 샘플은 고정 댓글을 만드는 방법을 보여줍니다.
Python
from google.oauth2.credentials import Credentials
from googleapiclient.errors import HttpError
# --- Configuration ---
# The ID of the file to comment on.
# Example: '1_aBcDeFgHiJkLmNoPqRsTuVwXyZ'
FILE_ID = 'FILE_ID'
# The text content of the comment.
COMMENT_TEXT = 'This is an example of an anchored comment.'
# The line number to anchor the comment to.
# Note: Line numbers are based on the revision.
ANCHOR_LINE = 10
# --- End of user-configuration section ---
SCOPES = ["https://www.googleapis.com/auth/drive"]
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
def create_anchored_comment():
"""
Create an anchored comment on a specific line in a Google Doc.
Returns:
The created comment object or None if an error occurred.
"""
try:
# Build the Drive API service
service = build("drive", "v3", credentials=creds)
# Define the anchor region for the comment.
# For Google Docs, the region is typically defined by 'line' and 'revision'.
# Other file types might use different region classifiers.
anchor = {
'region': {
'kind': 'drive#commentRegion',
'line': ANCHOR_LINE,
'rev': 'head'
}
}
# The comment body.
comment_body = {
'content': COMMENT_TEXT,
'anchor': anchor
}
# Create the comment request.
comment = (
service.comments()
.create(fileId=FILE_ID, fields="*", body=comment_body)
.execute()
)
print(f"Comment ID: {comment.get('id')}")
return comment
except HttpError as error:
print(f"An error occurred: {error}")
return None
create_anchored_comment()
Drive API는 anchor
문자열이 포함된 comments
리소스 객체의 인스턴스를 반환합니다.
고정되지 않은 댓글 추가
고정되지 않은 댓글을 추가하려면 fileId
매개변수와 댓글이 포함된 comments
리소스를 사용하여 create
메서드를 호출합니다.
댓글은 일반 텍스트로 삽입되지만 응답 본문은 표시를 위해 형식이 지정된 콘텐츠가 포함된 htmlContent
필드를 제공합니다.
다음 코드 샘플은 고정되지 않은 댓글을 만드는 방법을 보여줍니다.
Python
from google.oauth2.credentials import Credentials
from googleapiclient.errors import HttpError
# --- Configuration ---
# The ID of the file to comment on.
# Example: '1_aBcDeFgHiJkLmNoPqRsTuVwXyZ'
FILE_ID = 'FILE_ID'
# The text content of the comment.
COMMENT_TEXT = 'This is an example of an unanchored comment.'
# --- End of user-configuration section ---
SCOPES = ["https://www.googleapis.com/auth/drive"]
creds = Credentials.from_authorized_user_file("token.json", SCOPES)
def create_unanchored_comment():
"""
Create an unanchored comment on a specific line in a Google Doc.
Returns:
The created comment object or None if an error occurred.
"""
try:
# Build the Drive API service
service = build("drive", "v3", credentials=creds)
# The comment body. For an unanchored comment,
# omit the 'anchor' property.
comment_body = {
'content': COMMENT_TEXT
}
# Create the comment request.
comment = (
service.comments()
.create(fileId=FILE_ID, fields="*", body=comment_body)
.execute()
)
print(f"Comment ID: {comment.get('id')}")
return comment
except HttpError as error:
print(f"An error occurred: {error}")
return None
create_unanchored_comment()
댓글에 답글 추가하기
댓글에 답글을 추가하려면 fileId
및 commentId
매개변수와 함께 replies
리소스에서 create
메서드를 사용합니다. 요청 본문은 content
필드를 사용하여 답글을 추가합니다.
답글은 일반 텍스트로 삽입되지만 응답 본문은 표시용으로 형식이 지정된 콘텐츠가 포함된 htmlContent
필드를 제공합니다.
이 메서드는 fields
필드에 나열된 필드를 반환합니다.
요청
이 예에서는 fileId
및 commentId
경로 매개변수와 여러 필드를 제공합니다.
POST https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID/replies?fields=id,comment
요청 본문
{ "content": "This is a reply to a comment." }
댓글 해결
댓글에 대한 답글을 게시해야만 댓글이 해결된 것으로 표시됩니다.
댓글을 해결하려면 fileId
및 commentId
매개변수와 함께 replies
리소스에서 create
메서드를 사용합니다.
요청 본문은 action
필드를 사용하여 댓글을 해결합니다. content
필드를 설정하여 댓글을 닫는 답글을 추가할 수도 있습니다.
댓글이 해결되면 드라이브에서 comments
리소스를 resolved: true
로 표시합니다. 삭제된 댓글과 달리 해결된 댓글에는 htmlContent
또는 content
필드가 포함될 수 있습니다.
앱에서 댓글을 해결하면 UI에 댓글이 처리되었음을 표시해야 합니다. 예를 들어 앱에서 다음 작업을 할 수 있습니다.
- 추가 답글을 허용하지 않고 이전 답글과 원본 댓글을 모두 어둡게 표시합니다.
- 해결된 댓글 숨기기
요청
이 예에서는 fileId
및 commentId
경로 매개변수와 여러 필드를 제공합니다.
POST https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID/replies?fields=id,comment
요청 본문
{ "action": "resolve", "content": "This comment has been resolved." }
댓글 가져오기
파일에 대한 댓글을 가져오려면 fileId
및 commentId
매개변수와 함께 comments
리소스에서 get
메서드를 사용합니다. 댓글 ID를 모르는 경우 list
메서드를 사용하여 모든 댓글을 나열할 수 있습니다.
이 메서드는 comments
리소스의 인스턴스를 반환합니다.
결과에 삭제된 댓글을 포함하려면 includedDeleted
쿼리 매개변수를 true
로 설정합니다.
요청
이 예에서는 fileId
및 commentId
경로 매개변수와 여러 필드를 제공합니다.
GET https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID?fields=id,comment,modifiedTime,resolved
댓글 나열
파일의 댓글을 나열하려면 fileId
매개변수와 함께 comments
리소스에서 list
메서드를 사용합니다. 이 메서드는 댓글 목록을 반환합니다.
다음 쿼리 매개변수를 전달하여 댓글의 페이지로 나누기를 맞춤설정하거나 댓글을 필터링합니다.
includeDeleted
: 삭제된 댓글을 포함하려면true
로 설정합니다. 삭제된 댓글에는htmlContent
또는content
필드가 포함되지 않습니다.pageSize
: 페이지당 반환할 최대 댓글 수입니다.pageToken
: 이전 목록 호출에서 수신된 페이지 토큰입니다. 후속 페이지를 가져오려면 이 토큰을 제공하세요.startModifiedTime
: 결과 댓글의modifiedTime
필드의 최소값입니다.
요청
이 예에서는 fileId
경로 매개변수, includeDeleted
쿼리 매개변수, 여러 필드를 제공합니다.
GET https://www.googleapis.com/drive/v3/files/FILE_ID/comments?includeDeleted=true&fields=(id,comment,kind,modifiedTime,resolved)
댓글 업데이트
파일에 대한 댓글을 업데이트하려면 fileId
및 commentId
매개변수와 함께 comments
리소스에서 update
메서드를 사용합니다. 요청 본문은 content
필드를 사용하여 댓글을 업데이트합니다.
comments
리소스의 불리언 resolved
필드는 읽기 전용입니다. 댓글에 대한 답글을 게시해야만 댓글이 해결됩니다. 자세한 내용은 댓글 해결하기를 참고하세요.
이 메서드는 fields
쿼리 매개변수에 나열된 필드를 반환합니다.
요청
이 예에서는 fileId
및 commentId
경로 매개변수와 여러 필드를 제공합니다.
PATCH https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID?fields=id,comment
요청 본문
{ "content": "This comment is now updated." }
댓글 삭제
파일의 댓글을 삭제하려면 fileId
및 commentId
매개변수와 함께 comments
리소스에서 delete
메서드를 사용합니다.
댓글이 삭제되면 Drive에서 댓글 리소스를 deleted: true
로 표시합니다. 삭제된 댓글에는 htmlContent
또는 content
필드가 포함되지 않습니다.
요청
이 예에서는 fileId
및 commentId
경로 매개변수를 제공합니다.
DELETE https://www.googleapis.com/drive/v3/files/FILE_ID/comments/COMMENT_ID