Class Position

위치

특정 요소를 기준으로 한 문서 내 위치에 대한 참조입니다. 사용자의 커서는 여러 용도 중에서 Position로 표현됩니다. 스크립트는 스크립트가 문서에 바인딩된 경우에만 스크립트를 실행하는 사용자의 커서에 액세스할 수 있습니다.

// Insert some text at the cursor position and make it bold.
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
  // Attempt to insert text at the cursor position. If the insertion returns null, the cursor's
  // containing element doesn't allow insertions, so show the user an error message.
  var element = cursor.insertText('ಠ‿ಠ');
  if (element) {
    element.setBold(true);
  } else {
    DocumentApp.getUi().alert('Cannot insert text here.');
  }
} else {
  DocumentApp.getUi().alert('Cannot find a cursor.');
}

방법

메서드반환 유형간략한 설명
getElement()ElementPosition가 포함된 요소를 가져옵니다.
getOffset()IntegerPosition을 포함하는 요소 내에서 이 Position의 상대적 위치를 가져옵니다.
getSurroundingText()TextPosition를 포함하는 Paragraph 또는 ListItem의 텍스트와 서식을 나타내는 인위적 Text 요소를 직접 또는 하위 요소 체인을 통해 생성합니다.
getSurroundingTextOffset()IntegergetSurroundingText()에서 반환된 Text 요소 내에서 이 Position의 오프셋을 가져옵니다.
insertBookmark()BookmarkPosition에 새 Bookmark를 만들어 삽입합니다.
insertInlineImage(image)InlineImage지정된 이미지 blob에서 이 Position에 새 InlineImage를 만들고 삽입합니다.
insertText(text)TextPosition에 지정된 텍스트를 삽입합니다.

자세한 문서

getElement()

Position가 포함된 요소를 가져옵니다. 이 요소는 Text 요소이거나 Paragraph와 같은 컨테이너 요소입니다. 두 경우 모두 요소 내의 상대 위치는 getOffset()를 사용하여 확인할 수 있습니다.

리턴

Element: 이 Position 객체가 있는 컨테이너 또는 Text 요소


getOffset()

Position을 포함하는 요소 내에서 이 Position의 상대적 위치를 가져옵니다. 요소가 Text 요소인 경우 offset은 Position 앞에 있는 문자 수 (즉, 이 Position 다음에 오는 문자의 색인)입니다. 다른 요소의 경우 offset은 같은 컨테이너 요소 내에서 이 Position 앞에 있는 하위 요소의 개수 (Position 다음에 오는 하위 요소의 색인)입니다.

리턴

Integer: Text 요소의 경우 이 Position 앞의 문자 수, 다른 요소의 경우 동일한 컨테이너 요소 내에서 이 Position 앞에 있는 하위 요소 수입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSurroundingText()

Position를 포함하는 Paragraph 또는 ListItem의 텍스트와 서식을 나타내는 인위적 Text 요소를 직접 또는 하위 요소 체인을 통해 생성합니다. 반환된 Text 요소에서 Position의 오프셋을 확인하려면 getSurroundingTextOffset()를 사용합니다.

리턴

Text - Position가 포함된 Paragraph 또는 ListItem에서 editAsText()를 직접 호출하거나 하위 요소 체인을 통해 호출한 결과와 동일한 요소

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSurroundingTextOffset()

getSurroundingText()에서 반환된 Text 요소 내에서 이 Position의 오프셋을 가져옵니다. 오프셋은 Position 앞의 문자 수(즉, 이 Position 뒤의 문자 색인)입니다.

리턴

Integer — 직접 또는 하위 요소 체인을 통해 Position을 포함하는 Paragraph 또는 ListItem에서 이 Position 앞에 있는 문자 수

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertBookmark()

Position에 새 Bookmark를 만들어 삽입합니다.

리턴

Bookmark — 새 북마크

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertInlineImage(image)

지정된 이미지 blob에서 이 Position에 새 InlineImage를 만들고 삽입합니다.

매개변수

이름유형설명
imageBlobSourcePosition에 삽입할 이미지 데이터

리턴

InlineImage: 새 이미지 요소 또는 이 Position가 있는 요소에서 이미지 삽입을 허용하지 않는 경우 null입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

insertText(text)

Position에 지정된 텍스트를 삽입합니다. 이 메서드는 문자열이 기존 Text 요소 내에 삽입되더라도 새 Text 요소를 생성하므로 새 요소의 스타일을 쉽게 지정할 수 있습니다.

매개변수

이름유형설명
textStringPosition에 삽입할 문자열

리턴

Text: 새 텍스트 요소 또는 이 Position가 있는 요소에서 텍스트 삽입을 허용하지 않는 경우 null입니다.

승인

이 방법을 사용하는 스크립트는 다음 범위 중 하나 이상을 승인해야 합니다.

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents